public void moveKBArticle(
      long userId, long resourcePrimKey, long parentResourcePrimKey, double priority)
      throws PortalException, SystemException {

    // KB article

    validate(priority);

    updatePermissionFields(resourcePrimKey, parentResourcePrimKey);

    List<KBArticle> kbArticles =
        getKBArticleVersions(
            resourcePrimKey,
            WorkflowConstants.STATUS_ANY,
            QueryUtil.ALL_POS,
            QueryUtil.ALL_POS,
            new KBArticleVersionComparator());

    for (KBArticle kbArticle : kbArticles) {
      kbArticle.setParentResourcePrimKey(parentResourcePrimKey);
      kbArticle.setPriority(priority);

      kbArticlePersistence.update(kbArticle, false);
    }

    // Social

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

    if (kbArticle.isApproved() || !kbArticle.isFirstVersion()) {
      socialActivityLocalService.addActivity(
          userId,
          kbArticle.getGroupId(),
          KBArticle.class.getName(),
          resourcePrimKey,
          AdminActivityKeys.MOVE_KB_ARTICLE,
          StringPool.BLANK,
          0);
    }
  }
  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;
  }