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[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(
121 IGImage.class.getName(), imageId);
122
123 ExpandoBridge expandoBridge = image.getExpandoBridge();
124
125 Document document = new DocumentImpl();
126
127 document.addUID(PORTLET_ID, imageId);
128
129 document.addModifiedDate(modifiedDate);
130
131 document.addKeyword(Field.COMPANY_ID, companyId);
132 document.addKeyword(Field.PORTLET_ID, PORTLET_ID);
133 document.addKeyword(Field.GROUP_ID, groupId);
134 document.addKeyword(Field.SCOPE_GROUP_ID, scopeGroupId);
135 document.addKeyword(Field.USER_ID, userId);
136
137 document.addText(Field.TITLE, name);
138 document.addText(Field.DESCRIPTION, description);
139 document.addKeyword(Field.ASSET_CATEGORY_IDS, assetCategoryIds);
140 document.addKeyword(Field.ASSET_TAG_NAMES, assetTagNames);
141
142 document.addKeyword(Field.FOLDER_ID, folderId);
143 document.addKeyword(Field.ENTRY_CLASS_NAME, IGImage.class.getName());
144 document.addKeyword(Field.ENTRY_CLASS_PK, imageId);
145
146 ExpandoBridgeIndexerUtil.addAttributes(document, expandoBridge);
147
148 return document;
149 }
150
151 protected void doReindex(Object obj) throws Exception {
152 IGImage image = (IGImage)obj;
153
154 Document document = getDocument(image);
155
156 SearchEngineUtil.updateDocument(image.getCompanyId(), document);
157 }
158
159 protected void doReindex(String className, long classPK) throws Exception {
160 IGImage image = IGImageLocalServiceUtil.getImage(classPK);
161
162 doReindex(image);
163 }
164
165 protected void doReindex(String[] ids) throws Exception {
166 long companyId = GetterUtil.getLong(ids[0]);
167
168 reindexFolders(companyId);
169 reindexRoot(companyId);
170 }
171
172 protected String getPortletId(SearchContext searchContext) {
173 return PORTLET_ID;
174 }
175
176 protected void reindexFolders(long companyId) throws Exception {
177 int folderCount = IGFolderLocalServiceUtil.getCompanyFoldersCount(
178 companyId);
179
180 int folderPages = folderCount / Indexer.DEFAULT_INTERVAL;
181
182 for (int i = 0; i <= folderPages; i++) {
183 int folderStart = (i * Indexer.DEFAULT_INTERVAL);
184 int folderEnd = folderStart + Indexer.DEFAULT_INTERVAL;
185
186 reindexFolders(companyId, folderStart, folderEnd);
187 }
188 }
189
190 protected void reindexFolders(
191 long companyId, int folderStart, int folderEnd)
192 throws Exception {
193
194 List<IGFolder> folders = IGFolderLocalServiceUtil.getCompanyFolders(
195 companyId, folderStart, folderEnd);
196
197 for (IGFolder folder : folders) {
198 long groupId = folder.getGroupId();
199 long folderId = folder.getFolderId();
200
201 int entryCount = IGImageLocalServiceUtil.getImagesCount(
202 groupId, folderId);
203
204 int entryPages = entryCount / Indexer.DEFAULT_INTERVAL;
205
206 for (int i = 0; i <= entryPages; i++) {
207 int entryStart = (i * Indexer.DEFAULT_INTERVAL);
208 int entryEnd = entryStart + Indexer.DEFAULT_INTERVAL;
209
210 reindexImages(
211 companyId, groupId, folderId, entryStart, entryEnd);
212 }
213 }
214 }
215
216 protected void reindexImages(
217 long companyId, long groupId, long folderId, int entryStart,
218 int entryEnd)
219 throws Exception {
220
221 List<IGImage> images = IGImageLocalServiceUtil.getImages(
222 groupId, folderId, entryStart, entryEnd);
223
224 if (images.isEmpty()) {
225 return;
226 }
227
228 Collection<Document> documents = new ArrayList<Document>();
229
230 for (IGImage image : images) {
231 Document document = getDocument(image);
232
233 documents.add(document);
234 }
235
236 SearchEngineUtil.updateDocuments(companyId, documents);
237 }
238
239 protected void reindexRoot(long companyId) throws Exception {
240 int groupCount = GroupLocalServiceUtil.getCompanyGroupsCount(companyId);
241
242 int groupPages = groupCount / Indexer.DEFAULT_INTERVAL;
243
244 for (int i = 0; i <= groupPages; i++) {
245 int groupStart = (i * Indexer.DEFAULT_INTERVAL);
246 int groupEnd = groupStart + Indexer.DEFAULT_INTERVAL;
247
248 reindexRoot(companyId, groupStart, groupEnd);
249 }
250 }
251
252 protected void reindexRoot(long companyId, int groupStart, int groupEnd)
253 throws Exception {
254
255 List<Group> groups = GroupLocalServiceUtil.getCompanyGroups(
256 companyId, groupStart, groupEnd);
257
258 for (Group group : groups) {
259 long groupId = group.getGroupId();
260 long folderId = IGFolderConstants.DEFAULT_PARENT_FOLDER_ID;
261
262 int entryCount = IGImageLocalServiceUtil.getImagesCount(
263 groupId, folderId);
264
265 int entryPages = entryCount / Indexer.DEFAULT_INTERVAL;
266
267 for (int j = 0; j <= entryPages; j++) {
268 int entryStart = (j * Indexer.DEFAULT_INTERVAL);
269 int entryEnd = entryStart + Indexer.DEFAULT_INTERVAL;
270
271 reindexImages(
272 companyId, groupId, folderId, entryStart, entryEnd);
273 }
274 }
275 }
276
277 }