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.documentlibrary.util;
016    
017    import com.liferay.documentlibrary.model.FileModel;
018    import com.liferay.portal.kernel.portlet.LiferayPortletURL;
019    import com.liferay.portal.kernel.portlet.LiferayWindowState;
020    import com.liferay.portal.kernel.search.BaseIndexer;
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.IndexerRegistryUtil;
025    import com.liferay.portal.kernel.search.SearchContext;
026    import com.liferay.portal.kernel.search.SearchEngineUtil;
027    import com.liferay.portal.kernel.search.Summary;
028    import com.liferay.portal.kernel.util.GetterUtil;
029    import com.liferay.portal.kernel.util.StringUtil;
030    import com.liferay.portal.kernel.util.Validator;
031    import com.liferay.portal.model.Group;
032    import com.liferay.portal.security.permission.ActionKeys;
033    import com.liferay.portal.security.permission.PermissionChecker;
034    import com.liferay.portal.service.GroupLocalServiceUtil;
035    import com.liferay.portal.util.PortletKeys;
036    import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
037    import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
038    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
039    import com.liferay.portlet.documentlibrary.model.DLFolder;
040    import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
041    import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
042    import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
043    
044    import java.util.Date;
045    import java.util.List;
046    
047    import javax.portlet.PortletRequest;
048    import javax.portlet.PortletURL;
049    import javax.portlet.WindowStateException;
050    
051    /**
052     * @author Brian Wing Shun Chan
053     * @author Raymond Augé
054     */
055    public class DLIndexer extends BaseIndexer {
056    
057            public static final String[] CLASS_NAMES = {DLFileEntry.class.getName()};
058    
059            public static final String PORTLET_ID = PortletKeys.DOCUMENT_LIBRARY;
060    
061            public DLIndexer() {
062                    IndexerRegistryUtil.register(
063                            new com.liferay.documentlibrary.util.DLIndexer());
064            }
065    
066            public String[] getClassNames() {
067                    return CLASS_NAMES;
068            }
069    
070            public Summary getSummary(
071                    Document document, String snippet, PortletURL portletURL) {
072    
073                    LiferayPortletURL liferayPortletURL = (LiferayPortletURL)portletURL;
074    
075                    liferayPortletURL.setLifecycle(PortletRequest.ACTION_PHASE);
076    
077                    try {
078                            liferayPortletURL.setWindowState(LiferayWindowState.EXCLUSIVE);
079                    }
080                    catch (WindowStateException wse) {
081                    }
082    
083                    String groupId = document.get("scopeGroupId");
084                    String folderId = document.get("folderId");
085                    String fileName = document.get("path");
086    
087                    String title = fileName;
088    
089                    String content = snippet;
090    
091                    if (Validator.isNull(snippet)) {
092                            content = StringUtil.shorten(document.get(Field.CONTENT), 200);
093                    }
094    
095                    portletURL.setParameter("struts_action", "/document_library/get_file");
096                    portletURL.setParameter("groupId", groupId);
097                    portletURL.setParameter("folderId", folderId);
098                    portletURL.setParameter("name", fileName);
099    
100                    return new Summary(title, content, portletURL);
101            }
102    
103            protected void doDelete(Object obj) throws Exception {
104                    DLFileEntry fileEntry = (DLFileEntry)obj;
105    
106                    FileModel fileModel = new FileModel();
107    
108                    fileModel.setCompanyId(fileEntry.getCompanyId());
109                    fileModel.setFileName(fileEntry.getName());
110                    fileModel.setPortletId(PORTLET_ID);
111                    fileModel.setRepositoryId(fileEntry.getRepositoryId());
112    
113                    Indexer indexer = IndexerRegistryUtil.getIndexer(FileModel.class);
114    
115                    indexer.delete(fileModel);
116            }
117    
118            protected void doReindex(String[] ids) throws Exception {
119                    long companyId = GetterUtil.getLong(ids[0]);
120    
121                    reindexFolders(companyId);
122                    reindexRoot(companyId);
123            }
124    
125            protected void doReindex(String className, long classPK) throws Exception {
126                    DLFileEntry fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
127                            classPK);
128    
129                    doReindex(fileEntry);
130            }
131    
132            protected void doReindex(Object obj) throws Exception {
133                    DLFileEntry fileEntry = (DLFileEntry)obj;
134    
135                    Document document = getDocument(fileEntry);
136    
137                    if (document != null) {
138                            SearchEngineUtil.updateDocument(fileEntry.getCompanyId(), document);
139                    }
140            }
141    
142            protected Document doGetDocument(Object obj) throws Exception {
143                    DLFileEntry fileEntry = (DLFileEntry)obj;
144    
145                    long companyId = fileEntry.getCompanyId();
146                    long groupId = fileEntry.getGroupId();
147                    long userId = fileEntry.getUserId();
148                    long repositoryId = fileEntry.getRepositoryId();
149                    String fileName = fileEntry.getName();
150                    long fileEntryId = fileEntry.getFileEntryId();
151                    String properties = fileEntry.getLuceneProperties();
152                    Date modifiedDate = fileEntry.getModifiedDate();
153    
154                    long[] assetCategoryIds = AssetCategoryLocalServiceUtil.getCategoryIds(
155                            DLFileEntry.class.getName(), fileEntryId);
156                    String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(
157                            DLFileEntry.class.getName(), fileEntryId);
158    
159                    FileModel fileModel = new FileModel();
160    
161                    fileModel.setAssetCategoryIds(assetCategoryIds);
162                    fileModel.setAssetTagNames(assetTagNames);
163                    fileModel.setCompanyId(companyId);
164                    fileModel.setFileEntryId(fileEntryId);
165                    fileModel.setFileName(fileName);
166                    fileModel.setGroupId(groupId);
167                    fileModel.setUserId(userId);
168                    fileModel.setModifiedDate(modifiedDate);
169                    fileModel.setPortletId(PORTLET_ID);
170                    fileModel.setProperties(properties);
171                    fileModel.setRepositoryId(repositoryId);
172    
173                    Indexer indexer = IndexerRegistryUtil.getIndexer(FileModel.class);
174    
175                    return indexer.getDocument(fileModel);
176            }
177    
178            protected String getPortletId(SearchContext searchContext) {
179                    return PORTLET_ID;
180            }
181    
182            protected boolean hasPermission(
183                            PermissionChecker permissionChecker, long entryClassPK,
184                            String actionId)
185                    throws Exception {
186    
187                    return DLFileEntryPermission.contains(
188                            permissionChecker, entryClassPK, ActionKeys.VIEW);
189            }
190    
191            protected boolean isFilterSearch() {
192                    return _FILTER_SEARCH;
193            }
194    
195            protected void reindexFolders(long companyId) throws Exception {
196                    int folderCount = DLFolderLocalServiceUtil.getCompanyFoldersCount(
197                            companyId);
198    
199                    int folderPages = folderCount / Indexer.DEFAULT_INTERVAL;
200    
201                    for (int i = 0; i <= folderPages; i++) {
202                            int folderStart = (i * Indexer.DEFAULT_INTERVAL);
203                            int folderEnd = folderStart + Indexer.DEFAULT_INTERVAL;
204    
205                            reindexFolders(companyId, folderStart, folderEnd);
206                    }
207            }
208    
209            protected void reindexFolders(
210                            long companyId, int folderStart, int folderEnd)
211                    throws Exception {
212    
213                    List<DLFolder> folders = DLFolderLocalServiceUtil.getCompanyFolders(
214                            companyId, folderStart, folderEnd);
215    
216                    for (DLFolder folder : folders) {
217                            String portletId = PortletKeys.DOCUMENT_LIBRARY;
218                            long groupId = folder.getGroupId();
219                            long folderId = folder.getFolderId();
220    
221                            String[] newIds = {
222                                    String.valueOf(companyId), portletId,
223                                    String.valueOf(groupId), String.valueOf(folderId)
224                            };
225    
226                            Indexer indexer = IndexerRegistryUtil.getIndexer(FileModel.class);
227    
228                            indexer.reindex(newIds);
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                            String portletId = PortletKeys.DOCUMENT_LIBRARY;
253                            long groupId = group.getGroupId();
254                            long folderId = groupId;
255    
256                            String[] newIds = {
257                                    String.valueOf(companyId), portletId,
258                                    String.valueOf(groupId), String.valueOf(folderId)
259                            };
260    
261                            Indexer indexer = IndexerRegistryUtil.getIndexer(FileModel.class);
262    
263                            indexer.reindex(newIds);
264                    }
265            }
266    
267            private static final boolean _FILTER_SEARCH = true;
268    
269    }