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.blogs.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.StringUtil;
026: import com.liferay.portal.lucene.LuceneFields;
027: import com.liferay.portal.lucene.LuceneUtil;
028: import com.liferay.portal.util.PortletKeys;
029: import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
030: import com.liferay.util.Html;
031:
032: import java.io.IOException;
033:
034: import javax.portlet.PortletURL;
035:
036: import org.apache.lucene.document.Document;
037: import org.apache.lucene.index.IndexWriter;
038: import org.apache.lucene.index.Term;
039:
040: /**
041: * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
042: *
043: * @author Brian Wing Shun Chan
044: * @author Harry Mark
045: *
046: */
047: public class Indexer implements
048: com.liferay.portal.kernel.search.Indexer {
049:
050: public static final String PORTLET_ID = PortletKeys.BLOGS;
051:
052: public static void addEntry(long companyId, long groupId,
053: long userId, long entryId, String title, String content,
054: String[] tagsEntries) throws IOException {
055:
056: Document doc = getAddEntryDocument(companyId, groupId, userId,
057: entryId, title, content, tagsEntries);
058:
059: IndexWriter writer = null;
060:
061: try {
062: writer = LuceneUtil.getWriter(companyId);
063:
064: writer.addDocument(doc);
065: } finally {
066: if (writer != null) {
067: LuceneUtil.write(companyId);
068: }
069: }
070: }
071:
072: public static void deleteEntry(long companyId, long entryId)
073: throws IOException {
074:
075: LuceneUtil.deleteDocuments(companyId, new Term(
076: LuceneFields.UID, LuceneFields.getUID(PORTLET_ID,
077: entryId)));
078: }
079:
080: public static Document getAddEntryDocument(long companyId,
081: long groupId, long userId, long entryId, String title,
082: String content, String[] tagsEntries) {
083:
084: content = Html.stripHtml(content);
085:
086: Document doc = new Document();
087:
088: LuceneUtil.addKeyword(doc, LuceneFields.UID, LuceneFields
089: .getUID(PORTLET_ID, entryId));
090:
091: LuceneUtil.addKeyword(doc, LuceneFields.COMPANY_ID, companyId);
092: LuceneUtil.addKeyword(doc, LuceneFields.PORTLET_ID, PORTLET_ID);
093: LuceneUtil.addKeyword(doc, LuceneFields.GROUP_ID, groupId);
094: LuceneUtil.addKeyword(doc, LuceneFields.USER_ID, userId);
095:
096: LuceneUtil.addText(doc, LuceneFields.TITLE, title);
097: LuceneUtil.addText(doc, LuceneFields.CONTENT, content);
098:
099: LuceneUtil.addModifiedDate(doc);
100:
101: LuceneUtil.addKeyword(doc, "entryId", entryId);
102:
103: LuceneUtil.addKeyword(doc, LuceneFields.TAG_ENTRY, tagsEntries);
104:
105: return doc;
106: }
107:
108: public static void updateEntry(long companyId, long groupId,
109: long userId, long entryId, String title, String content,
110: String[] tagsEntries) throws IOException {
111:
112: try {
113: deleteEntry(companyId, entryId);
114: } catch (IOException ioe) {
115: }
116:
117: addEntry(companyId, groupId, userId, entryId, title, content,
118: tagsEntries);
119: }
120:
121: public DocumentSummary getDocumentSummary(
122: com.liferay.portal.kernel.search.Document doc,
123: PortletURL portletURL) {
124:
125: // Title
126:
127: String title = doc.get(LuceneFields.TITLE);
128:
129: // Content
130:
131: String content = doc.get(LuceneFields.CONTENT);
132:
133: content = StringUtil.shorten(content, 200);
134:
135: // URL
136:
137: String entryId = doc.get("entryId");
138:
139: portletURL.setParameter("struts_action", "/blogs/view_entry");
140: portletURL.setParameter("entryId", entryId);
141:
142: return new DocumentSummary(title, content, portletURL);
143: }
144:
145: public void reIndex(String[] ids) throws SearchException {
146: try {
147: BlogsEntryLocalServiceUtil.reIndex(ids);
148: } catch (Exception e) {
149: throw new SearchException(e);
150: }
151: }
152:
153: }
|