コード例 #1
0
  protected Activity getActivity(ThemeDisplay themeDisplay, SocialActivity socialActivity)
      throws Exception {

    Activity activity = null;

    String className = socialActivity.getClassName();

    if (className.equals(Activity.class.getName())) {
      activity = getExternalActivity(socialActivity);
    } else {
      activity =
          new ActivityImpl(
              String.valueOf(socialActivity.getClassPK()),
              String.valueOf(socialActivity.getUserId()));

      HttpServletRequest request = HttpServletRequestThreadLocal.getHttpServletRequest();

      request.setAttribute(WebKeys.THEME_DISPLAY, themeDisplay);

      ServiceContext serviceContext = ServiceContextFactory.getInstance(request);

      serviceContext.setCompanyId(themeDisplay.getCompanyId());
      serviceContext.setUserId(themeDisplay.getUserId());

      SocialActivityFeedEntry socialActivityFeedEntry = null;
      SocialActivityInterpreterLocalServiceUtil.interpret(
          StringPool.BLANK, socialActivity, serviceContext);

      activity.setBody(socialActivityFeedEntry.getBody());
      activity.setTitle(socialActivityFeedEntry.getTitle());
      activity.setUrl(socialActivityFeedEntry.getLink());
    }

    return activity;
  }
コード例 #2
0
  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);
  }
コード例 #3
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;
  }
コード例 #4
0
 public void testGetExpectedActivity() throws Exception {
   Activity activity =
       db.getActivity(
               CANON_USER,
               SELF_GROUP,
               APP_ID,
               ImmutableSet.of("appId", "body", "mediaItems"),
               APP_ID,
               new FakeGadgetToken())
           .get();
   assertNotNull(activity);
   // Check that some fields are fetched and others are not
   assertNotNull(activity.getBody());
   assertNull(activity.getBodyId());
 }
コード例 #5
0
  private com.skt.opensocial.persistence.Activity setActivityDBFromActivity(
      Person user, String appId, Activity activity) {
    com.skt.opensocial.persistence.Activity activityDB =
        new com.skt.opensocial.persistence.Activity();

    activityDB.setAppId(appId);

    activityDB.setBody(activity.getBody());
    activityDB.setBodyId(activity.getBodyId());
    activityDB.setExternalId(activity.getExternalId());

    if (activity.getPriority() != null) activityDB.setPriority(new Double(activity.getPriority()));
    else activityDB.setPriority(new Double(0));

    activityDB.setStreamFaviconUrl(activity.getStreamFaviconUrl());
    activityDB.setStreamSourceUrl(activity.getStreamSourceUrl());
    activityDB.setStreamTitle(activity.getStreamTitle());
    activityDB.setStreamUrl(activity.getStreamUrl());
    activityDB.setTitle(activity.getTitle());

    // there should one of Title or TitleId
    if (activityDB.getTitle() == null) activityDB.setTitleId(activity.getTitleId());

    activityDB.setUpdated(activity.getUpdated());
    activityDB.setUrl(activity.getUrl());
    activityDB.setPerson(user);

    return activityDB;
  }
コード例 #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);
    }
  }