protected void checkPopulatedServiceContext(
      ServiceContext serviceContext, WikiPage page, boolean hasExpandoValues) throws Exception {

    long[] assetCategoryIds =
        AssetCategoryLocalServiceUtil.getCategoryIds(
            WikiPage.class.getName(), page.getResourcePrimKey());

    Assert.assertArrayEquals(serviceContext.getAssetCategoryIds(), assetCategoryIds);

    AssetEntry assetEntry =
        AssetEntryLocalServiceUtil.getEntry(WikiPage.class.getName(), page.getResourcePrimKey());

    List<AssetLink> assetLinks = AssetLinkLocalServiceUtil.getLinks(assetEntry.getEntryId());

    long[] assetLinkEntryIds = ListUtil.toLongArray(assetLinks, AssetLink.ENTRY_ID2_ACCESSOR);

    Assert.assertArrayEquals(serviceContext.getAssetLinkEntryIds(), assetLinkEntryIds);

    String[] assetTagNames =
        AssetTagLocalServiceUtil.getTagNames(WikiPage.class.getName(), page.getResourcePrimKey());

    Assert.assertArrayEquals(serviceContext.getAssetTagNames(), assetTagNames);

    if (hasExpandoValues) {
      ExpandoBridge expandoBridge = page.getExpandoBridge();

      AssertUtils.assertEquals(
          expandoBridge.getAttributes(), serviceContext.getExpandoBridgeAttributes());
    }
  }
  @Override
  public CalendarResource updateCalendarResource(
      long calendarResourceId,
      Map<Locale, String> nameMap,
      Map<Locale, String> descriptionMap,
      boolean active,
      ServiceContext serviceContext)
      throws PortalException {

    // Calendar resource

    validate(nameMap);

    CalendarResource calendarResource =
        calendarResourcePersistence.findByPrimaryKey(calendarResourceId);

    calendarResource.setModifiedDate(serviceContext.getModifiedDate(null));
    calendarResource.setNameMap(nameMap);
    calendarResource.setDescriptionMap(descriptionMap);
    calendarResource.setActive(active);

    calendarResourcePersistence.update(calendarResource);

    // Asset

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

    return calendarResource;
  }
  public void updateFoo(
      long fooId,
      String field1,
      boolean field2,
      int field3,
      Date field4,
      String field5,
      ServiceContext serviceContext)
      throws PortalException, SystemException {

    User user = userPersistence.findByPrimaryKey(serviceContext.getUserId());

    Foo foo = fooPersistence.findByPrimaryKey(fooId);

    foo.setModifiedDate(new Date());
    foo.setField1(field1);
    foo.setField2(field2);
    foo.setField3(field3);
    foo.setField4(field4);
    foo.setField5(field5);
    foo.setExpandoBridgeAttributes(serviceContext);

    fooPersistence.update(foo, false);

    updateAsset(
        user.getUserId(),
        foo,
        serviceContext.getAssetCategoryIds(),
        serviceContext.getAssetTagNames());
  }
  protected void cascadeFileEntryTypes(
      long userId,
      long groupId,
      long folderId,
      long defaultFileEntryTypeId,
      List<Long> fileEntryTypeIds,
      ServiceContext serviceContext)
      throws PortalException {

    List<DLFileEntry> dlFileEntries = dlFileEntryPersistence.findByG_F(groupId, folderId);

    for (DLFileEntry dlFileEntry : dlFileEntries) {
      Long fileEntryTypeId = dlFileEntry.getFileEntryTypeId();

      if (fileEntryTypeIds.contains(fileEntryTypeId)) {
        continue;
      }

      DLFileVersion dlFileVersion =
          dlFileVersionLocalService.getLatestFileVersion(dlFileEntry.getFileEntryId(), true);

      if (dlFileVersion.isPending()) {
        workflowInstanceLinkLocalService.deleteWorkflowInstanceLink(
            dlFileVersion.getCompanyId(),
            groupId,
            DLFileEntry.class.getName(),
            dlFileVersion.getFileVersionId());
      }

      dlFileEntryLocalService.updateFileEntryType(
          userId, dlFileEntry.getFileEntryId(), defaultFileEntryTypeId, serviceContext);

      dlAppHelperLocalService.updateAsset(
          userId,
          new LiferayFileEntry(dlFileEntry),
          new LiferayFileVersion(dlFileVersion),
          serviceContext.getAssetCategoryIds(),
          serviceContext.getAssetTagNames(),
          serviceContext.getAssetLinkEntryIds());
    }

    List<DLFolder> subFolders = dlFolderPersistence.findByG_M_P_H(groupId, false, folderId, false);

    for (DLFolder subFolder : subFolders) {
      long subFolderId = subFolder.getFolderId();

      if (subFolder.getRestrictionType() == DLFolderConstants.RESTRICTION_TYPE_INHERIT) {

        continue;
      }

      cascadeFileEntryTypes(
          userId, groupId, subFolderId, defaultFileEntryTypeId, fileEntryTypeIds, serviceContext);
    }
  }
  @Override
  public MicroblogsEntry addMicroblogsEntry(
      long userId,
      long creatorClassNameId,
      long creatorClassPK,
      String content,
      int type,
      long parentMicroblogsEntryId,
      int socialRelationType,
      ServiceContext serviceContext)
      throws PortalException {

    // Microblogs entry

    User user = userPersistence.findByPrimaryKey(userId);

    Date now = new Date();

    validate(type, parentMicroblogsEntryId);

    long microblogsEntryId = counterLocalService.increment();

    if (parentMicroblogsEntryId == 0) {
      parentMicroblogsEntryId = microblogsEntryId;
    }

    MicroblogsEntry microblogsEntry = microblogsEntryPersistence.create(microblogsEntryId);

    microblogsEntry.setCompanyId(user.getCompanyId());
    microblogsEntry.setUserId(user.getUserId());
    microblogsEntry.setUserName(user.getFullName());
    microblogsEntry.setCreateDate(now);
    microblogsEntry.setModifiedDate(now);
    microblogsEntry.setCreatorClassNameId(creatorClassNameId);
    microblogsEntry.setCreatorClassPK(creatorClassPK);
    microblogsEntry.setContent(content);
    microblogsEntry.setType(type);
    microblogsEntry.setParentMicroblogsEntryId(parentMicroblogsEntryId);
    microblogsEntry.setSocialRelationType(socialRelationType);

    microblogsEntryPersistence.update(microblogsEntry);

    // Resources

    resourceLocalService.addModelResources(microblogsEntry, serviceContext);

    // Asset

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

    return microblogsEntry;
  }
  public void updateFileEntry(
      long userId,
      FileEntry fileEntry,
      FileVersion sourceFileVersion,
      FileVersion destinationFileVersion,
      ServiceContext serviceContext)
      throws PortalException, SystemException {

    updateAsset(
        userId,
        fileEntry,
        destinationFileVersion,
        serviceContext.getAssetCategoryIds(),
        serviceContext.getAssetTagNames(),
        serviceContext.getAssetLinkEntryIds());

    registerDLProcessorCallback(fileEntry, sourceFileVersion);
  }
  public void addFoo(
      String field1,
      boolean field2,
      int field3,
      Date field4,
      String field5,
      ServiceContext serviceContext)
      throws PortalException, SystemException {

    // Foo

    User user = userLocalService.getUserById(serviceContext.getUserId());
    long groupId = serviceContext.getScopeGroupId();
    Date now = new Date();

    long fooId = counterLocalService.increment();

    Foo foo = fooPersistence.create(fooId);

    foo.setGroupId(groupId);
    foo.setCompanyId(user.getCompanyId());
    foo.setUserId(user.getUserId());
    foo.setUserName(user.getFullName());
    foo.setCreateDate(serviceContext.getCreateDate(now));
    foo.setModifiedDate(serviceContext.getModifiedDate(now));
    foo.setField1(field1);
    foo.setField2(field2);
    foo.setField3(field3);
    foo.setField4(field4);
    foo.setField5(field5);
    foo.setExpandoBridgeAttributes(serviceContext);

    fooPersistence.update(foo, false);

    // Asset

    updateAsset(
        user.getUserId(),
        foo,
        serviceContext.getAssetCategoryIds(),
        serviceContext.getAssetTagNames());
  }
  @Override
  public FileEntry copyFileEntry(
      long userId, long groupId, long fileEntryId, long destFolderId, ServiceContext serviceContext)
      throws PortalException {

    FileEntry fileEntry =
        super.copyFileEntry(userId, groupId, fileEntryId, destFolderId, serviceContext);

    DLAppHelperLocalServiceUtil.updateAsset(
        userId,
        fileEntry,
        fileEntry.getFileVersion(),
        serviceContext.getAssetCategoryIds(),
        serviceContext.getAssetTagNames(),
        serviceContext.getAssetLinkEntryIds());

    _workflowCapability.addFileEntry(userId, fileEntry, serviceContext);

    return fileEntry;
  }
  public void addFileEntry(
      long userId, FileEntry fileEntry, FileVersion fileVersion, ServiceContext serviceContext)
      throws PortalException, SystemException {

    updateAsset(
        userId,
        fileEntry,
        fileVersion,
        serviceContext.getAssetCategoryIds(),
        serviceContext.getAssetTagNames(),
        serviceContext.getAssetLinkEntryIds());

    if (PropsValues.DL_FILE_ENTRY_COMMENTS_ENABLED) {
      mbMessageLocalService.addDiscussionMessage(
          fileEntry.getUserId(), fileEntry.getUserName(),
          fileEntry.getGroupId(), DLFileEntryConstants.getClassName(),
          fileEntry.getFileEntryId(), WorkflowConstants.ACTION_PUBLISH);
    }

    if (fileVersion instanceof LiferayFileVersion) {
      DLFileVersion dlFileVersion = (DLFileVersion) fileVersion.getModel();

      Map<String, Serializable> workflowContext = new HashMap<String, Serializable>();

      workflowContext.put("event", DLSyncConstants.EVENT_ADD);

      WorkflowHandlerRegistryUtil.startWorkflowInstance(
          dlFileVersion.getCompanyId(),
          dlFileVersion.getGroupId(),
          userId,
          DLFileEntryConstants.getClassName(),
          dlFileVersion.getFileVersionId(),
          dlFileVersion,
          serviceContext,
          workflowContext);
    }

    registerDLProcessorCallback(fileEntry, null);
  }
  public MicroblogsEntry updateMicroblogsEntry(
      long microblogsEntryId, String content, int socialRelationType, ServiceContext serviceContext)
      throws PortalException, SystemException {

    // Microblogs entry

    MicroblogsEntry microblogsEntry =
        microblogsEntryPersistence.findByPrimaryKey(microblogsEntryId);

    microblogsEntry.setModifiedDate(new Date());
    microblogsEntry.setContent(content);
    microblogsEntry.setSocialRelationType(socialRelationType);

    microblogsEntryPersistence.update(microblogsEntry);

    // Asset

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

    return microblogsEntry;
  }
  @Override
  public FileEntry addFileEntry(
      long userId,
      long folderId,
      String sourceFileName,
      String mimeType,
      String title,
      String description,
      String changeLog,
      InputStream is,
      long size,
      ServiceContext serviceContext)
      throws PortalException {

    FileEntry fileEntry =
        super.addFileEntry(
            userId,
            folderId,
            sourceFileName,
            mimeType,
            title,
            description,
            changeLog,
            is,
            size,
            serviceContext);

    DLAppHelperLocalServiceUtil.updateAsset(
        userId,
        fileEntry,
        fileEntry.getFileVersion(),
        serviceContext.getAssetCategoryIds(),
        serviceContext.getAssetTagNames(),
        serviceContext.getAssetLinkEntryIds());

    _workflowCapability.addFileEntry(userId, fileEntry, serviceContext);

    return fileEntry;
  }
  @Override
  public CalendarResource addCalendarResource(
      long userId,
      long groupId,
      long classNameId,
      long classPK,
      String classUuid,
      String code,
      Map<Locale, String> nameMap,
      Map<Locale, String> descriptionMap,
      boolean active,
      ServiceContext serviceContext)
      throws PortalException {

    // Calendar resource

    User user = userPersistence.findByPrimaryKey(userId);

    long calendarResourceId = counterLocalService.increment();

    if (classNameId == classNameLocalService.getClassNameId(CalendarResource.class)) {

      classPK = calendarResourceId;
    }

    if (PortletPropsValues.CALENDAR_RESOURCE_FORCE_AUTOGENERATE_CODE || Validator.isNull(code)) {

      code = String.valueOf(calendarResourceId);
    } else {
      code = code.trim();
      code = StringUtil.toUpperCase(code);
    }

    Date now = new Date();

    validate(groupId, classNameId, classPK, code, nameMap);

    CalendarResource calendarResource = calendarResourcePersistence.create(calendarResourceId);

    calendarResource.setUuid(serviceContext.getUuid());
    calendarResource.setGroupId(groupId);
    calendarResource.setCompanyId(user.getCompanyId());
    calendarResource.setUserId(user.getUserId());
    calendarResource.setUserName(user.getFullName());
    calendarResource.setCreateDate(serviceContext.getCreateDate(now));
    calendarResource.setModifiedDate(serviceContext.getModifiedDate(now));
    calendarResource.setClassNameId(classNameId);
    calendarResource.setClassPK(classPK);
    calendarResource.setClassUuid(classUuid);
    calendarResource.setCode(code);
    calendarResource.setNameMap(nameMap);
    calendarResource.setDescriptionMap(descriptionMap);
    calendarResource.setActive(active);

    calendarResourcePersistence.update(calendarResource);

    // Resources

    resourceLocalService.addModelResources(calendarResource, serviceContext);

    // Calendar

    if (!ExportImportThreadLocal.isImportInProcess()) {
      serviceContext.setAddGroupPermissions(true);
      serviceContext.setAddGuestPermissions(true);

      calendarLocalService.addCalendar(
          userId,
          calendarResource.getGroupId(),
          calendarResourceId,
          nameMap,
          descriptionMap,
          calendarResource.getTimeZoneId(),
          PortletPropsValues.CALENDAR_COLOR_DEFAULT,
          true,
          false,
          false,
          serviceContext);
    }

    // Asset

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

    return calendarResource;
  }
  public sample.lrdev05.database.model.Books addBooks(
      Books booksParam, long userId, ServiceContext serviceContext) {
    sample.lrdev05.database.model.Books booksVar;

    try {
      booksVar = booksPersistence.create(counterLocalService.increment(Books.class.toString()));
    } catch (SystemException e) {
      e.printStackTrace();
      return booksVar = null;
    }

    booksVar.setTitle(booksParam.getTitle());
    booksVar.setAuthor(booksParam.getAuthor());
    booksVar.setIsbn(booksParam.getIsbn());
    booksVar.setSummary(booksParam.getSummary());
    booksVar.setCompanyId(booksParam.getCompanyId());
    booksVar.setGroupId(booksParam.getGroupId());
    booksVar.setTitle(booksParam.getTitle());
    booksVar.setStatus(WorkflowConstants.STATUS_DRAFT);

    try {
      resourceLocalService.addResources(
          booksParam.getCompanyId(),
          booksParam.getGroupId(),
          userId,
          Books.class.getName(),
          booksParam.getPrimaryKey(),
          false,
          true,
          true);
      assetEntryLocalService.updateEntry(
          userId,
          booksVar.getGroupId(),
          Books.class.getName(),
          booksVar.getBookId(),
          serviceContext.getAssetCategoryIds(),
          serviceContext.getAssetTagNames());
      // Workflow
      WorkflowHandlerRegistryUtil.startWorkflowInstance(
          booksVar.getCompanyId(),
          booksVar.getGroupId(),
          userId,
          Books.class.getName(),
          booksVar.getPrimaryKey(),
          booksVar,
          serviceContext);

      sendMessage(booksVar, serviceContext);

    } catch (PortalException e) {
      e.printStackTrace();
      return booksVar = null;
    } catch (SystemException e) {
      e.printStackTrace();
      return booksVar = null;
    }

    try {
      return booksPersistence.update(booksVar, false);
    } catch (SystemException e) {
      e.printStackTrace();
      return booksVar = null;
    }
  }
  @Indexable(type = IndexableType.REINDEX)
  public Album addAlbum(
      long userId,
      long artistId,
      String name,
      int year,
      InputStream inputStream,
      ServiceContext serviceContext)
      throws PortalException {

    long groupId = serviceContext.getScopeGroupId();

    User user = userPersistence.findByPrimaryKey(userId);

    Date now = new Date();

    validate(name);

    long albumId = counterLocalService.increment();

    Album album = albumPersistence.create(albumId);

    album.setUuid(serviceContext.getUuid());
    album.setGroupId(groupId);
    album.setCompanyId(user.getCompanyId());
    album.setUserId(user.getUserId());
    album.setUserName(user.getFullName());
    album.setCreateDate(serviceContext.getCreateDate(now));
    album.setModifiedDate(serviceContext.getModifiedDate(now));
    album.setArtistId(artistId);
    album.setName(name);
    album.setYear(year);
    album.setExpandoBridgeAttributes(serviceContext);

    albumPersistence.update(album);

    if (inputStream != null) {
      PortletFileRepositoryUtil.addPortletFileEntry(
          groupId,
          userId,
          Album.class.getName(),
          album.getAlbumId(),
          Constants.JUKEBOX_PORTLET_REPOSITORY,
          DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
          inputStream,
          String.valueOf(album.getAlbumId()),
          StringPool.BLANK,
          true);
    }

    // Resources

    if (serviceContext.isAddGroupPermissions() || serviceContext.isAddGuestPermissions()) {

      addEntryResources(
          album, serviceContext.isAddGroupPermissions(), serviceContext.isAddGuestPermissions());
    } else {
      addEntryResources(
          album, serviceContext.getGroupPermissions(), serviceContext.getGuestPermissions());
    }

    // Message boards

    mbMessageLocalService.addDiscussionMessage(
        userId,
        album.getUserName(),
        groupId,
        Album.class.getName(),
        albumId,
        WorkflowConstants.ACTION_PUBLISH);

    // Asset

    updateAsset(
        userId,
        album,
        serviceContext.getAssetCategoryIds(),
        serviceContext.getAssetTagNames(),
        serviceContext.getAssetLinkEntryIds());

    return album;
  }
  @Indexable(type = IndexableType.REINDEX)
  public Album updateAlbum(
      long userId,
      long albumId,
      long artistId,
      String name,
      int year,
      InputStream inputStream,
      ServiceContext serviceContext)
      throws PortalException {

    // Event

    User user = userPersistence.findByPrimaryKey(userId);

    validate(name);

    Album album = albumPersistence.findByPrimaryKey(albumId);

    album.setModifiedDate(serviceContext.getModifiedDate(null));
    album.setArtistId(artistId);
    album.setName(name);
    album.setYear(year);
    album.setExpandoBridgeAttributes(serviceContext);

    albumPersistence.update(album);

    if (inputStream != null) {
      Repository repository =
          PortletFileRepositoryUtil.fetchPortletRepository(
              serviceContext.getScopeGroupId(), Constants.JUKEBOX_PORTLET_REPOSITORY);

      if (repository != null) {
        try {
          PortletFileRepositoryUtil.deletePortletFileEntry(
              repository.getRepositoryId(),
              DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
              String.valueOf(album.getAlbumId()));
        } catch (Exception e) {
          if (_log.isDebugEnabled()) {
            _log.debug("Cannot delete album cover");
          }
        }
      }

      PortletFileRepositoryUtil.addPortletFileEntry(
          serviceContext.getScopeGroupId(),
          userId,
          Album.class.getName(),
          album.getAlbumId(),
          Constants.JUKEBOX_PORTLET_REPOSITORY,
          DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
          inputStream,
          String.valueOf(album.getAlbumId()),
          StringPool.BLANK,
          true);
    }

    // Asset

    updateAsset(
        userId,
        album,
        serviceContext.getAssetCategoryIds(),
        serviceContext.getAssetTagNames(),
        serviceContext.getAssetLinkEntryIds());

    return album;
  }
