@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);
    }
  }
  @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);
    }
  }