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