1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.journal.util;
24  
25  import com.liferay.portal.kernel.search.Document;
26  import com.liferay.portal.kernel.search.DocumentImpl;
27  import com.liferay.portal.kernel.search.DocumentSummary;
28  import com.liferay.portal.kernel.search.Field;
29  import com.liferay.portal.kernel.search.SearchEngineUtil;
30  import com.liferay.portal.kernel.search.SearchException;
31  import com.liferay.portal.kernel.util.HtmlUtil;
32  import com.liferay.portal.kernel.util.StringPool;
33  import com.liferay.portal.kernel.util.StringUtil;
34  import com.liferay.portal.kernel.xml.Element;
35  import com.liferay.portal.kernel.xml.SAXReaderUtil;
36  import com.liferay.portal.util.PortletKeys;
37  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
38  
39  import java.util.Date;
40  
41  import javax.portlet.PortletURL;
42  
43  /**
44   * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   * @author Harry Mark
48   * @author Bruno Farache
49   *
50   */
51  public class Indexer implements com.liferay.portal.kernel.search.Indexer {
52  
53      public static final String PORTLET_ID = PortletKeys.JOURNAL;
54  
55      public static void addArticle(
56              long companyId, long groupId, String articleId, double version,
57              String title, String description, String content, String type,
58              Date displayDate, String[] tagsEntries)
59          throws SearchException {
60  
61          Document doc = getArticleDocument(
62              companyId, groupId, articleId, version, title, description, content,
63              type, displayDate, tagsEntries);
64  
65          SearchEngineUtil.addDocument(companyId, doc);
66      }
67  
68      public static void deleteArticle(long companyId, String articleId)
69          throws SearchException {
70  
71          SearchEngineUtil.deleteDocument(companyId, getArticleUID(articleId));
72      }
73  
74      public static Document getArticleDocument(
75          long companyId, long groupId, String articleId, double version,
76          String title, String description, String content, String type,
77          Date displayDate, String[] tagsEntries) {
78  
79          if ((content != null) &&
80              ((content.indexOf("<dynamic-content>") != -1) ||
81               (content.indexOf("<static-content") != -1))) {
82  
83              content = _getIndexableContent(content);
84  
85              content = StringUtil.replace(
86                  content, "<![CDATA[", StringPool.BLANK);
87              content = StringUtil.replace(content, "]]>", StringPool.BLANK);
88          }
89  
90          content = StringUtil.replace(content, "&amp;", "&");
91          content = StringUtil.replace(content, "&lt;", "<");
92          content = StringUtil.replace(content, "&gt;", ">");
93  
94          content = HtmlUtil.extractText(content);
95  
96          Document doc = new DocumentImpl();
97  
98          doc.addUID(PORTLET_ID, articleId);
99  
100         doc.addKeyword(Field.COMPANY_ID, companyId);
101         doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
102         doc.addKeyword(Field.GROUP_ID, groupId);
103 
104         doc.addText(Field.TITLE, title);
105         doc.addText(Field.CONTENT, content);
106         doc.addText(Field.DESCRIPTION, description);
107 
108         doc.addModifiedDate();
109 
110         doc.addKeyword(Field.ENTRY_CLASS_PK, articleId);
111         doc.addKeyword("version", version);
112         doc.addKeyword("type", type);
113         doc.addDate("displayDate", displayDate);
114 
115         doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
116 
117         return doc;
118     }
119 
120     public static String getArticleUID(String articleId) {
121         Document doc = new DocumentImpl();
122 
123         doc.addUID(PORTLET_ID, articleId);
124 
125         return doc.get(Field.UID);
126     }
127 
128     public static void updateArticle(
129             long companyId, long groupId, String articleId, double version,
130             String title, String description, String content, String type,
131             Date displayDate, String[] tagsEntries)
132         throws SearchException {
133 
134         Document doc = getArticleDocument(
135             companyId, groupId, articleId, version, title, description, content,
136             type, displayDate, tagsEntries);
137 
138         SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
139     }
140 
141     public DocumentSummary getDocumentSummary(
142         com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
143 
144         // Title
145 
146         String title = doc.get(Field.TITLE);
147 
148         // Content
149 
150         String content = doc.get(Field.CONTENT);
151 
152         content = StringUtil.shorten(content, 200);
153 
154         // Portlet URL
155 
156         String groupId = doc.get("groupId");
157         String articleId = doc.get(Field.ENTRY_CLASS_PK);
158         String version = doc.get("version");
159 
160         portletURL.setParameter("struts_action", "/journal/edit_article");
161         portletURL.setParameter("groupId", groupId);
162         portletURL.setParameter("articleId", articleId);
163         portletURL.setParameter("version", version);
164 
165         return new DocumentSummary(title, content, portletURL);
166     }
167 
168     public void reIndex(String[] ids) throws SearchException {
169         try {
170             JournalArticleLocalServiceUtil.reIndex(ids);
171         }
172         catch (Exception e) {
173             throw new SearchException(e);
174         }
175     }
176 
177     private static String _getIndexableContent(String content) {
178         try {
179             StringBuilder sb = new StringBuilder();
180 
181             com.liferay.portal.kernel.xml.Document doc = SAXReaderUtil.read(
182                 content);
183 
184             Element root = doc.getRootElement();
185 
186             _getIndexableContent(sb, root);
187 
188             return sb.toString();
189         }
190         catch (Exception e) {
191             e.printStackTrace();
192 
193             return content;
194         }
195     }
196 
197     private static void _getIndexableContent(StringBuilder sb, Element root)
198         throws Exception {
199 
200         for (Element el : root.elements()) {
201             String elType = el.attributeValue("type", StringPool.BLANK);
202 
203             if (elType.equals("text") || elType.equals("text_box") ||
204                 elType.equals("text_area")) {
205 
206                 Element dynamicContent = el.element("dynamic-content");
207 
208                 String text = dynamicContent.getText();
209 
210                 sb.append(text);
211                 sb.append(StringPool.BLANK);
212             }
213             else if (el.getName().equals("static-content")) {
214                 String text = el.getText();
215 
216                 sb.append(text);
217                 sb.append(StringPool.BLANK);
218             }
219 
220             _getIndexableContent(sb, el);
221         }
222     }
223 
224 }