001
014
015 package com.liferay.portlet.bookmarks.util;
016
017 import com.liferay.portal.kernel.search.BaseIndexer;
018 import com.liferay.portal.kernel.search.BooleanClauseOccur;
019 import com.liferay.portal.kernel.search.BooleanQuery;
020 import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
021 import com.liferay.portal.kernel.search.Document;
022 import com.liferay.portal.kernel.search.Field;
023 import com.liferay.portal.kernel.search.Indexer;
024 import com.liferay.portal.kernel.search.SearchContext;
025 import com.liferay.portal.kernel.search.SearchEngineUtil;
026 import com.liferay.portal.kernel.search.Summary;
027 import com.liferay.portal.kernel.util.GetterUtil;
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.bookmarks.model.BookmarksEntry;
032 import com.liferay.portlet.bookmarks.model.BookmarksFolder;
033 import com.liferay.portlet.bookmarks.model.BookmarksFolderConstants;
034 import com.liferay.portlet.bookmarks.service.BookmarksEntryLocalServiceUtil;
035 import com.liferay.portlet.bookmarks.service.BookmarksFolderLocalServiceUtil;
036 import com.liferay.portlet.bookmarks.service.BookmarksFolderServiceUtil;
037
038 import java.util.ArrayList;
039 import java.util.Collection;
040 import java.util.List;
041 import java.util.Locale;
042
043 import javax.portlet.PortletURL;
044
045
050 public class BookmarksIndexer extends BaseIndexer {
051
052 public static final String[] CLASS_NAMES = {BookmarksEntry.class.getName()};
053
054 public static final String PORTLET_ID = PortletKeys.BOOKMARKS;
055
056 public String[] getClassNames() {
057 return CLASS_NAMES;
058 }
059
060 public String getPortletId() {
061 return PORTLET_ID;
062 }
063
064 @Override
065 public void postProcessContextQuery(
066 BooleanQuery contextQuery, SearchContext searchContext)
067 throws Exception {
068
069 long[] folderIds = searchContext.getFolderIds();
070
071 if ((folderIds != null) && (folderIds.length > 0)) {
072 if (folderIds[0] ==
073 BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
074
075 return;
076 }
077
078 BooleanQuery folderIdsQuery = BooleanQueryFactoryUtil.create(
079 searchContext);
080
081 for (long folderId : folderIds) {
082 try {
083 BookmarksFolderServiceUtil.getFolder(folderId);
084 }
085 catch (Exception e) {
086 continue;
087 }
088
089 folderIdsQuery.addTerm(Field.FOLDER_ID, folderId);
090 }
091
092 contextQuery.add(folderIdsQuery, BooleanClauseOccur.MUST);
093 }
094 }
095
096 @Override
097 protected void doDelete(Object obj) throws Exception {
098 BookmarksEntry entry = (BookmarksEntry)obj;
099
100 deleteDocument(entry.getCompanyId(), entry.getEntryId());
101 }
102
103 @Override
104 protected Document doGetDocument(Object obj) throws Exception {
105 BookmarksEntry entry = (BookmarksEntry)obj;
106
107 Document document = getBaseModelDocument(PORTLET_ID, entry);
108
109 document.addText(Field.DESCRIPTION, entry.getDescription());
110 document.addKeyword(Field.FOLDER_ID, entry.getFolderId());
111 document.addText(Field.TITLE, entry.getName());
112 document.addText(Field.URL, entry.getUrl());
113
114 return document;
115 }
116
117 @Override
118 protected Summary doGetSummary(
119 Document document, Locale locale, String snippet,
120 PortletURL portletURL) {
121
122 String title = document.get(Field.TITLE);
123
124 String url = document.get(Field.URL);
125
126 String entryId = document.get(Field.ENTRY_CLASS_PK);
127
128 portletURL.setParameter("struts_action", "/bookmarks/view_entry");
129 portletURL.setParameter("entryId", entryId);
130
131 return new Summary(title, url, portletURL);
132 }
133
134 @Override
135 protected void doReindex(Object obj) throws Exception {
136 BookmarksEntry entry = (BookmarksEntry)obj;
137
138 Document document = getDocument(entry);
139
140 SearchEngineUtil.updateDocument(entry.getCompanyId(), document);
141 }
142
143 @Override
144 protected void doReindex(String className, long classPK) throws Exception {
145 BookmarksEntry entry = BookmarksEntryLocalServiceUtil.getEntry(classPK);
146
147 doReindex(entry);
148 }
149
150 @Override
151 protected void doReindex(String[] ids) throws Exception {
152 long companyId = GetterUtil.getLong(ids[0]);
153
154 reindexFolders(companyId);
155 reindexRoot(companyId);
156 }
157
158 @Override
159 protected String getPortletId(SearchContext searchContext) {
160 return PORTLET_ID;
161 }
162
163 protected void reindexEntries(
164 long companyId, long groupId, long folderId, int entryStart,
165 int entryEnd)
166 throws Exception {
167
168 List<BookmarksEntry> entries =
169 BookmarksEntryLocalServiceUtil.getEntries(
170 groupId, folderId, entryStart, entryEnd);
171
172 if (entries.isEmpty()) {
173 return;
174 }
175
176 Collection<Document> documents = new ArrayList<Document>();
177
178 for (BookmarksEntry entry : entries) {
179 Document document = getDocument(entry);
180
181 documents.add(document);
182 }
183
184 SearchEngineUtil.updateDocuments(companyId, documents);
185 }
186
187 protected void reindexFolders(long companyId) throws Exception {
188 int folderCount =
189 BookmarksFolderLocalServiceUtil.getCompanyFoldersCount(companyId);
190
191 int folderPages = folderCount / Indexer.DEFAULT_INTERVAL;
192
193 for (int i = 0; i <= folderPages; i++) {
194 int folderStart = (i * Indexer.DEFAULT_INTERVAL);
195 int folderEnd = folderStart + Indexer.DEFAULT_INTERVAL;
196
197 reindexFolders(companyId, folderStart, folderEnd);
198 }
199 }
200
201 protected void reindexFolders(
202 long companyId, int folderStart, int folderEnd)
203 throws Exception {
204
205 List<BookmarksFolder> folders =
206 BookmarksFolderLocalServiceUtil.getCompanyFolders(
207 companyId, folderStart, folderEnd);
208
209 for (BookmarksFolder folder : folders) {
210 long groupId = folder.getGroupId();
211 long folderId = folder.getFolderId();
212
213 int entryCount = BookmarksEntryLocalServiceUtil.getEntriesCount(
214 groupId, folderId);
215
216 int entryPages = entryCount / Indexer.DEFAULT_INTERVAL;
217
218 for (int i = 0; i <= entryPages; i++) {
219 int entryStart = (i * Indexer.DEFAULT_INTERVAL);
220 int entryEnd = entryStart + Indexer.DEFAULT_INTERVAL;
221
222 reindexEntries(
223 companyId, groupId, folderId, entryStart, entryEnd);
224 }
225 }
226 }
227
228 protected void reindexRoot(long companyId) throws Exception {
229 int groupCount = GroupLocalServiceUtil.getCompanyGroupsCount(companyId);
230
231 int groupPages = groupCount / Indexer.DEFAULT_INTERVAL;
232
233 for (int i = 0; i <= groupPages; i++) {
234 int groupStart = (i * Indexer.DEFAULT_INTERVAL);
235 int groupEnd = groupStart + Indexer.DEFAULT_INTERVAL;
236
237 reindexRoot(companyId, groupStart, groupEnd);
238 }
239 }
240
241 protected void reindexRoot(long companyId, int groupStart, int groupEnd)
242 throws Exception {
243
244 List<Group> groups = GroupLocalServiceUtil.getCompanyGroups(
245 companyId, groupStart, groupEnd);
246
247 for (Group group : groups) {
248 long groupId = group.getGroupId();
249 long folderId = BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID;
250
251 int entryCount = BookmarksEntryLocalServiceUtil.getEntriesCount(
252 groupId, folderId);
253
254 int entryPages = entryCount / Indexer.DEFAULT_INTERVAL;
255
256 for (int i = 0; i <= entryPages; i++) {
257 int entryStart = (i * Indexer.DEFAULT_INTERVAL);
258 int entryEnd = entryStart + Indexer.DEFAULT_INTERVAL;
259
260 reindexEntries(
261 companyId, groupId, folderId, entryStart, entryEnd);
262 }
263 }
264 }
265
266 }