1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.imagegallery.util;
24  
25  import com.liferay.portal.kernel.search.DocumentSummary;
26  import com.liferay.portal.kernel.search.SearchException;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.lucene.LuceneFields;
29  import com.liferay.portal.lucene.LuceneUtil;
30  import com.liferay.portal.util.PortletKeys;
31  import com.liferay.portlet.imagegallery.service.IGFolderLocalServiceUtil;
32  
33  import java.io.IOException;
34  
35  import javax.portlet.PortletURL;
36  
37  import org.apache.lucene.document.Document;
38  import org.apache.lucene.index.IndexWriter;
39  import org.apache.lucene.index.Term;
40  
41  /**
42   * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   *
46   */
47  public class Indexer implements com.liferay.portal.kernel.search.Indexer {
48  
49      public static final String PORTLET_ID = PortletKeys.IMAGE_GALLERY;
50  
51      public static void addImage(
52              long companyId, long groupId, long folderId, long imageId,
53              String description, String[] tagsEntries)
54          throws IOException {
55  
56          Document doc = getAddImageDocument(
57              companyId, groupId, folderId, imageId, description, tagsEntries);
58  
59          IndexWriter writer = null;
60  
61          try {
62              writer = LuceneUtil.getWriter(companyId);
63  
64              writer.addDocument(doc);
65          }
66          finally {
67              if (writer != null) {
68                  LuceneUtil.write(companyId);
69              }
70          }
71      }
72  
73      public static void deleteImage(long companyId, long imageId)
74          throws IOException {
75  
76          LuceneUtil.deleteDocuments(
77              companyId,
78              new Term(
79                  LuceneFields.UID, LuceneFields.getUID(PORTLET_ID, imageId)));
80      }
81  
82      public static Document getAddImageDocument(
83          long companyId, long groupId, long folderId, long imageId,
84          String description, String[] tagsEntries) {
85  
86          Document doc = new Document();
87  
88          LuceneUtil.addKeyword(
89              doc, LuceneFields.UID, LuceneFields.getUID(PORTLET_ID, imageId));
90  
91          LuceneUtil.addKeyword(doc, LuceneFields.COMPANY_ID, companyId);
92          LuceneUtil.addKeyword(doc, LuceneFields.PORTLET_ID, PORTLET_ID);
93          LuceneUtil.addKeyword(doc, LuceneFields.GROUP_ID, groupId);
94  
95          LuceneUtil.addText(doc, LuceneFields.DESCRIPTION, description);
96  
97          LuceneUtil.addModifiedDate(doc);
98  
99          LuceneUtil.addKeyword(doc, "folderId", folderId);
100         LuceneUtil.addKeyword(doc, "imageId", imageId);
101 
102         LuceneUtil.addKeyword(doc, LuceneFields.TAG_ENTRY, tagsEntries);
103 
104         return doc;
105     }
106 
107     public static void updateImage(
108             long companyId, long groupId, long folderId, long imageId,
109             String description, String[] tagsEntries)
110         throws IOException {
111 
112         try {
113             deleteImage(companyId, imageId);
114         }
115         catch (IOException ioe) {
116         }
117 
118         addImage(
119             companyId, groupId, folderId, imageId, description, tagsEntries);
120     }
121 
122     public DocumentSummary getDocumentSummary(
123         com.liferay.portal.kernel.search.Document doc, 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 imageId = doc.get("imageId");
138 
139         portletURL.setParameter("struts_action", "/image_gallery/edit_image");
140         portletURL.setParameter("imageId", imageId);
141 
142         return new DocumentSummary(title, content, portletURL);
143     }
144 
145     public void reIndex(String[] ids) throws SearchException {
146         try {
147             IGFolderLocalServiceUtil.reIndex(ids);
148         }
149         catch (Exception e) {
150             throw new SearchException(e);
151         }
152     }
153 
154 }