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.bookmarks.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.bookmarks.service.BookmarksFolderLocalServiceUtil;
030:
031: import java.io.IOException;
032:
033: import javax.portlet.PortletURL;
034:
035: import org.apache.lucene.document.Document;
036: import org.apache.lucene.index.IndexWriter;
037: import org.apache.lucene.index.Term;
038:
039: /**
040: * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
041: *
042: * @author Brian Wing Shun Chan
043: *
044: */
045: public class Indexer implements
046: com.liferay.portal.kernel.search.Indexer {
047:
048: public static final String PORTLET_ID = PortletKeys.BOOKMARKS;
049:
050: public static void addEntry(long companyId, long groupId,
051: long folderId, long entryId, String title, String content,
052: String description, String[] tagsEntries)
053: throws IOException {
054:
055: Document doc = getAddEntryDocument(companyId, groupId,
056: folderId, entryId, title, content, description,
057: 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 folderId, long entryId, String title,
082: String content, String description, String[] tagsEntries) {
083:
084: Document doc = new Document();
085:
086: LuceneUtil.addKeyword(doc, LuceneFields.UID, LuceneFields
087: .getUID(PORTLET_ID, entryId));
088:
089: LuceneUtil.addKeyword(doc, LuceneFields.COMPANY_ID, companyId);
090: LuceneUtil.addKeyword(doc, LuceneFields.PORTLET_ID, PORTLET_ID);
091: LuceneUtil.addKeyword(doc, LuceneFields.GROUP_ID, groupId);
092:
093: LuceneUtil.addText(doc, LuceneFields.TITLE, title);
094: LuceneUtil.addText(doc, LuceneFields.CONTENT, content);
095: LuceneUtil.addText(doc, LuceneFields.DESCRIPTION, description);
096:
097: LuceneUtil.addModifiedDate(doc);
098:
099: LuceneUtil.addKeyword(doc, "folderId", folderId);
100: LuceneUtil.addKeyword(doc, "entryId", entryId);
101:
102: LuceneUtil.addKeyword(doc, LuceneFields.TAG_ENTRY, tagsEntries);
103:
104: return doc;
105: }
106:
107: public static void updateEntry(long companyId, long groupId,
108: long folderId, long entryId, String title, String content,
109: String description, String[] tagsEntries)
110: throws IOException {
111:
112: try {
113: deleteEntry(companyId, entryId);
114: } catch (IOException ioe) {
115: }
116:
117: addEntry(companyId, groupId, folderId, entryId, title, content,
118: description, 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",
140: "/bookmarks/edit_entry");
141: portletURL.setParameter("entryId", entryId);
142:
143: return new DocumentSummary(title, content, portletURL);
144: }
145:
146: public void reIndex(String[] ids) throws SearchException {
147: try {
148: BookmarksFolderLocalServiceUtil.reIndex(ids);
149: } catch (Exception e) {
150: throw new SearchException(e);
151: }
152: }
153:
154: }
|