public void updateKBArticleAsset(
      long userId, KBArticle kbArticle, long[] assetCategoryIds, String[] assetTagNames)
      throws PortalException, SystemException {
    // TBD
    long classTypeId = 0;

    assetEntryLocalService.updateEntry(
        userId,
        kbArticle.getGroupId(),
        KBArticle.class.getName(),
        kbArticle.getClassPK(),
        kbArticle.getUuid(),
        classTypeId,
        assetCategoryIds,
        assetTagNames,
        false,
        null,
        null,
        null,
        null,
        ContentTypes.TEXT_HTML,
        kbArticle.getTitle(),
        kbArticle.getDescription(),
        null,
        null,
        null,
        0,
        0,
        null,
        false);
  }
예제 #2
0
  @Override
  protected void doReindex(Object obj) throws Exception {
    KBArticle kbArticle = (KBArticle) obj;

    SearchEngineUtil.updateDocument(
        getSearchEngineId(), kbArticle.getCompanyId(), getDocument(kbArticle));
  }
  public void updateKBArticlesPriorities(Map<Long, Double> resourcePrimKeyToPriorityMap)
      throws PortalException, SystemException {

    for (double priority : resourcePrimKeyToPriorityMap.values()) {
      validate(priority);
    }

    long[] resourcePrimKeys =
        StringUtil.split(StringUtil.merge(resourcePrimKeyToPriorityMap.keySet()), 0L);

    List<KBArticle> kbArticles1 =
        getKBArticles(resourcePrimKeys, WorkflowConstants.STATUS_ANY, null);

    for (KBArticle kbArticle1 : kbArticles1) {
      double priority = resourcePrimKeyToPriorityMap.get(kbArticle1.getResourcePrimKey());

      List<KBArticle> kbArticles2 =
          getKBArticleVersions(
              kbArticle1.getResourcePrimKey(),
              WorkflowConstants.STATUS_ANY,
              QueryUtil.ALL_POS,
              QueryUtil.ALL_POS,
              null);

      for (KBArticle kbArticle2 : kbArticles2) {
        kbArticle2.setPriority(priority);

        kbArticlePersistence.update(kbArticle2, false);
      }
    }
  }
  protected List<Layout> getCandidateLayouts(long plid, boolean privateLayout, KBArticle kbArticle)
      throws Exception {

    List<Layout> candidateLayouts = new ArrayList<>();

    Group group = GroupLocalServiceUtil.getGroup(kbArticle.getGroupId());

    if (group.isLayout()) {
      Layout layout = LayoutLocalServiceUtil.getLayout(group.getClassPK());

      candidateLayouts.add(layout);

      group = layout.getGroup();
    }

    List<Layout> layouts =
        LayoutLocalServiceUtil.getLayouts(
            group.getGroupId(), privateLayout, LayoutConstants.TYPE_PORTLET);

    candidateLayouts.addAll(layouts);

    Layout layout = LayoutLocalServiceUtil.getLayout(plid);

    if ((layout.getGroupId() == kbArticle.getGroupId()) && layout.isTypePortlet()) {

      candidateLayouts.remove(layout);
      candidateLayouts.add(0, layout);
    }

    return candidateLayouts;
  }
  protected void processKBArticleFiles(
      long userId, long groupId, ZipReader zipReader, ServiceContext serviceContext)
      throws KBArticleImportException {

    long parentResourcePrimKey = KBArticleConstants.DEFAULT_PARENT_RESOURCE_PRIM_KEY;

    String markdown = zipReader.getEntryAsString(PortletPropsValues.MARKDOWN_IMPORTER_ARTICLE_HOME);

    if (Validator.isNotNull(markdown)) {
      KBArticle parentKBArticle =
          addKBArticleMarkdown(
              userId,
              groupId,
              KBArticleConstants.DEFAULT_PARENT_RESOURCE_PRIM_KEY,
              markdown,
              PortletPropsValues.MARKDOWN_IMPORTER_ARTICLE_HOME,
              zipReader,
              serviceContext);

      parentResourcePrimKey = parentKBArticle.getResourcePrimKey();
    }

    processSectionKBArticleFiles(
        userId,
        groupId,
        parentResourcePrimKey,
        zipReader,
        getFolderNameFileEntryNamesMap(zipReader),
        serviceContext);
  }
  protected void deleteAssets(KBArticle kbArticle) throws PortalException, SystemException {

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

    if (!kbArticle.isApproved() && !kbArticle.isFirstVersion()) {
      assetEntryLocalService.deleteEntry(KBArticle.class.getName(), kbArticle.getResourcePrimKey());
    }
  }
  protected void deleteKBArticleAttachments(KBArticle kbArticle)
      throws PortalException, SystemException {

    deleteKBArticleAttachments(kbArticle, kbArticle.getClassPK());

    if (!kbArticle.isApproved() && !kbArticle.isFirstVersion()) {
      deleteKBArticleAttachments(kbArticle, kbArticle.getResourcePrimKey());
    }
  }
  protected void deleteSubscriptions(KBArticle kbArticle) throws PortalException, SystemException {

    List<Subscription> subscriptions =
        subscriptionLocalService.getSubscriptions(
            kbArticle.getCompanyId(), KBArticle.class.getName(), kbArticle.getResourcePrimKey());

    for (Subscription subscription : subscriptions) {
      unsubscribeKBArticle(subscription.getUserId(), subscription.getClassPK());
    }
  }
  public void updateKBArticleResources(
      KBArticle kbArticle, String[] groupPermissions, String[] guestPermissions)
      throws PortalException, SystemException {

    resourceLocalService.updateResources(
        kbArticle.getCompanyId(),
        kbArticle.getGroupId(),
        KBArticle.class.getName(),
        kbArticle.getResourcePrimKey(),
        groupPermissions,
        guestPermissions);
  }
  protected long getRootResourcePrimKey(long resourcePrimKey, long parentResourcePrimKey)
      throws PortalException, SystemException {

    if (parentResourcePrimKey == KBArticleConstants.DEFAULT_PARENT_RESOURCE_PRIM_KEY) {

      return resourcePrimKey;
    }

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

    return kbArticle.getRootResourcePrimKey();
  }
  @Override
  public int compare(Object obj1, Object obj2) {
    KBArticle kbArticle1 = (KBArticle) obj1;
    KBArticle kbArticle2 = (KBArticle) obj2;

    int value =
        kbArticle1.getUserName().toLowerCase().compareTo(kbArticle2.getUserName().toLowerCase());

    if (_ascending) {
      return value;
    } else {
      return -value;
    }
  }
