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[] tagsCategories, String[] tagsEntries,
59              ExpandoBridge expandoBridge)
60          throws SearchException {
61  
62          Document doc = getImageDocument(
63              companyId, groupId, folderId, imageId, name, description,
64              modifiedDate, tagsCategories, tagsEntries, expandoBridge);
65  
66          SearchEngineUtil.addDocument(companyId, doc);
67      }
68  
69      public static void deleteImage(long companyId, long imageId)
70          throws SearchException {
71  
72          SearchEngineUtil.deleteDocument(companyId, getImageUID(imageId));
73      }
74  
75      public static Document getImageDocument(
76          long companyId, long groupId, long folderId, long imageId,
77          String name, String description, Date modifiedDate,
78          String[] tagsCategories, String[] tagsEntries,
79          ExpandoBridge expandoBridge) {
80  
81          Document doc = new DocumentImpl();
82  
83          doc.addUID(PORTLET_ID, imageId);
84  
85          doc.addModifiedDate(modifiedDate);
86  
87          doc.addKeyword(Field.COMPANY_ID, companyId);
88          doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
89          doc.addKeyword(Field.GROUP_ID, groupId);
90  
91          doc.addText(Field.TITLE, name);
92          doc.addText(Field.DESCRIPTION, description);
93          doc.addKeyword(Field.TAGS_CATEGORIES, tagsCategories);
94          doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
95  
96          doc.addKeyword("folderId", folderId);
97          doc.addKeyword(Field.ENTRY_CLASS_NAME, IGImage.class.getName());
98          doc.addKeyword(Field.ENTRY_CLASS_PK, imageId);
99  
100         ExpandoBridgeIndexerUtil.addAttributes(doc, expandoBridge);
101 
102         return doc;
103     }
104 
105     public static String getImageUID(long imageId) {
106         Document doc = new DocumentImpl();
107 
108         doc.addUID(PORTLET_ID, imageId);
109 
110         return doc.get(Field.UID);
111     }
112 
113     public static void updateImage(
114             long companyId, long groupId, long folderId, long imageId,
115             String name, String description, Date modifiedDate,
116             String[] tagsCategories, String[] tagsEntries,
117             ExpandoBridge expandoBridge)
118         throws SearchException {
119 
120         Document doc = getImageDocument(
121             companyId, groupId, folderId, imageId, name, description,
122             modifiedDate, tagsCategories, tagsEntries, expandoBridge);
123 
124         SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
125     }
126 
127     public String[] getClassNames() {
128         return _CLASS_NAMES;
129     }
130 
131     public DocumentSummary getDocumentSummary(
132         com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
133 
134         // Title
135 
136         String title = doc.get(Field.TITLE);
137 
138         // Content
139 
140         String content = doc.get(Field.DESCRIPTION);
141 
142         content = StringUtil.shorten(content, 200);
143 
144         // Portlet URL
145 
146         String imageId = doc.get(Field.ENTRY_CLASS_PK);
147 
148         portletURL.setParameter("struts_action", "/image_gallery/edit_image");
149         portletURL.setParameter(Field.ENTRY_CLASS_PK, imageId);
150 
151         return new DocumentSummary(title, content, portletURL);
152     }
153 
154     public void reIndex(String className, long classPK) throws SearchException {
155         try {
156             IGImageLocalServiceUtil.reIndex(classPK);
157         }
158         catch (Exception e) {
159             throw new SearchException(e);
160         }
161     }
162 
163     public void reIndex(String[] ids) throws SearchException {
164         try {
165             IGFolderLocalServiceUtil.reIndex(ids);
166         }
167         catch (Exception e) {
168             throw new SearchException(e);
169         }
170     }
171 
172     private static final String[] _CLASS_NAMES = new String[] {
173         IGImage.class.getName()
174     };
175 
176 }