1
14
15 package com.liferay.portlet.imagegallery.util;
16
17 import com.liferay.portal.kernel.search.BaseIndexer;
18 import com.liferay.portal.kernel.search.Document;
19 import com.liferay.portal.kernel.search.DocumentImpl;
20 import com.liferay.portal.kernel.search.Field;
21 import com.liferay.portal.kernel.search.Indexer;
22 import com.liferay.portal.kernel.search.SearchContext;
23 import com.liferay.portal.kernel.search.SearchEngineUtil;
24 import com.liferay.portal.kernel.search.Summary;
25 import com.liferay.portal.kernel.util.GetterUtil;
26 import com.liferay.portal.kernel.util.StringUtil;
27 import com.liferay.portal.kernel.util.Validator;
28 import com.liferay.portal.model.Group;
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.ArrayList;
43 import java.util.Collection;
44 import java.util.Date;
45 import java.util.List;
46
47 import javax.portlet.PortletURL;
48
49
56 public class IGIndexer extends BaseIndexer {
57
58 public static final String[] CLASS_NAMES = {IGImage.class.getName()};
59
60 public static final String PORTLET_ID = PortletKeys.IMAGE_GALLERY;
61
62 public String[] getClassNames() {
63 return CLASS_NAMES;
64 }
65
66 public Summary getSummary(
67 Document document, String snippet, PortletURL portletURL) {
68
69 String title = document.get(Field.TITLE);
70
71 String content = snippet;
72
73 if (Validator.isNull(snippet)) {
74 content = StringUtil.shorten(document.get(Field.DESCRIPTION), 200);
75 }
76
77 String imageId = document.get(Field.ENTRY_CLASS_PK);
78
79 portletURL.setParameter("struts_action", "/image_gallery/view_image");
80 portletURL.setParameter("imageId", imageId);
81
82 return new Summary(title, content, portletURL);
83 }
84
85 protected void checkSearchFolderId(
86 long folderId, SearchContext searchContext)
87 throws Exception {
88
89 IGFolderServiceUtil.getFolder(folderId);
90 }
91
92 protected void doDelete(Object obj) throws Exception {
93 IGImage image = (IGImage)obj;
94
95 Document document = new DocumentImpl();
96
97 document.addUID(PORTLET_ID, image.getImageId());
98
99 SearchEngineUtil.deleteDocument(
100 image.getCompanyId(), document.get(Field.UID));
101 }
102
103 protected Document doGetDocument(Object obj) throws Exception {
104 IGImage image = (IGImage)obj;
105
106 long companyId = image.getCompanyId();
107 long groupId = getParentGroupId(image.getGroupId());
108 long scopeGroupId = image.getGroupId();
109 long userId = image.getUserId();
110 long folderId = image.getFolderId();
111 long imageId = image.getImageId();
112 String name = image.getName();
113 String description = image.getDescription();
114 Date modifiedDate = image.getModifiedDate();
115
116 long[] assetCategoryIds = AssetCategoryLocalServiceUtil.getCategoryIds(
117 IGImage.class.getName(), imageId);
118 String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(
119 IGImage.class.getName(), imageId);
120
121 ExpandoBridge expandoBridge = image.getExpandoBridge();
122
123 Document document = new DocumentImpl();
124
125 document.addUID(PORTLET_ID, imageId);
126
127 document.addModifiedDate(modifiedDate);
128
129 document.addKeyword(Field.COMPANY_ID, companyId);
130 document.addKeyword(Field.PORTLET_ID, PORTLET_ID);
131 document.addKeyword(Field.GROUP_ID, groupId);
132 document.addKeyword(Field.SCOPE_GROUP_ID, scopeGroupId);
133 document.addKeyword(Field.USER_ID, userId);
134
135 document.addText(Field.TITLE, name);
136 document.addText(Field.DESCRIPTION, description);
137 document.addKeyword(Field.ASSET_CATEGORY_IDS, assetCategoryIds);
138 document.addKeyword(Field.ASSET_TAG_NAMES, assetTagNames);
139
140 document.addKeyword(Field.FOLDER_ID, folderId);
141 document.addKeyword(Field.ENTRY_CLASS_NAME, IGImage.class.getName());
142 document.addKeyword(Field.ENTRY_CLASS_PK, imageId);
143
144 ExpandoBridgeIndexerUtil.addAttributes(document, expandoBridge);
145
146 return document;
147 }
148
149 protected void doReindex(Object obj) throws Exception {
150 IGImage image = (IGImage)obj;
151
152 Document document = getDocument(image);
153
154 SearchEngineUtil.updateDocument(image.getCompanyId(), document);
155 }
156
157 protected void doReindex(String className, long classPK) throws Exception {
158 IGImage image = IGImageLocalServiceUtil.getImage(classPK);
159
160 doReindex(image);
161 }
162
163 protected void doReindex(String[] ids) throws Exception {
164 long companyId = GetterUtil.getLong(ids[0]);
165
166 reindexFolders(companyId);
167 reindexRoot(companyId);
168 }
169
170 protected String getPortletId(SearchContext searchContext) {
171 return PORTLET_ID;
172 }
173
174 protected void reindexFolders(long companyId) throws Exception {
175 int folderCount = IGFolderLocalServiceUtil.getCompanyFoldersCount(
176 companyId);
177
178 int folderPages = folderCount / Indexer.DEFAULT_INTERVAL;
179
180 for (int i = 0; i <= folderPages; i++) {
181 int folderStart = (i * Indexer.DEFAULT_INTERVAL);
182 int folderEnd = folderStart + Indexer.DEFAULT_INTERVAL;
183
184 reindexFolders(companyId, folderStart, folderEnd);
185 }
186 }
187
188 protected void reindexFolders(
189 long companyId, int folderStart, int folderEnd)
190 throws Exception {
191
192 List<IGFolder> folders = IGFolderLocalServiceUtil.getCompanyFolders(
193 companyId, folderStart, folderEnd);
194
195 for (IGFolder folder : folders) {
196 long groupId = folder.getGroupId();
197 long folderId = folder.getFolderId();
198
199 int entryCount = IGImageLocalServiceUtil.getImagesCount(
200 groupId, folderId);
201
202 int entryPages = entryCount / Indexer.DEFAULT_INTERVAL;
203
204 for (int i = 0; i <= entryPages; i++) {
205 int entryStart = (i * Indexer.DEFAULT_INTERVAL);
206 int entryEnd = entryStart + Indexer.DEFAULT_INTERVAL;
207
208 reindexImages(
209 companyId, groupId, folderId, entryStart, entryEnd);
210 }
211 }
212 }
213
214 protected void reindexImages(
215 long companyId, long groupId, long folderId, int entryStart,
216 int entryEnd)
217 throws Exception {
218
219 List<IGImage> images = IGImageLocalServiceUtil.getImages(
220 groupId, folderId, entryStart, entryEnd);
221
222 if (images.isEmpty()) {
223 return;
224 }
225
226 Collection<Document> documents = new ArrayList<Document>();
227
228 for (IGImage image : images) {
229 Document document = getDocument(image);
230
231 documents.add(document);
232 }
233
234 SearchEngineUtil.updateDocuments(companyId, documents);
235 }
236
237 protected void reindexRoot(long companyId) throws Exception {
238 int groupCount = GroupLocalServiceUtil.getCompanyGroupsCount(companyId);
239
240 int groupPages = groupCount / Indexer.DEFAULT_INTERVAL;
241
242 for (int i = 0; i <= groupPages; i++) {
243 int groupStart = (i * Indexer.DEFAULT_INTERVAL);
244 int groupEnd = groupStart + Indexer.DEFAULT_INTERVAL;
245
246 reindexRoot(companyId, groupStart, groupEnd);
247 }
248 }
249
250 protected void reindexRoot(long companyId, int groupStart, int groupEnd)
251 throws Exception {
252
253 List<Group> groups = GroupLocalServiceUtil.getCompanyGroups(
254 companyId, groupStart, groupEnd);
255
256 for (Group group : groups) {
257 long groupId = group.getGroupId();
258 long folderId = IGFolderConstants.DEFAULT_PARENT_FOLDER_ID;
259
260 int entryCount = IGImageLocalServiceUtil.getImagesCount(
261 groupId, folderId);
262
263 int entryPages = entryCount / Indexer.DEFAULT_INTERVAL;
264
265 for (int j = 0; j <= entryPages; j++) {
266 int entryStart = (j * Indexer.DEFAULT_INTERVAL);
267 int entryEnd = entryStart + Indexer.DEFAULT_INTERVAL;
268
269 reindexImages(
270 companyId, groupId, folderId, entryStart, entryEnd);
271 }
272 }
273 }
274
275 }