1
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.StringUtil;
29 import com.liferay.portal.lucene.LuceneFields;
30 import com.liferay.portal.lucene.LuceneUtil;
31 import com.liferay.portal.util.PortletKeys;
32 import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
33 import com.liferay.util.Html;
34
35 import java.io.IOException;
36
37 import javax.portlet.PortletURL;
38
39 import org.apache.lucene.document.Document;
40 import org.apache.lucene.document.Field;
41 import org.apache.lucene.index.IndexReader;
42 import org.apache.lucene.index.IndexWriter;
43 import org.apache.lucene.index.Term;
44 import org.apache.lucene.queryParser.ParseException;
45 import org.apache.lucene.search.BooleanQuery;
46 import org.apache.lucene.search.Hits;
47 import org.apache.lucene.search.Searcher;
48
49
56 public class Indexer implements com.liferay.portal.kernel.search.Indexer {
57
58 public static final String PORTLET_ID = PortletKeys.MESSAGE_BOARDS;
59
60 public static void addMessage(
61 long companyId, long groupId, String userName, long categoryId,
62 long threadId, long messageId, String title, String content,
63 String[] tagsEntries)
64 throws IOException {
65
66 Document doc = getAddMessageDocument(
67 companyId, groupId, userName, categoryId, threadId, messageId,
68 title, content, tagsEntries);
69
70 IndexWriter writer = null;
71
72 try {
73 writer = LuceneUtil.getWriter(companyId);
74
75 writer.addDocument(doc);
76 }
77 finally {
78 if (writer != null) {
79 LuceneUtil.write(companyId);
80 }
81 }
82 }
83
84 public static void deleteMessage(long companyId, long messageId)
85 throws IOException {
86
87 LuceneUtil.deleteDocuments(
88 companyId,
89 new Term(
90 LuceneFields.UID, LuceneFields.getUID(PORTLET_ID, messageId)));
91 }
92
93 public static void deleteMessages(long companyId, long threadId)
94 throws IOException, ParseException {
95
96 BooleanQuery booleanQuery = new BooleanQuery();
97
98 LuceneUtil.addRequiredTerm(
99 booleanQuery, LuceneFields.PORTLET_ID, PORTLET_ID);
100
101 LuceneUtil.addRequiredTerm(booleanQuery, "threadId", threadId);
102
103 Searcher searcher = LuceneUtil.getSearcher(companyId);
104
105 try {
106 Hits hits = searcher.search(booleanQuery);
107
108 if (hits.length() > 0) {
109 IndexReader reader = null;
110
111 try {
112 LuceneUtil.acquireLock(companyId);
113
114 reader = LuceneUtil.getReader(companyId);
115
116 for (int i = 0; i < hits.length(); i++) {
117 Document doc = hits.doc(i);
118
119 Field field = doc.getField(LuceneFields.UID);
120
121 reader.deleteDocuments(
122 new Term(LuceneFields.UID, field.stringValue()));
123 }
124 }
125 finally {
126 if (reader != null) {
127 reader.close();
128 }
129
130 LuceneUtil.releaseLock(companyId);
131 }
132 }
133 }
134 finally {
135 LuceneUtil.closeSearcher(searcher);
136 }
137 }
138
139 public static Document getAddMessageDocument(
140 long companyId, long groupId, String userName, long categoryId,
141 long threadId, long messageId, String title, String content,
142 String[] tagsEntries) {
143
144 content = Html.stripHtml(content);
145
146 Document doc = new Document();
147
148 LuceneUtil.addKeyword(
149 doc, LuceneFields.UID, LuceneFields.getUID(PORTLET_ID, messageId));
150
151 LuceneUtil.addKeyword(doc, LuceneFields.COMPANY_ID, companyId);
152 LuceneUtil.addKeyword(doc, LuceneFields.PORTLET_ID, PORTLET_ID);
153 LuceneUtil.addKeyword(doc, LuceneFields.GROUP_ID, groupId);
154
155 LuceneUtil.addText(doc, LuceneFields.USER_NAME, userName);
156 LuceneUtil.addText(doc, LuceneFields.TITLE, title);
157 LuceneUtil.addText(doc, LuceneFields.CONTENT, content);
158
159 LuceneUtil.addModifiedDate(doc);
160
161 LuceneUtil.addKeyword(doc, "categoryId", categoryId);
162 LuceneUtil.addKeyword(doc, "threadId", threadId);
163 LuceneUtil.addKeyword(doc, "messageId", messageId);
164
165 LuceneUtil.addKeyword(doc, LuceneFields.TAG_ENTRY, tagsEntries);
166
167 return doc;
168 }
169
170 public static void updateMessage(
171 long companyId, long groupId, String userName, long categoryId,
172 long threadId, long messageId, String title, String content,
173 String[] tagsEntries)
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, tagsEntries);
185 }
186
187 public DocumentSummary getDocumentSummary(
188 com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
189
190
192 String title = doc.get(LuceneFields.TITLE);
193
194
196 String content = doc.get(LuceneFields.CONTENT);
197
198 content = StringUtil.shorten(content, 200);
199
200
202 long messageId = GetterUtil.getLong(doc.get("messageId"));
203
204 portletURL.setParameter(
205 "struts_action", "/message_boards/view_message");
206 portletURL.setParameter("messageId", String.valueOf(messageId));
207
208 return new DocumentSummary(title, content, portletURL);
209 }
210
211 public void reIndex(String[] ids) throws SearchException {
212 try {
213 MBCategoryLocalServiceUtil.reIndex(ids);
214 }
215 catch (Exception e) {
216 throw new SearchException(e);
217 }
218 }
219
220 }