/**
   * Tests getPeople with a couple of UserIds
   *
   * @throws Exception
   */
  @Test
  public void testGetExpectedPeople() throws Exception {
    CollectionOptions options = new CollectionOptions();
    options.setSortBy(PersonService.ALL_FILTER);
    options.setSortOrder(SortOrder.ascending);
    options.setFilter(null);
    options.setFilterOperation(FilterOperation.contains);
    options.setFilterValue("");
    options.setFirst(0);
    options.setMax(5);

    Set<UserId> userIds = new java.util.HashSet<UserId>();
    userIds.add(JOHN_DOE);
    userIds.add(JANE_DOE);
    userIds.add(MAIJA_M);

    RestfulCollection<Person> peopleCollection =
        personService
            .getPeople(
                userIds, null, options, Collections.<String>emptySet(), new FakeGadgetToken())
            .get();
    List<Person> personList = peopleCollection.getEntry();
    assertEquals(true, containsPerson(personList, JOHN_DOE.getUserId()));
    assertEquals(true, containsPerson(personList, MAIJA_M.getUserId()));
    assertEquals(false, containsPerson(personList, GEORGE_DOE.getUserId()));
  }
 /**
  * Tests getPerson with a UserId typed as Userid. The Id string is set in the UserId.
  *
  * @throws Exception
  */
 @Test
 public void testGetExpectedPersonByUserId() throws Exception {
   Future<Person> selectedObject =
       personService.getPerson(JOHN_DOE, Collections.<String>emptySet(), new FakeGadgetToken());
   Person person = selectedObject.get();
   assertEquals(JOHN_DOE.getUserId(), person.getId());
 }
  public void doCreateActivity(
      UserId userId,
      GroupId groupId,
      String appId,
      Set<String> fields,
      Activity activity,
      SecurityToken securityToken)
      throws Exception {

    long userIdLong = GetterUtil.getLong(userId.getUserId(securityToken));

    String activityAppId = activity.getAppId();

    JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();

    SerializerUtil.copyProperties(activity, extraDataJSONObject, _ACTIVITY_FIELDS);

    SocialActivityLocalServiceUtil.addActivity(
        userIdLong,
        0L,
        Activity.class.getName(),
        activity.getPostedTime(),
        activityAppId.hashCode(),
        extraDataJSONObject.toString(),
        0L);
  }
 /**
  * Tests getPerson with a UserId typed as Viewerid. The Id string is set in the token in the field
  * viewerId.
  *
  * @throws Exception
  */
 @Test
 public void testGetExpectedPersonByViewerId() throws Exception {
   SecurityToken token = FakeGadgetToken.createToken("viewerId=john.doe");
   Future<Person> selectedObject =
       personService.getPerson(VIEWER, Collections.<String>emptySet(), token);
   Person person = selectedObject.get();
   assertEquals(JOHN_DOE.getUserId(), person.getId());
 }
  public void testGetExpectedAppDataForPlural() throws Exception {
    DataCollection responseItem =
        db.getPersonData(
                ImmutableSet.of(CANON_USER, JOHN_DOE),
                SELF_GROUP,
                APP_ID,
                Collections.<String>emptySet(),
                new FakeGadgetToken())
            .get();
    assertFalse(responseItem.getEntry().isEmpty());
    assertFalse(responseItem.getEntry().get(CANONICAL_USER_ID).isEmpty());

    assertSame(2, responseItem.getEntry().get(CANONICAL_USER_ID).size());
    assertTrue(responseItem.getEntry().get(CANONICAL_USER_ID).containsKey("count"));
    assertTrue(responseItem.getEntry().get(CANONICAL_USER_ID).containsKey("size"));

    assertFalse(responseItem.getEntry().get(JOHN_DOE.getUserId()).isEmpty());
    assertSame(1, responseItem.getEntry().get(JOHN_DOE.getUserId()).size());
    assertTrue(responseItem.getEntry().get(JOHN_DOE.getUserId()).containsKey("count"));
  }
  public RestfulCollection<Activity> doGetActivities(
      Set<UserId> userIds,
      GroupId groupId,
      String appId,
      Set<String> fields,
      CollectionOptions collectionOptions,
      SecurityToken securityToken)
      throws Exception {

    ThemeDisplay themeDisplay = getThemeDisplay(securityToken);

    List<Activity> activities = new ArrayList<Activity>();

    for (UserId userId : userIds) {
      long userIdLong = GetterUtil.getLong(userId.getUserId(securityToken));

      List<Activity> personActivities = getActivities(themeDisplay, userIdLong);

      activities.addAll(personActivities);
    }

    return new RestfulCollection<Activity>(
        activities, collectionOptions.getFirst(), activities.size(), collectionOptions.getMax());
  }
 public Set<UserId> getUsers() {
   List<String> ids = getListParameter(USER_ID);
   if (ids.isEmpty()) {
     if (token.getViewerId() != null) {
       // Assume @me
       ids = Lists.newArrayList(token.getViewerId());
     } else {
       throw new IllegalArgumentException("No userId provided and viewer not available");
     }
   }
   Set<UserId> userIds = Sets.newLinkedHashSet();
   for (String id : ids) {
     userIds.add(UserId.fromJson(id));
   }
   return userIds;
 }
  public RestfulCollection<Activity> doGetActivities(
      UserId userId,
      GroupId groupId,
      String appId,
      Set<String> fields,
      CollectionOptions collectionOptions,
      Set<String> activityIds,
      SecurityToken securityToken)
      throws Exception {

    ThemeDisplay themeDisplay = getThemeDisplay(securityToken);

    long userIdLong = GetterUtil.getLong(userId.getUserId(securityToken));

    List<Activity> activities = getActivities(themeDisplay, userIdLong);

    return new RestfulCollection<Activity>(
        activities, collectionOptions.getFirst(), activities.size(), collectionOptions.getMax());
  }
