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.kernel.util.Validator;
25  import com.liferay.portal.model.Group;
26  import com.liferay.portal.service.GroupLocalServiceUtil;
27  import com.liferay.portal.util.PortletKeys;
28  import com.liferay.portlet.expando.model.ExpandoBridge;
29  import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
30  import com.liferay.portlet.imagegallery.model.IGImage;
31  import com.liferay.portlet.imagegallery.service.IGFolderLocalServiceUtil;
32  import com.liferay.portlet.imagegallery.service.IGImageLocalServiceUtil;
33  
34  import java.util.Date;
35  
36  import javax.portlet.PortletURL;
37  
38  /**
39   * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   * @author Bruno Farache
43   * @author Raymond Augé
44   */
45  public class Indexer implements com.liferay.portal.kernel.search.Indexer {
46  
47      public static final String PORTLET_ID = PortletKeys.IMAGE_GALLERY;
48  
49      public static void addImage(
50              long companyId, long groupId, long userId, long folderId,
51              long imageId, String name, String description, Date modifiedDate,
52              String[] tagsCategories, String[] tagsEntries,
53              ExpandoBridge expandoBridge)
54          throws SearchException {
55  
56          Document doc = getImageDocument(
57              companyId, groupId, userId, folderId, imageId, name, description,
58              modifiedDate, tagsCategories, tagsEntries, expandoBridge);
59  
60          SearchEngineUtil.addDocument(companyId, doc);
61      }
62  
63      /**
64       * @deprecated
65       */
66      public static void addImage(
67              long companyId, long groupId, long folderId, long imageId,
68              String name, String description, Date modifiedDate,
69              String[] tagsCategories, String[] tagsEntries,
70              ExpandoBridge expandoBridge)
71          throws SearchException {
72  
73          addImage(
74              companyId, groupId, 0, folderId, imageId, name, description,
75              modifiedDate, tagsCategories, tagsEntries, expandoBridge);
76      }
77  
78      public static void deleteImage(long companyId, long imageId)
79          throws SearchException {
80  
81          SearchEngineUtil.deleteDocument(companyId, getImageUID(imageId));
82      }
83  
84      public static Document getImageDocument(
85          long companyId, long groupId, long userId, long folderId, long imageId,
86          String name, String description, Date modifiedDate,
87          String[] tagsCategories, String[] tagsEntries,
88          ExpandoBridge expandoBridge) {
89  
90          long scopeGroupId = groupId;
91  
92          try {
93              Group group = GroupLocalServiceUtil.getGroup(groupId);
94  
95              if (group.isLayout()) {
96                  groupId = group.getParentGroupId();
97              }
98          }
99          catch (Exception e) {
100         }
101 
102         Document doc = new DocumentImpl();
103 
104         doc.addUID(PORTLET_ID, imageId);
105 
106         doc.addModifiedDate(modifiedDate);
107 
108         doc.addKeyword(Field.COMPANY_ID, companyId);
109         doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
110         doc.addKeyword(Field.GROUP_ID, groupId);
111         doc.addKeyword(Field.SCOPE_GROUP_ID, scopeGroupId);
112         doc.addKeyword(Field.USER_ID, userId);
113 
114         doc.addText(Field.TITLE, name);
115         doc.addText(Field.DESCRIPTION, description);
116         doc.addKeyword(Field.TAGS_CATEGORIES, tagsCategories);
117         doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
118 
119         doc.addKeyword("folderId", folderId);
120         doc.addKeyword(Field.ENTRY_CLASS_NAME, IGImage.class.getName());
121         doc.addKeyword(Field.ENTRY_CLASS_PK, imageId);
122 
123         ExpandoBridgeIndexerUtil.addAttributes(doc, expandoBridge);
124 
125         return doc;
126     }
127 
128     /**
129      * @deprecated
130      */
131     public static Document getImageDocument(
132         long companyId, long groupId, long folderId, long imageId,
133         String name, String description, Date modifiedDate,
134         String[] tagsCategories, String[] tagsEntries,
135         ExpandoBridge expandoBridge) {
136 
137         return getImageDocument(
138             companyId, groupId, 0, folderId, imageId, name, description,
139             modifiedDate, tagsCategories, tagsEntries, expandoBridge);
140     }
141 
142     public static String getImageUID(long imageId) {
143         Document doc = new DocumentImpl();
144 
145         doc.addUID(PORTLET_ID, imageId);
146 
147         return doc.get(Field.UID);
148     }
149 
150     public static void updateImage(
151             long companyId, long groupId, long userId, long folderId,
152             long imageId, String name, String description, Date modifiedDate,
153             String[] tagsCategories, String[] tagsEntries,
154             ExpandoBridge expandoBridge)
155         throws SearchException {
156 
157         Document doc = getImageDocument(
158             companyId, groupId, userId, folderId, imageId, name, description,
159             modifiedDate, tagsCategories, tagsEntries, expandoBridge);
160 
161         SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
162     }
163 
164     /**
165      * @deprecated
166      */
167     public static void updateImage(
168             long companyId, long groupId, long folderId, long imageId,
169             String name, String description, Date modifiedDate,
170             String[] tagsCategories, String[] tagsEntries,
171             ExpandoBridge expandoBridge)
172         throws SearchException {
173 
174         updateImage(
175             companyId, groupId, 0, folderId, imageId, name, description,
176             modifiedDate, tagsCategories, tagsEntries, expandoBridge);
177     }
178 
179     public String[] getClassNames() {
180         return _CLASS_NAMES;
181     }
182 
183     public DocumentSummary getDocumentSummary(
184         Document doc, String snippet, PortletURL portletURL) {
185 
186         // Title
187 
188         String title = doc.get(Field.TITLE);
189 
190         // Content
191 
192         String content = snippet;
193 
194         if (Validator.isNull(snippet)) {
195             content = StringUtil.shorten(doc.get(Field.DESCRIPTION), 200);
196         }
197 
198         // Portlet URL
199 
200         String imageId = doc.get(Field.ENTRY_CLASS_PK);
201 
202         portletURL.setParameter("struts_action", "/image_gallery/edit_image");
203         portletURL.setParameter("imageId", imageId);
204 
205         return new DocumentSummary(title, content, portletURL);
206     }
207 
208     public void reIndex(String className, long classPK) throws SearchException {
209         try {
210             IGImageLocalServiceUtil.reIndex(classPK);
211         }
212         catch (Exception e) {
213             throw new SearchException(e);
214         }
215     }
216 
217     public void reIndex(String[] ids) throws SearchException {
218         try {
219             IGFolderLocalServiceUtil.reIndex(ids);
220         }
221         catch (Exception e) {
222             throw new SearchException(e);
223         }
224     }
225 
226     private static final String[] _CLASS_NAMES = new String[] {
227         IGImage.class.getName()
228     };
229 
230 }