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