예제 #16
0
  public void merge(ServiceContext serviceContext) {
    setAddGroupPermissions(serviceContext.isAddGroupPermissions());
    setAddGuestPermissions(serviceContext.isAddGuestPermissions());

    if (serviceContext.getAssetCategoryIds() != null) {
      setAssetCategoryIds(serviceContext.getAssetCategoryIds());
    }

    if (serviceContext.getAssetLinkEntryIds() != null) {
      setAssetLinkEntryIds(serviceContext.getAssetLinkEntryIds());
    }

    if (serviceContext.getAssetTagNames() != null) {
      setAssetTagNames(serviceContext.getAssetTagNames());
    }

    if (serviceContext.getAttributes() != null) {
      setAttributes(serviceContext.getAttributes());
    }

    if (Validator.isNotNull(serviceContext.getCommand())) {
      setCommand(serviceContext.getCommand());
    }

    if (serviceContext.getCompanyId() > 0) {
      setCompanyId(serviceContext.getCompanyId());
    }

    if (serviceContext.getCreateDate() != null) {
      setCreateDate(serviceContext.getCreateDate());
    }

    if (Validator.isNotNull(serviceContext.getCurrentURL())) {
      setCurrentURL(serviceContext.getCurrentURL());
    }

    if (serviceContext.getExpandoBridgeAttributes() != null) {
      setExpandoBridgeAttributes(serviceContext.getExpandoBridgeAttributes());
    }

    if (serviceContext.getGroupPermissions() != null) {
      setGroupPermissions(serviceContext.getGroupPermissions());
    }

    if (serviceContext.getGuestPermissions() != null) {
      setGuestPermissions(serviceContext.getGuestPermissions());
    }

    if (serviceContext.getHeaders() != null) {
      setHeaders(serviceContext.getHeaders());
    }

    setFailOnPortalException(serviceContext.isFailOnPortalException());
    setLanguageId(serviceContext.getLanguageId());

    if (Validator.isNotNull(serviceContext.getLayoutFullURL())) {
      setLayoutFullURL(serviceContext.getLayoutFullURL());
    }

    if (Validator.isNotNull(serviceContext.getLayoutURL())) {
      setLayoutURL(serviceContext.getLayoutURL());
    }

    if (serviceContext.getModifiedDate() != null) {
      setModifiedDate(serviceContext.getModifiedDate());
    }

    if (Validator.isNotNull(serviceContext.getPathMain())) {
      setPathMain(serviceContext.getPathMain());
    }

    if (serviceContext.getPlid() > 0) {
      setPlid(serviceContext.getPlid());
    }

    if (Validator.isNotNull(serviceContext.getPortalURL())) {
      setPortalURL(serviceContext.getPortalURL());
    }

    if (serviceContext.getPortletPreferencesIds() != null) {
      setPortletPreferencesIds(serviceContext.getPortletPreferencesIds());
    }

    if (Validator.isNotNull(serviceContext.getRemoteAddr())) {
      setRemoteAddr(serviceContext.getRemoteAddr());
    }

    if (Validator.isNotNull(serviceContext.getRemoteHost())) {
      setRemoteHost(serviceContext.getRemoteHost());
    }

    if (serviceContext.getScopeGroupId() > 0) {
      setScopeGroupId(serviceContext.getScopeGroupId());
    }

    setSignedIn(serviceContext.isSignedIn());

    if (Validator.isNotNull(serviceContext.getUserDisplayURL())) {
      setUserDisplayURL(serviceContext.getUserDisplayURL());
    }

    if (serviceContext.getUserId() > 0) {
      setUserId(serviceContext.getUserId());
    }

    if (Validator.isNotNull(serviceContext.getUuid())) {
      setUuid(serviceContext.getUuid());
    }

    if (serviceContext.getWorkflowAction() > 0) {
      setWorkflowAction(serviceContext.getWorkflowAction());
    }
  }
  public KBArticle addKBArticle(
      long userId,
      long parentResourcePrimKey,
      String title,
      String content,
      String description,
      String[] sections,
      String dirName,
      ServiceContext serviceContext)
      throws PortalException, SystemException {

    // KB article

    User user = userPersistence.findByPrimaryKey(userId);
    long groupId = serviceContext.getScopeGroupId();
    double priority = getPriority(groupId, parentResourcePrimKey);
    Date now = new Date();

    validate(title, content);

    long kbArticleId = counterLocalService.increment();

    long resourcePrimKey = counterLocalService.increment();

    long rootResourcePrimKey = getRootResourcePrimKey(resourcePrimKey, parentResourcePrimKey);

    KBArticle kbArticle = kbArticlePersistence.create(kbArticleId);

    kbArticle.setUuid(serviceContext.getUuid());
    kbArticle.setResourcePrimKey(resourcePrimKey);
    kbArticle.setGroupId(groupId);
    kbArticle.setCompanyId(user.getCompanyId());
    kbArticle.setUserId(user.getUserId());
    kbArticle.setUserName(user.getFullName());
    kbArticle.setCreateDate(serviceContext.getCreateDate(now));
    kbArticle.setModifiedDate(serviceContext.getModifiedDate(now));
    kbArticle.setRootResourcePrimKey(rootResourcePrimKey);
    kbArticle.setParentResourcePrimKey(parentResourcePrimKey);
    kbArticle.setVersion(KBArticleConstants.DEFAULT_VERSION);
    kbArticle.setTitle(title);
    kbArticle.setContent(content);
    kbArticle.setDescription(description);
    kbArticle.setPriority(priority);
    kbArticle.setSections(StringUtil.merge(AdminUtil.escapeSections(sections)));
    kbArticle.setViewCount(0);
    kbArticle.setLatest(true);
    kbArticle.setMain(false);
    kbArticle.setStatus(WorkflowConstants.STATUS_DRAFT);

    kbArticlePersistence.update(kbArticle, false);

    // Resources

    resourceLocalService.addModelResources(kbArticle, serviceContext);

    // Asset

    updateKBArticleAsset(
        userId, kbArticle, serviceContext.getAssetCategoryIds(), serviceContext.getAssetTagNames());

    // Attachments

    addKBArticleAttachments(kbArticle, dirName, serviceContext);

    // Workflow

    WorkflowHandlerRegistryUtil.startWorkflowInstance(
        user.getCompanyId(),
        groupId,
        userId,
        KBArticle.class.getName(),
        resourcePrimKey,
        kbArticle,
        serviceContext);

    return kbArticle;
  }
  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;
  }
  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 KBArticle updateKBArticle(
      long userId,
      long resourcePrimKey,
      String title,
      String content,
      String description,
      String[] sections,
      String dirName,
      ServiceContext serviceContext)
      throws PortalException, SystemException {

    // KB article

    User user = userPersistence.findByPrimaryKey(userId);
    int version = KBArticleConstants.DEFAULT_VERSION;
    int status = WorkflowConstants.STATUS_DRAFT;

    validate(title, content);

    KBArticle oldKBArticle = getLatestKBArticle(resourcePrimKey, WorkflowConstants.STATUS_ANY);

    long oldResourcePrimKey = oldKBArticle.getResourcePrimKey();
    long oldGroupId = oldKBArticle.getGroupId();
    Date oldCreateDate = oldKBArticle.getCreateDate();
    long oldRootResourcePrimKey = oldKBArticle.getRootResourcePrimKey();
    long oldParentResourcePrimKey = oldKBArticle.getParentResourcePrimKey();
    int oldVersion = oldKBArticle.getVersion();
    double oldPriority = oldKBArticle.getPriority();
    int oldViewCount = oldKBArticle.getViewCount();
    int oldStatus = oldKBArticle.getStatus();

    KBArticle kbArticle = null;

    if (oldStatus == WorkflowConstants.STATUS_APPROVED) {
      long kbArticleId = counterLocalService.increment();

      kbArticle = kbArticlePersistence.create(kbArticleId);
      version = oldVersion + 1;
    } else {
      kbArticle = oldKBArticle;
      version = oldVersion;
    }

    if (oldStatus == WorkflowConstants.STATUS_PENDING) {
      status = WorkflowConstants.STATUS_PENDING;
    }

    kbArticle.setResourcePrimKey(oldResourcePrimKey);
    kbArticle.setGroupId(oldGroupId);
    kbArticle.setCompanyId(user.getCompanyId());
    kbArticle.setUserId(user.getUserId());
    kbArticle.setUserName(user.getFullName());
    kbArticle.setCreateDate(oldCreateDate);
    kbArticle.setModifiedDate(serviceContext.getModifiedDate(null));
    kbArticle.setRootResourcePrimKey(oldRootResourcePrimKey);
    kbArticle.setParentResourcePrimKey(oldParentResourcePrimKey);
    kbArticle.setVersion(version);
    kbArticle.setTitle(title);
    kbArticle.setContent(content);
    kbArticle.setDescription(description);
    kbArticle.setPriority(oldPriority);
    kbArticle.setSections(StringUtil.merge(AdminUtil.escapeSections(sections)));
    kbArticle.setViewCount(oldViewCount);
    kbArticle.setLatest(true);
    kbArticle.setMain(false);
    kbArticle.setStatus(status);

    kbArticlePersistence.update(kbArticle, false);

    if (oldVersion < version) {
      oldKBArticle.setLatest(false);

      kbArticlePersistence.update(oldKBArticle, false);
    }

    // Resources

    if ((serviceContext.getGroupPermissions() != null)
        || (serviceContext.getGuestPermissions() != null)) {

      updateKBArticleResources(
          kbArticle, serviceContext.getGroupPermissions(), serviceContext.getGuestPermissions());
    }

    // Asset

    updateKBArticleAsset(
        userId, kbArticle, serviceContext.getAssetCategoryIds(), serviceContext.getAssetTagNames());

    // Attachments

    updateKBArticleAttachments(kbArticle, oldVersion, dirName, serviceContext);

    // Workflow

    WorkflowHandlerRegistryUtil.startWorkflowInstance(
        user.getCompanyId(),
        kbArticle.getGroupId(),
        userId,
        KBArticle.class.getName(),
        resourcePrimKey,
        kbArticle,
        serviceContext);

    return kbArticle;
  }
  @Override
  public MicroblogsEntry addMicroblogsEntry(
      long userId,
      String content,
      int type,
      long parentMicroblogsEntryId,
      int socialRelationType,
      ServiceContext serviceContext)
      throws PortalException {

    // Microblogs entry

    User user = userPersistence.findByPrimaryKey(userId);

    Date now = new Date();

    validate(type, parentMicroblogsEntryId);

    long microblogsEntryId = counterLocalService.increment();

    if (parentMicroblogsEntryId == 0) {
      parentMicroblogsEntryId = microblogsEntryId;
    }

    MicroblogsEntry microblogsEntry = microblogsEntryPersistence.create(microblogsEntryId);

    microblogsEntry.setCompanyId(user.getCompanyId());
    microblogsEntry.setUserId(user.getUserId());
    microblogsEntry.setUserName(user.getFullName());
    microblogsEntry.setCreateDate(now);
    microblogsEntry.setModifiedDate(now);
    microblogsEntry.setCreatorClassNameId(classNameLocalService.getClassNameId(User.class));
    microblogsEntry.setCreatorClassPK(user.getUserId());
    microblogsEntry.setContent(content);
    microblogsEntry.setType(type);
    microblogsEntry.setParentMicroblogsEntryId(parentMicroblogsEntryId);
    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("parentMicroblogsEntryId", parentMicroblogsEntryId);

    socialActivityLocalService.addActivity(
        userId,
        0,
        MicroblogsEntry.class.getName(),
        microblogsEntryId,
        activityKey,
        extraDataJSONObject.toString(),
        microblogsEntry.getParentMicroblogsEntryUserId());

    // Notification

    subscribeUsers(microblogsEntry, serviceContext);

    sendNotificationEvent(microblogsEntry, serviceContext);

    return microblogsEntry;
  }