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