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.HtmlUtil;
29 import com.liferay.portal.kernel.util.StringUtil;
30 import com.liferay.portal.lucene.LuceneFields;
31 import com.liferay.portal.lucene.LuceneUtil;
32 import com.liferay.portal.util.PortletKeys;
33 import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
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 = BBCodeUtil.getHTML(content);
145 content = HtmlUtil.extractText(content);
146
147 Document doc = new Document();
148
149 LuceneUtil.addKeyword(
150 doc, LuceneFields.UID, LuceneFields.getUID(PORTLET_ID, messageId));
151
152 LuceneUtil.addKeyword(doc, LuceneFields.COMPANY_ID, companyId);
153 LuceneUtil.addKeyword(doc, LuceneFields.PORTLET_ID, PORTLET_ID);
154 LuceneUtil.addKeyword(doc, LuceneFields.GROUP_ID, groupId);
155
156 LuceneUtil.addText(doc, LuceneFields.USER_NAME, userName);
157 LuceneUtil.addText(doc, LuceneFields.TITLE, title);
158 LuceneUtil.addText(doc, LuceneFields.CONTENT, content);
159
160 LuceneUtil.addModifiedDate(doc);
161
162 LuceneUtil.addKeyword(doc, "categoryId", categoryId);
163 LuceneUtil.addKeyword(doc, "threadId", threadId);
164 LuceneUtil.addKeyword(doc, "messageId", messageId);
165
166 LuceneUtil.addKeyword(doc, LuceneFields.TAG_ENTRY, tagsEntries);
167
168 return doc;
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 String[] tagsEntries)
175 throws IOException {
176
177 try {
178 deleteMessage(companyId, messageId);
179 }
180 catch (IOException ioe) {
181 }
182
183 addMessage(
184 companyId, groupId, userName, categoryId, threadId, messageId,
185 title, content, tagsEntries);
186 }
187
188 public DocumentSummary getDocumentSummary(
189 com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
190
191
193 String title = doc.get(LuceneFields.TITLE);
194
195
197 String content = doc.get(LuceneFields.CONTENT);
198
199 content = StringUtil.shorten(content, 200);
200
201
203 long messageId = GetterUtil.getLong(doc.get("messageId"));
204
205 portletURL.setParameter(
206 "struts_action", "/message_boards/view_message");
207 portletURL.setParameter("messageId", String.valueOf(messageId));
208
209 return new DocumentSummary(title, content, portletURL);
210 }
211
212 public void reIndex(String[] ids) throws SearchException {
213 try {
214 MBCategoryLocalServiceUtil.reIndex(ids);
215 }
216 catch (Exception e) {
217 throw new SearchException(e);
218 }
219 }
220
221 }