1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  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, Date modifiedDate, String[] tagsEntries)
63          throws SearchException {
64  
65          Document doc = getMessageDocument(
66              companyId, groupId, userId, userName, categoryId, threadId,
67              messageId, title, content, modifiedDate, 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, Date modifiedDate, 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(modifiedDate);
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, Date modifiedDate, String[] tagsEntries)
150         throws SearchException {
151 
152         Document doc = getMessageDocument(
153             companyId, groupId, userId, userName, categoryId, threadId,
154             messageId, title, content, modifiedDate, 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 = LogFactoryUtil.getLog(Indexer.class);
193 
194 }