1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.journal.util;
21  
22  import com.liferay.portal.kernel.search.Document;
23  import com.liferay.portal.kernel.search.DocumentImpl;
24  import com.liferay.portal.kernel.search.DocumentSummary;
25  import com.liferay.portal.kernel.search.Field;
26  import com.liferay.portal.kernel.search.SearchEngineUtil;
27  import com.liferay.portal.kernel.search.SearchException;
28  import com.liferay.portal.kernel.util.HtmlUtil;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.StringUtil;
31  import com.liferay.portal.kernel.xml.Element;
32  import com.liferay.portal.kernel.xml.SAXReaderUtil;
33  import com.liferay.portal.util.PortletKeys;
34  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
35  
36  import java.util.Date;
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   *
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, String articleId, double version,
54              String title, String description, String content, String type,
55              Date displayDate, String[] tagsEntries)
56          throws SearchException {
57  
58          Document doc = getArticleDocument(
59              companyId, groupId, articleId, version, title, description, content,
60              type, displayDate, tagsEntries);
61  
62          SearchEngineUtil.addDocument(companyId, doc);
63      }
64  
65      public static void deleteArticle(long companyId, String articleId)
66          throws SearchException {
67  
68          SearchEngineUtil.deleteDocument(companyId, getArticleUID(articleId));
69      }
70  
71      public static Document getArticleDocument(
72          long companyId, long groupId, String articleId, double version,
73          String title, String description, String content, String type,
74          Date displayDate, String[] tagsEntries) {
75  
76          if ((content != null) &&
77              ((content.indexOf("<dynamic-content>") != -1) ||
78               (content.indexOf("<static-content") != -1))) {
79  
80              content = _getIndexableContent(content);
81  
82              content = StringUtil.replace(
83                  content, "<![CDATA[", StringPool.BLANK);
84              content = StringUtil.replace(content, "]]>", StringPool.BLANK);
85          }
86  
87          content = StringUtil.replace(content, "&amp;", "&");
88          content = StringUtil.replace(content, "&lt;", "<");
89          content = StringUtil.replace(content, "&gt;", ">");
90  
91          content = HtmlUtil.extractText(content);
92  
93          Document doc = new DocumentImpl();
94  
95          doc.addUID(PORTLET_ID, articleId);
96  
97          doc.addModifiedDate(displayDate);
98  
99          doc.addKeyword(Field.COMPANY_ID, companyId);
100         doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
101         doc.addKeyword(Field.GROUP_ID, groupId);
102 
103         doc.addText(Field.TITLE, title);
104         doc.addText(Field.CONTENT, content);
105         doc.addText(Field.DESCRIPTION, description);
106         doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
107 
108         doc.addKeyword(Field.ENTRY_CLASS_PK, articleId);
109         doc.addKeyword(Field.VERSION, version);
110         doc.addKeyword(Field.TYPE, type);
111 
112         return doc;
113     }
114 
115     public static String getArticleUID(String articleId) {
116         Document doc = new DocumentImpl();
117 
118         doc.addUID(PORTLET_ID, articleId);
119 
120         return doc.get(Field.UID);
121     }
122 
123     public static void updateArticle(
124             long companyId, long groupId, String articleId, double version,
125             String title, String description, String content, String type,
126             Date displayDate, String[] tagsEntries)
127         throws SearchException {
128 
129         Document doc = getArticleDocument(
130             companyId, groupId, articleId, version, title, description, content,
131             type, displayDate, tagsEntries);
132 
133         SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
134     }
135 
136     public DocumentSummary getDocumentSummary(
137         com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
138 
139         // Title
140 
141         String title = doc.get(Field.TITLE);
142 
143         // Content
144 
145         String content = doc.get(Field.CONTENT);
146 
147         content = StringUtil.shorten(content, 200);
148 
149         // Portlet URL
150 
151         String groupId = doc.get("groupId");
152         String articleId = doc.get(Field.ENTRY_CLASS_PK);
153         String version = doc.get("version");
154 
155         portletURL.setParameter("struts_action", "/journal/edit_article");
156         portletURL.setParameter("groupId", groupId);
157         portletURL.setParameter("articleId", articleId);
158         portletURL.setParameter("version", version);
159 
160         return new DocumentSummary(title, content, portletURL);
161     }
162 
163     public void reIndex(String[] ids) throws SearchException {
164         try {
165             JournalArticleLocalServiceUtil.reIndex(ids);
166         }
167         catch (Exception e) {
168             throw new SearchException(e);
169         }
170     }
171 
172     private static String _getIndexableContent(String content) {
173         try {
174             StringBuilder sb = new StringBuilder();
175 
176             com.liferay.portal.kernel.xml.Document doc = SAXReaderUtil.read(
177                 content);
178 
179             Element root = doc.getRootElement();
180 
181             _getIndexableContent(sb, root);
182 
183             return sb.toString();
184         }
185         catch (Exception e) {
186             e.printStackTrace();
187 
188             return content;
189         }
190     }
191 
192     private static void _getIndexableContent(StringBuilder sb, Element root)
193         throws Exception {
194 
195         for (Element el : root.elements()) {
196             String elType = el.attributeValue("type", StringPool.BLANK);
197 
198             if (elType.equals("text") || elType.equals("text_box") ||
199                 elType.equals("text_area")) {
200 
201                 Element dynamicContent = el.element("dynamic-content");
202 
203                 String text = dynamicContent.getText();
204 
205                 sb.append(text);
206                 sb.append(StringPool.SPACE);
207             }
208             else if (el.getName().equals("static-content")) {
209                 String text = el.getText();
210 
211                 sb.append(text);
212                 sb.append(StringPool.SPACE);
213             }
214 
215             _getIndexableContent(sb, el);
216         }
217     }
218 
219 }