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