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.util.PortletKeys;
35  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
36  
37  import java.io.StringReader;
38  
39  import java.util.Date;
40  import java.util.List;
41  
42  import javax.portlet.PortletURL;
43  
44  import org.dom4j.Element;
45  import org.dom4j.io.SAXReader;
46  
47  /**
48   * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   * @author Harry Mark
52   * @author Bruno Farache
53   *
54   */
55  public class Indexer implements com.liferay.portal.kernel.search.Indexer {
56  
57      public static final String PORTLET_ID = PortletKeys.JOURNAL;
58  
59      public static void addArticle(
60              long companyId, long groupId, String articleId, double version,
61              String title, String description, String content, String type,
62              Date displayDate, String[] tagsEntries)
63          throws SearchException {
64  
65          Document doc = getArticleDocument(
66              companyId, groupId, articleId, version, title, description, content,
67              type, displayDate, tagsEntries);
68  
69          SearchEngineUtil.addDocument(companyId, doc);
70      }
71  
72      public static void deleteArticle(long companyId, String articleId)
73          throws SearchException {
74  
75          SearchEngineUtil.deleteDocument(companyId, getArticleUID(articleId));
76      }
77  
78      public static Document getArticleDocument(
79          long companyId, long groupId, String articleId, double version,
80          String title, String description, String content, String type,
81          Date displayDate, String[] tagsEntries) {
82  
83          if ((content != null) &&
84              ((content.indexOf("<dynamic-content>") != -1) ||
85               (content.indexOf("<static-content") != -1))) {
86  
87              content = _getIndexableContent(content);
88  
89              content = StringUtil.replace(
90                  content, "<![CDATA[", StringPool.BLANK);
91              content = StringUtil.replace(content, "]]>", StringPool.BLANK);
92          }
93  
94          content = StringUtil.replace(content, "&amp;", "&");
95          content = StringUtil.replace(content, "&lt;", "<");
96          content = StringUtil.replace(content, "&gt;", ">");
97  
98          content = HtmlUtil.extractText(content);
99  
100         Document doc = new DocumentImpl();
101 
102         doc.addUID(PORTLET_ID, articleId);
103 
104         doc.addKeyword(Field.COMPANY_ID, companyId);
105         doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
106         doc.addKeyword(Field.GROUP_ID, groupId);
107 
108         doc.addText(Field.TITLE, title);
109         doc.addText(Field.CONTENT, content);
110         doc.addText(Field.DESCRIPTION, description);
111 
112         doc.addModifiedDate();
113 
114         doc.addKeyword(Field.ENTRY_CLASS_PK, articleId);
115         doc.addKeyword("version", version);
116         doc.addKeyword("type", type);
117         doc.addDate("displayDate", displayDate);
118 
119         doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
120 
121         return doc;
122     }
123 
124     public static String getArticleUID(String articleId) {
125         Document doc = new DocumentImpl();
126 
127         doc.addUID(PORTLET_ID, articleId);
128 
129         return doc.get(Field.UID);
130     }
131 
132     public static void updateArticle(
133             long companyId, long groupId, String articleId, double version,
134             String title, String description, String content, String type,
135             Date displayDate, String[] tagsEntries)
136         throws SearchException {
137 
138         Document doc = getArticleDocument(
139             companyId, groupId, articleId, version, title, description, content,
140             type, displayDate, tagsEntries);
141 
142         SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
143     }
144 
145     public DocumentSummary getDocumentSummary(
146         com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
147 
148         // Title
149 
150         String title = doc.get(Field.TITLE);
151 
152         // Content
153 
154         String content = doc.get(Field.CONTENT);
155 
156         content = StringUtil.shorten(content, 200);
157 
158         // Portlet URL
159 
160         String groupId = doc.get("groupId");
161         String articleId = doc.get(Field.ENTRY_CLASS_PK);
162         String version = doc.get("version");
163 
164         portletURL.setParameter("struts_action", "/journal/edit_article");
165         portletURL.setParameter("groupId", groupId);
166         portletURL.setParameter("articleId", articleId);
167         portletURL.setParameter("version", version);
168 
169         return new DocumentSummary(title, content, portletURL);
170     }
171 
172     public void reIndex(String[] ids) throws SearchException {
173         try {
174             JournalArticleLocalServiceUtil.reIndex(ids);
175         }
176         catch (Exception e) {
177             throw new SearchException(e);
178         }
179     }
180 
181     private static String _getIndexableContent(String content) {
182         try {
183             StringBuilder sb = new StringBuilder();
184 
185             SAXReader reader = new SAXReader();
186 
187             org.dom4j.Document doc = reader.read(new StringReader(content));
188 
189             Element root = doc.getRootElement();
190 
191             _getIndexableContent(sb, root);
192 
193             return sb.toString();
194         }
195         catch (Exception e) {
196             e.printStackTrace();
197 
198             return content;
199         }
200     }
201 
202     private static void _getIndexableContent(StringBuilder sb, Element root)
203         throws Exception {
204 
205         for (Element el : (List<Element>)root.elements()) {
206             String elType = el.attributeValue("type", StringPool.BLANK);
207 
208             if (elType.equals("text") || elType.equals("text_box") ||
209                 elType.equals("text_area")) {
210 
211                 Element dynamicContent = el.element("dynamic-content");
212 
213                 String text = dynamicContent.getText();
214 
215                 sb.append(text);
216                 sb.append(StringPool.BLANK);
217             }
218             else if (el.getName().equals("static-content")) {
219                 String text = el.getText();
220 
221                 sb.append(text);
222                 sb.append(StringPool.BLANK);
223             }
224 
225             _getIndexableContent(sb, el);
226         }
227     }
228 
229 }