001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.messageboards.util;
022:
023: import com.liferay.portal.kernel.search.DocumentSummary;
024: import com.liferay.portal.kernel.search.SearchException;
025: import com.liferay.portal.kernel.util.GetterUtil;
026: import com.liferay.portal.kernel.util.StringUtil;
027: import com.liferay.portal.lucene.LuceneFields;
028: import com.liferay.portal.lucene.LuceneUtil;
029: import com.liferay.portal.util.PortletKeys;
030: import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
031: import com.liferay.util.Html;
032:
033: import java.io.IOException;
034:
035: import javax.portlet.PortletURL;
036:
037: import org.apache.lucene.document.Document;
038: import org.apache.lucene.document.Field;
039: import org.apache.lucene.index.IndexReader;
040: import org.apache.lucene.index.IndexWriter;
041: import org.apache.lucene.index.Term;
042: import org.apache.lucene.queryParser.ParseException;
043: import org.apache.lucene.search.BooleanQuery;
044: import org.apache.lucene.search.Hits;
045: import org.apache.lucene.search.Searcher;
046:
047: /**
048: * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
049: *
050: * @author Brian Wing Shun Chan
051: * @author Harry Mark
052: *
053: */
054: public class Indexer implements
055: com.liferay.portal.kernel.search.Indexer {
056:
057: public static final String PORTLET_ID = PortletKeys.MESSAGE_BOARDS;
058:
059: public static void addMessage(long companyId, long groupId,
060: String userName, long categoryId, long threadId,
061: long messageId, String title, String content,
062: String[] tagsEntries) throws IOException {
063:
064: Document doc = getAddMessageDocument(companyId, groupId,
065: userName, categoryId, threadId, messageId, title,
066: content, tagsEntries);
067:
068: IndexWriter writer = null;
069:
070: try {
071: writer = LuceneUtil.getWriter(companyId);
072:
073: writer.addDocument(doc);
074: } finally {
075: if (writer != null) {
076: LuceneUtil.write(companyId);
077: }
078: }
079: }
080:
081: public static void deleteMessage(long companyId, long messageId)
082: throws IOException {
083:
084: LuceneUtil.deleteDocuments(companyId, new Term(
085: LuceneFields.UID, LuceneFields.getUID(PORTLET_ID,
086: messageId)));
087: }
088:
089: public static void deleteMessages(long companyId, long threadId)
090: throws IOException, ParseException {
091:
092: BooleanQuery booleanQuery = new BooleanQuery();
093:
094: LuceneUtil.addRequiredTerm(booleanQuery,
095: LuceneFields.PORTLET_ID, PORTLET_ID);
096:
097: LuceneUtil.addRequiredTerm(booleanQuery, "threadId", threadId);
098:
099: Searcher searcher = LuceneUtil.getSearcher(companyId);
100:
101: try {
102: Hits hits = searcher.search(booleanQuery);
103:
104: if (hits.length() > 0) {
105: IndexReader reader = null;
106:
107: try {
108: LuceneUtil.acquireLock(companyId);
109:
110: reader = LuceneUtil.getReader(companyId);
111:
112: for (int i = 0; i < hits.length(); i++) {
113: Document doc = hits.doc(i);
114:
115: Field field = doc.getField(LuceneFields.UID);
116:
117: reader.deleteDocuments(new Term(
118: LuceneFields.UID, field.stringValue()));
119: }
120: } finally {
121: if (reader != null) {
122: reader.close();
123: }
124:
125: LuceneUtil.releaseLock(companyId);
126: }
127: }
128: } finally {
129: LuceneUtil.closeSearcher(searcher);
130: }
131: }
132:
133: public static Document getAddMessageDocument(long companyId,
134: long groupId, String userName, long categoryId,
135: long threadId, long messageId, String title,
136: String content, String[] tagsEntries) {
137:
138: content = Html.stripHtml(content);
139:
140: Document doc = new Document();
141:
142: LuceneUtil.addKeyword(doc, LuceneFields.UID, LuceneFields
143: .getUID(PORTLET_ID, messageId));
144:
145: LuceneUtil.addKeyword(doc, LuceneFields.COMPANY_ID, companyId);
146: LuceneUtil.addKeyword(doc, LuceneFields.PORTLET_ID, PORTLET_ID);
147: LuceneUtil.addKeyword(doc, LuceneFields.GROUP_ID, groupId);
148:
149: LuceneUtil.addText(doc, LuceneFields.USER_NAME, userName);
150: LuceneUtil.addText(doc, LuceneFields.TITLE, title);
151: LuceneUtil.addText(doc, LuceneFields.CONTENT, content);
152:
153: LuceneUtil.addModifiedDate(doc);
154:
155: LuceneUtil.addKeyword(doc, "categoryId", categoryId);
156: LuceneUtil.addKeyword(doc, "threadId", threadId);
157: LuceneUtil.addKeyword(doc, "messageId", messageId);
158:
159: LuceneUtil.addKeyword(doc, LuceneFields.TAG_ENTRY, tagsEntries);
160:
161: return doc;
162: }
163:
164: public static void updateMessage(long companyId, long groupId,
165: String userName, long categoryId, long threadId,
166: long messageId, String title, String content,
167: String[] tagsEntries) throws IOException {
168:
169: try {
170: deleteMessage(companyId, messageId);
171: } catch (IOException ioe) {
172: }
173:
174: addMessage(companyId, groupId, userName, categoryId, threadId,
175: messageId, title, content, tagsEntries);
176: }
177:
178: public DocumentSummary getDocumentSummary(
179: com.liferay.portal.kernel.search.Document doc,
180: PortletURL portletURL) {
181:
182: // Title
183:
184: String title = doc.get(LuceneFields.TITLE);
185:
186: // Content
187:
188: String content = doc.get(LuceneFields.CONTENT);
189:
190: content = StringUtil.shorten(content, 200);
191:
192: // URL
193:
194: long messageId = GetterUtil.getLong(doc.get("messageId"));
195:
196: portletURL.setParameter("struts_action",
197: "/message_boards/view_message");
198: portletURL.setParameter("messageId", String.valueOf(messageId));
199:
200: return new DocumentSummary(title, content, portletURL);
201: }
202:
203: public void reIndex(String[] ids) throws SearchException {
204: try {
205: MBCategoryLocalServiceUtil.reIndex(ids);
206: } catch (Exception e) {
207: throw new SearchException(e);
208: }
209: }
210:
211: }
|