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 updateStatus(
      long userId, long resourcePrimKey, int status, ServiceContext serviceContext)
      throws PortalException, SystemException {

    // KB article

    User user = userPersistence.findByPrimaryKey(userId);
    boolean main = false;
    Date now = new Date();

    if (status == WorkflowConstants.STATUS_APPROVED) {
      main = true;
    }

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

    kbArticle.setModifiedDate(serviceContext.getModifiedDate(now));
    kbArticle.setMain(main);
    kbArticle.setStatus(status);
    kbArticle.setStatusByUserId(user.getUserId());
    kbArticle.setStatusByUserName(user.getFullName());
    kbArticle.setStatusDate(serviceContext.getModifiedDate(now));

    kbArticlePersistence.update(kbArticle, false);

    if (status != WorkflowConstants.STATUS_APPROVED) {
      return kbArticle;
    }

    if (!kbArticle.isFirstVersion()) {
      KBArticle oldKBArticle =
          kbArticlePersistence.findByR_V(resourcePrimKey, kbArticle.getVersion() - 1);

      oldKBArticle.setMain(false);

      kbArticlePersistence.update(oldKBArticle, false);
    }

    // Asset

    AssetEntry assetEntry =
        assetEntryLocalService.getEntry(KBArticle.class.getName(), kbArticle.getKbArticleId());

    updateKBArticleAsset(userId, kbArticle, assetEntry.getCategoryIds(), assetEntry.getTagNames());

    assetEntryLocalService.deleteEntry(KBArticle.class.getName(), kbArticle.getKbArticleId());

    assetEntryLocalService.updateVisible(
        KBArticle.class.getName(), kbArticle.getResourcePrimKey(), true);

    // Social

    if (!kbArticle.isFirstVersion()) {
      socialActivityLocalService.addActivity(
          userId,
          kbArticle.getGroupId(),
          KBArticle.class.getName(),
          resourcePrimKey,
          AdminActivityKeys.UPDATE_KB_ARTICLE,
          StringPool.BLANK,
          0);
    } else {
      socialActivityLocalService.addActivity(
          userId,
          kbArticle.getGroupId(),
          KBArticle.class.getName(),
          resourcePrimKey,
          AdminActivityKeys.ADD_KB_ARTICLE,
          StringPool.BLANK,
          0);
    }

    // Indexer

    Indexer indexer = IndexerRegistryUtil.getIndexer(KBArticle.class);

    indexer.reindex(kbArticle);

    // Attachments

    if (!kbArticle.isFirstVersion()) {
      deleteKBArticleAttachments(kbArticle, resourcePrimKey);
    }

    String dirName = KBArticleConstants.DIR_NAME_PREFIX + kbArticle.getKbArticleId();

    addKBArticleAttachments(kbArticle, dirName, serviceContext);

    deleteKBArticleAttachments(kbArticle, kbArticle.getKbArticleId());

    // Subscriptions

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