use of com.liferay.journal.model.JournalArticle in project liferay-db-setup-core by ableneo.
the class SetupPages method addArticleDisplayPortletIntoPage.
private static void addArticleDisplayPortletIntoPage(final PageType page, final Layout layout, final ArticleDisplayPortletType portlet, final long companyId, final long groupId) throws PortalException {
if (isLinkPage(page)) {
return;
}
LOG.info("Adding ArticleDisplayPortlet: " + portlet.getColumn() + "@" + portlet.getColumnPosition() + "-" + portlet.getPortletId() + "; " + portlet.getArticleFolder().getFolderPath() + ":" + portlet.getArticleId());
PagePortletType toInsert = null;
long folderId = SetupArticles.getCreateFolderId(portlet.getArticleFolder().getFolderPath(), groupId, portlet.getArticleFolder().getRolePermissions());
JournalArticle journalArticle = SetupArticles.getJournalArticle(portlet.getArticleId(), folderId, groupId, portlet.getArticleFolder().getFolderPath());
if (journalArticle != null) {
Long assetEntryId = SetupArticles.getJournalAssetEntryId(journalArticle);
toInsert = new PagePortletType();
toInsert.setColumn(portlet.getColumn());
toInsert.setColumnPosition(portlet.getColumnPosition());
toInsert.setPortletId(portlet.getPortletId());
toInsert.getPortletPreference().addAll(portlet.getPortletPreference());
toInsert.setRolePermissions(portlet.getRolePermissions());
toInsert.getPortletPreference().add(newPortletPreference("groupId", String.valueOf(groupId)));
// String.valueOf(journalArticle.getPrimaryKey())));
toInsert.getPortletPreference().add(newPortletPreference("articleId", journalArticle.getArticleId()));
if (assetEntryId != null) {
toInsert.getPortletPreference().add(newPortletPreference("assetEntryId", String.valueOf(assetEntryId)));
}
} else {
LOG.error("No journal entry found = skip adding it to the wrappedPortlet. Manual task!");
toInsert = portlet;
}
insertPortletIntoPage(page, layout, toInsert, companyId, groupId);
}
use of com.liferay.journal.model.JournalArticle in project liferay-db-setup-core by ableneo.
the class TaggingUtil method associateTagsAndCategories.
public static void associateTagsAndCategories(long groupId, Article article, JournalArticle journalArticle) throws PortalException {
List<Tag> tags = article.getTag();
String[] tagNames = null;
if (tags != null) {
tagNames = tags.stream().map(Tag::getName).toArray(String[]::new);
}
long[] categoryIds = article.getCategory().stream().map(category -> ResolverUtil.lookupAll(groupId, journalArticle.getCompanyId(), category.getUuid(), article.getPath())).filter(Validator::isNumber).mapToLong(Long::parseLong).toArray();
AssetEntry entry = AssetEntryLocalServiceUtil.getEntry(JournalArticle.class.getName(), journalArticle.getResourcePrimKey());
AssetEntryLocalServiceUtil.updateEntry(SetupConfigurationThreadLocal.getRunAsUserId(), groupId, JournalArticle.class.getName(), entry.getClassPK(), categoryIds, tagNames);
}
use of com.liferay.journal.model.JournalArticle in project liferay-db-setup-core by ableneo.
the class SetupArticles method getJournalArticle.
public static JournalArticle getJournalArticle(String articleId, long folderId, long groupId, String folderPathForTheLog) {
JournalArticle journalArticle = null;
try {
List<JournalArticle> articlesInFolder = JournalArticleLocalServiceUtil.getArticles(groupId, folderId);
if (articlesInFolder == null || articlesInFolder.isEmpty()) {
LOG.warn(String.format("No such article : %1$s / %2$s (%3$s)", folderPathForTheLog, articleId, folderId));
return null;
}
List<JournalArticle> withSameArticleId = new ArrayList<JournalArticle>();
for (JournalArticle art : articlesInFolder) {
LOG.info(" - " + folderPathForTheLog + "/" + art.getArticleId());
if (articleId.equalsIgnoreCase(art.getArticleId())) {
/*if (art.getPrimaryKey() == journalArticle.getPrimaryKey()) {
LOG.info(String.format(" - found article [%1$s] which is the latest [%1$s]", articleId));
withSameArticleId.add(0, art);
} else */
if (art.getStatus() == WorkflowConstants.STATUS_APPROVED) {
LOG.info(String.format(" - found article [%1$s] with same name [%1$s]", articleId));
withSameArticleId.add(art);
} else {
LOG.info(String.format(" - found article which is not 'approved' [%1$s], leave-alone", articleId));
}
}
}
if (false == withSameArticleId.isEmpty()) {
if (withSameArticleId.size() == 1) {
LOG.info(String.format("FOUND article with ID: %1$s and directory:%2$s (%3$s)", articleId, folderPathForTheLog, folderId));
journalArticle = withSameArticleId.get(0);
} else {
LOG.warn(String.format("MULTIPLE article with ID: %1$s and directory:%2$s (%3$s)", articleId, folderPathForTheLog, folderId));
//
for (int i = 0; i < withSameArticleId.size(); i++) {
JournalArticle ja = withSameArticleId.get(i);
if (ja.getLastPublishDate() != null) {
if (journalArticle == null) {
journalArticle = ja;
} else {
if (ja.getLastPublishDate() != null && ja.getLastPublishDate().after(journalArticle.getLastPublishDate())) {
journalArticle = ja;
}
}
}
}
if (journalArticle == null) {
LOG.warn(String.format("No article amongst multiple: %1$s and directory:%2$s (%3$s)", articleId, folderPathForTheLog, folderId));
} else {
LOG.info(String.format("SELECTED article with ID: %1$s and directory:%2$s (%3$s)", articleId, folderPathForTheLog, folderId));
}
}
} else {
LOG.warn(String.format("Cannot find article with ID: %1$s and directory:%2$s (%3$s)", articleId, folderPathForTheLog, folderId));
}
} catch (SystemException e) {
LOG.error(String.format("Error while trying to find article with ID: %1$s", articleId), e);
}
return journalArticle;
}
use of com.liferay.journal.model.JournalArticle in project liferay-db-setup-core by ableneo.
the class ResolverUtil method lookupArticleWithArticleId.
public static String lookupArticleWithArticleId(final String content, final String locationHint, final long groupId, final long company, final int typeOfId) {
String contentCopy = content;
String retVal = contentCopy;
long siteGroupId = groupId;
int pos = -1;
String lookup = ARTICLE_BY_ART_ID;
if (typeOfId == 0) {
lookup = lookup.replace(IDTYPE, "ID");
} else if (typeOfId == 1) {
lookup = lookup.replace(IDTYPE, "UUID");
} else if (typeOfId == 2) {
lookup = lookup.replace(IDTYPE, "RESID");
}
while (contentCopy != null && contentCopy.indexOf(lookup) > -1) {
pos = contentCopy.indexOf(lookup);
int pos2 = contentCopy.indexOf(CLOSING_TAG, pos + 1);
if (pos2 > -1) {
String name = contentCopy.substring(pos + lookup.length(), pos2);
// check for the reference to another site
String[] refSegs = ResolverUtil.separateSiteRef(name);
if (!refSegs[0].equals("")) {
siteGroupId = ResolverUtil.getSiteGroupIdByName(refSegs[0], company, locationHint);
name = refSegs[1];
}
String templateId = "";
try {
JournalArticle ja = JournalArticleLocalServiceUtil.fetchLatestArticle(siteGroupId, name, WorkflowConstants.STATUS_APPROVED);
if (ja != null) {
if (typeOfId == 0) {
templateId = Long.toString(ja.getId());
} else if (typeOfId == 1) {
templateId = ja.getUuid();
} else if (typeOfId == 2) {
templateId = Long.toString(ja.getResourcePrimKey());
}
} else {
LOG.error(String.format("Article with article id %1$s not found for %2$s", name, locationHint));
templateId = "!!NOTFOUND!!";
}
} catch (SystemException e) {
LOG.error(String.format("Article with article id %1$s not found for %2$s", name, locationHint), e);
}
retVal = contentCopy.substring(0, pos) + templateId + contentCopy.substring(pos2 + CLOSING_TAG.length(), contentCopy.length());
contentCopy = retVal;
} else {
LOG.warn(String.format("Could not resolve template, as the syntax is offended, closing (%1$s) is missing for %2$s abort parsing, as this is possibly an error!", CLOSING_TAG, locationHint));
break;
}
}
return retVal;
}
use of com.liferay.journal.model.JournalArticle in project liferay-db-setup-core by ableneo.
the class SetupArticles method addJournalArticle.
public static void addJournalArticle(final Article article, final long groupId) {
LOG.info(String.format("Adding Journal Article %1$s", article.getTitle()));
long runAsUserId = SetupConfigurationThreadLocal.getRunAsUserId();
long companyId = SetupConfigurationThreadLocal.getRunInCompanyId();
String content = null;
long folderId = getCreateFolderId(article.getArticleFolderPath(), groupId, article.getRolePermissions());
try {
content = ResourcesUtil.getFileContent(article.getPath());
content = ResolverUtil.lookupAll(groupId, companyId, content, article.getPath());
LOG.info(String.format(" - Article File content for article ID: %1$s : path:%2$s", article.getArticleId(), article.getPath()));
} catch (IOException e) {
LOG.error(String.format("Error Reading Article File content for article ID: %1$s", article.getArticleId()));
}
Map<Locale, String> titleMap = TranslationMapUtil.getTranslationMap(article.getTitleTranslation(), groupId, article.getTitle(), String.format(" Article with title %1$s", article.getArticleId()));
Locale articleDefaultLocale = LocaleUtil.fromLanguageId(LocalizationUtil.getDefaultLanguageId(content));
if (!titleMap.containsKey(articleDefaultLocale)) {
titleMap.put(articleDefaultLocale, article.getTitle());
}
Map<Locale, String> descriptionMap = null;
if (article.getArticleDescription() != null && !article.getArticleDescription().isEmpty()) {
descriptionMap = TranslationMapUtil.getTranslationMap(article.getDescriptionTranslation(), groupId, article.getArticleDescription(), String.format(" Article with description %1$s", article.getArticleId()));
if (!descriptionMap.containsKey(articleDefaultLocale)) {
descriptionMap.put(articleDefaultLocale, article.getArticleDescription());
}
}
ServiceContext serviceContext = new ServiceContext();
serviceContext.setScopeGroupId(groupId);
JournalArticle journalArticle = null;
boolean generatedId = (article.getArticleId().isEmpty());
if (generatedId) {
LOG.info(String.format("Article %1$s will have autogenerated ID.", article.getTitle()));
} else {
journalArticle = getJournalArticle(article.getArticleId(), folderId, groupId, article.getArticleFolderPath());
}
try {
if (journalArticle == null) {
try {
journalArticle = JournalArticleLocalServiceUtil.addArticle(runAsUserId, groupId, folderId, 0, 0, article.getArticleId(), generatedId, JournalArticleConstants.VERSION_DEFAULT, titleMap, descriptionMap, content, article.getArticleStructureKey(), article.getArticleTemplateKey(), StringPool.BLANK, 1, 1, ARTICLE_PUBLISH_YEAR, 0, 0, 0, 0, 0, 0, 0, true, 0, 0, 0, 0, 0, true, true, false, StringPool.BLANK, null, null, StringPool.BLANK, serviceContext);
LOG.info(String.format("Added JournalArticle %1$s with ID: %2$s", journalArticle.getTitle(), journalArticle.getArticleId()));
Indexer bi = IndexerRegistryUtil.getIndexer(JournalArticle.class);
if (bi != null) {
bi.reindex(journalArticle);
}
} catch (PortalException e) {
LOG.error(String.format("Error while trying to add Article with Title: %1$s", article.getTitle()), e);
}
} else {
try {
LOG.info("Article " + article.getTitle() + " with article ID: " + article.getArticleId() + " already exists. Will be overwritten.");
journalArticle.setTitleMap(titleMap);
journalArticle.setContent(content);
journalArticle.setDescriptionMap(descriptionMap);
JournalArticleLocalServiceUtil.updateJournalArticle(journalArticle);
// if the folder changed, move it...
if (journalArticle.getFolderId() != folderId) {
JournalArticleLocalServiceUtil.moveArticle(groupId, journalArticle.getArticleId(), folderId, ServiceContextThreadLocal.getServiceContext());
}
LOG.info(String.format("Updated JournalArticle: %1$s", journalArticle.getTitle()));
} catch (PortalException e) {
LOG.error(String.format("Error while trying to update Article with Title: %1$s", article.getTitle()), e);
}
}
if (journalArticle != null) {
TaggingUtil.associateTagsAndCategories(groupId, article, journalArticle);
processRelatedAssets(article, journalArticle, runAsUserId, groupId, companyId);
SetupPermissions.updatePermission(String.format("Article %1$s", journalArticle.getArticleId()), companyId, journalArticle.getResourcePrimKey(), JournalArticle.class, article.getRolePermissions(), DEFAULT_PERMISSIONS);
article.setArticleId(String.valueOf(journalArticle.getId()));
} else {
LOG.error(String.format("Error while trying to add/update Article-Permission with Title: %1$s; see previous error.", article.getTitle()));
}
} catch (PortalException e) {
LOG.error(String.format("Error while trying to add/update Article-Permission with Title: %1$s", article.getTitle()), e);
}
}
Aggregations