/modules/apps/journal/journal-service/src/main/java/com/liferay/journal/internal/search/spi/model/index/contributor/JournalArticleModelDocumentContributor.java

https://github.com/sez11a/liferay-portal · Java · 283 lines · 204 code · 62 blank · 17 comment · 21 complexity · 292716460f345843fb7d1b47c6855ed9 MD5 · raw file

  1. /**
  2. * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. package com.liferay.journal.internal.search.spi.model.index.contributor;
  15. import com.liferay.dynamic.data.mapping.model.DDMStructure;
  16. import com.liferay.dynamic.data.mapping.service.DDMStructureLocalService;
  17. import com.liferay.dynamic.data.mapping.storage.DDMFormValues;
  18. import com.liferay.dynamic.data.mapping.storage.Fields;
  19. import com.liferay.dynamic.data.mapping.util.DDMIndexer;
  20. import com.liferay.dynamic.data.mapping.util.FieldsToDDMFormValuesConverter;
  21. import com.liferay.journal.internal.util.JournalUtil;
  22. import com.liferay.journal.model.JournalArticle;
  23. import com.liferay.journal.util.JournalConverter;
  24. import com.liferay.petra.string.CharPool;
  25. import com.liferay.petra.string.StringBundler;
  26. import com.liferay.petra.string.StringPool;
  27. import com.liferay.portal.kernel.exception.PortalException;
  28. import com.liferay.portal.kernel.log.Log;
  29. import com.liferay.portal.kernel.log.LogFactoryUtil;
  30. import com.liferay.portal.kernel.search.Document;
  31. import com.liferay.portal.kernel.search.Field;
  32. import com.liferay.portal.kernel.util.GetterUtil;
  33. import com.liferay.portal.kernel.util.Html;
  34. import com.liferay.portal.kernel.util.LocaleUtil;
  35. import com.liferay.portal.kernel.util.Localization;
  36. import com.liferay.portal.kernel.util.LocalizationUtil;
  37. import com.liferay.portal.kernel.util.Portal;
  38. import com.liferay.portal.kernel.util.StringUtil;
  39. import com.liferay.portal.search.model.uid.UIDFactory;
  40. import com.liferay.portal.search.spi.model.index.contributor.ModelDocumentContributor;
  41. import com.liferay.trash.TrashHelper;
  42. import org.osgi.service.component.annotations.Component;
  43. import org.osgi.service.component.annotations.Reference;
  44. /**
  45. * @author Lourdes Fernández Besada
  46. */
  47. @Component(
  48. immediate = true,
  49. property = "indexer.class.name=com.liferay.journal.model.JournalArticle",
  50. service = ModelDocumentContributor.class
  51. )
  52. public class JournalArticleModelDocumentContributor
  53. implements ModelDocumentContributor<JournalArticle> {
  54. @Override
  55. public void contribute(Document document, JournalArticle journalArticle) {
  56. if (_log.isDebugEnabled()) {
  57. _log.debug("Indexing article " + journalArticle);
  58. }
  59. _uidFactory.setUID(journalArticle, document);
  60. String articleId = journalArticle.getArticleId();
  61. if (journalArticle.isInTrash()) {
  62. articleId = _trashHelper.getOriginalTitle(articleId);
  63. }
  64. document.addKeywordSortable(Field.ARTICLE_ID, articleId);
  65. Localization localization = LocalizationUtil.getLocalization();
  66. String[] contentAvailableLanguageIds =
  67. localization.getAvailableLanguageIds(journalArticle.getDocument());
  68. DDMStructure ddmStructure = _ddmStructureLocalService.fetchStructure(
  69. _portal.getSiteGroupId(journalArticle.getGroupId()),
  70. _portal.getClassNameId(JournalArticle.class),
  71. journalArticle.getDDMStructureKey(), true);
  72. if (ddmStructure != null) {
  73. for (String contentAvailableLanguageId :
  74. contentAvailableLanguageIds) {
  75. String content = _extractDDMContent(
  76. journalArticle, ddmStructure, contentAvailableLanguageId);
  77. document.addText(
  78. localization.getLocalizedName(
  79. Field.CONTENT, contentAvailableLanguageId),
  80. content);
  81. }
  82. }
  83. String[] descriptionAvailableLanguageIds =
  84. localization.getAvailableLanguageIds(
  85. journalArticle.getDescriptionMapAsXML());
  86. for (String descriptionAvailableLanguageId :
  87. descriptionAvailableLanguageIds) {
  88. String description = _html.stripHtml(
  89. journalArticle.getDescription(descriptionAvailableLanguageId));
  90. document.addText(
  91. localization.getLocalizedName(
  92. Field.DESCRIPTION, descriptionAvailableLanguageId),
  93. description);
  94. }
  95. document.addDate(Field.DISPLAY_DATE, journalArticle.getDisplayDate());
  96. document.addDate(
  97. Field.EXPIRATION_DATE, journalArticle.getExpirationDate());
  98. document.addKeyword(Field.FOLDER_ID, journalArticle.getFolderId());
  99. document.addKeyword(Field.LAYOUT_UUID, journalArticle.getLayoutUuid());
  100. String[] titleAvailableLanguageIds =
  101. localization.getAvailableLanguageIds(
  102. journalArticle.getTitleMapAsXML());
  103. for (String titleAvailableLanguageId : titleAvailableLanguageIds) {
  104. String title = journalArticle.getTitle(titleAvailableLanguageId);
  105. document.addText(
  106. localization.getLocalizedName(
  107. Field.TITLE, titleAvailableLanguageId),
  108. title);
  109. }
  110. document.addKeyword(
  111. Field.TREE_PATH,
  112. StringUtil.split(journalArticle.getTreePath(), CharPool.SLASH));
  113. document.addKeyword(Field.VERSION, journalArticle.getVersion());
  114. document.addKeyword(
  115. "ddmStructureKey", journalArticle.getDDMStructureKey());
  116. document.addKeyword(
  117. "ddmTemplateKey", journalArticle.getDDMTemplateKey());
  118. String defaultLanguageId = localization.getDefaultLanguageId(
  119. journalArticle.getDocument());
  120. document.addText("defaultLanguageId", defaultLanguageId);
  121. document.addDate("displayDate", journalArticle.getDisplayDate());
  122. document.addKeyword("head", JournalUtil.isHead(journalArticle));
  123. boolean headListable = JournalUtil.isHeadListable(journalArticle);
  124. document.addKeyword("headListable", headListable);
  125. boolean latestArticle = JournalUtil.isLatestArticle(journalArticle);
  126. document.addKeyword("latest", latestArticle);
  127. // Scheduled listable articles should be visible in asset browser
  128. if (journalArticle.isScheduled() && headListable) {
  129. boolean visible = GetterUtil.getBoolean(document.get("visible"));
  130. if (!visible) {
  131. document.addKeyword("visible", true);
  132. }
  133. }
  134. for (String titleAvailableLanguageId : titleAvailableLanguageIds) {
  135. try {
  136. document.addKeywordSortable(
  137. localization.getLocalizedName(
  138. "urlTitle", titleAvailableLanguageId),
  139. journalArticle.getUrlTitle(
  140. LocaleUtil.fromLanguageId(titleAvailableLanguageId)));
  141. }
  142. catch (PortalException portalException) {
  143. if (_log.isDebugEnabled()) {
  144. _log.debug(
  145. StringBundler.concat(
  146. "Unable to get friendly URL for article ID ",
  147. journalArticle.getId(), " and language ID ",
  148. titleAvailableLanguageId),
  149. portalException);
  150. }
  151. }
  152. }
  153. document.addNumber(
  154. "versionCount", GetterUtil.getDouble(journalArticle.getVersion()));
  155. if (ddmStructure != null) {
  156. _addDDMStructureAttributes(ddmStructure, document, journalArticle);
  157. }
  158. if (_log.isDebugEnabled()) {
  159. _log.debug("Document " + journalArticle + " indexed successfully");
  160. }
  161. }
  162. private void _addDDMStructureAttributes(
  163. DDMStructure ddmStructure, Document document, JournalArticle article) {
  164. document.addKeyword(Field.CLASS_TYPE_ID, ddmStructure.getStructureId());
  165. DDMFormValues ddmFormValues = null;
  166. try {
  167. Fields fields = _journalConverter.getDDMFields(
  168. ddmStructure, article.getDocument());
  169. ddmFormValues = _fieldsToDDMFormValuesConverter.convert(
  170. ddmStructure, fields);
  171. }
  172. catch (Exception exception) {
  173. if (_log.isDebugEnabled()) {
  174. _log.debug(exception);
  175. }
  176. return;
  177. }
  178. if (ddmFormValues != null) {
  179. _ddmIndexer.addAttributes(document, ddmStructure, ddmFormValues);
  180. }
  181. }
  182. private String _extractDDMContent(
  183. JournalArticle article, DDMStructure ddmStructure, String languageId) {
  184. DDMFormValues ddmFormValues = null;
  185. try {
  186. Fields fields = _journalConverter.getDDMFields(
  187. ddmStructure, article.getDocument());
  188. ddmFormValues = _fieldsToDDMFormValuesConverter.convert(
  189. ddmStructure, fields);
  190. }
  191. catch (Exception exception) {
  192. if (_log.isDebugEnabled()) {
  193. _log.debug(exception);
  194. }
  195. return StringPool.BLANK;
  196. }
  197. if (ddmFormValues == null) {
  198. return StringPool.BLANK;
  199. }
  200. return _ddmIndexer.extractIndexableAttributes(
  201. ddmStructure, ddmFormValues, LocaleUtil.fromLanguageId(languageId));
  202. }
  203. private static final Log _log = LogFactoryUtil.getLog(
  204. JournalArticleModelDocumentContributor.class);
  205. @Reference
  206. private DDMIndexer _ddmIndexer;
  207. @Reference
  208. private DDMStructureLocalService _ddmStructureLocalService;
  209. @Reference
  210. private FieldsToDDMFormValuesConverter _fieldsToDDMFormValuesConverter;
  211. @Reference
  212. private Html _html;
  213. @Reference
  214. private JournalConverter _journalConverter;
  215. @Reference
  216. private Portal _portal;
  217. @Reference
  218. private TrashHelper _trashHelper;
  219. @Reference
  220. private UIDFactory _uidFactory;
  221. }