1   /**
2    * Copyright (c) 2000-2009 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.addModifiedDate(displayDate);
101 
102         doc.addKeyword(Field.COMPANY_ID, companyId);
103         doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
104         doc.addKeyword(Field.GROUP_ID, groupId);
105 
106         doc.addText(Field.TITLE, title);
107         doc.addText(Field.CONTENT, content);
108         doc.addText(Field.DESCRIPTION, description);
109         doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
110 
111         doc.addKeyword(Field.ENTRY_CLASS_PK, articleId);
112         doc.addKeyword(Field.VERSION, version);
113         doc.addKeyword(Field.TYPE, type);
114 
115         return doc;
116     }
117 
118     public static String getArticleUID(String articleId) {
119         Document doc = new DocumentImpl();
120 
121         doc.addUID(PORTLET_ID, articleId);
122 
123         return doc.get(Field.UID);
124     }
125 
126     public static void updateArticle(
127             long companyId, long groupId, String articleId, double version,
128             String title, String description, String content, String type,
129             Date displayDate, String[] tagsEntries)
130         throws SearchException {
131 
132         Document doc = getArticleDocument(
133             companyId, groupId, articleId, version, title, description, content,
134             type, displayDate, tagsEntries);
135 
136         SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
137     }
138 
139     public DocumentSummary getDocumentSummary(
140         com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
141 
142         // Title
143 
144         String title = doc.get(Field.TITLE);
145 
146         // Content
147 
148         String content = doc.get(Field.CONTENT);
149 
150         content = StringUtil.shorten(content, 200);
151 
152         // Portlet URL
153 
154         String groupId = doc.get("groupId");
155         String articleId = doc.get(Field.ENTRY_CLASS_PK);
156         String version = doc.get("version");
157 
158         portletURL.setParameter("struts_action", "/journal/edit_article");
159         portletURL.setParameter("groupId", groupId);
160         portletURL.setParameter("articleId", articleId);
161         portletURL.setParameter("version", version);
162 
163         return new DocumentSummary(title, content, portletURL);
164     }
165 
166     public void reIndex(String[] ids) throws SearchException {
167         try {
168             JournalArticleLocalServiceUtil.reIndex(ids);
169         }
170         catch (Exception e) {
171             throw new SearchException(e);
172         }
173     }
174 
175     private static String _getIndexableContent(String content) {
176         try {
177             StringBuilder sb = new StringBuilder();
178 
179             com.liferay.portal.kernel.xml.Document doc = SAXReaderUtil.read(
180                 content);
181 
182             Element root = doc.getRootElement();
183 
184             _getIndexableContent(sb, root);
185 
186             return sb.toString();
187         }
188         catch (Exception e) {
189             e.printStackTrace();
190 
191             return content;
192         }
193     }
194 
195     private static void _getIndexableContent(StringBuilder sb, Element root)
196         throws Exception {
197 
198         for (Element el : root.elements()) {
199             String elType = el.attributeValue("type", StringPool.BLANK);
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, el);
219         }
220     }
221 
222 }