001
014
015 package com.liferay.portlet.imagegallery.util;
016
017 import com.liferay.portal.kernel.search.BaseIndexer;
018 import com.liferay.portal.kernel.search.Document;
019 import com.liferay.portal.kernel.search.DocumentImpl;
020 import com.liferay.portal.kernel.search.Field;
021 import com.liferay.portal.kernel.search.Indexer;
022 import com.liferay.portal.kernel.search.SearchContext;
023 import com.liferay.portal.kernel.search.SearchEngineUtil;
024 import com.liferay.portal.kernel.search.Summary;
025 import com.liferay.portal.kernel.util.GetterUtil;
026 import com.liferay.portal.kernel.util.StringUtil;
027 import com.liferay.portal.kernel.util.Validator;
028 import com.liferay.portal.model.Group;
029 import com.liferay.portal.service.GroupLocalServiceUtil;
030 import com.liferay.portal.util.PortletKeys;
031 import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
032 import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
033 import com.liferay.portlet.expando.model.ExpandoBridge;
034 import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
035 import com.liferay.portlet.imagegallery.model.IGFolder;
036 import com.liferay.portlet.imagegallery.model.IGFolderConstants;
037 import com.liferay.portlet.imagegallery.model.IGImage;
038 import com.liferay.portlet.imagegallery.service.IGFolderLocalServiceUtil;
039 import com.liferay.portlet.imagegallery.service.IGFolderServiceUtil;
040 import com.liferay.portlet.imagegallery.service.IGImageLocalServiceUtil;
041
042 import java.util.ArrayList;
043 import java.util.Collection;
044 import java.util.Date;
045 import java.util.List;
046
047 import javax.portlet.PortletURL;
048
049
054 public class IGIndexer extends BaseIndexer {
055
056 public static final String[] CLASS_NAMES = {IGImage.class.getName()};
057
058 public static final String PORTLET_ID = PortletKeys.IMAGE_GALLERY;
059
060 public String[] getClassNames() {
061 return CLASS_NAMES;
062 }
063
064 public Summary getSummary(
065 Document document, String snippet, PortletURL portletURL) {
066
067 String title = document.get(Field.TITLE);
068
069 String content = snippet;
070
071 if (Validator.isNull(snippet)) {
072 content = StringUtil.shorten(document.get(Field.DESCRIPTION), 200);
073 }
074
075 String imageId = document.get(Field.ENTRY_CLASS_PK);
076
077 portletURL.setParameter("struts_action", "/image_gallery/view_image");
078 portletURL.setParameter("imageId", imageId);
079
080 return new Summary(title, content, portletURL);
081 }
082
083 protected void checkSearchFolderId(
084 long folderId, SearchContext searchContext)
085 throws Exception {
086
087 if (folderId == IGFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
088 return;
089 }
090
091 IGFolderServiceUtil.getFolder(folderId);
092 }
093
094 protected void doDelete(Object obj) throws Exception {
095 IGImage image = (IGImage)obj;
096
097 Document document = new DocumentImpl();
098
099 document.addUID(PORTLET_ID, image.getImageId());
100
101 SearchEngineUtil.deleteDocument(
102 image.getCompanyId(), document.get(Field.UID));
103 }
104
105 protected Document doGetDocument(Object obj) throws Exception {
106 IGImage image = (IGImage)obj;
107
108 long companyId = image.getCompanyId();
109 long groupId = getParentGroupId(image.getGroupId());
110 long scopeGroupId = image.getGroupId();
111 long userId = image.getUserId();
112 long folderId = image.getFolderId();
113 long imageId = image.getImageId();
114 String name = image.getName();
115 String description = image.getDescription();
116 Date modifiedDate = image.getModifiedDate();
117
118 long[] assetCategoryIds = AssetCategoryLocalServiceUtil.getCategoryIds(
119 IGImage.class.getName(), imageId);
120 String[] assetCategoryNames =
121 AssetCategoryLocalServiceUtil.getCategoryNames(
122 IGImage.class.getName(), imageId);
123 String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(
124 IGImage.class.getName(), imageId);
125
126 ExpandoBridge expandoBridge = image.getExpandoBridge();
127
128 Document document = new DocumentImpl();
129
130 document.addUID(PORTLET_ID, imageId);
131
132 document.addModifiedDate(modifiedDate);
133
134 document.addKeyword(Field.COMPANY_ID, companyId);
135 document.addKeyword(Field.PORTLET_ID, PORTLET_ID);
136 document.addKeyword(Field.GROUP_ID, groupId);
137 document.addKeyword(Field.SCOPE_GROUP_ID, scopeGroupId);
138 document.addKeyword(Field.USER_ID, userId);
139
140 document.addText(Field.TITLE, name);
141 document.addText(Field.DESCRIPTION, description);
142 document.addKeyword(Field.ASSET_CATEGORY_IDS, assetCategoryIds);
143 document.addKeyword(Field.ASSET_CATEGORY_NAMES, assetCategoryNames);
144 document.addKeyword(Field.ASSET_TAG_NAMES, assetTagNames);
145
146 document.addKeyword(Field.FOLDER_ID, folderId);
147 document.addKeyword(Field.ENTRY_CLASS_NAME, IGImage.class.getName());
148 document.addKeyword(Field.ENTRY_CLASS_PK, imageId);
149
150 ExpandoBridgeIndexerUtil.addAttributes(document, expandoBridge);
151
152 return document;
153 }
154
155 protected void doReindex(Object obj) throws Exception {
156 IGImage image = (IGImage)obj;
157
158 Document document = getDocument(image);
159
160 SearchEngineUtil.updateDocument(image.getCompanyId(), document);
161 }
162
163 protected void doReindex(String className, long classPK) throws Exception {
164 IGImage image = IGImageLocalServiceUtil.getImage(classPK);
165
166 doReindex(image);
167 }
168
169 protected void doReindex(String[] ids) throws Exception {
170 long companyId = GetterUtil.getLong(ids[0]);
171
172 reindexFolders(companyId);
173 reindexRoot(companyId);
174 }
175
176 protected String getPortletId(SearchContext searchContext) {
177 return PORTLET_ID;
178 }
179
180 protected void reindexFolders(long companyId) throws Exception {
181 int folderCount = IGFolderLocalServiceUtil.getCompanyFoldersCount(
182 companyId);
183
184 int folderPages = folderCount / Indexer.DEFAULT_INTERVAL;
185
186 for (int i = 0; i <= folderPages; i++) {
187 int folderStart = (i * Indexer.DEFAULT_INTERVAL);
188 int folderEnd = folderStart + Indexer.DEFAULT_INTERVAL;
189
190 reindexFolders(companyId, folderStart, folderEnd);
191 }
192 }
193
194 protected void reindexFolders(
195 long companyId, int folderStart, int folderEnd)
196 throws Exception {
197
198 List<IGFolder> folders = IGFolderLocalServiceUtil.getCompanyFolders(
199 companyId, folderStart, folderEnd);
200
201 for (IGFolder folder : folders) {
202 long groupId = folder.getGroupId();
203 long folderId = folder.getFolderId();
204
205 int entryCount = IGImageLocalServiceUtil.getImagesCount(
206 groupId, folderId);
207
208 int entryPages = entryCount / Indexer.DEFAULT_INTERVAL;
209
210 for (int i = 0; i <= entryPages; i++) {
211 int entryStart = (i * Indexer.DEFAULT_INTERVAL);
212 int entryEnd = entryStart + Indexer.DEFAULT_INTERVAL;
213
214 reindexImages(
215 companyId, groupId, folderId, entryStart, entryEnd);
216 }
217 }
218 }
219
220 protected void reindexImages(
221 long companyId, long groupId, long folderId, int entryStart,
222 int entryEnd)
223 throws Exception {
224
225 List<IGImage> images = IGImageLocalServiceUtil.getImages(
226 groupId, folderId, entryStart, entryEnd);
227
228 if (images.isEmpty()) {
229 return;
230 }
231
232 Collection<Document> documents = new ArrayList<Document>();
233
234 for (IGImage image : images) {
235 Document document = getDocument(image);
236
237 documents.add(document);
238 }
239
240 SearchEngineUtil.updateDocuments(companyId, documents);
241 }
242
243 protected void reindexRoot(long companyId) throws Exception {
244 int groupCount = GroupLocalServiceUtil.getCompanyGroupsCount(companyId);
245
246 int groupPages = groupCount / Indexer.DEFAULT_INTERVAL;
247
248 for (int i = 0; i <= groupPages; i++) {
249 int groupStart = (i * Indexer.DEFAULT_INTERVAL);
250 int groupEnd = groupStart + Indexer.DEFAULT_INTERVAL;
251
252 reindexRoot(companyId, groupStart, groupEnd);
253 }
254 }
255
256 protected void reindexRoot(long companyId, int groupStart, int groupEnd)
257 throws Exception {
258
259 List<Group> groups = GroupLocalServiceUtil.getCompanyGroups(
260 companyId, groupStart, groupEnd);
261
262 for (Group group : groups) {
263 long groupId = group.getGroupId();
264 long folderId = IGFolderConstants.DEFAULT_PARENT_FOLDER_ID;
265
266 int entryCount = IGImageLocalServiceUtil.getImagesCount(
267 groupId, folderId);
268
269 int entryPages = entryCount / Indexer.DEFAULT_INTERVAL;
270
271 for (int j = 0; j <= entryPages; j++) {
272 int entryStart = (j * Indexer.DEFAULT_INTERVAL);
273 int entryEnd = entryStart + Indexer.DEFAULT_INTERVAL;
274
275 reindexImages(
276 companyId, groupId, folderId, entryStart, entryEnd);
277 }
278 }
279 }
280
281 }