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.addModifiedDate();
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("version", version);
113         doc.addKeyword("type", type);
114         doc.addDate("displayDate", displayDate);
115 
116         return doc;
117     }
118 
119     public static String getArticleUID(String articleId) {
120         Document doc = new DocumentImpl();
121 
122         doc.addUID(PORTLET_ID, articleId);
123 
124         return doc.get(Field.UID);
125     }
126 
127     public static void updateArticle(
128             long companyId, long groupId, String articleId, double version,
129             String title, String description, String content, String type,
130             Date displayDate, String[] tagsEntries)
131         throws SearchException {
132 
133         Document doc = getArticleDocument(
134             companyId, groupId, articleId, version, title, description, content,
135             type, displayDate, tagsEntries);
136 
137         SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
138     }
139 
140     public DocumentSummary getDocumentSummary(
141         com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
142 
143         // Title
144 
145         String title = doc.get(Field.TITLE);
146 
147         // Content
148 
149         String content = doc.get(Field.CONTENT);
150 
151         content = StringUtil.shorten(content, 200);
152 
153         // Portlet URL
154 
155         String groupId = doc.get("groupId");
156         String articleId = doc.get(Field.ENTRY_CLASS_PK);
157         String version = doc.get("version");
158 
159         portletURL.setParameter("struts_action", "/journal/edit_article");
160         portletURL.setParameter("groupId", groupId);
161         portletURL.setParameter("articleId", articleId);
162         portletURL.setParameter("version", version);
163 
164         return new DocumentSummary(title, content, portletURL);
165     }
166 
167     public void reIndex(String[] ids) throws SearchException {
168         try {
169             JournalArticleLocalServiceUtil.reIndex(ids);
170         }
171         catch (Exception e) {
172             throw new SearchException(e);
173         }
174     }
175 
176     private static String _getIndexableContent(String content) {
177         try {
178             StringBuilder sb = new StringBuilder();
179 
180             com.liferay.portal.kernel.xml.Document doc = SAXReaderUtil.read(
181                 content);
182 
183             Element root = doc.getRootElement();
184 
185             _getIndexableContent(sb, root);
186 
187             return sb.toString();
188         }
189         catch (Exception e) {
190             e.printStackTrace();
191 
192             return content;
193         }
194     }
195 
196     private static void _getIndexableContent(StringBuilder sb, Element root)
197         throws Exception {
198 
199         for (Element el : root.elements()) {
200             String elType = el.attributeValue("type", StringPool.BLANK);
201 
202             if (elType.equals("text") || elType.equals("text_box") ||
203                 elType.equals("text_area")) {
204 
205                 Element dynamicContent = el.element("dynamic-content");
206 
207                 String text = dynamicContent.getText();
208 
209                 sb.append(text);
210                 sb.append(StringPool.BLANK);
211             }
212             else if (el.getName().equals("static-content")) {
213                 String text = el.getText();
214 
215                 sb.append(text);
216                 sb.append(StringPool.BLANK);
217             }
218 
219             _getIndexableContent(sb, el);
220         }
221     }
222 
223 }