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.SearchException;
26  import com.liferay.portal.lucene.LuceneFields;
27  import com.liferay.portal.lucene.LuceneUtil;
28  import com.liferay.portal.util.PortletKeys;
29  import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
30  import com.liferay.util.Html;
31  
32  import java.io.IOException;
33  
34  import org.apache.lucene.document.Document;
35  import org.apache.lucene.document.Field;
36  import org.apache.lucene.index.IndexReader;
37  import org.apache.lucene.index.IndexWriter;
38  import org.apache.lucene.index.Term;
39  import org.apache.lucene.queryParser.ParseException;
40  import org.apache.lucene.search.BooleanQuery;
41  import org.apache.lucene.search.Hits;
42  import org.apache.lucene.search.Searcher;
43  
44  /**
45   * <a href="IndexerImpl.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   * @author Harry Mark
49   *
50   */
51  public class IndexerImpl {
52  
53      public static final String PORTLET_ID = PortletKeys.MESSAGE_BOARDS;
54  
55      public static void addMessage(
56              long companyId, long groupId, String userName, long categoryId,
57              long threadId, long messageId, String title, String content)
58          throws IOException {
59  
60          Document doc = getAddMessageDocument(
61              companyId, groupId, userName, categoryId, threadId, messageId,
62              title, content);
63  
64          IndexWriter writer = null;
65  
66          try {
67              writer = LuceneUtil.getWriter(companyId);
68  
69              writer.addDocument(doc);
70          }
71          finally {
72              if (writer != null) {
73                  LuceneUtil.write(companyId);
74              }
75          }
76      }
77  
78      public static void deleteMessage(long companyId, long messageId)
79          throws IOException {
80  
81          LuceneUtil.deleteDocuments(
82              companyId,
83              new Term(
84                  LuceneFields.UID, LuceneFields.getUID(PORTLET_ID, messageId)));
85      }
86  
87      public static void deleteMessages(long companyId, long threadId)
88          throws IOException, ParseException {
89  
90          BooleanQuery booleanQuery = new BooleanQuery();
91  
92          LuceneUtil.addRequiredTerm(
93              booleanQuery, LuceneFields.PORTLET_ID, PORTLET_ID);
94  
95          LuceneUtil.addRequiredTerm(booleanQuery, "threadId", threadId);
96  
97          Searcher searcher = LuceneUtil.getSearcher(companyId);
98  
99          try {
100             Hits hits = searcher.search(booleanQuery);
101 
102             if (hits.length() > 0) {
103                 IndexReader reader = null;
104 
105                 try {
106                     LuceneUtil.acquireLock(companyId);
107 
108                     reader = LuceneUtil.getReader(companyId);
109 
110                     for (int i = 0; i < hits.length(); i++) {
111                         Document doc = hits.doc(i);
112 
113                         Field field = doc.getField(LuceneFields.UID);
114 
115                         reader.deleteDocuments(
116                             new Term(LuceneFields.UID, field.stringValue()));
117                     }
118                 }
119                 finally {
120                     if (reader != null) {
121                         reader.close();
122                     }
123 
124                     LuceneUtil.releaseLock(companyId);
125                 }
126             }
127         }
128         finally {
129             LuceneUtil.closeSearcher(searcher);
130         }
131     }
132 
133     public static Document getAddMessageDocument(
134         long companyId, long groupId, String userName, long categoryId,
135         long threadId, long messageId, String title, String content) {
136 
137         content = Html.stripHtml(content);
138 
139         Document doc = new Document();
140 
141         doc.add(
142             LuceneFields.getKeyword(
143                 LuceneFields.UID, LuceneFields.getUID(PORTLET_ID, messageId)));
144 
145         doc.add(LuceneFields.getKeyword(LuceneFields.COMPANY_ID, companyId));
146         doc.add(LuceneFields.getKeyword(LuceneFields.PORTLET_ID, PORTLET_ID));
147         doc.add(LuceneFields.getKeyword(LuceneFields.GROUP_ID, groupId));
148 
149         doc.add(LuceneFields.getText(LuceneFields.USER_NAME, userName));
150         doc.add(LuceneFields.getText(LuceneFields.TITLE, title));
151         doc.add(LuceneFields.getText(LuceneFields.CONTENT, content));
152 
153         doc.add(LuceneFields.getDate(LuceneFields.MODIFIED));
154 
155         doc.add(LuceneFields.getKeyword("categoryId", categoryId));
156         doc.add(LuceneFields.getKeyword("threadId", threadId));
157         doc.add(LuceneFields.getKeyword("messageId", messageId));
158 
159         return doc;
160     }
161 
162     public static void reIndex(String[] ids) throws SearchException {
163         try {
164             MBCategoryLocalServiceUtil.reIndex(ids);
165         }
166         catch (Exception e) {
167             throw new SearchException(e);
168         }
169     }
170 
171     public static void updateMessage(
172             long companyId, long groupId, String userName, long categoryId,
173             long threadId, long messageId, String title, String content)
174         throws IOException {
175 
176         try {
177             deleteMessage(companyId, messageId);
178         }
179         catch (IOException ioe) {
180         }
181 
182         addMessage(
183             companyId, groupId, userName, categoryId, threadId, messageId,
184             title, content);
185     }
186 
187 }