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