1
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.expando.model.ExpandoBridge;
42 import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
43 import com.liferay.portlet.messageboards.model.MBMessage;
44 import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
45 import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
46
47 import java.util.Date;
48
49 import javax.portlet.PortletURL;
50
51
60 public class Indexer implements com.liferay.portal.kernel.search.Indexer {
61
62 public static final String PORTLET_ID = PortletKeys.MESSAGE_BOARDS;
63
64 public static void addMessage(
65 long companyId, long groupId, long userId, String userName,
66 long categoryId, long threadId, long messageId, String title,
67 String content, boolean anonymous, Date modifiedDate,
68 String[] tagsEntries, ExpandoBridge expandoBridge)
69 throws SearchException {
70
71 Document doc = getMessageDocument(
72 companyId, groupId, userId, userName, categoryId, threadId,
73 messageId, title, content, anonymous, modifiedDate, tagsEntries,
74 expandoBridge);
75
76 SearchEngineUtil.addDocument(companyId, doc);
77 }
78
79 public static void deleteMessage(long companyId, long messageId)
80 throws SearchException {
81
82 SearchEngineUtil.deleteDocument(companyId, getMessageUID(messageId));
83 }
84
85 public static void deleteMessages(long companyId, long threadId)
86 throws SearchException {
87
88 BooleanQuery booleanQuery = BooleanQueryFactoryUtil.create();
89
90 booleanQuery.addRequiredTerm(Field.PORTLET_ID, PORTLET_ID);
91
92 booleanQuery.addRequiredTerm("threadId", threadId);
93
94 Hits hits = SearchEngineUtil.search(
95 companyId, booleanQuery, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
96
97 for (int i = 0; i < hits.getLength(); i++) {
98 Document doc = hits.doc(i);
99
100 SearchEngineUtil.deleteDocument(companyId, doc.get(Field.UID));
101 }
102 }
103
104 public static Document getMessageDocument(
105 long companyId, long groupId, long userId, String userName,
106 long categoryId, long threadId, long messageId, String title,
107 String content, boolean anonymous, Date modifiedDate,
108 String[] tagsEntries, ExpandoBridge expandoBridge) {
109
110 userName = PortalUtil.getUserName(userId, userName);
111
112 try {
113 content = BBCodeUtil.getHTML(content);
114 }
115 catch (Exception e) {
116 _log.error(
117 "Could not parse message " + messageId + ": " + e.getMessage());
118 }
119
120 content = HtmlUtil.extractText(content);
121
122 Document doc = new DocumentImpl();
123
124 doc.addUID(PORTLET_ID, messageId);
125
126 doc.addModifiedDate(modifiedDate);
127
128 doc.addKeyword(Field.COMPANY_ID, companyId);
129 doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
130 doc.addKeyword(Field.GROUP_ID, groupId);
131 doc.addKeyword(Field.USER_ID, userId);
132
133 if (!anonymous) {
134 doc.addText(Field.USER_NAME, userName);
135 }
136
137 doc.addText(Field.TITLE, title);
138 doc.addText(Field.CONTENT, content);
139 doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
140
141 doc.addKeyword("categoryId", categoryId);
142 doc.addKeyword("threadId", threadId);
143 doc.addKeyword(Field.ENTRY_CLASS_NAME, MBMessage.class.getName());
144 doc.addKeyword(Field.ENTRY_CLASS_PK, messageId);
145
146 ExpandoBridgeIndexerUtil.addAttributes(doc, expandoBridge);
147
148 return doc;
149 }
150
151 public static String getMessageUID(long messageId) {
152 Document doc = new DocumentImpl();
153
154 doc.addUID(PORTLET_ID, messageId);
155
156 return doc.get(Field.UID);
157 }
158
159 public static void updateMessage(
160 long companyId, long groupId, long userId, String userName,
161 long categoryId, long threadId, long messageId, String title,
162 String content, boolean anonymous, Date modifiedDate,
163 String[] tagsEntries, ExpandoBridge expandoBridge)
164 throws SearchException {
165
166 Document doc = getMessageDocument(
167 companyId, groupId, userId, userName, categoryId, threadId,
168 messageId, title, content, anonymous, modifiedDate, tagsEntries,
169 expandoBridge);
170
171 SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
172 }
173
174 public String[] getClassNames() {
175 return _CLASS_NAMES;
176 }
177
178 public DocumentSummary getDocumentSummary(
179 com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
180
181
183 String title = doc.get(Field.TITLE);
184
185
187 String content = doc.get(Field.CONTENT);
188
189 content = StringUtil.shorten(content, 200);
190
191
193 String messageId = doc.get(Field.ENTRY_CLASS_PK);
194
195 portletURL.setParameter(
196 "struts_action", "/message_boards/view_message");
197 portletURL.setParameter("messageId", messageId);
198
199 return new DocumentSummary(title, content, portletURL);
200 }
201
202 public void reIndex(String className, long classPK) throws SearchException {
203 try {
204 MBMessageLocalServiceUtil.reIndex(classPK);
205 }
206 catch (Exception e) {
207 throw new SearchException(e);
208 }
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 private static final String[] _CLASS_NAMES = new String[] {
221 MBMessage.class.getName()
222 };
223
224 private static Log _log = LogFactoryUtil.getLog(Indexer.class);
225
226 }