Search in sources :

Example 1 with KBArticleMarkdownConverter

use of com.liferay.knowledgebase.admin.importer.util.KBArticleMarkdownConverter in project liferay-ide by liferay.

the class KBArticleImporter method addKBArticleMarkdown.

protected KBArticle addKBArticleMarkdown(long userId, long groupId, long parentKBFolderId, long parentResourceClassNameId, long parentResourcePrimaryKey, String markdown, String fileEntryName, ZipReader zipReader, Map<String, String> metadata, PrioritizationStrategy prioritizationStrategy, ServiceContext serviceContext) throws KBArticleImportException, SystemException {
    if (Validator.isNull(markdown)) {
        throw new KBArticleImportException("Markdown is null for file entry " + fileEntryName);
    }
    KBArticleMarkdownConverter kbArticleMarkdownConverter = new KBArticleMarkdownConverter(markdown, fileEntryName, metadata);
    String urlTitle = kbArticleMarkdownConverter.getUrlTitle();
    KBArticle kbArticle = KBArticleLocalServiceUtil.fetchKBArticleByUrlTitle(groupId, parentKBFolderId, urlTitle);
    boolean newKBArticle = false;
    if (kbArticle == null) {
        newKBArticle = true;
    }
    try {
        if (kbArticle == null) {
            int workflowAction = serviceContext.getWorkflowAction();
            serviceContext.setWorkflowAction(WorkflowConstants.ACTION_SAVE_DRAFT);
            kbArticle = KBArticleLocalServiceUtil.addKBArticle(userId, parentResourceClassNameId, parentResourcePrimaryKey, kbArticleMarkdownConverter.getTitle(), urlTitle, markdown, null, kbArticleMarkdownConverter.getSourceURL(), null, null, serviceContext);
            serviceContext.setWorkflowAction(workflowAction);
        }
    } catch (Exception e) {
        StringBundler sb = new StringBundler(4);
        sb.append("Unable to add basic KB article for file entry ");
        sb.append(fileEntryName);
        sb.append(": ");
        sb.append(e.getLocalizedMessage());
        throw new KBArticleImportException(sb.toString(), e);
    }
    try {
        String html = kbArticleMarkdownConverter.processAttachmentsReferences(userId, kbArticle, zipReader, new HashMap<String, FileEntry>());
        kbArticle = KBArticleLocalServiceUtil.updateKBArticle(userId, kbArticle.getResourcePrimKey(), kbArticleMarkdownConverter.getTitle(), html, kbArticle.getDescription(), kbArticleMarkdownConverter.getSourceURL(), null, null, null, serviceContext);
        if (newKBArticle) {
            prioritizationStrategy.addKBArticle(kbArticle, fileEntryName);
        } else {
            prioritizationStrategy.updateKBArticle(kbArticle, fileEntryName);
        }
        return kbArticle;
    } catch (Exception e) {
        StringBundler sb = new StringBundler(4);
        sb.append("Unable to update KB article for file entry ");
        sb.append(fileEntryName);
        sb.append(": ");
        sb.append(e.getLocalizedMessage());
        throw new KBArticleImportException(sb.toString(), e);
    }
}
Also used : KBArticle(com.liferay.knowledgebase.model.KBArticle) KBArticleMarkdownConverter(com.liferay.knowledgebase.admin.importer.util.KBArticleMarkdownConverter) KBArticleImportException(com.liferay.knowledgebase.KBArticleImportException) FileEntry(com.liferay.portal.kernel.repository.model.FileEntry) PortalException(com.liferay.portal.kernel.exception.PortalException) SystemException(com.liferay.portal.kernel.exception.SystemException) IOException(java.io.IOException) KBArticleImportException(com.liferay.knowledgebase.KBArticleImportException) StringBundler(com.liferay.portal.kernel.util.StringBundler)

Aggregations

KBArticleImportException (com.liferay.knowledgebase.KBArticleImportException)1 KBArticleMarkdownConverter (com.liferay.knowledgebase.admin.importer.util.KBArticleMarkdownConverter)1 KBArticle (com.liferay.knowledgebase.model.KBArticle)1 PortalException (com.liferay.portal.kernel.exception.PortalException)1 SystemException (com.liferay.portal.kernel.exception.SystemException)1 FileEntry (com.liferay.portal.kernel.repository.model.FileEntry)1 StringBundler (com.liferay.portal.kernel.util.StringBundler)1 IOException (java.io.IOException)1