1   /**
2    * Copyright (c) 2000-2007 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.search.DocumentSummary;
26  import com.liferay.portal.kernel.search.SearchException;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.LongWrapper;
29  import com.liferay.portal.kernel.util.MethodWrapper;
30  import com.liferay.portal.kernel.util.StringUtil;
31  import com.liferay.portal.lucene.LuceneFields;
32  import com.liferay.portlet.messageboards.service.jms.IndexProducer;
33  
34  import java.io.IOException;
35  
36  import javax.portlet.PortletURL;
37  
38  import org.apache.commons.logging.Log;
39  import org.apache.commons.logging.LogFactory;
40  import org.apache.lucene.queryParser.ParseException;
41  
42  /**
43   * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   *
47   */
48  public class Indexer implements com.liferay.portal.kernel.search.Indexer {
49  
50      public static void addMessage(
51              long companyId, long groupId, String userName, long categoryId,
52              long threadId, long messageId, String title, String content)
53          throws IOException {
54  
55          try {
56              MethodWrapper methodWrapper = new MethodWrapper(
57                  IndexerImpl.class.getName(), "addMessage",
58                  new Object[] {
59                      new LongWrapper(companyId), new LongWrapper(groupId),
60                      userName, new LongWrapper(categoryId),
61                      new LongWrapper(threadId), new LongWrapper(messageId),
62                      title, content
63                  });
64  
65              IndexProducer.produce(methodWrapper);
66          }
67          catch (Exception e) {
68              if (e instanceof IOException) {
69                  throw (IOException)e;
70              }
71              else {
72                  _log.error(e);
73              }
74          }
75      }
76  
77      public static void deleteMessage(long companyId, long messageId)
78          throws IOException {
79  
80          try {
81              MethodWrapper methodWrapper = new MethodWrapper(
82                  IndexerImpl.class.getName(), "deleteMessage",
83                  new Object[] {
84                      new LongWrapper(companyId), new LongWrapper(messageId)
85                  });
86  
87              IndexProducer.produce(methodWrapper);
88          }
89          catch (Exception e) {
90              if (e instanceof IOException) {
91                  throw (IOException)e;
92              }
93              else {
94                  _log.error(e);
95              }
96          }
97      }
98  
99      public static void deleteMessages(long companyId, long threadId)
100         throws IOException, ParseException {
101 
102         try {
103             MethodWrapper methodWrapper = new MethodWrapper(
104                 IndexerImpl.class.getName(), "deleteMessages",
105                 new Object[] {
106                     new LongWrapper(companyId), new LongWrapper(threadId)
107                 });
108 
109             IndexProducer.produce(methodWrapper);
110         }
111         catch (Exception e) {
112             if (e instanceof IOException) {
113                 throw (IOException)e;
114             }
115             else {
116                 _log.error(e);
117             }
118         }
119     }
120 
121     public static void updateMessage(
122             long companyId, long groupId, String userName, long categoryId,
123             long threadId, long messageId, String title, String content)
124         throws IOException {
125 
126         try {
127             MethodWrapper methodWrapper = new MethodWrapper(
128                 IndexerImpl.class.getName(), "updateMessage",
129                 new Object[] {
130                     new LongWrapper(companyId), new LongWrapper(groupId),
131                     userName, new LongWrapper(categoryId),
132                     new LongWrapper(threadId), new LongWrapper(messageId),
133                     title, content
134                 });
135 
136             IndexProducer.produce(methodWrapper);
137         }
138         catch (Exception e) {
139             if (e instanceof IOException) {
140                 throw (IOException)e;
141             }
142             else {
143                 _log.error(e);
144             }
145         }
146     }
147 
148     public DocumentSummary getDocumentSummary(
149         com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
150 
151         // Title
152 
153         String title = doc.get(LuceneFields.TITLE);
154 
155         // Content
156 
157         String content = doc.get(LuceneFields.CONTENT);
158 
159         content = StringUtil.shorten(content, 200);
160 
161         // URL
162 
163         long messageId = GetterUtil.getLong(doc.get("messageId"));
164 
165         portletURL.setParameter(
166             "struts_action", "/message_boards/view_message");
167         portletURL.setParameter("messageId", String.valueOf(messageId));
168 
169         return new DocumentSummary(title, content, portletURL);
170     }
171 
172     public void reIndex(String[] ids) throws SearchException {
173         IndexerImpl.reIndex(ids);
174     }
175 
176     private static Log _log = LogFactory.getLog(Indexer.class);
177 
178 }