Search in sources :

Example 1 with KBArticleImportException

use of com.liferay.knowledgebase.KBArticleImportException 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 KBArticleImportException

use of com.liferay.knowledgebase.KBArticleImportException in project liferay-ide by liferay.

the class AdminPortlet method importFile.

public void importFile(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {
    InputStream inputStream = null;
    try {
        ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
        UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(actionRequest);
        long parentKBFolderId = ParamUtil.getLong(uploadPortletRequest, "parentKBFolderId", KBFolderConstants.DEFAULT_PARENT_FOLDER_ID);
        String fileName = uploadPortletRequest.getFileName("file");
        if (Validator.isNull(fileName)) {
            throw new KBArticleImportException("File name is null");
        }
        boolean prioritizeByNumericalPrefix = ParamUtil.getBoolean(uploadPortletRequest, "prioritizeByNumericalPrefix");
        inputStream = uploadPortletRequest.getFileAsStream("file");
        ServiceContext serviceContext = ServiceContextFactory.getInstance(AdminPortlet.class.getName(), actionRequest);
        serviceContext.setGuestPermissions(new String[] { ActionKeys.VIEW });
        int kbArticleCount = KBArticleServiceUtil.addKBArticlesMarkdown(themeDisplay.getScopeGroupId(), parentKBFolderId, fileName, prioritizeByNumericalPrefix, inputStream, serviceContext);
        SessionMessages.add(actionRequest, "importedKBArticlesCount", kbArticleCount);
    } catch (KBArticleImportException kbaie) {
        SessionErrors.add(actionRequest, kbaie.getClass(), kbaie);
    } finally {
        StreamUtil.cleanUp(inputStream);
    }
}
Also used : InputStream(java.io.InputStream) ServiceContext(com.liferay.portal.service.ServiceContext) KBArticleImportException(com.liferay.knowledgebase.KBArticleImportException) ThemeDisplay(com.liferay.portal.theme.ThemeDisplay) UploadPortletRequest(com.liferay.portal.kernel.upload.UploadPortletRequest)

Example 3 with KBArticleImportException

use of com.liferay.knowledgebase.KBArticleImportException in project liferay-ide by liferay.

the class KBArticleImporter method processZipFile.

public int processZipFile(long userId, long groupId, long parentKBFolderId, boolean prioritizeByNumericalPrefix, InputStream inputStream, ServiceContext serviceContext) throws PortalException, SystemException {
    if (inputStream == null) {
        throw new KBArticleImportException("Input stream is null");
    }
    try {
        ZipReader zipReader = ZipReaderFactoryUtil.getZipReader(inputStream);
        Map<String, String> metadata = getMetadata(zipReader);
        return processKBArticleFiles(userId, groupId, parentKBFolderId, prioritizeByNumericalPrefix, zipReader, metadata, serviceContext);
    } catch (IOException ioe) {
        throw new KBArticleImportException(ioe);
    }
}
Also used : KBArticleImportException(com.liferay.knowledgebase.KBArticleImportException) ZipReader(com.liferay.portal.kernel.zip.ZipReader) IOException(java.io.IOException)

Example 4 with KBArticleImportException

use of com.liferay.knowledgebase.KBArticleImportException in project liferay-ide by liferay.

the class KBArticleImporter method getMetadata.

protected Map<String, String> getMetadata(ZipReader zipReader) throws KBArticleImportException, SystemException {
    InputStream inputStream = null;
    try {
        inputStream = zipReader.getEntryAsInputStream(".METADATA");
        if (inputStream == null) {
            return Collections.emptyMap();
        }
        Properties properties = new Properties();
        properties.load(inputStream);
        Map<String, String> metadata = new HashMap<String, String>(properties.size());
        for (Object key : properties.keySet()) {
            Object value = properties.get(key);
            if (value != null) {
                metadata.put(key.toString(), value.toString());
            }
        }
        return metadata;
    } catch (IOException ioe) {
        throw new KBArticleImportException(ioe);
    } finally {
        StreamUtil.cleanUp(inputStream);
    }
}
Also used : HashMap(java.util.HashMap) InputStream(java.io.InputStream) KBArticleImportException(com.liferay.knowledgebase.KBArticleImportException) IOException(java.io.IOException) Properties(java.util.Properties)

Aggregations

KBArticleImportException (com.liferay.knowledgebase.KBArticleImportException)4 IOException (java.io.IOException)3 InputStream (java.io.InputStream)2 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 UploadPortletRequest (com.liferay.portal.kernel.upload.UploadPortletRequest)1 StringBundler (com.liferay.portal.kernel.util.StringBundler)1 ZipReader (com.liferay.portal.kernel.zip.ZipReader)1 ServiceContext (com.liferay.portal.service.ServiceContext)1 ThemeDisplay (com.liferay.portal.theme.ThemeDisplay)1 HashMap (java.util.HashMap)1 Properties (java.util.Properties)1