1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.imagegallery.util;
21  
22  import com.liferay.portal.kernel.search.Document;
23  import com.liferay.portal.kernel.search.DocumentImpl;
24  import com.liferay.portal.kernel.search.DocumentSummary;
25  import com.liferay.portal.kernel.search.Field;
26  import com.liferay.portal.kernel.search.SearchEngineUtil;
27  import com.liferay.portal.kernel.search.SearchException;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.util.PortletKeys;
30  import com.liferay.portlet.imagegallery.service.IGFolderLocalServiceUtil;
31  
32  import java.util.Date;
33  
34  import javax.portlet.PortletURL;
35  
36  /**
37   * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   * @author Bruno Farache
41   *
42   */
43  public class Indexer implements com.liferay.portal.kernel.search.Indexer {
44  
45      public static final String PORTLET_ID = PortletKeys.IMAGE_GALLERY;
46  
47      public static void addImage(
48              long companyId, long groupId, long folderId, long imageId,
49              String name, String description,  Date modifiedDate,
50              String[] tagsEntries)
51          throws SearchException {
52  
53          Document doc = getImageDocument(
54              companyId, groupId, folderId, imageId, name, description,
55              modifiedDate, 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, Date modifiedDate,
69          String[] tagsEntries) {
70  
71          Document doc = new DocumentImpl();
72  
73          doc.addUID(PORTLET_ID, imageId);
74  
75          doc.addModifiedDate(modifiedDate);
76  
77          doc.addKeyword(Field.COMPANY_ID, companyId);
78          doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
79          doc.addKeyword(Field.GROUP_ID, groupId);
80  
81          doc.addText(Field.TITLE, name);
82          doc.addText(Field.DESCRIPTION, description);
83  
84          doc.addKeyword("folderId", folderId);
85          doc.addKeyword(Field.ENTRY_CLASS_PK, imageId);
86  
87          doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
88  
89          return doc;
90      }
91  
92      public static String getImageUID(long imageId) {
93          Document doc = new DocumentImpl();
94  
95          doc.addUID(PORTLET_ID, imageId);
96  
97          return doc.get(Field.UID);
98      }
99  
100     public static void updateImage(
101             long companyId, long groupId, long folderId, long imageId,
102             String name, String description, Date modifiedDate,
103             String[] tagsEntries)
104         throws SearchException {
105 
106         Document doc = getImageDocument(
107             companyId, groupId, folderId, imageId, name, description,
108             modifiedDate, tagsEntries);
109 
110         SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
111     }
112 
113     public DocumentSummary getDocumentSummary(
114         com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
115 
116         // Title
117 
118         String title = doc.get(Field.TITLE);
119 
120         // Content
121 
122         String content = doc.get(Field.CONTENT);
123 
124         content = StringUtil.shorten(content, 200);
125 
126         // Portlet URL
127 
128         String imageId = doc.get(Field.ENTRY_CLASS_PK);
129 
130         portletURL.setParameter("struts_action", "/image_gallery/edit_image");
131         portletURL.setParameter(Field.ENTRY_CLASS_PK, imageId);
132 
133         return new DocumentSummary(title, content, portletURL);
134     }
135 
136     public void reIndex(String[] ids) throws SearchException {
137         try {
138             IGFolderLocalServiceUtil.reIndex(ids);
139         }
140         catch (Exception e) {
141             throw new SearchException(e);
142         }
143     }
144 
145 }