示例#1
0
 /* (non-Javadoc)
  * @see org.apache.shindig.social.opensocial.spi.ActivityService#getActivities(org.apache.shindig.social.opensocial.spi.UserId, org.apache.shindig.social.opensocial.spi.GroupId, java.lang.String, java.util.Set, org.apache.shindig.social.opensocial.spi.CollectionOptions, java.util.Set, org.apache.shindig.auth.SecurityToken)
  */
 public Future<RestfulCollection<Activity>> getActivities(
     UserId userId,
     GroupId groupId,
     String appId,
     Set<String> fields,
     CollectionOptions options,
     Set<String> activityIds,
     SecurityToken token)
     throws ProtocolException {
   return ImmediateFuture.newInstance(
       new RestfulCollection<Activity>(getActivities(userId, activityIds, token)));
 }
示例#2
0
 /* (non-Javadoc)
  * @see org.apache.shindig.social.opensocial.spi.ActivityService#getActivity(org.apache.shindig.social.opensocial.spi.UserId, org.apache.shindig.social.opensocial.spi.GroupId, java.lang.String, java.util.Set, java.lang.String, org.apache.shindig.auth.SecurityToken)
  */
 public Future<Activity> getActivity(
     UserId userId,
     GroupId groupId,
     String appId,
     Set<String> fields,
     String activityId,
     SecurityToken token)
     throws ProtocolException {
   Activity activity = getActivities(userId, activityId, token);
   if (activity != null) {
     return ImmediateFuture.newInstance(activity);
   }
   throw new ProtocolException(HttpServletResponse.SC_BAD_REQUEST, "Cant find activity");
 }
  public Future<Void> deleteActivities(
      UserId userId,
      GroupId groupId,
      String appId,
      Set<String> activityIds,
      SecurityToken securityToken)
      throws ProtocolException {

    try {
      doDeleteActivities(userId, groupId, appId, activityIds, securityToken);

      return ImmediateFuture.newInstance(null);
    } catch (Exception e) {
      if (_log.isDebugEnabled()) {
        _log.debug(e, e);
      }

      throw new ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage(), e);
    }
  }
  public Future<Activity> getActivity(
      UserId userId,
      GroupId groupId,
      String appId,
      Set<String> fields,
      String activityId,
      SecurityToken securityToken)
      throws ProtocolException {

    try {
      Activity activity = doGetActivity(userId, groupId, appId, fields, activityId, securityToken);

      return ImmediateFuture.newInstance(activity);
    } catch (Exception e) {
      if (_log.isDebugEnabled()) {
        _log.debug(e, e);
      }

      throw new ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage(), e);
    }
  }
  public Future<RestfulCollection<Activity>> getActivities(
      Set<UserId> userIds,
      GroupId groupId,
      String appId,
      Set<String> fields,
      CollectionOptions collectionOptions,
      SecurityToken securityToken)
      throws ProtocolException {

    try {
      RestfulCollection<Activity> activities =
          doGetActivities(userIds, groupId, appId, fields, collectionOptions, securityToken);

      return ImmediateFuture.newInstance(activities);
    } catch (Exception e) {
      if (_log.isDebugEnabled()) {
        _log.debug(e, e);
      }

      throw new ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage(), e);
    }
  }
示例#6
0
  @SuppressWarnings("unchecked")
  public Future<RestfulCollection<Activity>> getActivities(
      Set<UserId> userIds,
      GroupId groupId,
      String appId,
      Set<String> fields,
      CollectionOptions options,
      SecurityToken token)
      throws ProtocolException {

    List<Activity> activityList = Lists.newArrayList();

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

    try {

      Set<String> idSet = getIdSet(userIds, groupId, options, token);

      tran = hs.beginTransaction();

      for (String userId : idSet) {

        Person person = (Person) hs.get(Person.class, userId);

        if (person == null) continue;

        Set<com.skt.opensocial.persistence.Activity> activityDBSet = person.getActivities();

        for (com.skt.opensocial.persistence.Activity activityDB : activityDBSet) {
          if (appId != null) {
            if (activityDB.getAppId() != null && !activityDB.getAppId().equals(appId)) {
              continue;
            }
          }

          Activity activity = new ActivityImpl();
          activity = HDBTableMapper.getActivityFromActivityDB(activityDB);

          // *** get MediaItems ***//
          Criteria crit = hs.createCriteria(ActivityMediaItem.class);
          crit.setMaxResults(30);

          crit = hs.createCriteria(ActivityMediaItem.class);
          List<ActivityMediaItem> items = crit.add(Restrictions.eq("userId", userId)).list();
          if (items.size() > 0) {
            List<MediaItem> mediaItemList =
                HDBTableMapper.getMediaItemListFromMediaItemDBList(items);
            activity.setMediaItems(mediaItemList);
          }

          // *** get TemplateParams ***//
          Criteria crit2 = hs.createCriteria(ActivityMediaItem.class);
          crit2.setMaxResults(30);

          crit2 = hs.createCriteria(ActivityTemplateParam.class);
          List<ActivityTemplateParam> params = crit2.add(Restrictions.eq("userId", userId)).list();
          if (params.size() > 0) {
            Map<String, String> templateParamMap =
                HDBTableMapper.getTemplateParamMapFromTemplateParamDBList(params);
            activity.setTemplateParams(templateParamMap);
          }

          activityList.add(activity);
        }
      }

      tran.commit();

      return ImmediateFuture.newInstance(new RestfulCollection<Activity>(activityList));

    } catch (HibernateException e) {

      if (tran != null) tran.rollback();

      throw new ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage(), e);
    }
  }
