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