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);
  }
  public WallEntry addWallEntry(
      long groupId, long userId, String comments, ThemeDisplay themeDisplay)
      throws PortalException, SystemException {

    // Wall entry

    Group group = GroupLocalServiceUtil.getGroup(groupId);
    User user = userLocalService.getUserById(userId);
    Date now = new Date();

    long wallEntryId = counterLocalService.increment();

    WallEntry wallEntry = wallEntryPersistence.create(wallEntryId);

    wallEntry.setGroupId(groupId);
    wallEntry.setCompanyId(user.getCompanyId());
    wallEntry.setUserId(user.getUserId());
    wallEntry.setUserName(user.getFullName());
    wallEntry.setCreateDate(now);
    wallEntry.setModifiedDate(now);
    wallEntry.setComments(comments);

    wallEntryPersistence.update(wallEntry, false);

    // Email

    try {
      sendEmail(wallEntry, themeDisplay);
    } catch (Exception e) {
      throw new SystemException(e);
    }

    // Social

    if (userId != group.getClassPK()) {
      SocialActivityLocalServiceUtil.addActivity(
          userId,
          groupId,
          WallEntry.class.getName(),
          wallEntryId,
          WallActivityKeys.ADD_ENTRY,
          StringPool.BLANK,
          group.getClassPK());
    }

    return wallEntry;
  }
  public MicroblogsEntry addMicroblogsEntry(
      long userId,
      String content,
      int type,
      long receiverUserId,
      long receiverMicroblogsEntryId,
      int socialRelationType,
      ServiceContext serviceContext)
      throws PortalException, SystemException {

    // Microblogs entry

    User user = userPersistence.findByPrimaryKey(userId);

    if (receiverUserId == 0) {
      receiverUserId = userId;
    }

    Date now = new Date();

    validate(type, receiverMicroblogsEntryId);

    long microblogsEntryId = counterLocalService.increment();

    MicroblogsEntry microblogsEntry = microblogsEntryPersistence.create(microblogsEntryId);

    microblogsEntry.setCompanyId(user.getCompanyId());
    microblogsEntry.setUserId(user.getUserId());
    microblogsEntry.setUserName(user.getFullName());
    microblogsEntry.setCreateDate(now);
    microblogsEntry.setModifiedDate(now);
    microblogsEntry.setContent(content);
    microblogsEntry.setType(type);
    microblogsEntry.setReceiverUserId(receiverUserId);
    microblogsEntry.setReceiverMicroblogsEntryId(receiverMicroblogsEntryId);
    microblogsEntry.setSocialRelationType(socialRelationType);

    microblogsEntryPersistence.update(microblogsEntry);

    // Resources

    resourceLocalService.addModelResources(microblogsEntry, serviceContext);

    // Asset

    updateAsset(
        microblogsEntry, serviceContext.getAssetCategoryIds(), serviceContext.getAssetTagNames());

    // Social

    int activityKey = MicroblogsActivityKeys.ADD_ENTRY;

    if (type == MicroblogsEntryConstants.TYPE_REPLY) {
      activityKey = MicroblogsActivityKeys.REPLY_ENTRY;
    } else if (type == MicroblogsEntryConstants.TYPE_REPOST) {
      activityKey = MicroblogsActivityKeys.REPOST_ENTRY;
    }

    JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();

    extraDataJSONObject.put("content", microblogsEntry.getContent());
    extraDataJSONObject.put("receiverMicroblogsEntryId", receiverMicroblogsEntryId);

    SocialActivityLocalServiceUtil.addActivity(
        userId,
        0,
        MicroblogsEntry.class.getName(),
        microblogsEntryId,
        activityKey,
        extraDataJSONObject.toString(),
        receiverUserId);

    // Notification

    if (type == MicroblogsEntryConstants.TYPE_REPLY) {
      sendNotificationEvent(microblogsEntry);
    }

    return microblogsEntry;
  }
  public TasksEntry addTasksEntry(
      long userId,
      String title,
      int priority,
      long assigneeUserId,
      int dueDateMonth,
      int dueDateDay,
      int dueDateYear,
      int dueDateHour,
      int dueDateMinute,
      boolean neverDue,
      ServiceContext serviceContext)
      throws PortalException, SystemException {

    // Tasks entry

    User user = UserLocalServiceUtil.getUserById(userId);
    long groupId = serviceContext.getScopeGroupId();
    Date now = new Date();

    Date dueDate = null;

    if (!neverDue) {
      dueDate =
          PortalUtil.getDate(
              dueDateMonth,
              dueDateDay,
              dueDateYear,
              dueDateHour,
              dueDateMinute,
              user.getTimeZone(),
              new TasksEntryDueDateException());
    }

    long tasksEntryId = CounterLocalServiceUtil.increment();

    TasksEntry tasksEntry = tasksEntryPersistence.create(tasksEntryId);

    tasksEntry.setGroupId(groupId);
    tasksEntry.setCompanyId(user.getCompanyId());
    tasksEntry.setUserId(user.getUserId());
    tasksEntry.setUserName(user.getFullName());
    tasksEntry.setCreateDate(now);
    tasksEntry.setModifiedDate(now);
    tasksEntry.setTitle(title);
    tasksEntry.setPriority(priority);
    tasksEntry.setAssigneeUserId(assigneeUserId);
    tasksEntry.setDueDate(dueDate);
    tasksEntry.setStatus(TasksEntryConstants.STATUS_OPEN);

    tasksEntryPersistence.update(tasksEntry, false);

    // Asset

    updateAsset(
        userId,
        tasksEntry,
        serviceContext.getAssetCategoryIds(),
        serviceContext.getAssetTagNames());

    // Social

    SocialActivityLocalServiceUtil.addActivity(
        userId,
        groupId,
        TasksEntry.class.getName(),
        tasksEntryId,
        TasksActivityKeys.ADD_ENTRY,
        StringPool.BLANK,
        assigneeUserId);

    return tasksEntry;
  }
  public TasksEntry updateTasksEntry(
      long tasksEntryId,
      String title,
      int priority,
      long assigneeUserId,
      long resolverUserId,
      int dueDateMonth,
      int dueDateDay,
      int dueDateYear,
      int dueDateHour,
      int dueDateMinute,
      boolean neverDue,
      int status,
      ServiceContext serviceContext)
      throws PortalException, SystemException {

    // Tasks entry

    Date now = new Date();

    TasksEntry tasksEntry = tasksEntryPersistence.findByPrimaryKey(tasksEntryId);

    User user = UserLocalServiceUtil.getUserById(tasksEntry.getUserId());

    Date dueDate = null;

    if (!neverDue) {
      dueDate =
          PortalUtil.getDate(
              dueDateMonth,
              dueDateDay,
              dueDateYear,
              dueDateHour,
              dueDateMinute,
              user.getTimeZone(),
              new TasksEntryDueDateException());
    }

    tasksEntry.setModifiedDate(now);
    tasksEntry.setTitle(title);
    tasksEntry.setPriority(priority);
    tasksEntry.setAssigneeUserId(assigneeUserId);
    tasksEntry.setDueDate(dueDate);

    if (status == TasksEntryConstants.STATUS_RESOLVED) {
      tasksEntry.setResolverUserId(resolverUserId);
      tasksEntry.setFinishDate(now);
    } else {
      tasksEntry.setResolverUserId(0);
      tasksEntry.setFinishDate(null);
    }

    tasksEntry.setStatus(status);

    tasksEntryPersistence.update(tasksEntry, false);

    // Asset

    updateAsset(
        tasksEntry.getUserId(),
        tasksEntry,
        serviceContext.getAssetCategoryIds(),
        serviceContext.getAssetTagNames());

    // Social

    int activity = TasksActivityKeys.UPDATE_ENTRY;

    if (status == TasksEntryConstants.STATUS_RESOLVED) {
      activity = TasksActivityKeys.RESOLVE_ENTRY;
    } else if (status == TasksEntryConstants.STATUS_REOPENED) {
      activity = TasksActivityKeys.REOPEN_ENTRY;
    }

    SocialActivityLocalServiceUtil.addActivity(
        serviceContext.getUserId(),
        tasksEntry.getGroupId(),
        TasksEntry.class.getName(),
        tasksEntryId,
        activity,
        StringPool.BLANK,
        assigneeUserId);

    return tasksEntry;
  }