001    /**
002     * Copyright (c) 2000-2012 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.BooleanClauseOccur;
019    import com.liferay.portal.kernel.search.BooleanQuery;
020    import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
021    import com.liferay.portal.kernel.search.Document;
022    import com.liferay.portal.kernel.search.Field;
023    import com.liferay.portal.kernel.search.Indexer;
024    import com.liferay.portal.kernel.search.SearchContext;
025    import com.liferay.portal.kernel.search.SearchEngineUtil;
026    import com.liferay.portal.kernel.search.Summary;
027    import com.liferay.portal.kernel.util.GetterUtil;
028    import com.liferay.portal.model.Group;
029    import com.liferay.portal.service.GroupLocalServiceUtil;
030    import com.liferay.portal.util.PortletKeys;
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    
038    import java.util.ArrayList;
039    import java.util.Collection;
040    import java.util.List;
041    import java.util.Locale;
042    
043    import javax.portlet.PortletURL;
044    
045    /**
046     * @author Brian Wing Shun Chan
047     * @author Bruno Farache
048     * @author Raymond Augé
049     */
050    public class BookmarksIndexer extends BaseIndexer {
051    
052            public static final String[] CLASS_NAMES = {BookmarksEntry.class.getName()};
053    
054            public static final String PORTLET_ID = PortletKeys.BOOKMARKS;
055    
056            public String[] getClassNames() {
057                    return CLASS_NAMES;
058            }
059    
060            public String getPortletId() {
061                    return PORTLET_ID;
062            }
063    
064            @Override
065            public boolean isPermissionAware() {
066                    return _PERMISSION_AWARE;
067            }
068    
069            @Override
070            public void postProcessContextQuery(
071                            BooleanQuery contextQuery, SearchContext searchContext)
072                    throws Exception {
073    
074                    long[] folderIds = searchContext.getFolderIds();
075    
076                    if ((folderIds != null) && (folderIds.length > 0)) {
077                            if (folderIds[0] ==
078                                            BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
079    
080                                    return;
081                            }
082    
083                            BooleanQuery folderIdsQuery = BooleanQueryFactoryUtil.create(
084                                    searchContext);
085    
086                            for (long folderId : folderIds) {
087                                    try {
088                                            BookmarksFolderServiceUtil.getFolder(folderId);
089                                    }
090                                    catch (Exception e) {
091                                            continue;
092                                    }
093    
094                                    folderIdsQuery.addTerm(Field.FOLDER_ID, folderId);
095                            }
096    
097                            contextQuery.add(folderIdsQuery, BooleanClauseOccur.MUST);
098                    }
099            }
100    
101            @Override
102            protected void doDelete(Object obj) throws Exception {
103                    BookmarksEntry entry = (BookmarksEntry)obj;
104    
105                    deleteDocument(entry.getCompanyId(), entry.getEntryId());
106            }
107    
108            @Override
109            protected Document doGetDocument(Object obj) throws Exception {
110                    BookmarksEntry entry = (BookmarksEntry)obj;
111    
112                    Document document = getBaseModelDocument(PORTLET_ID, entry);
113    
114                    document.addText(Field.DESCRIPTION, entry.getDescription());
115                    document.addKeyword(Field.FOLDER_ID, entry.getFolderId());
116                    document.addText(Field.TITLE, entry.getName());
117                    document.addText(Field.URL, entry.getUrl());
118    
119                    return document;
120            }
121    
122            @Override
123            protected Summary doGetSummary(
124                    Document document, Locale locale, String snippet,
125                    PortletURL portletURL) {
126    
127                    String title = document.get(Field.TITLE);
128    
129                    String url = document.get(Field.URL);
130    
131                    String entryId = document.get(Field.ENTRY_CLASS_PK);
132    
133                    portletURL.setParameter("struts_action", "/bookmarks/view_entry");
134                    portletURL.setParameter("entryId", entryId);
135    
136                    return new Summary(title, url, portletURL);
137            }
138    
139            @Override
140            protected void doReindex(Object obj) throws Exception {
141                    BookmarksEntry entry = (BookmarksEntry)obj;
142    
143                    Document document = getDocument(entry);
144    
145                    SearchEngineUtil.updateDocument(entry.getCompanyId(), document);
146            }
147    
148            @Override
149            protected void doReindex(String className, long classPK) throws Exception {
150                    BookmarksEntry entry = BookmarksEntryLocalServiceUtil.getEntry(classPK);
151    
152                    doReindex(entry);
153            }
154    
155            @Override
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            @Override
164            protected String getPortletId(SearchContext searchContext) {
165                    return PORTLET_ID;
166            }
167    
168            protected void reindexEntries(
169                            long companyId, long groupId, long folderId, int entryStart,
170                            int entryEnd)
171                    throws Exception {
172    
173                    List<BookmarksEntry> entries =
174                            BookmarksEntryLocalServiceUtil.getEntries(
175                                    groupId, folderId, entryStart, entryEnd);
176    
177                    if (entries.isEmpty()) {
178                            return;
179                    }
180    
181                    Collection<Document> documents = new ArrayList<Document>();
182    
183                    for (BookmarksEntry entry : entries) {
184                            Document document = getDocument(entry);
185    
186                            documents.add(document);
187                    }
188    
189                    SearchEngineUtil.updateDocuments(companyId, documents);
190            }
191    
192            protected void reindexFolders(long companyId) throws Exception {
193                    int folderCount =
194                            BookmarksFolderLocalServiceUtil.getCompanyFoldersCount(companyId);
195    
196                    int folderPages = folderCount / Indexer.DEFAULT_INTERVAL;
197    
198                    for (int i = 0; i <= folderPages; i++) {
199                            int folderStart = (i * Indexer.DEFAULT_INTERVAL);
200                            int folderEnd = folderStart + Indexer.DEFAULT_INTERVAL;
201    
202                            reindexFolders(companyId, folderStart, folderEnd);
203                    }
204            }
205    
206            protected void reindexFolders(
207                            long companyId, int folderStart, int folderEnd)
208                    throws Exception {
209    
210                    List<BookmarksFolder> folders =
211                            BookmarksFolderLocalServiceUtil.getCompanyFolders(
212                                    companyId, folderStart, folderEnd);
213    
214                    for (BookmarksFolder folder : folders) {
215                            long groupId = folder.getGroupId();
216                            long folderId = folder.getFolderId();
217    
218                            int entryCount = BookmarksEntryLocalServiceUtil.getEntriesCount(
219                                    groupId, folderId);
220    
221                            int entryPages = entryCount / Indexer.DEFAULT_INTERVAL;
222    
223                            for (int i = 0; i <= entryPages; i++) {
224                                    int entryStart = (i * Indexer.DEFAULT_INTERVAL);
225                                    int entryEnd = entryStart + Indexer.DEFAULT_INTERVAL;
226    
227                                    reindexEntries(
228                                            companyId, groupId, folderId, entryStart, entryEnd);
229                            }
230                    }
231            }
232    
233            protected void reindexRoot(long companyId) throws Exception {
234                    int groupCount = GroupLocalServiceUtil.getCompanyGroupsCount(companyId);
235    
236                    int groupPages = groupCount / Indexer.DEFAULT_INTERVAL;
237    
238                    for (int i = 0; i <= groupPages; i++) {
239                            int groupStart = (i * Indexer.DEFAULT_INTERVAL);
240                            int groupEnd = groupStart + Indexer.DEFAULT_INTERVAL;
241    
242                            reindexRoot(companyId, groupStart, groupEnd);
243                    }
244            }
245    
246            protected void reindexRoot(long companyId, int groupStart, int groupEnd)
247                    throws Exception {
248    
249                    List<Group> groups = GroupLocalServiceUtil.getCompanyGroups(
250                            companyId, groupStart, groupEnd);
251    
252                    for (Group group : groups) {
253                            long groupId = group.getGroupId();
254                            long folderId = BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID;
255    
256                            int entryCount = BookmarksEntryLocalServiceUtil.getEntriesCount(
257                                    groupId, folderId);
258    
259                            int entryPages = entryCount / Indexer.DEFAULT_INTERVAL;
260    
261                            for (int i = 0; i <= entryPages; i++) {
262                                    int entryStart = (i * Indexer.DEFAULT_INTERVAL);
263                                    int entryEnd = entryStart + Indexer.DEFAULT_INTERVAL;
264    
265                                    reindexEntries(
266                                            companyId, groupId, folderId, entryStart, entryEnd);
267                            }
268                    }
269            }
270    
271            private static final boolean _PERMISSION_AWARE = true;
272    
273    }