Beispiel #1
0
  /* (non-Javadoc)
   * @see org.apache.shindig.social.opensocial.spi.ActivityService#createActivity(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.model.Activity, org.apache.shindig.auth.SecurityToken)
   */
  public Future<Void> createActivity(
      UserId userId,
      GroupId groupId,
      String appId,
      Set<String> fields,
      Activity activity,
      SecurityToken token)
      throws ProtocolException {
    String uid = SPIUtils.getUserList(userId, token);

    EntityManager entityManager = getEntityManager();
    try {
      activity.setPostedTime(new Date().getTime());
      activity.setAppId(appId);
      activity.setUserId(uid);
      activity.setUpdated(new Date());
      activity.setId("activity" + SPIUtils.generateId(uid + activity.getPostedTime()));
      activity.setUrl(activity.getUrl() + activity.getId());
      // TODO How should transactions be managed? Should samples be using warp-persist instead?
      if (!entityManager.getTransaction().isActive()) {
        entityManager.getTransaction().begin();
      }
      entityManager.persist(activity);
      entityManager.getTransaction().commit();

    } catch (Exception e) {
      throw new ProtocolException(
          HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Failed to create activity", e);
    } finally {
      entityManager.close();
    }

    return null;
  }
  @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);
    }
  }
  @SuppressWarnings("unchecked")
  public Future<Activity> getActivity(
      UserId userId,
      GroupId groupId,
      String appId,
      Set<String> fields,
      String activityId,
      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);

          if (activityId != null) {
            if (activity.getId() != null && !activityId.equals(activity.getId())) {
              continue;
            }
          }

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

          // 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();
          if (items.size() > 0) {
            List<MediaItem> mediaItemList =
                HDBTableMapper.getMediaItemListFromMediaItemDBList(items);
            activity.setMediaItems(mediaItemList);
          }

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

          // 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();
          if (params.size() > 0) {
            Map<String, String> templateParamMap =
                HDBTableMapper.getTemplateParamMapFromTemplateParamDBList(params);
            activity.setTemplateParams(templateParamMap);
          }

          tran.commit();

          return ImmediateFuture.newInstance(activity);
        }
      }

      tran.rollback();

      throw new ProtocolException(HttpServletResponse.SC_BAD_REQUEST, "Activity not found");

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

      throw new ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage(), e);
    }
  }