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