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