Exemplo n.º 9
0
  /** Get the set of user id's from a user and group */
  @SuppressWarnings("unchecked")
  private Set<String> getIdSet(UserId userId, GroupId groupId, SecurityToken token)
      throws HibernateException {

    String user = userId.getUserId(token);

    if (groupId == null) {
      return ImmutableSortedSet.of(user);
    }

    Set<String> idSet = new HashSet<String>();

    //		Session hs = HibernateUtil.getSessionFactory().getCurrentSession();
    //		Transaction tran = null;

    switch (groupId.getType()) {
      case all:
        // idSet.add(user);
        List<String> allUserIds = null;
        try {
          // List<String> allUserIds = sqlMap.queryForList(
          // "getAllUserIds" );
          Session hs = HibernateUtil.getSessionFactory().getCurrentSession();
          Transaction tran = hs.beginTransaction();

          // Criteria crit = hs.createCriteria(User.class);
          Query q = hs.createQuery("select id from User");
          allUserIds = (List<String>) q.list();

        } catch (HibernateException e) {

          HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().rollback();
          e.printStackTrace();
        }

        for (String id : allUserIds) {
          idSet.add(id);
        }
        break;
      case groupId:
      case friends:
        try {
          /**
           * This filter can be any field of the object being filtered or the special js filters,
           * hasApp or topFriends. Other special Filters defined in the OpenSocial v.9 specification
           * are
           *
           * <dl>
           *   <dt>all
           *   <dd>Retrieves all friends
           *   <dt>hasApp
           *   <dd>Retrieves all friends with any data for this application.
           *   <dt>'topFriends
           *   <dd>Retrieves only the user's top friends.
           *   <dt>isFriendsWith
           *   <dd>Only "hasApp filter" is implemented here
           * </dl>
           */
          Session hs = HibernateUtil.getSessionFactory().getCurrentSession();
          Transaction tran = hs.beginTransaction();

          // List<String> friendsIds = sqlMap.queryForList("getFriendsIds",user);

          List<String> friendsIds = new ArrayList<String>();

          User userObject = (User) hs.get(User.class, user);

          Set<User> friends = userObject.getFriendsByMe();
          Set<User> friendsByOthers = userObject.getFriendsByOther();

          friends.addAll(friendsByOthers);

          for (User friend : friends) {
            friendsIds.add(friend.getId());
          }
          idSet.addAll(friendsIds);

        } catch (HibernateException e) {

          HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().rollback();
          e.printStackTrace();
        }

        break;
      case self:
        idSet.add(user);
        break;
    }
    return idSet;
  }
Exemplo n.º 10
0
  public Future<Void> createActivity(
      UserId userId,
      GroupId groupId,
      String appId,
      Set<String> fields,
      Activity activity,
      SecurityToken token)
      throws ProtocolException {

    Session hs = HibernateUtil.getSessionFactory().getCurrentSession();
    Transaction tran = null;

    try {
      Long startTime = new Date().getTime();

      tran = hs.beginTransaction();

      // check if the person exists
      String user = userId.getUserId(token);
      Person person = (Person) hs.get(Person.class, user);

      if (person == null)
        throw new ProtocolException(HttpServletResponse.SC_BAD_REQUEST, "Person does not exist");

      if (activity.getStreamFaviconUrl() != null
          || activity.getStreamSourceUrl() != null
          || activity.getStreamTitle() != null
          || activity.getStreamUrl() != null)
        throw new ProtocolException(
            HttpServletResponse.SC_BAD_REQUEST, "Creating activity with stream params");

      // create Activity
      com.skt.opensocial.persistence.Activity activityDB =
          this.setActivityDBFromActivity(person, appId, activity);
      activityDB.setPerson(person);

      // auto increment "id" is the activity id
      Integer newId = (Integer) hs.save(activityDB);
      activityDB.setActivityId(newId.toString());

      // set up the posted time
      Long endTime = new Date().getTime();
      activityDB.setPostedTime(new Double(endTime - startTime));

      hs.saveOrUpdate(activityDB);

      // **** insert activity with mediaItems and templateParams separately ****//
      // create MediaItems of the Activity
      List<MediaItem> items = activity.getMediaItems();

      if (items != null) {
        for (MediaItem item : items) {
          com.skt.opensocial.persistence.ActivityMediaItem mediaItemDB =
              this.setMediaItemDBFromMediaItem(user, activityDB.getActivityId(), item);
          hs.saveOrUpdate(mediaItemDB);
        }
      }

      // create TemplateParams of the activity
      Map<String, String> params = activity.getTemplateParams();

      if (params != null) {
        Set<String> keys = params.keySet();
        for (String key : keys) {
          com.skt.opensocial.persistence.ActivityTemplateParam templateParamDB =
              this.setTemplateParamDBFromTemplateParma(
                  user, activityDB.getActivityId(), key, params.get(key));
          hs.saveOrUpdate(templateParamDB);
        }
      }

      tran.commit();

      return ImmediateFuture.newInstance(null);

    } catch (HibernateException e) {
      if (tran != null) tran.rollback();

      throw new ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage(), e);
    }
  }
Exemplo n.º 11
0
  private org.apache.rave.model.Person getPersonForId(UserId id, SecurityToken token) {

    return getFromRepository(id.getUserId(token));
  }