1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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.Field;
20  import com.liferay.portal.kernel.search.Indexer;
21  import com.liferay.portal.kernel.search.SearchContext;
22  import com.liferay.portal.kernel.search.SearchEngineUtil;
23  import com.liferay.portal.kernel.search.Summary;
24  import com.liferay.portal.kernel.util.GetterUtil;
25  import com.liferay.portal.kernel.util.StringUtil;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.model.Group;
28  import com.liferay.portal.search.BaseIndexer;
29  import com.liferay.portal.service.GroupLocalServiceUtil;
30  import com.liferay.portal.util.PortletKeys;
31  import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
32  import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
33  import com.liferay.portlet.expando.model.ExpandoBridge;
34  import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
35  import com.liferay.portlet.imagegallery.model.IGFolder;
36  import com.liferay.portlet.imagegallery.model.IGFolderConstants;
37  import com.liferay.portlet.imagegallery.model.IGImage;
38  import com.liferay.portlet.imagegallery.service.IGFolderLocalServiceUtil;
39  import com.liferay.portlet.imagegallery.service.IGFolderServiceUtil;
40  import com.liferay.portlet.imagegallery.service.IGImageLocalServiceUtil;
41  
42  import java.util.Date;
43  import java.util.List;
44  
45  import javax.portlet.PortletURL;
46  
47  /**
48   * <a href="IGIndexer.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   * @author Bruno Farache
52   * @author Raymond Augé
53   */
54  public class IGIndexer extends BaseIndexer {
55  
56      public static final String[] CLASS_NAMES = {IGImage.class.getName()};
57  
58      public static final String PORTLET_ID = PortletKeys.IMAGE_GALLERY;
59  
60      public String[] getClassNames() {
61          return CLASS_NAMES;
62      }
63  
64      public Summary getSummary(
65          Document document, String snippet, PortletURL portletURL) {
66  
67          String title = document.get(Field.TITLE);
68  
69          String content = snippet;
70  
71          if (Validator.isNull(snippet)) {
72              content = StringUtil.shorten(document.get(Field.DESCRIPTION), 200);
73          }
74  
75          String imageId = document.get(Field.ENTRY_CLASS_PK);
76  
77          portletURL.setParameter("struts_action", "/image_gallery/edit_image");
78          portletURL.setParameter("imageId", imageId);
79  
80          return new Summary(title, content, portletURL);
81      }
82  
83      protected void checkSearchFolderId(
84              long folderId, SearchContext searchContext)
85          throws Exception {
86  
87          IGFolderServiceUtil.getFolder(folderId);
88      }
89  
90      protected void doDelete(Object obj) throws Exception {
91          IGImage image = (IGImage)obj;
92  
93          Document document = new DocumentImpl();
94  
95          document.addUID(PORTLET_ID, image.getImageId());
96  
97          SearchEngineUtil.deleteDocument(
98              image.getCompanyId(), document.get(Field.UID));
99      }
100 
101     protected Document doGetDocument(Object obj) throws Exception {
102         IGImage image = (IGImage)obj;
103 
104         long companyId = image.getCompanyId();
105         long groupId = getParentGroupId(image.getGroupId());
106         long scopeGroupId = image.getGroupId();
107         long folderId = image.getFolderId();
108         long imageId = image.getImageId();
109         String name = image.getName();
110         String description = image.getDescription();
111         Date modifiedDate = image.getModifiedDate();
112 
113         long[] assetCategoryIds = AssetCategoryLocalServiceUtil.getCategoryIds(
114             IGImage.class.getName(), imageId);
115         String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(
116             IGImage.class.getName(), imageId);
117 
118         ExpandoBridge expandoBridge = image.getExpandoBridge();
119 
120         Document document = new DocumentImpl();
121 
122         document.addUID(PORTLET_ID, imageId);
123 
124         document.addModifiedDate(modifiedDate);
125 
126         document.addKeyword(Field.COMPANY_ID, companyId);
127         document.addKeyword(Field.PORTLET_ID, PORTLET_ID);
128         document.addKeyword(Field.GROUP_ID, groupId);
129         document.addKeyword(Field.SCOPE_GROUP_ID, scopeGroupId);
130 
131         document.addText(Field.TITLE, name);
132         document.addText(Field.DESCRIPTION, description);
133         document.addKeyword(Field.ASSET_CATEGORY_IDS, assetCategoryIds);
134         document.addKeyword(Field.ASSET_TAG_NAMES, assetTagNames);
135 
136         document.addKeyword(Field.FOLDER_ID, folderId);
137         document.addKeyword(Field.ENTRY_CLASS_NAME, IGImage.class.getName());
138         document.addKeyword(Field.ENTRY_CLASS_PK, imageId);
139 
140         ExpandoBridgeIndexerUtil.addAttributes(document, expandoBridge);
141 
142         return document;
143     }
144 
145     protected void doReindex(Object obj) throws Exception {
146         IGImage image = (IGImage)obj;
147 
148         Document document = getDocument(image);
149 
150         SearchEngineUtil.updateDocument(
151             image.getCompanyId(), document.get(Field.UID), document);
152     }
153 
154     protected void doReindex(String className, long classPK) throws Exception {
155         IGImage image = IGImageLocalServiceUtil.getImage(classPK);
156 
157         doReindex(image);
158     }
159 
160     protected void doReindex(String[] ids) throws Exception {
161         long companyId = GetterUtil.getLong(ids[0]);
162 
163         reindexFolders(companyId);
164         reindexRoot(companyId);
165     }
166 
167     protected String getPortletId(SearchContext searchContext) {
168         return PORTLET_ID;
169     }
170 
171     protected void reindexFolders(long companyId) throws Exception {
172         int folderCount = IGFolderLocalServiceUtil.getCompanyFoldersCount(
173             companyId);
174 
175         int folderPages = folderCount / Indexer.DEFAULT_INTERVAL;
176 
177         for (int i = 0; i <= folderPages; i++) {
178             int folderStart = (i * Indexer.DEFAULT_INTERVAL);
179             int folderEnd = folderStart + Indexer.DEFAULT_INTERVAL;
180 
181             reindexFolders(companyId, folderStart, folderEnd);
182         }
183     }
184 
185     protected void reindexFolders(
186             long companyId, int folderStart, int folderEnd)
187         throws Exception {
188 
189         List<IGFolder> folders = IGFolderLocalServiceUtil.getCompanyFolders(
190             companyId, folderStart, folderEnd);
191 
192         for (IGFolder folder : folders) {
193             long groupId = folder.getGroupId();
194             long folderId = folder.getFolderId();
195 
196             int entryCount = IGImageLocalServiceUtil.getImagesCount(
197                 groupId, folderId);
198 
199             int entryPages = entryCount / Indexer.DEFAULT_INTERVAL;
200 
201             for (int i = 0; i <= entryPages; i++) {
202                 int entryStart = (i * Indexer.DEFAULT_INTERVAL);
203                 int entryEnd = entryStart + Indexer.DEFAULT_INTERVAL;
204 
205                 reindexImages(groupId, folderId, entryStart, entryEnd);
206             }
207         }
208     }
209 
210     protected void reindexImages(
211             long groupId, long folderId, int entryStart, int entryEnd)
212         throws Exception {
213 
214         List<IGImage> images = IGImageLocalServiceUtil.getImages(
215             groupId, folderId, entryStart, entryEnd);
216 
217         for (IGImage image : images) {
218             reindex(image);
219         }
220     }
221 
222     protected void reindexRoot(long companyId) throws Exception {
223         int groupCount = GroupLocalServiceUtil.getCompanyGroupsCount(companyId);
224 
225         int groupPages = groupCount / Indexer.DEFAULT_INTERVAL;
226 
227         for (int i = 0; i <= groupPages; i++) {
228             int groupStart = (i * Indexer.DEFAULT_INTERVAL);
229             int groupEnd = groupStart + Indexer.DEFAULT_INTERVAL;
230 
231             reindexRoot(companyId, groupStart, groupEnd);
232         }
233     }
234 
235     protected void reindexRoot(long companyId, int groupStart, int groupEnd)
236         throws Exception {
237 
238         List<Group> groups = GroupLocalServiceUtil.getCompanyGroups(
239             companyId, groupStart, groupEnd);
240 
241         for (Group group : groups) {
242             long groupId = group.getGroupId();
243             long folderId = IGFolderConstants.DEFAULT_PARENT_FOLDER_ID;
244 
245             int entryCount = IGImageLocalServiceUtil.getImagesCount(
246                 groupId, folderId);
247 
248             int entryPages = entryCount / Indexer.DEFAULT_INTERVAL;
249 
250             for (int j = 0; j <= entryPages; j++) {
251                 int entryStart = (j * Indexer.DEFAULT_INTERVAL);
252                 int entryEnd = entryStart + Indexer.DEFAULT_INTERVAL;
253 
254                 reindexImages(groupId, folderId, entryStart, entryEnd);
255             }
256         }
257     }
258 
259 }