示例#7
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);
    }
  }
示例#8
0
  @SuppressWarnings("unchecked")
  public Future<Void> deleteActivities(
      UserId userId, GroupId groupId, String appId, Set<String> activityIds, SecurityToken token)
      throws ProtocolException {

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

    try {

      Set<String> idSet = getIdSet(userId, groupId, token);

      tran = hs.beginTransaction();

      for (String user : idSet) {

        Person person = (Person) hs.get(Person.class, user);

        if (person == null) continue;

        Set<com.skt.opensocial.persistence.Activity> activityDBSet = person.getActivities();

        for (com.skt.opensocial.persistence.Activity activityDB : activityDBSet) {
          if (appId != null) {
            if (activityDB.getAppId() != null && !activityDB.getAppId().equals(appId)) {
              continue;
            }
          }

          Activity activity = new ActivityImpl();
          activity = HDBTableMapper.getActivityFromActivityDB(activityDB);

          String activityId = activity.getId();

          if (activityIds != null) {
            if (activityId != null && !activityIds.contains(activityId)) {
              continue;
            }
          }

          // *** delete MediaItems ***//
          Criteria crit = hs.createCriteria(ActivityMediaItem.class);

          // filer by an userId and an activityId
          crit = hs.createCriteria(ActivityMediaItem.class);
          List<ActivityMediaItem> items =
              crit.add(Restrictions.eq("userId", user))
                  .add(Restrictions.eq("activityId", activityId))
                  .list();

          for (ActivityMediaItem item : items) {
            hs.delete(item);
          }

          // *** delete TemplateParams ***//
          Criteria crit2 = hs.createCriteria(ActivityTemplateParam.class);

          // filer by an userId and an activityId
          crit2 = hs.createCriteria(ActivityTemplateParam.class);
          List<ActivityTemplateParam> params =
              crit2
                  .add(Restrictions.eq("userId", user))
                  .add(Restrictions.eq("activityId", activityId))
                  .list();

          for (ActivityTemplateParam param : params) {
            hs.delete(param);
          }

          // *** delete activity ***//
          hs.delete(activity);
        }
      }

      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);
    }
  }
示例#9
0
  /* (non-Javadoc)
   * @see org.apache.shindig.social.opensocial.spi.ActivityService#getActivities(java.util.Set, org.apache.shindig.social.opensocial.spi.GroupId, java.lang.String, java.util.Set, org.apache.shindig.social.opensocial.spi.CollectionOptions, org.apache.shindig.auth.SecurityToken)
   */
  public Future<RestfulCollection<Activity>> getActivities(
      Set<UserId> userIds,
      GroupId groupId,
      String appId,
      Set<String> fields,
      CollectionOptions options,
      SecurityToken token)
      throws ProtocolException {

    List<Activity> plist = null;
    int lastPos = 1;

    StringBuilder sb = new StringBuilder();
    // sanitize the list to get the uid's and remove duplicates
    List<String> paramList = SPIUtils.getUserList(userIds, token);
    // select the group Id as this will drive the query
    switch (groupId.getType()) {
      case all:
        // select all contacts activity
        sb.append(ActivityDb.JPQL_FINDACTIVITY);
        JPQLUtils.addInClause(sb, "a", "userId", lastPos, paramList.size());
        sb.append(" or a.userId in (select m.person.id from GroupDb g join g.members m where ");
        lastPos = JPQLUtils.addInClause(sb, "g.owner", "id", lastPos, paramList.size());
        sb.append(")");
        break;
      case friends:
        // select all friends (subset of contacts)
        sb.append(ActivityDb.JPQL_FINDACTIVITY_BY_FRIENDS);
        lastPos = JPQLUtils.addInClause(sb, "p", "id", lastPos, paramList.size());
        sb.append(")) ");
        // TODO Group by doesn't work in HSQLDB or Derby - causes a "Not in aggregate function or
        // group by clause" jdbc exception
        // sb.append(" group by p ");
        break;
      case groupId:
        sb.append(ActivityDb.JPQL_FINDACTIVITY_BY_GROUP);
        paramList.add(groupId.getGroupId());
        lastPos += 2;
        break;
      case deleted:
        // ???
        break;
      case self:
        // select self
        sb.append(ActivityDb.JPQL_FINDACTIVITY);
        lastPos = JPQLUtils.addInClause(sb, "a", "userId", lastPos, paramList.size());
        break;
      default:
        throw new ProtocolException(HttpServletResponse.SC_BAD_REQUEST, "Group ID not recognized");
    }

    JPQLUtils.addFilterClause(sb, ActivityDb.FILTER_CAPABILITY, options, paramList);

    EntityManager entityManager = getEntityManager();

    // Get total results, that is count the total number of rows for this query
    Long totalResults = JPQLUtils.getTotalResults(entityManager, sb.toString(), paramList);
    if (options == null) options = new CollectionOptions();

    // Execute paginated query
    if (totalResults > 0) {
      JPQLUtils.addOrderClause(sb, options, "a");
      plist = JPQLUtils.getListQuery(entityManager, sb.toString(), paramList, options);
    }
    entityManager.close();
    if (plist == null) {
      plist = new ArrayList<Activity>();
    }

    // all of the above could equally have been placed into a thread to overlay the
    // db wait times.
    RestfulCollection<Activity> restCollection =
        new RestfulCollection<Activity>(
            plist, options.getFirst(), totalResults.intValue(), options.getMax());
    return ImmediateFuture.newInstance(restCollection);
  }