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