1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.portlet.journal.util;
16  
17  import com.liferay.portal.kernel.search.Document;
18  import com.liferay.portal.kernel.search.DocumentImpl;
19  import com.liferay.portal.kernel.search.DocumentSummary;
20  import com.liferay.portal.kernel.search.Field;
21  import com.liferay.portal.kernel.search.SearchEngineUtil;
22  import com.liferay.portal.kernel.search.SearchException;
23  import com.liferay.portal.kernel.util.HtmlUtil;
24  import com.liferay.portal.kernel.util.StringPool;
25  import com.liferay.portal.kernel.util.StringUtil;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.kernel.xml.Element;
28  import com.liferay.portal.kernel.xml.SAXReaderUtil;
29  import com.liferay.portal.util.PortletKeys;
30  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
31  
32  import java.util.Date;
33  import java.util.List;
34  
35  import javax.portlet.PortletURL;
36  
37  /**
38   * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   * @author Harry Mark
42   * @author Bruno Farache
43   */
44  public class Indexer implements com.liferay.portal.kernel.search.Indexer {
45  
46      public static final String PORTLET_ID = PortletKeys.JOURNAL;
47  
48      public static void addArticle(
49              long companyId, long groupId, String articleId, double version,
50              String title, String description, String content, String type,
51              Date displayDate, String[] tagsEntries)
52          throws SearchException {
53  
54          Document doc = getArticleDocument(
55              companyId, groupId, articleId, version, title, description, content,
56              type, displayDate, tagsEntries);
57  
58          SearchEngineUtil.addDocument(companyId, doc);
59      }
60  
61      public static void deleteArticle(
62              long companyId, long groupId, String articleId)
63          throws SearchException {
64  
65          SearchEngineUtil.deleteDocument(
66              companyId, getArticleUID(groupId, articleId));
67      }
68  
69      public static Document getArticleDocument(
70          long companyId, long groupId, String articleId, double version,
71          String title, String description, String content, String type,
72          Date displayDate, String[] tagsEntries) {
73  
74          Document doc = new DocumentImpl();
75  
76          if ((content != null) &&
77              ((content.indexOf("<dynamic-content") != -1) ||
78               (content.indexOf("<static-content") != -1))) {
79  
80              content = _getIndexableContent(doc, content);
81  
82              content = StringUtil.replace(
83                  content, "<![CDATA[", StringPool.BLANK);
84              content = StringUtil.replace(content, "]]>", StringPool.BLANK);
85          }
86  
87          content = StringUtil.replace(content, "&amp;", "&");
88          content = StringUtil.replace(content, "&lt;", "<");
89          content = StringUtil.replace(content, "&gt;", ">");
90  
91          content = HtmlUtil.extractText(content);
92  
93          doc.addUID(PORTLET_ID, groupId, articleId);
94  
95          doc.addModifiedDate(displayDate);
96  
97          doc.addKeyword(Field.COMPANY_ID, companyId);
98          doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
99          doc.addKeyword(Field.GROUP_ID, groupId);
100 
101         doc.addText(Field.TITLE, title);
102         doc.addText(Field.CONTENT, content);
103         doc.addText(Field.DESCRIPTION, description);
104         doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
105 
106         doc.addKeyword(Field.ENTRY_CLASS_PK, articleId);
107         doc.addKeyword(Field.VERSION, version);
108         doc.addKeyword(Field.TYPE, type);
109 
110         return doc;
111     }
112 
113     public static String getArticleUID(long groupId, String articleId) {
114         Document doc = new DocumentImpl();
115 
116         doc.addUID(PORTLET_ID, groupId, articleId);
117 
118         return doc.get(Field.UID);
119     }
120 
121     public static void updateArticle(
122             long companyId, long groupId, String articleId, double version,
123             String title, String description, String content, String type,
124             Date displayDate, String[] tagsEntries)
125         throws SearchException {
126 
127         Document doc = getArticleDocument(
128             companyId, groupId, articleId, version, title, description, content,
129             type, displayDate, tagsEntries);
130 
131         SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
132     }
133 
134     public DocumentSummary getDocumentSummary(
135         com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
136 
137         // Title
138 
139         String title = doc.get(Field.TITLE);
140 
141         // Content
142 
143         String content = doc.get(Field.CONTENT);
144 
145         content = StringUtil.shorten(content, 200);
146 
147         // Portlet URL
148 
149         String groupId = doc.get("groupId");
150         String articleId = doc.get(Field.ENTRY_CLASS_PK);
151         String version = doc.get("version");
152 
153         portletURL.setParameter("struts_action", "/journal/edit_article");
154         portletURL.setParameter("groupId", groupId);
155         portletURL.setParameter("articleId", articleId);
156         portletURL.setParameter("version", version);
157 
158         return new DocumentSummary(title, content, portletURL);
159     }
160 
161     public void reIndex(String[] ids) throws SearchException {
162         try {
163             JournalArticleLocalServiceUtil.reIndex(ids);
164         }
165         catch (Exception e) {
166             throw new SearchException(e);
167         }
168     }
169 
170     private static String _getIndexableContent(Document doc, String content) {
171         try {
172             StringBuilder sb = new StringBuilder();
173 
174             com.liferay.portal.kernel.xml.Document contentDoc =
175                 SAXReaderUtil.read(content);
176 
177             Element root = contentDoc.getRootElement();
178 
179             _getIndexableContent(sb, doc, root);
180 
181             return sb.toString();
182         }
183         catch (Exception e) {
184             e.printStackTrace();
185 
186             return content;
187         }
188     }
189 
190     private static void _getIndexableContent(
191             StringBuilder sb, Document doc, Element root)
192         throws Exception {
193 
194         for (Element el : root.elements()) {
195             String elType = el.attributeValue("type", StringPool.BLANK);
196             String elIndexType = el.attributeValue(
197                 "index-type", StringPool.BLANK);
198 
199             _indexField(doc, el, elType, elIndexType);
200 
201             if (elType.equals("text") || elType.equals("text_box") ||
202                 elType.equals("text_area")) {
203 
204                 for (Element dynamicContent : el.elements("dynamic-content")) {
205                     String text = dynamicContent.getText();
206 
207                     sb.append(text);
208                     sb.append(StringPool.SPACE);
209                 }
210             }
211             else if (el.getName().equals("static-content")) {
212                 String text = el.getText();
213 
214                 sb.append(text);
215                 sb.append(StringPool.SPACE);
216             }
217 
218             _getIndexableContent(sb, doc, el);
219         }
220     }
221 
222     private static void _indexField(
223         Document doc, Element el, String elType, String elIndexType) {
224 
225         if (Validator.isNull(elIndexType)) {
226             return;
227         }
228 
229         Element dynamicContent = el.element("dynamic-content");
230 
231         String name = el.attributeValue("name", StringPool.BLANK);
232         String[] value = new String[] {dynamicContent.getText()};
233 
234         if (elType.equals("multi-list")) {
235             List<Element> options = dynamicContent.elements();
236 
237             value = new String[options.size()];
238 
239             for (int i = 0; i < options.size(); i++) {
240                 value[i] = options.get(i).getText();
241             }
242         }
243 
244         if (elIndexType.equals("keyword")) {
245             doc.addKeyword(name, value);
246         }
247         else if (elIndexType.equals("text")) {
248             doc.addText(name, StringUtil.merge(value, StringPool.SPACE));
249         }
250     }
251 
252 }