
com.liferay.knowledge.base.internal.importer.KBArticleImporter Maven / Gradle / Ivy
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.knowledge.base.internal.importer;
import com.liferay.asset.kernel.exception.AssetCategoryException;
import com.liferay.knowledge.base.constants.KBArticleConstants;
import com.liferay.knowledge.base.constants.KBFolderConstants;
import com.liferay.knowledge.base.exception.KBArticleImportException;
import com.liferay.knowledge.base.internal.importer.util.KBArticleMarkdownConverter;
import com.liferay.knowledge.base.model.KBArticle;
import com.liferay.knowledge.base.service.KBArticleLocalServiceUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.repository.model.FileEntry;
import com.liferay.portal.kernel.service.ServiceContext;
import com.liferay.portal.kernel.util.PortalUtil;
import com.liferay.portal.kernel.util.StreamUtil;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.workflow.WorkflowConstants;
import com.liferay.portal.kernel.zip.ZipReader;
import com.liferay.portal.kernel.zip.ZipReaderFactoryUtil;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
/**
* @author James Hinkey
* @author Sergio González
* @author Jesse Rao
*/
@Component(service = KBArticleImporter.class)
public class KBArticleImporter {
public int processZipFile(
long userId, long groupId, long parentKBFolderId,
boolean prioritizeByNumericalPrefix, InputStream inputStream,
ServiceContext serviceContext)
throws PortalException {
if (inputStream == null) {
throw new KBArticleImportException("Input stream is null");
}
try {
ZipReader zipReader = ZipReaderFactoryUtil.getZipReader(
inputStream);
Map metadata = getMetadata(zipReader);
return processKBArticleFiles(
userId, groupId, parentKBFolderId, prioritizeByNumericalPrefix,
zipReader, metadata, serviceContext);
}
catch (IOException ioe) {
throw new KBArticleImportException(ioe);
}
}
protected KBArticle addKBArticleMarkdown(
long userId, long groupId, long parentKBFolderId,
long parentResourceClassNameId, long parentResourcePrimaryKey,
String markdown, String fileEntryName, ZipReader zipReader,
Map metadata,
PrioritizationStrategy prioritizationStrategy,
ServiceContext serviceContext)
throws KBArticleImportException {
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 (AssetCategoryException ace) {
throw new KBArticleImportException.MustHaveACategory(ace);
}
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());
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);
}
}
protected Map getMetadata(ZipReader zipReader)
throws KBArticleImportException {
InputStream inputStream = null;
try {
inputStream = zipReader.getEntryAsInputStream(".METADATA");
if (inputStream == null) {
return Collections.emptyMap();
}
Properties properties = new Properties();
properties.load(inputStream);
Map metadata = new HashMap<>(properties.size());
for (Map.Entry
© 2015 - 2025 Weber Informatics LLC | Privacy Policy