Search in sources :

Example 1 with KBArticle

use of com.liferay.knowledgebase.model.KBArticle 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)

Example 2 with KBArticle

use of com.liferay.knowledgebase.model.KBArticle in project liferay-ide by liferay.

the class KBArticleImporter method processKBArticleFiles.

protected int processKBArticleFiles(long userId, long groupId, long parentKBFolderId, boolean prioritizeByNumericalPrefix, ZipReader zipReader, Map<String, String> metadata, ServiceContext serviceContext) throws PortalException, SystemException {
    int importedKBArticleCount = 0;
    PrioritizationStrategy prioritizationStrategy = PrioritizationStrategy.create(groupId, parentKBFolderId, prioritizeByNumericalPrefix);
    Map<String, List<String>> folderNameFileEntryNamesMap = getFolderNameFileEntryNamesMap(zipReader);
    Set<String> folderNames = folderNameFileEntryNamesMap.keySet();
    for (String folderName : folderNames) {
        List<String> fileEntryNames = folderNameFileEntryNamesMap.get(folderName);
        String sectionIntroFileEntryName = null;
        List<String> sectionFileEntryNames = new ArrayList<String>();
        for (String fileEntryName : fileEntryNames) {
            if (fileEntryName.endsWith(PortletPropsValues.MARKDOWN_IMPORTER_ARTICLE_INTRO)) {
                sectionIntroFileEntryName = fileEntryName;
            } else {
                sectionFileEntryNames.add(fileEntryName);
            }
        }
        long parentResourceClassNameId = PortalUtil.getClassNameId(KBFolderConstants.getClassName());
        long parentResourcePrimaryKey = parentKBFolderId;
        long sectionResourceClassNameId = parentResourceClassNameId;
        long sectionResourcePrimaryKey = parentResourcePrimaryKey;
        if (Validator.isNotNull(sectionIntroFileEntryName)) {
            KBArticle sectionIntroKBArticle = addKBArticleMarkdown(userId, groupId, parentKBFolderId, sectionResourceClassNameId, sectionResourcePrimaryKey, zipReader.getEntryAsString(sectionIntroFileEntryName), sectionIntroFileEntryName, zipReader, metadata, prioritizationStrategy, serviceContext);
            sectionResourceClassNameId = PortalUtil.getClassNameId(KBArticleConstants.getClassName());
            sectionResourcePrimaryKey = sectionIntroKBArticle.getResourcePrimKey();
            importedKBArticleCount++;
        }
        for (String sectionFileEntryName : sectionFileEntryNames) {
            String sectionMarkdown = zipReader.getEntryAsString(sectionFileEntryName);
            if (Validator.isNull(sectionMarkdown)) {
                if (_log.isWarnEnabled()) {
                    _log.warn("Missing Markdown in file entry " + sectionFileEntryName);
                }
            }
            addKBArticleMarkdown(userId, groupId, parentKBFolderId, sectionResourceClassNameId, sectionResourcePrimaryKey, sectionMarkdown, sectionFileEntryName, zipReader, metadata, prioritizationStrategy, serviceContext);
            importedKBArticleCount++;
        }
    }
    prioritizationStrategy.prioritizeKBArticles();
    return importedKBArticleCount;
}
Also used : KBArticle(com.liferay.knowledgebase.model.KBArticle) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with KBArticle

use of com.liferay.knowledgebase.model.KBArticle in project liferay-ide by liferay.

the class PrioritizationStrategy method prioritizeKBArticles.

public void prioritizeKBArticles() throws PortalException, SystemException {
    if (_prioritizeByNumericalPrefix) {
        for (Map.Entry<String, Double> entry : _importedKBArticleUrlTitlesPrioritiesMap.entrySet()) {
            if (entry.getValue() < 1.0) {
                continue;
            }
            KBArticle kbArticle = KBArticleLocalServiceUtil.getKBArticleByUrlTitle(_groupId, _parentKBFolderId, entry.getKey());
            KBArticleLocalServiceUtil.updatePriority(kbArticle.getResourcePrimKey(), entry.getValue());
            remove(_importedKBArticlesMap, kbArticle);
        }
    }
    prioritizeKBArticles(_importedKBArticlesMap);
}
Also used : KBArticle(com.liferay.knowledgebase.model.KBArticle) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with KBArticle

use of com.liferay.knowledgebase.model.KBArticle in project liferay-ide by liferay.

the class KBArticleStagedModelDataHandler method doExportStagedModel.

@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);
}
Also used : KBFolder(com.liferay.knowledgebase.model.KBFolder) KBArticle(com.liferay.knowledgebase.model.KBArticle) Element(com.liferay.portal.kernel.xml.Element)

Example 5 with KBArticle

use of com.liferay.knowledgebase.model.KBArticle in project liferay-ide by liferay.

the class KBArticleStagedModelDataHandler method importKBArticleAttachments.

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);
        }
    }
}
Also used : KBArticle(com.liferay.knowledgebase.model.KBArticle) DuplicateFileException(com.liferay.portlet.documentlibrary.DuplicateFileException) ServiceContext(com.liferay.portal.service.ServiceContext) UnsyncByteArrayInputStream(com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream) InputStream(java.io.InputStream) Element(com.liferay.portal.kernel.xml.Element) UnsyncByteArrayInputStream(com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream)

Aggregations

KBArticle (com.liferay.knowledgebase.model.KBArticle)306 NoSuchArticleException (com.liferay.knowledgebase.NoSuchArticleException)185 StringBundler (com.liferay.portal.kernel.util.StringBundler)177 SystemException (com.liferay.portal.kernel.exception.SystemException)125 Session (com.liferay.portal.kernel.dao.orm.Session)119 SQLQuery (com.liferay.portal.kernel.dao.orm.SQLQuery)117 QueryPos (com.liferay.portal.kernel.dao.orm.QueryPos)116 ArrayList (java.util.ArrayList)82 List (java.util.List)77 Query (com.liferay.portal.kernel.dao.orm.Query)75 UnmodifiableList (com.liferay.portal.kernel.util.UnmodifiableList)73 KBArticleImpl (com.liferay.knowledgebase.model.impl.KBArticleImpl)47 FinderPath (com.liferay.portal.kernel.dao.orm.FinderPath)29 KBFolder (com.liferay.knowledgebase.model.KBFolder)8 NoSuchCommentException (com.liferay.knowledgebase.NoSuchCommentException)5 NoSuchSubscriptionException (com.liferay.portal.NoSuchSubscriptionException)5 PortalException (com.liferay.portal.kernel.exception.PortalException)5 PrincipalException (com.liferay.portal.security.auth.PrincipalException)5 IOException (java.io.IOException)5 HashMap (java.util.HashMap)4