1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.bookmarks.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.model.Group;
27  import com.liferay.portal.service.GroupLocalServiceUtil;
28  import com.liferay.portal.util.PortletKeys;
29  import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
30  import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
31  import com.liferay.portlet.bookmarks.model.BookmarksEntry;
32  import com.liferay.portlet.bookmarks.model.BookmarksFolder;
33  import com.liferay.portlet.bookmarks.model.BookmarksFolderConstants;
34  import com.liferay.portlet.bookmarks.service.BookmarksEntryLocalServiceUtil;
35  import com.liferay.portlet.bookmarks.service.BookmarksFolderLocalServiceUtil;
36  import com.liferay.portlet.bookmarks.service.BookmarksFolderServiceUtil;
37  import com.liferay.portlet.expando.model.ExpandoBridge;
38  import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
39  
40  import java.util.ArrayList;
41  import java.util.Collection;
42  import java.util.Date;
43  import java.util.List;
44  
45  import javax.portlet.PortletURL;
46  
47  /**
48   * <a href="BookmarksIndexer.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   * @author Bruno Farache
52   * @author Raymond Augé
53   */
54  public class BookmarksIndexer extends BaseIndexer {
55  
56      public static final String[] CLASS_NAMES = {BookmarksEntry.class.getName()};
57  
58      public static final String PORTLET_ID = PortletKeys.BOOKMARKS;
59  
60      public String[] getClassNames() {
61          return CLASS_NAMES;
62      }
63  
64      public Summary getSummary(
65          Document document, String snippet, PortletURL portletURL) {
66  
67          String title = document.get(Field.TITLE);
68  
69          String url = document.get(Field.URL);
70  
71          String entryId = document.get(Field.ENTRY_CLASS_PK);
72  
73          portletURL.setParameter("struts_action", "/bookmarks/view_entry");
74          portletURL.setParameter("entryId", entryId);
75  
76          return new Summary(title, url, portletURL);
77      }
78  
79      protected void checkSearchFolderId(
80              long folderId, SearchContext searchContext)
81          throws Exception {
82  
83          BookmarksFolderServiceUtil.getFolder(folderId);
84      }
85  
86      protected void doDelete(Object obj) throws Exception {
87          BookmarksEntry entry = (BookmarksEntry)obj;
88  
89          Document document = new DocumentImpl();
90  
91          document.addUID(PORTLET_ID, entry.getEntryId());
92  
93          SearchEngineUtil.deleteDocument(
94              entry.getCompanyId(), document.get(Field.UID));
95      }
96  
97      protected Document doGetDocument(Object obj) throws Exception {
98          BookmarksEntry entry = (BookmarksEntry)obj;
99  
100         long companyId = entry.getCompanyId();
101         long groupId = getParentGroupId(entry.getGroupId());
102         long scopeGroupId = entry.getGroupId();
103         long userId = entry.getUserId();
104         long folderId = entry.getFolderId();
105         long entryId = entry.getEntryId();
106         String name = entry.getName();
107         String url = entry.getUrl();
108         String comments = entry.getComments();
109         Date modifiedDate = entry.getModifiedDate();
110 
111         long[] assetCategoryIds = AssetCategoryLocalServiceUtil.getCategoryIds(
112             BookmarksEntry.class.getName(), entryId);
113         String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(
114             BookmarksEntry.class.getName(), entryId);
115 
116         ExpandoBridge expandoBridge = entry.getExpandoBridge();
117 
118         Document document = new DocumentImpl();
119 
120         document.addUID(PORTLET_ID, entryId);
121 
122         document.addModifiedDate(modifiedDate);
123 
124         document.addKeyword(Field.COMPANY_ID, companyId);
125         document.addKeyword(Field.PORTLET_ID, PORTLET_ID);
126         document.addKeyword(Field.GROUP_ID, groupId);
127         document.addKeyword(Field.SCOPE_GROUP_ID, scopeGroupId);
128         document.addKeyword(Field.USER_ID, userId);
129 
130         document.addText(Field.TITLE, name);
131         document.addKeyword(Field.ASSET_CATEGORY_IDS, assetCategoryIds);
132         document.addKeyword(Field.ASSET_TAG_NAMES, assetTagNames);
133 
134         document.addKeyword(Field.FOLDER_ID, folderId);
135         document.addKeyword(
136             Field.ENTRY_CLASS_NAME, BookmarksEntry.class.getName());
137         document.addKeyword(Field.ENTRY_CLASS_PK, entryId);
138         document.addText(Field.URL, url);
139         document.addText(Field.COMMENTS, comments);
140 
141         ExpandoBridgeIndexerUtil.addAttributes(document, expandoBridge);
142 
143         return document;
144     }
145 
146     protected void doReindex(Object obj) throws Exception {
147         BookmarksEntry entry = (BookmarksEntry)obj;
148 
149         Document document = getDocument(entry);
150 
151         SearchEngineUtil.updateDocument(entry.getCompanyId(), document);
152     }
153 
154     protected void doReindex(String className, long classPK) throws Exception {
155         BookmarksEntry entry = BookmarksEntryLocalServiceUtil.getEntry(classPK);
156 
157         doReindex(entry);
158     }
159 
160     protected void doReindex(String[] ids) throws Exception {
161         long companyId = GetterUtil.getLong(ids[0]);
162 
163         reindexFolders(companyId);
164         reindexRoot(companyId);
165     }
166 
167     protected String getPortletId(SearchContext searchContext) {
168         return PORTLET_ID;
169     }
170 
171     protected void reindexEntries(
172             long companyId, long groupId, long folderId, int entryStart,
173             int entryEnd)
174         throws Exception {
175 
176         List<BookmarksEntry> entries =
177             BookmarksEntryLocalServiceUtil.getEntries(
178                 groupId, folderId, entryStart, entryEnd);
179 
180         if (entries.isEmpty()) {
181             return;
182         }
183 
184         Collection<Document> documents = new ArrayList<Document>();
185 
186         for (BookmarksEntry entry : entries) {
187             Document document = getDocument(entry);
188 
189             documents.add(document);
190         }
191 
192         SearchEngineUtil.updateDocuments(companyId, documents);
193     }
194 
195     protected void reindexFolders(long companyId) throws Exception {
196         int folderCount =
197             BookmarksFolderLocalServiceUtil.getCompanyFoldersCount(companyId);
198 
199         int folderPages = folderCount / Indexer.DEFAULT_INTERVAL;
200 
201         for (int i = 0; i <= folderPages; i++) {
202             int folderStart = (i * Indexer.DEFAULT_INTERVAL);
203             int folderEnd = folderStart + Indexer.DEFAULT_INTERVAL;
204 
205             reindexFolders(companyId, folderStart, folderEnd);
206         }
207     }
208 
209     protected void reindexFolders(
210             long companyId, int folderStart, int folderEnd)
211         throws Exception {
212 
213         List<BookmarksFolder> folders =
214             BookmarksFolderLocalServiceUtil.getCompanyFolders(
215                 companyId, folderStart, folderEnd);
216 
217         for (BookmarksFolder folder : folders) {
218             long groupId = folder.getGroupId();
219             long folderId = folder.getFolderId();
220 
221             int entryCount = BookmarksEntryLocalServiceUtil.getEntriesCount(
222                 groupId, folderId);
223 
224             int entryPages = entryCount / Indexer.DEFAULT_INTERVAL;
225 
226             for (int i = 0; i <= entryPages; i++) {
227                 int entryStart = (i * Indexer.DEFAULT_INTERVAL);
228                 int entryEnd = entryStart + Indexer.DEFAULT_INTERVAL;
229 
230                 reindexEntries(
231                     companyId, groupId, folderId, entryStart, entryEnd);
232             }
233         }
234     }
235 
236     protected void reindexRoot(long companyId) throws Exception {
237         int groupCount = GroupLocalServiceUtil.getCompanyGroupsCount(companyId);
238 
239         int groupPages = groupCount / Indexer.DEFAULT_INTERVAL;
240 
241         for (int i = 0; i <= groupPages; i++) {
242             int groupStart = (i * Indexer.DEFAULT_INTERVAL);
243             int groupEnd = groupStart + Indexer.DEFAULT_INTERVAL;
244 
245             reindexRoot(companyId, groupStart, groupEnd);
246         }
247     }
248 
249     protected void reindexRoot(long companyId, int groupStart, int groupEnd)
250         throws Exception {
251 
252         List<Group> groups = GroupLocalServiceUtil.getCompanyGroups(
253             companyId, groupStart, groupEnd);
254 
255         for (Group group : groups) {
256             long groupId = group.getGroupId();
257             long folderId = BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID;
258 
259             int entryCount = BookmarksEntryLocalServiceUtil.getEntriesCount(
260                 groupId, folderId);
261 
262             int entryPages = entryCount / Indexer.DEFAULT_INTERVAL;
263 
264             for (int i = 0; i <= entryPages; i++) {
265                 int entryStart = (i * Indexer.DEFAULT_INTERVAL);
266                 int entryEnd = entryStart + Indexer.DEFAULT_INTERVAL;
267 
268                 reindexEntries(
269                     companyId, groupId, folderId, entryStart, entryEnd);
270             }
271         }
272     }
273 
274 }