1   /**
2    * Copyright (c) 2000-2009 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.Document;
26  import com.liferay.portal.kernel.search.DocumentImpl;
27  import com.liferay.portal.kernel.search.DocumentSummary;
28  import com.liferay.portal.kernel.search.Field;
29  import com.liferay.portal.kernel.search.SearchEngineUtil;
30  import com.liferay.portal.kernel.search.SearchException;
31  import com.liferay.portal.kernel.util.StringUtil;
32  import com.liferay.portal.util.PortletKeys;
33  import com.liferay.portlet.imagegallery.service.IGFolderLocalServiceUtil;
34  
35  import java.util.Date;
36  
37  import javax.portlet.PortletURL;
38  
39  /**
40   * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   * @author Bruno Farache
44   *
45   */
46  public class Indexer implements com.liferay.portal.kernel.search.Indexer {
47  
48      public static final String PORTLET_ID = PortletKeys.IMAGE_GALLERY;
49  
50      public static void addImage(
51              long companyId, long groupId, long folderId, long imageId,
52              String name, String description,  Date modifiedDate,
53              String[] tagsEntries)
54          throws SearchException {
55  
56          Document doc = getImageDocument(
57              companyId, groupId, folderId, imageId, name, description,
58              modifiedDate, tagsEntries);
59  
60          SearchEngineUtil.addDocument(companyId, doc);
61      }
62  
63      public static void deleteImage(long companyId, long imageId)
64          throws SearchException {
65  
66          SearchEngineUtil.deleteDocument(companyId, getImageUID(imageId));
67      }
68  
69      public static Document getImageDocument(
70          long companyId, long groupId, long folderId, long imageId,
71          String name, String description, Date modifiedDate,
72          String[] tagsEntries) {
73  
74          Document doc = new DocumentImpl();
75  
76          doc.addUID(PORTLET_ID, imageId);
77  
78          doc.addModifiedDate(modifiedDate);
79  
80          doc.addKeyword(Field.COMPANY_ID, companyId);
81          doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
82          doc.addKeyword(Field.GROUP_ID, groupId);
83  
84          doc.addText(Field.TITLE, name);
85          doc.addText(Field.DESCRIPTION, description);
86  
87          doc.addKeyword("folderId", folderId);
88          doc.addKeyword(Field.ENTRY_CLASS_PK, imageId);
89  
90          doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
91  
92          return doc;
93      }
94  
95      public static String getImageUID(long imageId) {
96          Document doc = new DocumentImpl();
97  
98          doc.addUID(PORTLET_ID, imageId);
99  
100         return doc.get(Field.UID);
101     }
102 
103     public static void updateImage(
104             long companyId, long groupId, long folderId, long imageId,
105             String name, String description, Date modifiedDate,
106             String[] tagsEntries)
107         throws SearchException {
108 
109         Document doc = getImageDocument(
110             companyId, groupId, folderId, imageId, name, description,
111             modifiedDate, tagsEntries);
112 
113         SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
114     }
115 
116     public DocumentSummary getDocumentSummary(
117         com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
118 
119         // Title
120 
121         String title = doc.get(Field.TITLE);
122 
123         // Content
124 
125         String content = doc.get(Field.CONTENT);
126 
127         content = StringUtil.shorten(content, 200);
128 
129         // Portlet URL
130 
131         String imageId = doc.get(Field.ENTRY_CLASS_PK);
132 
133         portletURL.setParameter("struts_action", "/image_gallery/edit_image");
134         portletURL.setParameter(Field.ENTRY_CLASS_PK, imageId);
135 
136         return new DocumentSummary(title, content, portletURL);
137     }
138 
139     public void reIndex(String[] ids) throws SearchException {
140         try {
141             IGFolderLocalServiceUtil.reIndex(ids);
142         }
143         catch (Exception e) {
144             throw new SearchException(e);
145         }
146     }
147 
148 }