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