1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.portlet.imagegallery.util;
16  
17  import com.liferay.portal.kernel.search.Document;
18  import com.liferay.portal.kernel.search.DocumentImpl;
19  import com.liferay.portal.kernel.search.DocumentSummary;
20  import com.liferay.portal.kernel.search.Field;
21  import com.liferay.portal.kernel.search.SearchEngineUtil;
22  import com.liferay.portal.kernel.search.SearchException;
23  import com.liferay.portal.kernel.util.StringUtil;
24  import com.liferay.portal.util.PortletKeys;
25  import com.liferay.portlet.imagegallery.service.IGFolderLocalServiceUtil;
26  
27  import java.util.Date;
28  
29  import javax.portlet.PortletURL;
30  
31  /**
32   * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   * @author Bruno Farache
36   */
37  public class Indexer implements com.liferay.portal.kernel.search.Indexer {
38  
39      public static final String PORTLET_ID = PortletKeys.IMAGE_GALLERY;
40  
41      public static void addImage(
42              long companyId, long groupId, long folderId, long imageId,
43              String name, String description,  Date modifiedDate,
44              String[] tagsEntries)
45          throws SearchException {
46  
47          Document doc = getImageDocument(
48              companyId, groupId, folderId, imageId, name, description,
49              modifiedDate, tagsEntries);
50  
51          SearchEngineUtil.addDocument(companyId, doc);
52      }
53  
54      public static void deleteImage(long companyId, long imageId)
55          throws SearchException {
56  
57          SearchEngineUtil.deleteDocument(companyId, getImageUID(imageId));
58      }
59  
60      public static Document getImageDocument(
61          long companyId, long groupId, long folderId, long imageId,
62          String name, String description, Date modifiedDate,
63          String[] tagsEntries) {
64  
65          Document doc = new DocumentImpl();
66  
67          doc.addUID(PORTLET_ID, imageId);
68  
69          doc.addModifiedDate(modifiedDate);
70  
71          doc.addKeyword(Field.COMPANY_ID, companyId);
72          doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
73          doc.addKeyword(Field.GROUP_ID, groupId);
74  
75          doc.addText(Field.TITLE, name);
76          doc.addText(Field.DESCRIPTION, description);
77  
78          doc.addKeyword("folderId", folderId);
79          doc.addKeyword(Field.ENTRY_CLASS_PK, imageId);
80  
81          doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
82  
83          return doc;
84      }
85  
86      public static String getImageUID(long imageId) {
87          Document doc = new DocumentImpl();
88  
89          doc.addUID(PORTLET_ID, imageId);
90  
91          return doc.get(Field.UID);
92      }
93  
94      public static void updateImage(
95              long companyId, long groupId, long folderId, long imageId,
96              String name, String description, Date modifiedDate,
97              String[] tagsEntries)
98          throws SearchException {
99  
100         Document doc = getImageDocument(
101             companyId, groupId, folderId, imageId, name, description,
102             modifiedDate, tagsEntries);
103 
104         SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
105     }
106 
107     public DocumentSummary getDocumentSummary(
108         com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
109 
110         // Title
111 
112         String title = doc.get(Field.TITLE);
113 
114         // Content
115 
116         String content = doc.get(Field.CONTENT);
117 
118         content = StringUtil.shorten(content, 200);
119 
120         // Portlet URL
121 
122         String imageId = doc.get(Field.ENTRY_CLASS_PK);
123 
124         portletURL.setParameter("struts_action", "/image_gallery/edit_image");
125         portletURL.setParameter("imageId", imageId);
126 
127         return new DocumentSummary(title, content, portletURL);
128     }
129 
130     public void reIndex(String[] ids) throws SearchException {
131         try {
132             IGFolderLocalServiceUtil.reIndex(ids);
133         }
134         catch (Exception e) {
135             throw new SearchException(e);
136         }
137     }
138 
139 }