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.messageboards.util;
24  
25  import com.liferay.portal.kernel.search.BooleanQuery;
26  import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
27  import com.liferay.portal.kernel.search.Document;
28  import com.liferay.portal.kernel.search.DocumentImpl;
29  import com.liferay.portal.kernel.search.DocumentSummary;
30  import com.liferay.portal.kernel.search.Field;
31  import com.liferay.portal.kernel.search.Hits;
32  import com.liferay.portal.kernel.search.SearchEngineUtil;
33  import com.liferay.portal.kernel.search.SearchException;
34  import com.liferay.portal.kernel.util.HtmlUtil;
35  import com.liferay.portal.kernel.util.StringUtil;
36  import com.liferay.portal.util.PortletKeys;
37  import com.liferay.portlet.messageboards.model.MBMessage;
38  import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
39  
40  import javax.portlet.PortletURL;
41  
42  import org.apache.commons.logging.Log;
43  import org.apache.commons.logging.LogFactory;
44  
45  /**
46   * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   * @author Harry Mark
50   * @author Bruno Farache
51   *
52   */
53  public class Indexer implements com.liferay.portal.kernel.search.Indexer {
54  
55      public static final String PORTLET_ID = PortletKeys.MESSAGE_BOARDS;
56  
57      public static void addMessage(
58              long companyId, long groupId, String userName, long categoryId,
59              long threadId, long messageId, String title, String content,
60              String[] tagsEntries)
61          throws SearchException {
62  
63          Document doc = getMessageDocument(
64              companyId, groupId, userName, categoryId, threadId, messageId,
65              title, content, tagsEntries);
66  
67          SearchEngineUtil.addDocument(companyId, doc);
68      }
69  
70      public static void deleteMessage(long companyId, long messageId)
71          throws SearchException {
72  
73          SearchEngineUtil.deleteDocument(companyId, getMessageUID(messageId));
74      }
75  
76      public static void deleteMessages(long companyId, long threadId)
77          throws SearchException {
78  
79          BooleanQuery booleanQuery = BooleanQueryFactoryUtil.create();
80  
81          booleanQuery.addRequiredTerm(Field.PORTLET_ID, PORTLET_ID);
82  
83          booleanQuery.addRequiredTerm("threadId", threadId);
84  
85          Hits hits = SearchEngineUtil.search(
86              companyId, booleanQuery, SearchEngineUtil.ALL_POS,
87              SearchEngineUtil.ALL_POS);
88  
89          for (int i = 0; i < hits.getLength(); i++) {
90              Document doc = hits.doc(i);
91  
92              SearchEngineUtil.deleteDocument(companyId, doc.get(Field.UID));
93          }
94      }
95  
96      public static Document getMessageDocument(
97          long companyId, long groupId, String userName, long categoryId,
98          long threadId, long messageId, String title, String content,
99          String[] tagsEntries) {
100 
101         try {
102             content = BBCodeUtil.getHTML(content);
103         }
104         catch (Exception e) {
105             _log.error(
106                 "Could not parse message " + messageId + ": " + e.getMessage());
107         }
108 
109         content = HtmlUtil.extractText(content);
110 
111         Document doc = new DocumentImpl();
112 
113         doc.addUID(PORTLET_ID, messageId);
114 
115         doc.addKeyword(Field.COMPANY_ID, companyId);
116         doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
117         doc.addKeyword(Field.GROUP_ID, groupId);
118 
119         doc.addText(Field.USER_NAME, userName);
120         doc.addText(Field.TITLE, title);
121         doc.addText(Field.CONTENT, content);
122 
123         doc.addModifiedDate();
124 
125         doc.addKeyword("categoryId", categoryId);
126         doc.addKeyword("threadId", threadId);
127 
128         doc.addKeyword(Field.ENTRY_CLASS_NAME, MBMessage.class.getName());
129         doc.addKeyword(Field.ENTRY_CLASS_PK, messageId);
130 
131         doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
132 
133         return doc;
134     }
135 
136     public static String getMessageUID(long messageId) {
137         Document doc = new DocumentImpl();
138 
139         doc.addUID(PORTLET_ID, messageId);
140 
141         return doc.get(Field.UID);
142     }
143 
144     public static void updateMessage(
145             long companyId, long groupId, String userName, long categoryId,
146             long threadId, long messageId, String title, String content,
147             String[] tagsEntries)
148         throws SearchException {
149 
150         Document doc = getMessageDocument(
151             companyId, groupId, userName, categoryId, threadId, messageId,
152             title, content, tagsEntries);
153 
154         SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
155     }
156 
157     public DocumentSummary getDocumentSummary(
158         com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
159 
160         // Title
161 
162         String title = doc.get(Field.TITLE);
163 
164         // Content
165 
166         String content = doc.get(Field.CONTENT);
167 
168         content = StringUtil.shorten(content, 200);
169 
170         // Portlet URL
171 
172         String messageId = doc.get(Field.ENTRY_CLASS_PK);
173 
174         portletURL.setParameter(
175             "struts_action", "/message_boards/view_message");
176         portletURL.setParameter("messageId", messageId);
177 
178         return new DocumentSummary(title, content, portletURL);
179     }
180 
181     public void reIndex(String[] ids) throws SearchException {
182         try {
183             MBCategoryLocalServiceUtil.reIndex(ids);
184         }
185         catch (Exception e) {
186             throw new SearchException(e);
187         }
188     }
189 
190     private static Log _log = LogFactory.getLog(Indexer.class);
191 
192 }