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.imagegallery.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.imagegallery.service.IGFolderLocalServiceUtil;
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.IMAGE_GALLERY;
049:
050: public static void addImage(long companyId, long groupId,
051: long folderId, long imageId, String description,
052: String[] tagsEntries) throws IOException {
053:
054: Document doc = getAddImageDocument(companyId, groupId,
055: folderId, imageId, description, tagsEntries);
056:
057: IndexWriter writer = null;
058:
059: try {
060: writer = LuceneUtil.getWriter(companyId);
061:
062: writer.addDocument(doc);
063: } finally {
064: if (writer != null) {
065: LuceneUtil.write(companyId);
066: }
067: }
068: }
069:
070: public static void deleteImage(long companyId, long imageId)
071: throws IOException {
072:
073: LuceneUtil.deleteDocuments(companyId, new Term(
074: LuceneFields.UID, LuceneFields.getUID(PORTLET_ID,
075: imageId)));
076: }
077:
078: public static Document getAddImageDocument(long companyId,
079: long groupId, long folderId, long imageId,
080: String description, String[] tagsEntries) {
081:
082: Document doc = new Document();
083:
084: LuceneUtil.addKeyword(doc, LuceneFields.UID, LuceneFields
085: .getUID(PORTLET_ID, imageId));
086:
087: LuceneUtil.addKeyword(doc, LuceneFields.COMPANY_ID, companyId);
088: LuceneUtil.addKeyword(doc, LuceneFields.PORTLET_ID, PORTLET_ID);
089: LuceneUtil.addKeyword(doc, LuceneFields.GROUP_ID, groupId);
090:
091: LuceneUtil.addText(doc, LuceneFields.DESCRIPTION, description);
092:
093: LuceneUtil.addModifiedDate(doc);
094:
095: LuceneUtil.addKeyword(doc, "folderId", folderId);
096: LuceneUtil.addKeyword(doc, "imageId", imageId);
097:
098: LuceneUtil.addKeyword(doc, LuceneFields.TAG_ENTRY, tagsEntries);
099:
100: return doc;
101: }
102:
103: public static void updateImage(long companyId, long groupId,
104: long folderId, long imageId, String description,
105: String[] tagsEntries) throws IOException {
106:
107: try {
108: deleteImage(companyId, imageId);
109: } catch (IOException ioe) {
110: }
111:
112: addImage(companyId, groupId, folderId, imageId, description,
113: tagsEntries);
114: }
115:
116: public DocumentSummary getDocumentSummary(
117: com.liferay.portal.kernel.search.Document doc,
118: PortletURL portletURL) {
119:
120: // Title
121:
122: String title = doc.get(LuceneFields.TITLE);
123:
124: // Content
125:
126: String content = doc.get(LuceneFields.CONTENT);
127:
128: content = StringUtil.shorten(content, 200);
129:
130: // URL
131:
132: String imageId = doc.get("imageId");
133:
134: portletURL.setParameter("struts_action",
135: "/image_gallery/edit_image");
136: portletURL.setParameter("imageId", imageId);
137:
138: return new DocumentSummary(title, content, portletURL);
139: }
140:
141: public void reIndex(String[] ids) throws SearchException {
142: try {
143: IGFolderLocalServiceUtil.reIndex(ids);
144: } catch (Exception e) {
145: throw new SearchException(e);
146: }
147: }
148:
149: }
|