예제 #12
0
  @Override
  protected Document doGetDocument(Object obj) throws Exception {
    KBArticle kbArticle = (KBArticle) obj;

    Document document = getBaseModelDocument(PORTLET_ID, kbArticle);

    document.addText(Field.CONTENT, HtmlUtil.extractText(kbArticle.getContent()));
    document.addText(Field.DESCRIPTION, kbArticle.getDescription());
    document.addText(Field.TITLE, kbArticle.getTitle());

    document.addKeyword("titleKeyword", kbArticle.getTitle(), true);

    return document;
  }
예제 #13
0
  protected void reindexKBArticles(KBArticle kbArticle) throws Exception {

    // See KBArticlePermission#contains

    List<KBArticle> kbArticles =
        KBArticleLocalServiceUtil.getKBArticleAndAllDescendants(
            kbArticle.getResourcePrimKey(), WorkflowConstants.STATUS_APPROVED, null);

    Collection<Document> documents = new ArrayList<Document>();

    for (KBArticle curKBArticle : kbArticles) {
      documents.add(getDocument(curKBArticle));
    }

    SearchEngineUtil.updateDocuments(kbArticle.getCompanyId(), documents);
  }
  protected PortletURL getKBArticleURL(
      long plid, String portletId, KBArticle kbArticle, HttpServletRequest request)
      throws Exception {

    long resourcePrimKey = ParamUtil.getLong(request, "resourcePrimKey");

    String mvcPath = null;

    String rootPortletId = PortletConstants.getRootPortletId(portletId);

    if (rootPortletId.equals(PortletKeys.KNOWLEDGE_BASE_ARTICLE)) {
      mvcPath = "/article/view_article.jsp";
    } else if (rootPortletId.equals(PortletKeys.KNOWLEDGE_BASE_SECTION)) {
      mvcPath = "/section/view_article.jsp";
    }

    PortletURL portletURL =
        PortletURLFactoryUtil.create(request, portletId, plid, PortletRequest.RENDER_PHASE);

    if (mvcPath != null) {
      portletURL.setParameter("mvcPath", mvcPath);
    }

    if ((kbArticle == null) || Validator.isNull(kbArticle.getUrlTitle())) {
      portletURL.setParameter("resourcePrimKey", String.valueOf(resourcePrimKey));
    } else {
      portletURL.setParameter("urlTitle", kbArticle.getUrlTitle());

      if (kbArticle.getKbFolderId() != KBFolderConstants.DEFAULT_PARENT_FOLDER_ID) {

        KBFolder kbFolder = KBFolderLocalServiceUtil.getKBFolder(kbArticle.getKbFolderId());

        portletURL.setParameter("kbFolderUrlTitle", String.valueOf(kbFolder.getUrlTitle()));
      }
    }

    portletURL.setPortletMode(PortletMode.VIEW);

    portletURL.setWindowState(LiferayWindowState.NORMAL);

    if (rootPortletId.equals(PortletKeys.KNOWLEDGE_BASE_SECTION)) {
      portletURL.setWindowState(LiferayWindowState.MAXIMIZED);
    }

    return portletURL;
  }
  protected void updateKBArticleAttachments(
      KBArticle kbArticle, int oldVersion, String dirName, ServiceContext serviceContext)
      throws PortalException, SystemException {

    if (kbArticle.getVersion() > oldVersion) {
      String oldDirName = KBArticleConstants.DIR_NAME_PREFIX + kbArticle.getResourcePrimKey();

      if (Validator.isNull(dirName)) {
        addKBArticleAttachments(kbArticle, oldDirName, serviceContext);
      } else {
        addKBArticleAttachments(kbArticle, dirName, serviceContext);
      }
    } else if (Validator.isNotNull(dirName)) {
      deleteKBArticleAttachments(kbArticle, kbArticle.getClassPK());

      addKBArticleAttachments(kbArticle, dirName, serviceContext);
    }
  }
  public void updateViewCount(long userId, long resourcePrimKey, int viewCount)
      throws PortalException, SystemException {

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

    kbArticle.setViewCount(viewCount);

    kbArticlePersistence.update(kbArticle, false);

    if (kbArticle.isApproved() || kbArticle.isFirstVersion()) {
      return;
    }

    kbArticle = getLatestKBArticle(resourcePrimKey, WorkflowConstants.STATUS_APPROVED);

    kbArticle.setViewCount(viewCount);

    kbArticlePersistence.update(kbArticle, false);
  }
  protected void importKBArticleAttachments(
      PortletDataContext portletDataContext, KBArticle kbArticle, KBArticle importedKBArticle)
      throws Exception {

    List<Element> dlFileEntryElements =
        portletDataContext.getReferenceDataElements(kbArticle, DLFileEntry.class);

    ServiceContext serviceContext = new ServiceContext();

    serviceContext.setCompanyId(portletDataContext.getCompanyId());
    serviceContext.setScopeGroupId(portletDataContext.getScopeGroupId());

    InputStream inputStream = null;

    for (Element dlFileEntryElement : dlFileEntryElements) {
      try {
        byte[] bytes =
            portletDataContext.getZipEntryAsByteArray(dlFileEntryElement.attributeValue("path"));

        inputStream = new UnsyncByteArrayInputStream(bytes);

        String fileName = dlFileEntryElement.attributeValue("file-name");

        String mimeType = KnowledgeBaseUtil.getMimeType(bytes, fileName);

        PortletFileRepositoryUtil.addPortletFileEntry(
            portletDataContext.getScopeGroupId(),
            portletDataContext.getUserId(importedKBArticle.getUserUuid()),
            KBArticle.class.getName(),
            importedKBArticle.getClassPK(),
            PortletKeys.KNOWLEDGE_BASE_ADMIN,
            importedKBArticle.getAttachmentsFolderId(),
            inputStream,
            fileName,
            mimeType,
            true);
      } catch (DuplicateFileException dfe) {
        continue;
      } finally {
        StreamUtil.cleanUp(inputStream);
      }
    }
  }
  protected void deleteKBArticleAttachments(KBArticle kbArticle, long folderId)
      throws PortalException, SystemException {

    try {
      DLStoreUtil.deleteDirectory(
          kbArticle.getCompanyId(),
          CompanyConstants.SYSTEM,
          KBArticleConstants.DIR_NAME_PREFIX + folderId);
    } catch (NoSuchDirectoryException nsde) {
      _log.error("No directory found for " + nsde.getMessage());
    }
  }
  protected void addKBArticleAttachments(
      KBArticle kbArticle, String dirName, ServiceContext serviceContext)
      throws PortalException, SystemException {

    try {
      DLStoreUtil.addDirectory(
          serviceContext.getCompanyId(),
          CompanyConstants.SYSTEM,
          kbArticle.getAttachmentsDirName());
    } catch (DuplicateDirectoryException dde) {
      _log.error("Directory already exists for " + dde.getMessage());
    }

    if (Validator.isNull(dirName)) {
      return;
    }

    String[] fileNames =
        DLStoreUtil.getFileNames(serviceContext.getCompanyId(), CompanyConstants.SYSTEM, dirName);

    for (String fileName : fileNames) {
      InputStream inputStream = null;

      try {
        inputStream =
            DLStoreUtil.getFileAsStream(
                serviceContext.getCompanyId(), CompanyConstants.SYSTEM, fileName);

        addAttachment(
            kbArticle.getAttachmentsDirName(),
            FileUtil.getShortFileName(fileName),
            inputStream,
            serviceContext);
      } catch (DuplicateFileException dfe) {
        _log.error("File already exists for " + dfe.getMessage());
      } finally {
        StreamUtil.cleanUp(inputStream);
      }
    }
  }
  public static void updateAttachments(KBArticle kbArticle) {
    try {
      long folderId = kbArticle.getClassPK();

      String oldDirName = "knowledgebase/articles/" + folderId;
      String newDirName = "knowledgebase/kbarticles/" + folderId;

      DLLocalServiceUtil.addDirectory(
          kbArticle.getCompanyId(), CompanyConstants.SYSTEM, newDirName);

      String[] fileNames =
          DLLocalServiceUtil.getFileNames(
              kbArticle.getCompanyId(), CompanyConstants.SYSTEM, oldDirName);

      ServiceContext serviceContext = new ServiceContext();

      serviceContext.setCompanyId(kbArticle.getCompanyId());
      serviceContext.setScopeGroupId(kbArticle.getGroupId());

      for (String fileName : fileNames) {
        String shortFileName = FileUtil.getShortFileName(fileName);
        byte[] bytes =
            DLLocalServiceUtil.getFile(kbArticle.getCompanyId(), CompanyConstants.SYSTEM, fileName);

        DLLocalServiceUtil.addFile(
            kbArticle.getCompanyId(),
            CompanyConstants.SYSTEM_STRING,
            GroupConstants.DEFAULT_PARENT_GROUP_ID,
            CompanyConstants.SYSTEM,
            newDirName + StringPool.SLASH + shortFileName,
            0,
            StringPool.BLANK,
            serviceContext.getModifiedDate(null),
            serviceContext,
            bytes);
      }

      DLLocalServiceUtil.deleteDirectory(
          kbArticle.getCompanyId(),
          CompanyConstants.SYSTEM_STRING,
          CompanyConstants.SYSTEM,
          oldDirName);

      if (_log.isInfoEnabled()) {
        _log.info("Added attachments for " + folderId);
      }
    } catch (Exception e) {
      _log.error(e.getMessage());
    }
  }
  protected void putTitle(JSONObject jsonObject, KBComment kbComment) {
    KBArticle kbArticle = null;
    KBTemplate kbTemplate = null;

    String className = kbComment.getClassName();

    try {
      if (className.equals(KBArticle.class.getName())) {
        kbArticle =
            KBArticleLocalServiceUtil.getLatestKBArticle(
                kbComment.getClassPK(), WorkflowConstants.STATUS_APPROVED);

        jsonObject.put("title", kbArticle.getTitle());
      } else if (className.equals(KBTemplate.class.getName())) {
        kbTemplate = KBTemplateLocalServiceUtil.getKBTemplate(kbComment.getClassPK());

        jsonObject.put("title", kbTemplate.getTitle());
      }
    } catch (Exception e) {
      _log.error(e);
    }
  }
  protected Map<String, String> getEmailKBArticleDiffs(KBArticle kbArticle) {
    Map<String, String> emailKBArticleDiffs = new HashMap<String, String>();

    for (String param : new String[] {"content", "title"}) {
      String value = BeanPropertiesUtil.getString(kbArticle, param);

      try {
        value =
            AdminUtil.getKBArticleDiff(
                kbArticle.getResourcePrimKey(),
                kbArticle.getVersion() - 1,
                kbArticle.getVersion(),
                param);
      } catch (Exception e) {
        _log.error(e, e);
      }

      emailKBArticleDiffs.put(param, value);
    }

    return emailKBArticleDiffs;
  }
  @Override
  protected void doExportStagedModel(PortletDataContext portletDataContext, KBArticle kbArticle)
      throws Exception {

    if (kbArticle.getParentResourcePrimKey() != KBFolderConstants.DEFAULT_PARENT_FOLDER_ID) {

      long kbArticleClassNameId = PortalUtil.getClassNameId(KBArticleConstants.getClassName());

      if (kbArticle.getParentResourceClassNameId() == kbArticleClassNameId) {

        KBArticle parentKBArticle =
            KBArticleLocalServiceUtil.getLatestKBArticle(
                kbArticle.getParentResourcePrimKey(), WorkflowConstants.STATUS_APPROVED);

        StagedModelDataHandlerUtil.exportReferenceStagedModel(
            portletDataContext,
            kbArticle,
            parentKBArticle,
            PortletDataContext.REFERENCE_TYPE_PARENT);
      } else {
        KBFolder parentKBFolder =
            KBFolderLocalServiceUtil.getKBFolder(kbArticle.getParentResourcePrimKey());

        StagedModelDataHandlerUtil.exportReferenceStagedModel(
            portletDataContext,
            kbArticle,
            parentKBFolder,
            PortletDataContext.REFERENCE_TYPE_PARENT);
      }
    }

    Element kbArticleElement = portletDataContext.getExportDataElement(kbArticle);

    exportKBArticleAttachments(portletDataContext, kbArticleElement, kbArticle);

    portletDataContext.addClassedModel(
        kbArticleElement, ExportImportPathUtil.getModelPath(kbArticle), kbArticle);
  }
  protected double getPriority(long groupId, long parentResourcePrimKey) throws SystemException {

    if (!PortletPropsValues.ADMIN_KB_ARTICLE_INCREMENT_PRIORITY_ENABLED) {
      return KBArticleConstants.DEFAULT_VERSION;
    }

    List<KBArticle> kbArticles =
        getSiblingKBArticles(
            groupId,
            parentResourcePrimKey,
            WorkflowConstants.STATUS_ANY,
            0,
            1,
            new KBArticlePriorityComparator());

    if (kbArticles.isEmpty()) {
      return KBArticleConstants.DEFAULT_PRIORITY;
    }

    KBArticle kbArticle = kbArticles.get(0);

    return Math.floor(kbArticle.getPriority()) + 1;
  }
  public String updateAttachments(
      long resourcePrimKey, String dirName, ServiceContext serviceContext)
      throws PortalException, SystemException {

    if (Validator.isNotNull(dirName)) {
      return dirName;
    }

    dirName = "knowledgebase/temp/attachments/" + counterLocalService.increment();

    DLStoreUtil.addDirectory(serviceContext.getCompanyId(), CompanyConstants.SYSTEM, dirName);

    if (resourcePrimKey <= 0) {
      return dirName;
    }

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

    for (String fileName : kbArticle.getAttachmentsFileNames()) {
      String shortFileName = FileUtil.getShortFileName(fileName);

      InputStream inputStream = null;

      try {
        inputStream =
            DLStoreUtil.getFileAsStream(
                kbArticle.getCompanyId(), CompanyConstants.SYSTEM, fileName);

        addAttachment(dirName, shortFileName, inputStream, serviceContext);
      } finally {
        StreamUtil.cleanUp(inputStream);
      }
    }

    return dirName;
  }
  protected void exportKBArticleAttachments(
      PortletDataContext portletDataContext, Element kbArticleElement, KBArticle kbArticle)
      throws Exception {

    List<FileEntry> attachmentsFileEntries = kbArticle.getAttachmentsFileEntries();

    for (FileEntry fileEntry : attachmentsFileEntries) {
      String path = ExportImportPathUtil.getModelPath(kbArticle, fileEntry.getTitle());

      Element fileEntryElement = portletDataContext.getExportDataElement(fileEntry);

      fileEntryElement.addAttribute("path", path);
      fileEntryElement.addAttribute("file-name", fileEntry.getTitle());

      portletDataContext.addZipEntry(path, fileEntry.getContentStream());

      portletDataContext.addReferenceElement(
          kbArticle, kbArticleElement, fileEntry, PortletDataContext.REFERENCE_TYPE_WEAK, false);
    }
  }
  protected void updatePermissionFields(long resourcePrimKey, long parentResourcePrimKey)
      throws PortalException, SystemException {

    // See KBArticlePermission#contains

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

    if (kbArticle.getParentResourcePrimKey() == parentResourcePrimKey) {
      return;
    }

    long rootResourcePrimKey = getRootResourcePrimKey(resourcePrimKey, parentResourcePrimKey);

    if (kbArticle.getRootResourcePrimKey() == rootResourcePrimKey) {
      return;
    }

    // Sync database

    List<KBArticle> kbArticles1 =
        getKBArticleAndAllDescendants(resourcePrimKey, WorkflowConstants.STATUS_ANY, null);

    for (KBArticle kbArticle1 : kbArticles1) {
      List<KBArticle> kbArticles2 =
          getKBArticleVersions(
              kbArticle1.getResourcePrimKey(),
              WorkflowConstants.STATUS_ANY,
              QueryUtil.ALL_POS,
              QueryUtil.ALL_POS,
              null);

      for (KBArticle kbArticle2 : kbArticles2) {
        kbArticle2.setRootResourcePrimKey(rootResourcePrimKey);

        kbArticlePersistence.update(kbArticle2, false);
      }
    }

    // Sync indexed permission fields

    SearchEngineUtil.updatePermissionFields(
        KBArticle.class.getName(), String.valueOf(resourcePrimKey));
  }
  public static long[] getAssetTagIds(long[] groupIds, KBArticle kbArticle) throws PortalException {

    List<AssetTag> assetTags =
        AssetTagServiceUtil.getTags(KBArticle.class.getName(), kbArticle.getClassPK());

    long[] tagIds =
        AssetTagLocalServiceUtil.getTagIds(
            groupIds, StringUtil.split(ListUtil.toString(assetTags, "name")));

    Set<Long> filteredTagIds = new LinkedHashSet<>();

    for (long tagId : tagIds) {
      try {
        AssetTagServiceUtil.getTag(tagId);
      } catch (PrincipalException pe) {
        continue;
      }

      filteredTagIds.add(tagId);
    }

    return StringUtil.split(StringUtil.merge(filteredTagIds), 0L);
  }
  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 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;
  }