1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.documentlibrary.util;
16  
17  import com.liferay.documentlibrary.model.FileModel;
18  import com.liferay.portal.kernel.portlet.LiferayPortletURL;
19  import com.liferay.portal.kernel.portlet.LiferayWindowState;
20  import com.liferay.portal.kernel.search.BaseIndexer;
21  import com.liferay.portal.kernel.search.Document;
22  import com.liferay.portal.kernel.search.Field;
23  import com.liferay.portal.kernel.search.Indexer;
24  import com.liferay.portal.kernel.search.IndexerRegistryUtil;
25  import com.liferay.portal.kernel.search.SearchContext;
26  import com.liferay.portal.kernel.search.SearchEngineUtil;
27  import com.liferay.portal.kernel.search.Summary;
28  import com.liferay.portal.kernel.util.GetterUtil;
29  import com.liferay.portal.kernel.util.StringUtil;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.model.Group;
32  import com.liferay.portal.service.GroupLocalServiceUtil;
33  import com.liferay.portal.util.PortletKeys;
34  import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
35  import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
36  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
37  import com.liferay.portlet.documentlibrary.model.DLFolder;
38  import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
39  import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
40  
41  import java.util.Date;
42  import java.util.List;
43  
44  import javax.portlet.PortletRequest;
45  import javax.portlet.PortletURL;
46  import javax.portlet.WindowStateException;
47  
48  /**
49   * <a href="DLIndexer.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Brian Wing Shun Chan
52   * @author Raymond Augé
53   */
54  public class DLIndexer extends BaseIndexer {
55  
56      public static final String[] CLASS_NAMES = {DLFileEntry.class.getName()};
57  
58      public static final String PORTLET_ID = PortletKeys.DOCUMENT_LIBRARY;
59  
60      public DLIndexer() {
61          IndexerRegistryUtil.register(
62              new com.liferay.documentlibrary.util.DLIndexer());
63      }
64  
65      public String[] getClassNames() {
66          return CLASS_NAMES;
67      }
68  
69      public Summary getSummary(
70          Document document, String snippet, PortletURL portletURL) {
71  
72          LiferayPortletURL liferayPortletURL = (LiferayPortletURL)portletURL;
73  
74          liferayPortletURL.setLifecycle(PortletRequest.ACTION_PHASE);
75  
76          try {
77              liferayPortletURL.setWindowState(LiferayWindowState.EXCLUSIVE);
78          }
79          catch (WindowStateException wse) {
80          }
81  
82          String groupId = document.get("scopeGroupId");
83          String folderId = document.get("folderId");
84          String fileName = document.get("path");
85  
86          String title = fileName;
87  
88          String content = snippet;
89  
90          if (Validator.isNull(snippet)) {
91              content = StringUtil.shorten(document.get(Field.CONTENT), 200);
92          }
93  
94          portletURL.setParameter("struts_action", "/document_library/get_file");
95          portletURL.setParameter("groupId", groupId);
96          portletURL.setParameter("folderId", folderId);
97          portletURL.setParameter("name", fileName);
98  
99          return new Summary(title, content, portletURL);
100     }
101 
102     protected void doDelete(Object obj) throws Exception {
103         DLFileEntry fileEntry = (DLFileEntry)obj;
104 
105         FileModel fileModel = new FileModel();
106 
107         fileModel.setCompanyId(fileEntry.getCompanyId());
108         fileModel.setFileName(fileEntry.getName());
109         fileModel.setPortletId(PORTLET_ID);
110         fileModel.setRepositoryId(fileEntry.getRepositoryId());
111 
112         Indexer indexer = IndexerRegistryUtil.getIndexer(FileModel.class);
113 
114         indexer.delete(fileModel);
115     }
116 
117     protected void doReindex(String[] ids) throws Exception {
118         long companyId = GetterUtil.getLong(ids[0]);
119 
120         reindexFolders(companyId);
121         reindexRoot(companyId);
122     }
123 
124     protected void doReindex(String className, long classPK) throws Exception {
125         DLFileEntry fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
126             classPK);
127 
128         doReindex(fileEntry);
129     }
130 
131     protected void doReindex(Object obj) throws Exception {
132         DLFileEntry fileEntry = (DLFileEntry)obj;
133 
134         Document document = getDocument(fileEntry);
135 
136         if (document != null) {
137             SearchEngineUtil.updateDocument(fileEntry.getCompanyId(), document);
138         }
139     }
140 
141     protected Document doGetDocument(Object obj) throws Exception {
142         DLFileEntry fileEntry = (DLFileEntry)obj;
143 
144         long companyId = fileEntry.getCompanyId();
145         long groupId = fileEntry.getGroupId();
146         long userId = fileEntry.getUserId();
147         long repositoryId = fileEntry.getRepositoryId();
148         String fileName = fileEntry.getName();
149         long fileEntryId = fileEntry.getFileEntryId();
150         String properties = fileEntry.getLuceneProperties();
151         Date modifiedDate = fileEntry.getModifiedDate();
152 
153         long[] assetCategoryIds = AssetCategoryLocalServiceUtil.getCategoryIds(
154             DLFileEntry.class.getName(), fileEntryId);
155         String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(
156             DLFileEntry.class.getName(), fileEntryId);
157 
158         FileModel fileModel = new FileModel();
159 
160         fileModel.setAssetCategoryIds(assetCategoryIds);
161         fileModel.setAssetTagNames(assetTagNames);
162         fileModel.setCompanyId(companyId);
163         fileModel.setFileEntryId(fileEntryId);
164         fileModel.setFileName(fileName);
165         fileModel.setGroupId(groupId);
166         fileModel.setUserId(userId);
167         fileModel.setModifiedDate(modifiedDate);
168         fileModel.setPortletId(PORTLET_ID);
169         fileModel.setProperties(properties);
170         fileModel.setRepositoryId(repositoryId);
171 
172         Indexer indexer = IndexerRegistryUtil.getIndexer(FileModel.class);
173 
174         return indexer.getDocument(fileModel);
175     }
176 
177     protected String getPortletId(SearchContext searchContext) {
178         return PORTLET_ID;
179     }
180 
181     protected void reindexFolders(long companyId) throws Exception {
182         int folderCount = DLFolderLocalServiceUtil.getCompanyFoldersCount(
183             companyId);
184 
185         int folderPages = folderCount / Indexer.DEFAULT_INTERVAL;
186 
187         for (int i = 0; i <= folderPages; i++) {
188             int folderStart = (i * Indexer.DEFAULT_INTERVAL);
189             int folderEnd = folderStart + Indexer.DEFAULT_INTERVAL;
190 
191             reindexFolders(companyId, folderStart, folderEnd);
192         }
193     }
194 
195     protected void reindexFolders(
196             long companyId, int folderStart, int folderEnd)
197         throws Exception {
198 
199         List<DLFolder> folders = DLFolderLocalServiceUtil.getCompanyFolders(
200             companyId, folderStart, folderEnd);
201 
202         for (DLFolder folder : folders) {
203             String portletId = PortletKeys.DOCUMENT_LIBRARY;
204             long groupId = folder.getGroupId();
205             long folderId = folder.getFolderId();
206 
207             String[] newIds = {
208                 String.valueOf(companyId), portletId,
209                 String.valueOf(groupId), String.valueOf(folderId)
210             };
211 
212             Indexer indexer = IndexerRegistryUtil.getIndexer(FileModel.class);
213 
214             indexer.reindex(newIds);
215         }
216     }
217 
218     protected void reindexRoot(long companyId) throws Exception {
219         int groupCount = GroupLocalServiceUtil.getCompanyGroupsCount(companyId);
220 
221         int groupPages = groupCount / Indexer.DEFAULT_INTERVAL;
222 
223         for (int i = 0; i <= groupPages; i++) {
224             int groupStart = (i * Indexer.DEFAULT_INTERVAL);
225             int groupEnd = groupStart + Indexer.DEFAULT_INTERVAL;
226 
227             reindexRoot(companyId, groupStart, groupEnd);
228         }
229     }
230 
231     protected void reindexRoot(long companyId, int groupStart, int groupEnd)
232         throws Exception {
233 
234         List<Group> groups = GroupLocalServiceUtil.getCompanyGroups(
235             companyId, groupStart, groupEnd);
236 
237         for (Group group : groups) {
238             String portletId = PortletKeys.DOCUMENT_LIBRARY;
239             long groupId = group.getGroupId();
240             long folderId = groupId;
241 
242             String[] newIds = {
243                 String.valueOf(companyId), portletId,
244                 String.valueOf(groupId), String.valueOf(folderId)
245             };
246 
247             Indexer indexer = IndexerRegistryUtil.getIndexer(FileModel.class);
248 
249             indexer.reindex(newIds);
250         }
251     }
252 
253 }