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.Document;
18  import com.liferay.portal.kernel.search.DocumentImpl;
19  import com.liferay.portal.kernel.search.Field;
20  import com.liferay.portal.kernel.search.Indexer;
21  import com.liferay.portal.kernel.search.SearchContext;
22  import com.liferay.portal.kernel.search.SearchEngineUtil;
23  import com.liferay.portal.kernel.search.Summary;
24  import com.liferay.portal.kernel.util.GetterUtil;
25  import com.liferay.portal.model.Group;
26  import com.liferay.portal.search.BaseIndexer;
27  import com.liferay.portal.service.GroupLocalServiceUtil;
28  import com.liferay.portal.util.PortletKeys;
29  import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
30  import com.liferay.portlet.bookmarks.model.BookmarksEntry;
31  import com.liferay.portlet.bookmarks.model.BookmarksFolder;
32  import com.liferay.portlet.bookmarks.model.BookmarksFolderConstants;
33  import com.liferay.portlet.bookmarks.service.BookmarksEntryLocalServiceUtil;
34  import com.liferay.portlet.bookmarks.service.BookmarksFolderLocalServiceUtil;
35  import com.liferay.portlet.bookmarks.service.BookmarksFolderServiceUtil;
36  import com.liferay.portlet.expando.model.ExpandoBridge;
37  import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
38  
39  import java.util.Date;
40  import java.util.List;
41  
42  import javax.portlet.PortletURL;
43  
44  /**
45   * <a href="BookmarksIndexer.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   * @author Bruno Farache
49   * @author Raymond Augé
50   */
51  public class BookmarksIndexer extends BaseIndexer {
52  
53      public static final String[] CLASS_NAMES = {BookmarksEntry.class.getName()};
54  
55      public static final String PORTLET_ID = PortletKeys.BOOKMARKS;
56  
57      public String[] getClassNames() {
58          return CLASS_NAMES;
59      }
60  
61      public Summary getSummary(
62          Document document, String snippet, PortletURL portletURL) {
63  
64          String title = document.get(Field.TITLE);
65  
66          String url = document.get(Field.URL);
67  
68          String entryId = document.get(Field.ENTRY_CLASS_PK);
69  
70          portletURL.setParameter("struts_action", "/bookmarks/view_entry");
71          portletURL.setParameter("entryId", entryId);
72  
73          return new Summary(title, url, portletURL);
74      }
75  
76      protected void checkSearchFolderId(
77              long folderId, SearchContext searchContext)
78          throws Exception {
79  
80          BookmarksFolderServiceUtil.getFolder(folderId);
81      }
82  
83      protected void doDelete(Object obj) throws Exception {
84          BookmarksEntry entry = (BookmarksEntry)obj;
85  
86          Document document = new DocumentImpl();
87  
88          document.addUID(PORTLET_ID, entry.getEntryId());
89  
90          SearchEngineUtil.deleteDocument(
91              entry.getCompanyId(), document.get(Field.UID));
92      }
93  
94      protected Document doGetDocument(Object obj) throws Exception {
95          BookmarksEntry entry = (BookmarksEntry)obj;
96  
97          long companyId = entry.getCompanyId();
98          long groupId = getParentGroupId(entry.getGroupId());
99          long scopeGroupId = entry.getGroupId();
100         long folderId = entry.getFolderId();
101         long entryId = entry.getEntryId();
102         String name = entry.getName();
103         String url = entry.getUrl();
104         String comments = entry.getComments();
105         Date modifiedDate = entry.getModifiedDate();
106 
107         String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(
108             BookmarksEntry.class.getName(), entryId);
109 
110         ExpandoBridge expandoBridge = entry.getExpandoBridge();
111 
112         Document document = new DocumentImpl();
113 
114         document.addUID(PORTLET_ID, entryId);
115 
116         document.addModifiedDate(modifiedDate);
117 
118         document.addKeyword(Field.COMPANY_ID, companyId);
119         document.addKeyword(Field.PORTLET_ID, PORTLET_ID);
120         document.addKeyword(Field.GROUP_ID, groupId);
121         document.addKeyword(Field.SCOPE_GROUP_ID, scopeGroupId);
122 
123         document.addText(Field.TITLE, name);
124         document.addKeyword(Field.ASSET_TAG_NAMES, assetTagNames);
125 
126         document.addKeyword(Field.FOLDER_ID, folderId);
127         document.addKeyword(
128             Field.ENTRY_CLASS_NAME, BookmarksEntry.class.getName());
129         document.addKeyword(Field.ENTRY_CLASS_PK, entryId);
130         document.addText(Field.URL, url);
131         document.addText(Field.COMMENTS, comments);
132 
133         ExpandoBridgeIndexerUtil.addAttributes(document, expandoBridge);
134 
135         return document;
136     }
137 
138     protected void doReindex(Object obj) throws Exception {
139         BookmarksEntry entry = (BookmarksEntry)obj;
140 
141         Document document = getDocument(entry);
142 
143         SearchEngineUtil.updateDocument(
144             entry.getCompanyId(), document.get(Field.UID), document);
145     }
146 
147     protected void doReindex(String className, long classPK) throws Exception {
148         BookmarksEntry entry = BookmarksEntryLocalServiceUtil.getEntry(classPK);
149 
150         doReindex(entry);
151     }
152 
153     protected void doReindex(String[] ids) throws Exception {
154         long companyId = GetterUtil.getLong(ids[0]);
155 
156         reindexFolders(companyId);
157         reindexRoot(companyId);
158     }
159 
160     protected String getPortletId(SearchContext searchContext) {
161         return PORTLET_ID;
162     }
163 
164     protected void reindexEntries(
165             long groupId, long folderId, int entryStart, int entryEnd)
166         throws Exception {
167 
168         List<BookmarksEntry> entries =
169             BookmarksEntryLocalServiceUtil.getEntries(
170                 groupId, folderId, entryStart, entryEnd);
171 
172         for (BookmarksEntry entry : entries) {
173             reindex(entry);
174         }
175     }
176 
177     protected void reindexFolders(long companyId) throws Exception {
178         int folderCount =
179             BookmarksFolderLocalServiceUtil.getCompanyFoldersCount(companyId);
180 
181         int folderPages = folderCount / Indexer.DEFAULT_INTERVAL;
182 
183         for (int i = 0; i <= folderPages; i++) {
184             int folderStart = (i * Indexer.DEFAULT_INTERVAL);
185             int folderEnd = folderStart + Indexer.DEFAULT_INTERVAL;
186 
187             reindexFolders(companyId, folderStart, folderEnd);
188         }
189     }
190 
191     protected void reindexFolders(
192             long companyId, int folderStart, int folderEnd)
193         throws Exception {
194 
195         List<BookmarksFolder> folders =
196             BookmarksFolderLocalServiceUtil.getCompanyFolders(
197                 companyId, folderStart, folderEnd);
198 
199         for (BookmarksFolder folder : folders) {
200             long groupId = folder.getGroupId();
201             long folderId = folder.getFolderId();
202 
203             int entryCount = BookmarksEntryLocalServiceUtil.getEntriesCount(
204                 groupId, folderId);
205 
206             int entryPages = entryCount / Indexer.DEFAULT_INTERVAL;
207 
208             for (int i = 0; i <= entryPages; i++) {
209                 int entryStart = (i * Indexer.DEFAULT_INTERVAL);
210                 int entryEnd = entryStart + Indexer.DEFAULT_INTERVAL;
211 
212                 reindexEntries(groupId, folderId, entryStart, entryEnd);
213             }
214         }
215     }
216 
217     protected void reindexRoot(long companyId) throws Exception {
218         int groupCount = GroupLocalServiceUtil.getCompanyGroupsCount(companyId);
219 
220         int groupPages = groupCount / Indexer.DEFAULT_INTERVAL;
221 
222         for (int i = 0; i <= groupPages; i++) {
223             int groupStart = (i * Indexer.DEFAULT_INTERVAL);
224             int groupEnd = groupStart + Indexer.DEFAULT_INTERVAL;
225 
226             reindexRoot(companyId, groupStart, groupEnd);
227         }
228     }
229 
230     protected void reindexRoot(long companyId, int groupStart, int groupEnd)
231         throws Exception {
232 
233         List<Group> groups = GroupLocalServiceUtil.getCompanyGroups(
234             companyId, groupStart, groupEnd);
235 
236         for (Group group : groups) {
237             long groupId = group.getGroupId();
238             long folderId = BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID;
239 
240             int entryCount = BookmarksEntryLocalServiceUtil.getEntriesCount(
241                 groupId, folderId);
242 
243             int entryPages = entryCount / Indexer.DEFAULT_INTERVAL;
244 
245             for (int i = 0; i <= entryPages; i++) {
246                 int entryStart = (i * Indexer.DEFAULT_INTERVAL);
247                 int entryEnd = entryStart + Indexer.DEFAULT_INTERVAL;
248 
249                 reindexEntries(groupId, folderId, entryStart, entryEnd);
250             }
251         }
252     }
253 
254 }