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.messageboards.util;
21  
22  import com.liferay.portal.kernel.dao.orm.QueryUtil;
23  import com.liferay.portal.kernel.log.Log;
24  import com.liferay.portal.kernel.log.LogFactoryUtil;
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.PortalUtil;
37  import com.liferay.portal.util.PortletKeys;
38  import com.liferay.portlet.messageboards.model.MBMessage;
39  import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
40  
41  import java.util.Date;
42  
43  import javax.portlet.PortletURL;
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, long userId, String userName,
59              long categoryId, long threadId, long messageId, String title,
60              String content, Date modifiedDate, String[] tagsEntries)
61          throws SearchException {
62  
63          Document doc = getMessageDocument(
64              companyId, groupId, userId, userName, categoryId, threadId,
65              messageId, title, content, modifiedDate, 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, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
87  
88          for (int i = 0; i < hits.getLength(); i++) {
89              Document doc = hits.doc(i);
90  
91              SearchEngineUtil.deleteDocument(companyId, doc.get(Field.UID));
92          }
93      }
94  
95      public static Document getMessageDocument(
96          long companyId, long groupId, long userId, String userName,
97          long categoryId, long threadId, long messageId, String title,
98          String content, Date modifiedDate, String[] tagsEntries) {
99  
100         userName = PortalUtil.getUserName(userId, userName);
101 
102         try {
103             content = BBCodeUtil.getHTML(content);
104         }
105         catch (Exception e) {
106             _log.error(
107                 "Could not parse message " + messageId + ": " + e.getMessage());
108         }
109 
110         content = HtmlUtil.extractText(content);
111 
112         Document doc = new DocumentImpl();
113 
114         doc.addUID(PORTLET_ID, messageId);
115 
116         doc.addModifiedDate(modifiedDate);
117 
118         doc.addKeyword(Field.COMPANY_ID, companyId);
119         doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
120         doc.addKeyword(Field.GROUP_ID, groupId);
121         doc.addKeyword(Field.USER_ID, userId);
122         doc.addText(Field.USER_NAME, userName);
123 
124         doc.addText(Field.TITLE, title);
125         doc.addText(Field.CONTENT, content);
126         doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
127 
128         doc.addKeyword("categoryId", categoryId);
129         doc.addKeyword("threadId", threadId);
130         doc.addKeyword(Field.ENTRY_CLASS_NAME, MBMessage.class.getName());
131         doc.addKeyword(Field.ENTRY_CLASS_PK, messageId);
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, long userId, String userName,
146             long categoryId, long threadId, long messageId, String title,
147             String content, Date modifiedDate, String[] tagsEntries)
148         throws SearchException {
149 
150         Document doc = getMessageDocument(
151             companyId, groupId, userId, userName, categoryId, threadId,
152             messageId, title, content, modifiedDate, 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 = LogFactoryUtil.getLog(Indexer.class);
191 
192 }