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