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.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[] assetCategoryNames =
157                            AssetCategoryLocalServiceUtil.getCategoryNames(
158                                    DLFileEntry.class.getName(), fileEntryId);
159                    String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(
160                            DLFileEntry.class.getName(), fileEntryId);
161    
162                    FileModel fileModel = new FileModel();
163    
164                    fileModel.setAssetCategoryIds(assetCategoryIds);
165                    fileModel.setAssetCategoryNames(assetCategoryNames);
166                    fileModel.setAssetTagNames(assetTagNames);
167                    fileModel.setCompanyId(companyId);
168                    fileModel.setFileEntryId(fileEntryId);
169                    fileModel.setFileName(fileName);
170                    fileModel.setGroupId(groupId);
171                    fileModel.setUserId(userId);
172                    fileModel.setModifiedDate(modifiedDate);
173                    fileModel.setPortletId(PORTLET_ID);
174                    fileModel.setProperties(properties);
175                    fileModel.setRepositoryId(repositoryId);
176    
177                    Indexer indexer = IndexerRegistryUtil.getIndexer(FileModel.class);
178    
179                    return indexer.getDocument(fileModel);
180            }
181    
182            protected String getPortletId(SearchContext searchContext) {
183                    return PORTLET_ID;
184            }
185    
186            protected boolean hasPermission(
187                            PermissionChecker permissionChecker, long entryClassPK,
188                            String actionId)
189                    throws Exception {
190    
191                    return DLFileEntryPermission.contains(
192                            permissionChecker, entryClassPK, ActionKeys.VIEW);
193            }
194    
195            protected boolean isFilterSearch() {
196                    return _FILTER_SEARCH;
197            }
198    
199            protected void reindexFolders(long companyId) throws Exception {
200                    int folderCount = DLFolderLocalServiceUtil.getCompanyFoldersCount(
201                            companyId);
202    
203                    int folderPages = folderCount / Indexer.DEFAULT_INTERVAL;
204    
205                    for (int i = 0; i <= folderPages; i++) {
206                            int folderStart = (i * Indexer.DEFAULT_INTERVAL);
207                            int folderEnd = folderStart + Indexer.DEFAULT_INTERVAL;
208    
209                            reindexFolders(companyId, folderStart, folderEnd);
210                    }
211            }
212    
213            protected void reindexFolders(
214                            long companyId, int folderStart, int folderEnd)
215                    throws Exception {
216    
217                    List<DLFolder> folders = DLFolderLocalServiceUtil.getCompanyFolders(
218                            companyId, folderStart, folderEnd);
219    
220                    for (DLFolder folder : folders) {
221                            String portletId = PortletKeys.DOCUMENT_LIBRARY;
222                            long groupId = folder.getGroupId();
223                            long folderId = folder.getFolderId();
224    
225                            String[] newIds = {
226                                    String.valueOf(companyId), portletId,
227                                    String.valueOf(groupId), String.valueOf(folderId)
228                            };
229    
230                            Indexer indexer = IndexerRegistryUtil.getIndexer(FileModel.class);
231    
232                            indexer.reindex(newIds);
233                    }
234            }
235    
236            protected void reindexRoot(long companyId) throws Exception {
237                    int groupCount = GroupLocalServiceUtil.getCompanyGroupsCount(companyId);
238    
239                    int groupPages = groupCount / Indexer.DEFAULT_INTERVAL;
240    
241                    for (int i = 0; i <= groupPages; i++) {
242                            int groupStart = (i * Indexer.DEFAULT_INTERVAL);
243                            int groupEnd = groupStart + Indexer.DEFAULT_INTERVAL;
244    
245                            reindexRoot(companyId, groupStart, groupEnd);
246                    }
247            }
248    
249            protected void reindexRoot(long companyId, int groupStart, int groupEnd)
250                    throws Exception {
251    
252                    List<Group> groups = GroupLocalServiceUtil.getCompanyGroups(
253                            companyId, groupStart, groupEnd);
254    
255                    for (Group group : groups) {
256                            String portletId = PortletKeys.DOCUMENT_LIBRARY;
257                            long groupId = group.getGroupId();
258                            long folderId = groupId;
259    
260                            String[] newIds = {
261                                    String.valueOf(companyId), portletId,
262                                    String.valueOf(groupId), String.valueOf(folderId)
263                            };
264    
265                            Indexer indexer = IndexerRegistryUtil.getIndexer(FileModel.class);
266    
267                            indexer.reindex(newIds);
268                    }
269            }
270    
271            private static final boolean _FILTER_SEARCH = true;
272    
273    }