001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.bookmarks.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.model.Group;
027    import com.liferay.portal.service.GroupLocalServiceUtil;
028    import com.liferay.portal.util.PortletKeys;
029    import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
030    import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
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    import com.liferay.portlet.expando.model.ExpandoBridge;
038    import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
039    
040    import java.util.ArrayList;
041    import java.util.Collection;
042    import java.util.Date;
043    import java.util.List;
044    
045    import javax.portlet.PortletURL;
046    
047    /**
048     * @author Brian Wing Shun Chan
049     * @author Bruno Farache
050     * @author Raymond Augé
051     */
052    public class BookmarksIndexer extends BaseIndexer {
053    
054            public static final String[] CLASS_NAMES = {BookmarksEntry.class.getName()};
055    
056            public static final String PORTLET_ID = PortletKeys.BOOKMARKS;
057    
058            public String[] getClassNames() {
059                    return CLASS_NAMES;
060            }
061    
062            public Summary getSummary(
063                    Document document, String snippet, PortletURL portletURL) {
064    
065                    String title = document.get(Field.TITLE);
066    
067                    String url = document.get(Field.URL);
068    
069                    String entryId = document.get(Field.ENTRY_CLASS_PK);
070    
071                    portletURL.setParameter("struts_action", "/bookmarks/view_entry");
072                    portletURL.setParameter("entryId", entryId);
073    
074                    return new Summary(title, url, portletURL);
075            }
076    
077            protected void checkSearchFolderId(
078                            long folderId, SearchContext searchContext)
079                    throws Exception {
080    
081                    if (folderId == BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
082                            return;
083                    }
084    
085                    BookmarksFolderServiceUtil.getFolder(folderId);
086            }
087    
088            protected void doDelete(Object obj) throws Exception {
089                    BookmarksEntry entry = (BookmarksEntry)obj;
090    
091                    Document document = new DocumentImpl();
092    
093                    document.addUID(PORTLET_ID, entry.getEntryId());
094    
095                    SearchEngineUtil.deleteDocument(
096                            entry.getCompanyId(), document.get(Field.UID));
097            }
098    
099            protected Document doGetDocument(Object obj) throws Exception {
100                    BookmarksEntry entry = (BookmarksEntry)obj;
101    
102                    long companyId = entry.getCompanyId();
103                    long groupId = getParentGroupId(entry.getGroupId());
104                    long scopeGroupId = entry.getGroupId();
105                    long userId = entry.getUserId();
106                    long folderId = entry.getFolderId();
107                    long entryId = entry.getEntryId();
108                    String name = entry.getName();
109                    String url = entry.getUrl();
110                    String comments = entry.getComments();
111                    Date modifiedDate = entry.getModifiedDate();
112    
113                    long[] assetCategoryIds = AssetCategoryLocalServiceUtil.getCategoryIds(
114                            BookmarksEntry.class.getName(), entryId);
115                    String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(
116                            BookmarksEntry.class.getName(), entryId);
117    
118                    ExpandoBridge expandoBridge = entry.getExpandoBridge();
119    
120                    Document document = new DocumentImpl();
121    
122                    document.addUID(PORTLET_ID, entryId);
123    
124                    document.addModifiedDate(modifiedDate);
125    
126                    document.addKeyword(Field.COMPANY_ID, companyId);
127                    document.addKeyword(Field.PORTLET_ID, PORTLET_ID);
128                    document.addKeyword(Field.GROUP_ID, groupId);
129                    document.addKeyword(Field.SCOPE_GROUP_ID, scopeGroupId);
130                    document.addKeyword(Field.USER_ID, userId);
131    
132                    document.addText(Field.TITLE, name);
133                    document.addKeyword(Field.ASSET_CATEGORY_IDS, assetCategoryIds);
134                    document.addKeyword(Field.ASSET_TAG_NAMES, assetTagNames);
135    
136                    document.addKeyword(Field.FOLDER_ID, folderId);
137                    document.addKeyword(
138                            Field.ENTRY_CLASS_NAME, BookmarksEntry.class.getName());
139                    document.addKeyword(Field.ENTRY_CLASS_PK, entryId);
140                    document.addText(Field.URL, url);
141                    document.addText(Field.COMMENTS, comments);
142    
143                    ExpandoBridgeIndexerUtil.addAttributes(document, expandoBridge);
144    
145                    return document;
146            }
147    
148            protected void doReindex(Object obj) throws Exception {
149                    BookmarksEntry entry = (BookmarksEntry)obj;
150    
151                    Document document = getDocument(entry);
152    
153                    SearchEngineUtil.updateDocument(entry.getCompanyId(), document);
154            }
155    
156            protected void doReindex(String className, long classPK) throws Exception {
157                    BookmarksEntry entry = BookmarksEntryLocalServiceUtil.getEntry(classPK);
158    
159                    doReindex(entry);
160            }
161    
162            protected void doReindex(String[] ids) throws Exception {
163                    long companyId = GetterUtil.getLong(ids[0]);
164    
165                    reindexFolders(companyId);
166                    reindexRoot(companyId);
167            }
168    
169            protected String getPortletId(SearchContext searchContext) {
170                    return PORTLET_ID;
171            }
172    
173            protected void reindexEntries(
174                            long companyId, long groupId, long folderId, int entryStart,
175                            int entryEnd)
176                    throws Exception {
177    
178                    List<BookmarksEntry> entries =
179                            BookmarksEntryLocalServiceUtil.getEntries(
180                                    groupId, folderId, entryStart, entryEnd);
181    
182                    if (entries.isEmpty()) {
183                            return;
184                    }
185    
186                    Collection<Document> documents = new ArrayList<Document>();
187    
188                    for (BookmarksEntry entry : entries) {
189                            Document document = getDocument(entry);
190    
191                            documents.add(document);
192                    }
193    
194                    SearchEngineUtil.updateDocuments(companyId, documents);
195            }
196    
197            protected void reindexFolders(long companyId) throws Exception {
198                    int folderCount =
199                            BookmarksFolderLocalServiceUtil.getCompanyFoldersCount(companyId);
200    
201                    int folderPages = folderCount / Indexer.DEFAULT_INTERVAL;
202    
203                    for (int i = 0; i <= folderPages; i++) {
204                            int folderStart = (i * Indexer.DEFAULT_INTERVAL);
205                            int folderEnd = folderStart + Indexer.DEFAULT_INTERVAL;
206    
207                            reindexFolders(companyId, folderStart, folderEnd);
208                    }
209            }
210    
211            protected void reindexFolders(
212                            long companyId, int folderStart, int folderEnd)
213                    throws Exception {
214    
215                    List<BookmarksFolder> folders =
216                            BookmarksFolderLocalServiceUtil.getCompanyFolders(
217                                    companyId, folderStart, folderEnd);
218    
219                    for (BookmarksFolder folder : folders) {
220                            long groupId = folder.getGroupId();
221                            long folderId = folder.getFolderId();
222    
223                            int entryCount = BookmarksEntryLocalServiceUtil.getEntriesCount(
224                                    groupId, folderId);
225    
226                            int entryPages = entryCount / Indexer.DEFAULT_INTERVAL;
227    
228                            for (int i = 0; i <= entryPages; i++) {
229                                    int entryStart = (i * Indexer.DEFAULT_INTERVAL);
230                                    int entryEnd = entryStart + Indexer.DEFAULT_INTERVAL;
231    
232                                    reindexEntries(
233                                            companyId, groupId, folderId, entryStart, entryEnd);
234                            }
235                    }
236            }
237    
238            protected void reindexRoot(long companyId) throws Exception {
239                    int groupCount = GroupLocalServiceUtil.getCompanyGroupsCount(companyId);
240    
241                    int groupPages = groupCount / Indexer.DEFAULT_INTERVAL;
242    
243                    for (int i = 0; i <= groupPages; i++) {
244                            int groupStart = (i * Indexer.DEFAULT_INTERVAL);
245                            int groupEnd = groupStart + Indexer.DEFAULT_INTERVAL;
246    
247                            reindexRoot(companyId, groupStart, groupEnd);
248                    }
249            }
250    
251            protected void reindexRoot(long companyId, int groupStart, int groupEnd)
252                    throws Exception {
253    
254                    List<Group> groups = GroupLocalServiceUtil.getCompanyGroups(
255                            companyId, groupStart, groupEnd);
256    
257                    for (Group group : groups) {
258                            long groupId = group.getGroupId();
259                            long folderId = BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID;
260    
261                            int entryCount = BookmarksEntryLocalServiceUtil.getEntriesCount(
262                                    groupId, folderId);
263    
264                            int entryPages = entryCount / Indexer.DEFAULT_INTERVAL;
265    
266                            for (int i = 0; i <= entryPages; i++) {
267                                    int entryStart = (i * Indexer.DEFAULT_INTERVAL);
268                                    int entryEnd = entryStart + Indexer.DEFAULT_INTERVAL;
269    
270                                    reindexEntries(
271                                            companyId, groupId, folderId, entryStart, entryEnd);
272                            }
273                    }
274            }
275    
276    }