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