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