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.PortletKeys;
38  import com.liferay.portlet.messageboards.model.MBMessage;
39  import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
40  
41  import javax.portlet.PortletURL;
42  
43  import org.apache.commons.logging.Log;
44  import org.apache.commons.logging.LogFactory;
45  
46  /**
47   * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Brian Wing Shun Chan
50   * @author Harry Mark
51   * @author Bruno Farache
52   *
53   */
54  public class Indexer implements com.liferay.portal.kernel.search.Indexer {
55  
56      public static final String PORTLET_ID = PortletKeys.MESSAGE_BOARDS;
57  
58      public static void addMessage(
59              long companyId, long groupId, String userName, long categoryId,
60              long threadId, long messageId, String title, String content,
61              String[] tagsEntries)
62          throws SearchException {
63  
64          Document doc = getMessageDocument(
65              companyId, groupId, userName, categoryId, threadId, messageId,
66              title, content, tagsEntries);
67  
68          SearchEngineUtil.addDocument(companyId, doc);
69      }
70  
71      public static void deleteMessage(long companyId, long messageId)
72          throws SearchException {
73  
74          SearchEngineUtil.deleteDocument(companyId, getMessageUID(messageId));
75      }
76  
77      public static void deleteMessages(long companyId, long threadId)
78          throws SearchException {
79  
80          BooleanQuery booleanQuery = BooleanQueryFactoryUtil.create();
81  
82          booleanQuery.addRequiredTerm(Field.PORTLET_ID, PORTLET_ID);
83  
84          booleanQuery.addRequiredTerm("threadId", threadId);
85  
86          Hits hits = SearchEngineUtil.search(
87              companyId, booleanQuery, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
88  
89          for (int i = 0; i < hits.getLength(); i++) {
90              Document doc = hits.doc(i);
91  
92              SearchEngineUtil.deleteDocument(companyId, doc.get(Field.UID));
93          }
94      }
95  
96      public static Document getMessageDocument(
97          long companyId, long groupId, String userName, long categoryId,
98          long threadId, long messageId, String title, String content,
99          String[] tagsEntries) {
100 
101         try {
102             content = BBCodeUtil.getHTML(content);
103         }
104         catch (Exception e) {
105             _log.error(
106                 "Could not parse message " + messageId + ": " + e.getMessage());
107         }
108 
109         content = HtmlUtil.extractText(content);
110 
111         Document doc = new DocumentImpl();
112 
113         doc.addUID(PORTLET_ID, messageId);
114 
115         doc.addKeyword(Field.COMPANY_ID, companyId);
116         doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
117         doc.addKeyword(Field.GROUP_ID, groupId);
118 
119         doc.addText(Field.USER_NAME, userName);
120         doc.addText(Field.TITLE, title);
121         doc.addText(Field.CONTENT, content);
122 
123         doc.addModifiedDate();
124 
125         doc.addKeyword("categoryId", categoryId);
126         doc.addKeyword("threadId", threadId);
127 
128         doc.addKeyword(Field.ENTRY_CLASS_NAME, MBMessage.class.getName());
129         doc.addKeyword(Field.ENTRY_CLASS_PK, messageId);
130 
131         doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
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, String userName, long categoryId,
146             long threadId, long messageId, String title, String content,
147             String[] tagsEntries)
148         throws SearchException {
149 
150         Document doc = getMessageDocument(
151             companyId, groupId, userName, categoryId, threadId, messageId,
152             title, content, 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 = LogFactory.getLog(Indexer.class);
191 
192 }