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.expando.model.ExpandoBridge;
31  import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
32  import com.liferay.portlet.journal.model.JournalArticle;
33  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
34  
35  import java.util.Date;
36  import java.util.List;
37  
38  import javax.portlet.PortletURL;
39  
40  /**
41   * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   * @author Harry Mark
45   * @author Bruno Farache
46   * @author Raymond Augé
47   */
48  public class Indexer implements com.liferay.portal.kernel.search.Indexer {
49  
50      public static final String PORTLET_ID = PortletKeys.JOURNAL;
51  
52      public static void addArticle(
53              long companyId, long groupId, long userId, long resourcePrimKey,
54              String articleId, double version, String title, String description,
55              String content, String type, Date displayDate,
56              String[] tagsCategories, String[] tagsEntries,
57              ExpandoBridge expandoBridge)
58          throws SearchException {
59  
60          Document doc = getArticleDocument(
61              companyId, groupId, userId, resourcePrimKey, articleId, version,
62              title, description, content, type, displayDate, tagsCategories,
63              tagsEntries, expandoBridge);
64  
65          SearchEngineUtil.addDocument(companyId, doc);
66      }
67  
68      /**
69       * @deprecated
70       */
71      public static void addArticle(
72              long companyId, long groupId, long resourcePrimKey,
73              String articleId, double version, String title, String description,
74              String content, String type, Date displayDate,
75              String[] tagsCategories, String[] tagsEntries,
76              ExpandoBridge expandoBridge)
77          throws SearchException {
78  
79          addArticle(
80              companyId, groupId, 0, resourcePrimKey, articleId, version, title,
81              description, content, type, displayDate, tagsCategories,
82              tagsEntries, expandoBridge);
83      }
84  
85      public static void deleteArticle(
86              long companyId, long groupId, String articleId)
87          throws SearchException {
88  
89          SearchEngineUtil.deleteDocument(
90              companyId, getArticleUID(groupId, articleId));
91      }
92  
93      public static Document getArticleDocument(
94          long companyId, long groupId, long userId, long resourcePrimKey,
95          String articleId, double version, String title, String description,
96          String content, String type, Date displayDate, String[] tagsCategories,
97          String[] tagsEntries, ExpandoBridge expandoBridge) {
98  
99          Document doc = new DocumentImpl();
100 
101         if ((content != null) &&
102             ((content.indexOf("<dynamic-content") != -1) ||
103              (content.indexOf("<static-content") != -1))) {
104 
105             content = _getIndexableContent(doc, content);
106 
107             content = StringUtil.replace(
108                 content, "<![CDATA[", StringPool.BLANK);
109             content = StringUtil.replace(content, "]]>", StringPool.BLANK);
110         }
111 
112         content = StringUtil.replace(content, "&amp;", "&");
113         content = StringUtil.replace(content, "&lt;", "<");
114         content = StringUtil.replace(content, "&gt;", ">");
115 
116         content = HtmlUtil.extractText(content);
117 
118         doc.addUID(PORTLET_ID, groupId, articleId);
119 
120         doc.addModifiedDate(displayDate);
121 
122         doc.addKeyword(Field.COMPANY_ID, companyId);
123         doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
124         doc.addKeyword(Field.GROUP_ID, groupId);
125         doc.addKeyword(Field.USER_ID, userId);
126 
127         doc.addText(Field.TITLE, title);
128         doc.addText(Field.CONTENT, content);
129         doc.addText(Field.DESCRIPTION, description);
130         doc.addKeyword(Field.TAGS_CATEGORIES, tagsCategories);
131         doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
132 
133         doc.addKeyword(Field.ENTRY_CLASS_NAME, JournalArticle.class.getName());
134         doc.addKeyword(Field.ENTRY_CLASS_PK, articleId);
135         doc.addKeyword(Field.ROOT_ENTRY_CLASS_PK, resourcePrimKey);
136         doc.addKeyword(Field.VERSION, version);
137         doc.addKeyword(Field.TYPE, type);
138 
139         ExpandoBridgeIndexerUtil.addAttributes(doc, expandoBridge);
140 
141         return doc;
142     }
143 
144     /**
145      * @deprecated
146      */
147     public static Document getArticleDocument(
148         long companyId, long groupId, long resourcePrimKey, String articleId,
149         double version, String title, String description, String content,
150         String type, Date displayDate, String[] tagsCategories,
151         String[] tagsEntries, ExpandoBridge expandoBridge) {
152 
153         return getArticleDocument(
154             companyId, groupId, 0, resourcePrimKey, articleId, version, title,
155             description, content, type, displayDate, tagsCategories,
156             tagsEntries, expandoBridge);
157     }
158 
159     public static String getArticleUID(long groupId, String articleId) {
160         Document doc = new DocumentImpl();
161 
162         doc.addUID(PORTLET_ID, groupId, articleId);
163 
164         return doc.get(Field.UID);
165     }
166 
167     public static void updateArticle(
168             long companyId, long groupId, long userId, long resourcePrimKey,
169             String articleId, double version, String title, String description,
170             String content, String type, Date displayDate,
171             String[] tagsCategories, String[] tagsEntries,
172             ExpandoBridge expandoBridge)
173         throws SearchException {
174 
175         Document doc = getArticleDocument(
176             companyId, groupId, userId, resourcePrimKey, articleId, version,
177             title, description, content, type, displayDate, tagsCategories,
178             tagsEntries, expandoBridge);
179 
180         SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
181     }
182 
183     /**
184      * @deprecated
185      */
186     public static void updateArticle(
187             long companyId, long groupId, long resourcePrimKey,
188             String articleId, double version, String title, String description,
189             String content, String type, Date displayDate,
190             String[] tagsCategories, String[] tagsEntries,
191             ExpandoBridge expandoBridge)
192         throws SearchException {
193 
194         updateArticle(
195             companyId, groupId, 0, resourcePrimKey, articleId, version, title,
196             description, content, type, displayDate, tagsCategories,
197             tagsEntries, expandoBridge);
198     }
199 
200     public String[] getClassNames() {
201         return _CLASS_NAMES;
202     }
203 
204     public DocumentSummary getDocumentSummary(
205         Document doc, String snippet, PortletURL portletURL) {
206 
207         // Title
208 
209         String title = doc.get(Field.TITLE);
210 
211         // Content
212 
213         String content = snippet;
214 
215         if (Validator.isNull(snippet)) {
216             content = StringUtil.shorten(doc.get(Field.CONTENT), 200);
217         }
218 
219         // Portlet URL
220 
221         String groupId = doc.get("groupId");
222         String articleId = doc.get(Field.ENTRY_CLASS_PK);
223         String version = doc.get("version");
224 
225         portletURL.setParameter("struts_action", "/journal/edit_article");
226         portletURL.setParameter("groupId", groupId);
227         portletURL.setParameter("articleId", articleId);
228         portletURL.setParameter("version", version);
229 
230         return new DocumentSummary(title, content, portletURL);
231     }
232 
233     public void reIndex(String className, long classPK) throws SearchException {
234         try {
235             JournalArticleLocalServiceUtil.reIndex(classPK);
236         }
237         catch (Exception e) {
238             throw new SearchException(e);
239         }
240     }
241 
242     public void reIndex(String[] ids) throws SearchException {
243         try {
244             JournalArticleLocalServiceUtil.reIndex(ids);
245         }
246         catch (Exception e) {
247             throw new SearchException(e);
248         }
249     }
250 
251     private static String _getIndexableContent(Document doc, String content) {
252         try {
253             StringBuilder sb = new StringBuilder();
254 
255             com.liferay.portal.kernel.xml.Document contentDoc =
256                 SAXReaderUtil.read(content);
257 
258             Element root = contentDoc.getRootElement();
259 
260             _getIndexableContent(sb, doc, root);
261 
262             return sb.toString();
263         }
264         catch (Exception e) {
265             e.printStackTrace();
266 
267             return content;
268         }
269     }
270 
271     private static void _getIndexableContent(
272             StringBuilder sb, Document doc, Element root)
273         throws Exception {
274 
275         for (Element el : root.elements()) {
276             String elType = el.attributeValue("type", StringPool.BLANK);
277             String elIndexType = el.attributeValue(
278                 "index-type", StringPool.BLANK);
279 
280             _indexField(doc, el, elType, elIndexType);
281 
282             if (elType.equals("text") || elType.equals("text_box") ||
283                 elType.equals("text_area")) {
284 
285                 for (Element dynamicContent : el.elements("dynamic-content")) {
286                     String text = dynamicContent.getText();
287 
288                     sb.append(text);
289                     sb.append(StringPool.SPACE);
290                 }
291             }
292             else if (el.getName().equals("static-content")) {
293                 String text = el.getText();
294 
295                 sb.append(text);
296                 sb.append(StringPool.SPACE);
297             }
298 
299             _getIndexableContent(sb, doc, el);
300         }
301     }
302 
303     private static void _indexField(
304         Document doc, Element el, String elType, String elIndexType) {
305 
306         if (Validator.isNull(elIndexType)) {
307             return;
308         }
309 
310         Element dynamicContent = el.element("dynamic-content");
311 
312         String name = el.attributeValue("name", StringPool.BLANK);
313         String[] value = new String[] {dynamicContent.getText()};
314 
315         if (elType.equals("multi-list")) {
316             List<Element> options = dynamicContent.elements();
317 
318             value = new String[options.size()];
319 
320             for (int i = 0; i < options.size(); i++) {
321                 value[i] = options.get(i).getText();
322             }
323         }
324 
325         if (elIndexType.equals("keyword")) {
326             doc.addKeyword(name, value);
327         }
328         else if (elIndexType.equals("text")) {
329             doc.addText(name, StringUtil.merge(value, StringPool.SPACE));
330         }
331     }
332 
333     private static final String[] _CLASS_NAMES = new String[] {
334         JournalArticle.class.getName()
335     };
336 
337 }