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