001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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[] assetCategoryNames =
116                            AssetCategoryLocalServiceUtil.getCategoryNames(
117                                    BookmarksEntry.class.getName(), entryId);
118                    String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(
119                            BookmarksEntry.class.getName(), entryId);
120    
121                    ExpandoBridge expandoBridge = entry.getExpandoBridge();
122    
123                    Document document = new DocumentImpl();
124    
125                    document.addUID(PORTLET_ID, entryId);
126    
127                    document.addModifiedDate(modifiedDate);
128    
129                    document.addKeyword(Field.COMPANY_ID, companyId);
130                    document.addKeyword(Field.PORTLET_ID, PORTLET_ID);
131                    document.addKeyword(Field.GROUP_ID, groupId);
132                    document.addKeyword(Field.SCOPE_GROUP_ID, scopeGroupId);
133                    document.addKeyword(Field.USER_ID, userId);
134    
135                    document.addText(Field.TITLE, name);
136                    document.addKeyword(Field.ASSET_CATEGORY_IDS, assetCategoryIds);
137                    document.addKeyword(Field.ASSET_CATEGORY_NAMES, assetCategoryNames);
138                    document.addKeyword(Field.ASSET_TAG_NAMES, assetTagNames);
139    
140                    document.addKeyword(Field.FOLDER_ID, folderId);
141                    document.addKeyword(
142                            Field.ENTRY_CLASS_NAME, BookmarksEntry.class.getName());
143                    document.addKeyword(Field.ENTRY_CLASS_PK, entryId);
144                    document.addText(Field.URL, url);
145                    document.addText(Field.COMMENTS, comments);
146    
147                    ExpandoBridgeIndexerUtil.addAttributes(document, expandoBridge);
148    
149                    return document;
150            }
151    
152            protected void doReindex(Object obj) throws Exception {
153                    BookmarksEntry entry = (BookmarksEntry)obj;
154    
155                    Document document = getDocument(entry);
156    
157                    SearchEngineUtil.updateDocument(entry.getCompanyId(), document);
158            }
159    
160            protected void doReindex(String className, long classPK) throws Exception {
161                    BookmarksEntry entry = BookmarksEntryLocalServiceUtil.getEntry(classPK);
162    
163                    doReindex(entry);
164            }
165    
166            protected void doReindex(String[] ids) throws Exception {
167                    long companyId = GetterUtil.getLong(ids[0]);
168    
169                    reindexFolders(companyId);
170                    reindexRoot(companyId);
171            }
172    
173            protected String getPortletId(SearchContext searchContext) {
174                    return PORTLET_ID;
175            }
176    
177            protected void reindexEntries(
178                            long companyId, long groupId, long folderId, int entryStart,
179                            int entryEnd)
180                    throws Exception {
181    
182                    List<BookmarksEntry> entries =
183                            BookmarksEntryLocalServiceUtil.getEntries(
184                                    groupId, folderId, entryStart, entryEnd);
185    
186                    if (entries.isEmpty()) {
187                            return;
188                    }
189    
190                    Collection<Document> documents = new ArrayList<Document>();
191    
192                    for (BookmarksEntry entry : entries) {
193                            Document document = getDocument(entry);
194    
195                            documents.add(document);
196                    }
197    
198                    SearchEngineUtil.updateDocuments(companyId, documents);
199            }
200    
201            protected void reindexFolders(long companyId) throws Exception {
202                    int folderCount =
203                            BookmarksFolderLocalServiceUtil.getCompanyFoldersCount(companyId);
204    
205                    int folderPages = folderCount / Indexer.DEFAULT_INTERVAL;
206    
207                    for (int i = 0; i <= folderPages; i++) {
208                            int folderStart = (i * Indexer.DEFAULT_INTERVAL);
209                            int folderEnd = folderStart + Indexer.DEFAULT_INTERVAL;
210    
211                            reindexFolders(companyId, folderStart, folderEnd);
212                    }
213            }
214    
215            protected void reindexFolders(
216                            long companyId, int folderStart, int folderEnd)
217                    throws Exception {
218    
219                    List<BookmarksFolder> folders =
220                            BookmarksFolderLocalServiceUtil.getCompanyFolders(
221                                    companyId, folderStart, folderEnd);
222    
223                    for (BookmarksFolder folder : folders) {
224                            long groupId = folder.getGroupId();
225                            long folderId = folder.getFolderId();
226    
227                            int entryCount = BookmarksEntryLocalServiceUtil.getEntriesCount(
228                                    groupId, folderId);
229    
230                            int entryPages = entryCount / Indexer.DEFAULT_INTERVAL;
231    
232                            for (int i = 0; i <= entryPages; i++) {
233                                    int entryStart = (i * Indexer.DEFAULT_INTERVAL);
234                                    int entryEnd = entryStart + Indexer.DEFAULT_INTERVAL;
235    
236                                    reindexEntries(
237                                            companyId, groupId, folderId, entryStart, entryEnd);
238                            }
239                    }
240            }
241    
242            protected void reindexRoot(long companyId) throws Exception {
243                    int groupCount = GroupLocalServiceUtil.getCompanyGroupsCount(companyId);
244    
245                    int groupPages = groupCount / Indexer.DEFAULT_INTERVAL;
246    
247                    for (int i = 0; i <= groupPages; i++) {
248                            int groupStart = (i * Indexer.DEFAULT_INTERVAL);
249                            int groupEnd = groupStart + Indexer.DEFAULT_INTERVAL;
250    
251                            reindexRoot(companyId, groupStart, groupEnd);
252                    }
253            }
254    
255            protected void reindexRoot(long companyId, int groupStart, int groupEnd)
256                    throws Exception {
257    
258                    List<Group> groups = GroupLocalServiceUtil.getCompanyGroups(
259                            companyId, groupStart, groupEnd);
260    
261                    for (Group group : groups) {
262                            long groupId = group.getGroupId();
263                            long folderId = BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID;
264    
265                            int entryCount = BookmarksEntryLocalServiceUtil.getEntriesCount(
266                                    groupId, folderId);
267    
268                            int entryPages = entryCount / Indexer.DEFAULT_INTERVAL;
269    
270                            for (int i = 0; i <= entryPages; i++) {
271                                    int entryStart = (i * Indexer.DEFAULT_INTERVAL);
272                                    int entryEnd = entryStart + Indexer.DEFAULT_INTERVAL;
273    
274                                    reindexEntries(
275                                            companyId, groupId, folderId, entryStart, entryEnd);
276                            }
277                    }
278            }
279    
280    }