use of com.liferay.portal.service.ServiceContext 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);
}
}
}
use of com.liferay.portal.service.ServiceContext in project liferay-ide by liferay.
the class KBCommentStagedModelDataHandler method doImportStagedModel.
@Override
protected void doImportStagedModel(PortletDataContext portletDataContext, KBComment kbComment) throws Exception {
long userId = portletDataContext.getUserId(kbComment.getUserUuid());
Map<Long, Long> relatedClassPKs = (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(kbComment.getClassName());
long newClassPK = MapUtil.getLong(relatedClassPKs, kbComment.getClassPK(), kbComment.getClassPK());
ServiceContext serviceContext = portletDataContext.createServiceContext(kbComment);
KBComment importedKBComment = null;
if (portletDataContext.isDataStrategyMirror()) {
KBComment existingKBComment = KBCommentUtil.fetchByUUID_G(kbComment.getUuid(), portletDataContext.getScopeGroupId());
if (existingKBComment == null) {
serviceContext.setUuid(kbComment.getUuid());
importedKBComment = KBCommentLocalServiceUtil.addKBComment(userId, kbComment.getClassNameId(), newClassPK, kbComment.getContent(), kbComment.getUserRating(), serviceContext);
} else {
importedKBComment = KBCommentLocalServiceUtil.updateKBComment(existingKBComment.getKbCommentId(), kbComment.getClassNameId(), newClassPK, kbComment.getContent(), kbComment.getUserRating(), kbComment.getStatus(), serviceContext);
}
} else {
importedKBComment = KBCommentLocalServiceUtil.addKBComment(userId, kbComment.getClassNameId(), newClassPK, kbComment.getContent(), kbComment.getUserRating(), serviceContext);
}
portletDataContext.importClassedModel(kbComment, importedKBComment);
}
use of com.liferay.portal.service.ServiceContext in project liferay-ide by liferay.
the class KBFolderStagedModelDataHandler method doImportStagedModel.
@Override
protected void doImportStagedModel(PortletDataContext portletDataContext, KBFolder kbFolder) throws Exception {
long userId = portletDataContext.getUserId(kbFolder.getUserUuid());
if (kbFolder.getParentKBFolderId() != KBFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
StagedModelDataHandlerUtil.importReferenceStagedModels(portletDataContext, kbFolder, KBFolder.class);
}
ServiceContext serviceContext = portletDataContext.createServiceContext(kbFolder);
KBFolder importedKBFolder = null;
if (portletDataContext.isDataStrategyMirror()) {
KBFolder existingKBFolder = KBFolderUtil.fetchByUUID_G(kbFolder.getUuid(), portletDataContext.getScopeGroupId());
if (existingKBFolder == null) {
importedKBFolder = KBFolderLocalServiceUtil.addKBFolder(userId, portletDataContext.getScopeGroupId(), kbFolder.getClassNameId(), kbFolder.getParentKBFolderId(), kbFolder.getName(), kbFolder.getDescription(), serviceContext);
} else {
importedKBFolder = KBFolderLocalServiceUtil.updateKBFolder(kbFolder.getClassNameId(), kbFolder.getParentKBFolderId(), kbFolder.getKbFolderId(), kbFolder.getName(), kbFolder.getDescription());
}
} else {
importedKBFolder = KBFolderLocalServiceUtil.addKBFolder(userId, portletDataContext.getScopeGroupId(), kbFolder.getClassNameId(), kbFolder.getParentKBFolderId(), kbFolder.getName(), kbFolder.getDescription(), serviceContext);
}
portletDataContext.importClassedModel(kbFolder, importedKBFolder);
Map<Long, Long> kbFolderIds = (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(KBFolder.class);
kbFolderIds.put(kbFolder.getKbFolderId(), importedKBFolder.getKbFolderId());
}
use of com.liferay.portal.service.ServiceContext in project liferay-ide by liferay.
the class KBTemplateStagedModelDataHandler method doImportStagedModel.
@Override
protected void doImportStagedModel(PortletDataContext portletDataContext, KBTemplate kbTemplate) throws Exception {
long userId = portletDataContext.getUserId(kbTemplate.getUserUuid());
ServiceContext serviceContext = portletDataContext.createServiceContext(kbTemplate);
KBTemplate importedKBTemplate = null;
if (portletDataContext.isDataStrategyMirror()) {
KBTemplate existingKBTemplate = KBTemplateUtil.fetchByUUID_G(kbTemplate.getUuid(), portletDataContext.getScopeGroupId());
if (existingKBTemplate == null) {
serviceContext.setUuid(kbTemplate.getUuid());
importedKBTemplate = KBTemplateLocalServiceUtil.addKBTemplate(userId, kbTemplate.getTitle(), kbTemplate.getContent(), serviceContext);
} else {
importedKBTemplate = KBTemplateLocalServiceUtil.updateKBTemplate(existingKBTemplate.getKbTemplateId(), kbTemplate.getTitle(), kbTemplate.getContent(), serviceContext);
}
} else {
importedKBTemplate = KBTemplateLocalServiceUtil.addKBTemplate(userId, kbTemplate.getTitle(), kbTemplate.getContent(), serviceContext);
}
portletDataContext.importClassedModel(kbTemplate, importedKBTemplate);
}
use of com.liferay.portal.service.ServiceContext 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);
}
}
Aggregations