1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portlet.messageboards.util;
16  
17  import com.liferay.portal.kernel.dao.orm.QueryUtil;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.search.BooleanQuery;
21  import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
22  import com.liferay.portal.kernel.search.Document;
23  import com.liferay.portal.kernel.search.DocumentImpl;
24  import com.liferay.portal.kernel.search.DocumentSummary;
25  import com.liferay.portal.kernel.search.Field;
26  import com.liferay.portal.kernel.search.Hits;
27  import com.liferay.portal.kernel.search.SearchEngineUtil;
28  import com.liferay.portal.kernel.search.SearchException;
29  import com.liferay.portal.kernel.util.HtmlUtil;
30  import com.liferay.portal.kernel.util.StringUtil;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.model.Group;
33  import com.liferay.portal.service.GroupLocalServiceUtil;
34  import com.liferay.portal.util.PortalUtil;
35  import com.liferay.portal.util.PortletKeys;
36  import com.liferay.portlet.expando.model.ExpandoBridge;
37  import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
38  import com.liferay.portlet.messageboards.model.MBMessage;
39  import com.liferay.portlet.messageboards.model.MBThread;
40  import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
41  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
42  import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
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   * @author Raymond Augé
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, boolean anonymous, Date modifiedDate,
64              String[] tagsEntries, ExpandoBridge expandoBridge)
65          throws SearchException {
66  
67          Document doc = getMessageDocument(
68              companyId, groupId, userId, userName, categoryId, threadId,
69              messageId, title, content, anonymous, modifiedDate, tagsEntries,
70              expandoBridge);
71  
72          SearchEngineUtil.addDocument(companyId, doc);
73      }
74  
75      public static void deleteMessage(long companyId, long messageId)
76          throws SearchException {
77  
78          SearchEngineUtil.deleteDocument(companyId, getMessageUID(messageId));
79      }
80  
81      public static void deleteMessages(long companyId, long threadId)
82          throws SearchException {
83  
84          BooleanQuery booleanQuery = BooleanQueryFactoryUtil.create();
85  
86          booleanQuery.addRequiredTerm(Field.PORTLET_ID, PORTLET_ID);
87  
88          booleanQuery.addRequiredTerm("threadId", threadId);
89  
90          Hits hits = SearchEngineUtil.search(
91              companyId, booleanQuery, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
92  
93          for (int i = 0; i < hits.getLength(); i++) {
94              Document doc = hits.doc(i);
95  
96              SearchEngineUtil.deleteDocument(companyId, doc.get(Field.UID));
97          }
98      }
99  
100     public static Document getMessageDocument(
101         long companyId, long groupId, long userId, String userName,
102         long categoryId, long threadId, long messageId, String title,
103         String content, boolean anonymous, Date modifiedDate,
104         String[] tagsEntries, ExpandoBridge expandoBridge) {
105 
106         long scopeGroupId = groupId;
107 
108         try {
109             Group group = GroupLocalServiceUtil.getGroup(groupId);
110 
111             if (group.isLayout()) {
112                 groupId = group.getParentGroupId();
113             }
114         }
115         catch (Exception e) {
116         }
117 
118         userName = PortalUtil.getUserName(userId, userName);
119 
120         try {
121             content = BBCodeUtil.getHTML(content);
122         }
123         catch (Exception e) {
124             _log.error(
125                 "Could not parse message " + messageId + ": " + e.getMessage());
126         }
127 
128         content = HtmlUtil.extractText(content);
129 
130         Document doc = new DocumentImpl();
131 
132         doc.addUID(PORTLET_ID, messageId);
133 
134         doc.addModifiedDate(modifiedDate);
135 
136         doc.addKeyword(Field.COMPANY_ID, companyId);
137         doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
138         doc.addKeyword(Field.GROUP_ID, groupId);
139         doc.addKeyword(Field.SCOPE_GROUP_ID, scopeGroupId);
140         doc.addKeyword(Field.USER_ID, userId);
141 
142         if (!anonymous) {
143             doc.addText(Field.USER_NAME, userName);
144         }
145 
146         doc.addText(Field.TITLE, title);
147         doc.addText(Field.CONTENT, content);
148         doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
149 
150         doc.addKeyword("categoryId", categoryId);
151         doc.addKeyword("threadId", threadId);
152         doc.addKeyword(Field.ENTRY_CLASS_NAME, MBMessage.class.getName());
153         doc.addKeyword(Field.ENTRY_CLASS_PK, messageId);
154 
155         try {
156             MBThread thread = MBThreadLocalServiceUtil.getMBThread(threadId);
157 
158             doc.addKeyword(
159                 Field.ROOT_ENTRY_CLASS_PK, thread.getRootMessageId());
160         }
161         catch (Exception e) {
162         }
163 
164         ExpandoBridgeIndexerUtil.addAttributes(doc, expandoBridge);
165 
166         return doc;
167     }
168 
169     public static String getMessageUID(long messageId) {
170         Document doc = new DocumentImpl();
171 
172         doc.addUID(PORTLET_ID, messageId);
173 
174         return doc.get(Field.UID);
175     }
176 
177     public static void updateMessage(
178             long companyId, long groupId, long userId, String userName,
179             long categoryId, long threadId, long messageId, String title,
180             String content, boolean anonymous, Date modifiedDate,
181             String[] tagsEntries, ExpandoBridge expandoBridge)
182         throws SearchException {
183 
184         Document doc = getMessageDocument(
185             companyId, groupId, userId, userName, categoryId, threadId,
186             messageId, title, content, anonymous, modifiedDate, tagsEntries,
187             expandoBridge);
188 
189         SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
190     }
191 
192     public String[] getClassNames() {
193         return _CLASS_NAMES;
194     }
195 
196     public DocumentSummary getDocumentSummary(
197         Document doc, String snippet, PortletURL portletURL) {
198 
199         // Title
200 
201         String title = doc.get(Field.TITLE);
202 
203         // Content
204 
205         String content = snippet;
206 
207         if (Validator.isNull(snippet)) {
208             content = StringUtil.shorten(doc.get(Field.CONTENT), 200);
209         }
210 
211         // Portlet URL
212 
213         String messageId = doc.get(Field.ENTRY_CLASS_PK);
214 
215         portletURL.setParameter(
216             "struts_action", "/message_boards/view_message");
217         portletURL.setParameter("messageId", messageId);
218 
219         return new DocumentSummary(title, content, portletURL);
220     }
221 
222     public void reIndex(String className, long classPK) throws SearchException {
223         try {
224             MBMessageLocalServiceUtil.reIndex(classPK);
225         }
226         catch (Exception e) {
227             throw new SearchException(e);
228         }
229     }
230 
231     public void reIndex(String[] ids) throws SearchException {
232         try {
233             MBCategoryLocalServiceUtil.reIndex(ids);
234         }
235         catch (Exception e) {
236             throw new SearchException(e);
237         }
238     }
239 
240     private static final String[] _CLASS_NAMES = new String[] {
241         MBMessage.class.getName()
242     };
243 
244     private static Log _log = LogFactoryUtil.getLog(Indexer.class);
245 
246 }