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                    BookmarksFolderServiceUtil.getFolder(folderId);
082            }
083    
084            protected void doDelete(Object obj) throws Exception {
085                    BookmarksEntry entry = (BookmarksEntry)obj;
086    
087                    Document document = new DocumentImpl();
088    
089                    document.addUID(PORTLET_ID, entry.getEntryId());
090    
091                    SearchEngineUtil.deleteDocument(
092                            entry.getCompanyId(), document.get(Field.UID));
093            }
094    
095            protected Document doGetDocument(Object obj) throws Exception {
096                    BookmarksEntry entry = (BookmarksEntry)obj;
097    
098                    long companyId = entry.getCompanyId();
099                    long groupId = getParentGroupId(entry.getGroupId());
100                    long scopeGroupId = entry.getGroupId();
101                    long userId = entry.getUserId();
102                    long folderId = entry.getFolderId();
103                    long entryId = entry.getEntryId();
104                    String name = entry.getName();
105                    String url = entry.getUrl();
106                    String comments = entry.getComments();
107                    Date modifiedDate = entry.getModifiedDate();
108    
109                    long[] assetCategoryIds = AssetCategoryLocalServiceUtil.getCategoryIds(
110                            BookmarksEntry.class.getName(), entryId);
111                    String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(
112                            BookmarksEntry.class.getName(), entryId);
113    
114                    ExpandoBridge expandoBridge = entry.getExpandoBridge();
115    
116                    Document document = new DocumentImpl();
117    
118                    document.addUID(PORTLET_ID, entryId);
119    
120                    document.addModifiedDate(modifiedDate);
121    
122                    document.addKeyword(Field.COMPANY_ID, companyId);
123                    document.addKeyword(Field.PORTLET_ID, PORTLET_ID);
124                    document.addKeyword(Field.GROUP_ID, groupId);
125                    document.addKeyword(Field.SCOPE_GROUP_ID, scopeGroupId);
126                    document.addKeyword(Field.USER_ID, userId);
127    
128                    document.addText(Field.TITLE, name);
129                    document.addKeyword(Field.ASSET_CATEGORY_IDS, assetCategoryIds);
130                    document.addKeyword(Field.ASSET_TAG_NAMES, assetTagNames);
131    
132                    document.addKeyword(Field.FOLDER_ID, folderId);
133                    document.addKeyword(
134                            Field.ENTRY_CLASS_NAME, BookmarksEntry.class.getName());
135                    document.addKeyword(Field.ENTRY_CLASS_PK, entryId);
136                    document.addText(Field.URL, url);
137                    document.addText(Field.COMMENTS, comments);
138    
139                    ExpandoBridgeIndexerUtil.addAttributes(document, expandoBridge);
140    
141                    return document;
142            }
143    
144            protected void doReindex(Object obj) throws Exception {
145                    BookmarksEntry entry = (BookmarksEntry)obj;
146    
147                    Document document = getDocument(entry);
148    
149                    SearchEngineUtil.updateDocument(entry.getCompanyId(), document);
150            }
151    
152            protected void doReindex(String className, long classPK) throws Exception {
153                    BookmarksEntry entry = BookmarksEntryLocalServiceUtil.getEntry(classPK);
154    
155                    doReindex(entry);
156            }
157    
158            protected void doReindex(String[] ids) throws Exception {
159                    long companyId = GetterUtil.getLong(ids[0]);
160    
161                    reindexFolders(companyId);
162                    reindexRoot(companyId);
163            }
164    
165            protected String getPortletId(SearchContext searchContext) {
166                    return PORTLET_ID;
167            }
168    
169            protected void reindexEntries(
170                            long companyId, long groupId, long folderId, int entryStart,
171                            int entryEnd)
172                    throws Exception {
173    
174                    List<BookmarksEntry> entries =
175                            BookmarksEntryLocalServiceUtil.getEntries(
176                                    groupId, folderId, entryStart, entryEnd);
177    
178                    if (entries.isEmpty()) {
179                            return;
180                    }
181    
182                    Collection<Document> documents = new ArrayList<Document>();
183    
184                    for (BookmarksEntry entry : entries) {
185                            Document document = getDocument(entry);
186    
187                            documents.add(document);
188                    }
189    
190                    SearchEngineUtil.updateDocuments(companyId, documents);
191            }
192    
193            protected void reindexFolders(long companyId) throws Exception {
194                    int folderCount =
195                            BookmarksFolderLocalServiceUtil.getCompanyFoldersCount(companyId);
196    
197                    int folderPages = folderCount / Indexer.DEFAULT_INTERVAL;
198    
199                    for (int i = 0; i <= folderPages; i++) {
200                            int folderStart = (i * Indexer.DEFAULT_INTERVAL);
201                            int folderEnd = folderStart + Indexer.DEFAULT_INTERVAL;
202    
203                            reindexFolders(companyId, folderStart, folderEnd);
204                    }
205            }
206    
207            protected void reindexFolders(
208                            long companyId, int folderStart, int folderEnd)
209                    throws Exception {
210    
211                    List<BookmarksFolder> folders =
212                            BookmarksFolderLocalServiceUtil.getCompanyFolders(
213                                    companyId, folderStart, folderEnd);
214    
215                    for (BookmarksFolder folder : folders) {
216                            long groupId = folder.getGroupId();
217                            long folderId = folder.getFolderId();
218    
219                            int entryCount = BookmarksEntryLocalServiceUtil.getEntriesCount(
220                                    groupId, folderId);
221    
222                            int entryPages = entryCount / Indexer.DEFAULT_INTERVAL;
223    
224                            for (int i = 0; i <= entryPages; i++) {
225                                    int entryStart = (i * Indexer.DEFAULT_INTERVAL);
226                                    int entryEnd = entryStart + Indexer.DEFAULT_INTERVAL;
227    
228                                    reindexEntries(
229                                            companyId, groupId, folderId, entryStart, entryEnd);
230                            }
231                    }
232            }
233    
234            protected void reindexRoot(long companyId) throws Exception {
235                    int groupCount = GroupLocalServiceUtil.getCompanyGroupsCount(companyId);
236    
237                    int groupPages = groupCount / Indexer.DEFAULT_INTERVAL;
238    
239                    for (int i = 0; i <= groupPages; i++) {
240                            int groupStart = (i * Indexer.DEFAULT_INTERVAL);
241                            int groupEnd = groupStart + Indexer.DEFAULT_INTERVAL;
242    
243                            reindexRoot(companyId, groupStart, groupEnd);
244                    }
245            }
246    
247            protected void reindexRoot(long companyId, int groupStart, int groupEnd)
248                    throws Exception {
249    
250                    List<Group> groups = GroupLocalServiceUtil.getCompanyGroups(
251                            companyId, groupStart, groupEnd);
252    
253                    for (Group group : groups) {
254                            long groupId = group.getGroupId();
255                            long folderId = BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID;
256    
257                            int entryCount = BookmarksEntryLocalServiceUtil.getEntriesCount(
258                                    groupId, folderId);
259    
260                            int entryPages = entryCount / Indexer.DEFAULT_INTERVAL;
261    
262                            for (int i = 0; i <= entryPages; i++) {
263                                    int entryStart = (i * Indexer.DEFAULT_INTERVAL);
264                                    int entryEnd = entryStart + Indexer.DEFAULT_INTERVAL;
265    
266                                    reindexEntries(
267                                            companyId, groupId, folderId, entryStart, entryEnd);
268                            }
269                    }
270            }
271    
272    }