1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class Indexer implements com.liferay.portal.kernel.search.Indexer {
51  
52      public static final String PORTLET_ID = PortletKeys.JOURNAL;
53  
54      public static void addArticle(
55              long companyId, long groupId, String articleId, double version,
56              String title, String description, String content, String type,
57              Date displayDate, String[] tagsEntries)
58          throws SearchException {
59  
60          Document doc = getArticleDocument(
61              companyId, groupId, articleId, version, title, description, content,
62              type, displayDate, tagsEntries);
63  
64          SearchEngineUtil.addDocument(companyId, doc);
65      }
66  
67      public static void deleteArticle(
68              long companyId, long groupId, String articleId)
69          throws SearchException {
70  
71          SearchEngineUtil.deleteDocument(
72              companyId, getArticleUID(groupId, articleId));
73      }
74  
75      public static Document getArticleDocument(
76          long companyId, long groupId, String articleId, double version,
77          String title, String description, String content, String type,
78          Date displayDate, String[] tagsEntries) {
79  
80          if ((content != null) &&
81              ((content.indexOf("<dynamic-content") != -1) ||
82               (content.indexOf("<static-content") != -1))) {
83  
84              content = _getIndexableContent(content);
85  
86              content = StringUtil.replace(
87                  content, "<![CDATA[", StringPool.BLANK);
88              content = StringUtil.replace(content, "]]>", StringPool.BLANK);
89          }
90  
91          content = StringUtil.replace(content, "&amp;", "&");
92          content = StringUtil.replace(content, "&lt;", "<");
93          content = StringUtil.replace(content, "&gt;", ">");
94  
95          content = HtmlUtil.extractText(content);
96  
97          Document doc = new DocumentImpl();
98  
99          doc.addUID(PORTLET_ID, groupId, articleId);
100 
101         doc.addModifiedDate(displayDate);
102 
103         doc.addKeyword(Field.COMPANY_ID, companyId);
104         doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
105         doc.addKeyword(Field.GROUP_ID, groupId);
106 
107         doc.addText(Field.TITLE, title);
108         doc.addText(Field.CONTENT, content);
109         doc.addText(Field.DESCRIPTION, description);
110         doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
111 
112         doc.addKeyword(Field.ENTRY_CLASS_PK, articleId);
113         doc.addKeyword(Field.VERSION, version);
114         doc.addKeyword(Field.TYPE, type);
115 
116         return doc;
117     }
118 
119     public static String getArticleUID(long groupId, String articleId) {
120         Document doc = new DocumentImpl();
121 
122         doc.addUID(PORTLET_ID, groupId, 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                 for (Element dynamicContent : el.elements("dynamic-content")) {
206                     String text = dynamicContent.getText();
207 
208                     sb.append(text);
209                     sb.append(StringPool.SPACE);
210                 }
211             }
212             else if (el.getName().equals("static-content")) {
213                 String text = el.getText();
214 
215                 sb.append(text);
216                 sb.append(StringPool.SPACE);
217             }
218 
219             _getIndexableContent(sb, el);
220         }
221     }
222 
223 }