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.documentlibrary.util;
16  
17  import com.liferay.documentlibrary.model.FileModel;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.search.Document;
21  import com.liferay.portal.kernel.search.DocumentImpl;
22  import com.liferay.portal.kernel.search.Field;
23  import com.liferay.portal.kernel.search.SearchContext;
24  import com.liferay.portal.kernel.search.SearchEngineUtil;
25  import com.liferay.portal.kernel.search.SearchException;
26  import com.liferay.portal.kernel.search.Summary;
27  import com.liferay.portal.search.BaseIndexer;
28  import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
29  import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
30  import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
31  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
32  import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryImpl;
33  import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
34  import com.liferay.portlet.expando.model.ExpandoBridge;
35  import com.liferay.portlet.expando.util.ExpandoBridgeFactoryUtil;
36  import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
37  
38  import java.io.IOException;
39  import java.io.InputStream;
40  
41  import java.util.Date;
42  
43  import javax.portlet.PortletURL;
44  
45  /**
46   * <a href="DLIndexer.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   * @author Harry Mark
50   * @author Bruno Farache
51   * @author Raymond Augé
52   */
53  public class DLIndexer extends BaseIndexer {
54  
55      public static final String[] CLASS_NAMES = {FileModel.class.getName()};
56  
57      public String[] getClassNames() {
58          return CLASS_NAMES;
59      }
60  
61      public Summary getSummary(
62          Document document, String snippet, PortletURL portletURL) {
63  
64          return null;
65      }
66  
67      protected void doDelete(Object obj) throws Exception {
68          FileModel fileModel = (FileModel)obj;
69  
70          Document document = new DocumentImpl();
71  
72          document.addUID(
73              fileModel.getPortletId(), fileModel.getRepositoryId(),
74              fileModel.getFileName());
75  
76          SearchEngineUtil.deleteDocument(
77              fileModel.getCompanyId(), document.get(Field.UID));
78      }
79  
80      protected Document doGetDocument(Object obj) throws Exception {
81          FileModel fileModel = (FileModel)obj;
82  
83          long companyId = fileModel.getCompanyId();
84          String portletId = fileModel.getPortletId();
85          long groupId = getParentGroupId(fileModel.getGroupId());
86          long scopeGroupId = fileModel.getGroupId();
87          long userId = fileModel.getUserId();
88          long folderId = DLFileEntryImpl.getFolderId(
89              groupId, fileModel.getRepositoryId());
90          long repositoryId = fileModel.getRepositoryId();
91          String fileName = fileModel.getFileName();
92          long fileEntryId = fileModel.getFileEntryId();
93          String properties = fileModel.getProperties();
94          Date modifiedDate = fileModel.getModifiedDate();
95          long[] assetCategoryIds = fileModel.getAssetCategoryIds();
96          String[] assetTagNames = fileModel.getAssetTagNames();
97  
98          DLFileEntry fileEntry = null;
99  
100         try {
101             if (fileEntryId > 0) {
102                 fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
103                     fileEntryId);
104             }
105             else {
106                 fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
107                     groupId, folderId, fileName);
108             }
109         }
110         catch (NoSuchFileEntryException nsfe) {
111             if (_log.isDebugEnabled()) {
112                 _log.debug(
113                     "Not indexing document " + companyId + " " + portletId +
114                         " " + scopeGroupId + " " + repositoryId + " " +
115                             fileName + " " + fileEntryId);
116             }
117 
118             return null;
119         }
120 
121         if (properties == null) {
122             properties = fileEntry.getLuceneProperties();
123         }
124 
125         if (modifiedDate == null) {
126             modifiedDate = fileEntry.getModifiedDate();
127         }
128 
129         if (assetCategoryIds == null) {
130             assetCategoryIds = AssetCategoryLocalServiceUtil.getCategoryIds(
131                 DLFileEntry.class.getName(), fileEntry.getFileEntryId());
132         }
133 
134         if (assetTagNames == null) {
135             assetTagNames = AssetTagLocalServiceUtil.getTagNames(
136                 DLFileEntry.class.getName(), fileEntry.getFileEntryId());
137         }
138 
139         if (_log.isDebugEnabled()) {
140             _log.debug(
141                 "Indexing document " + companyId + " " + portletId + " " +
142                     scopeGroupId + " " + repositoryId + " " + fileName + " " +
143                         fileEntryId);
144         }
145 
146         InputStream is = null;
147 
148         try {
149             Hook hook = HookFactory.getInstance();
150 
151             is = hook.getFileAsStream(companyId, repositoryId, fileName);
152         }
153         catch (Exception e) {
154         }
155 
156         if (is == null) {
157             if (_log.isDebugEnabled()) {
158                 _log.debug(
159                     "Document " + companyId + " " + portletId + " " +
160                         scopeGroupId + " " + repositoryId + " " + fileName +
161                             " " + fileEntryId + " does not have any content");
162             }
163 
164             return null;
165         }
166 
167         Document document = new DocumentImpl();
168 
169         document.addUID(portletId, repositoryId, fileName);
170 
171         document.addModifiedDate(modifiedDate);
172 
173         document.addKeyword(Field.COMPANY_ID, companyId);
174         document.addKeyword(Field.PORTLET_ID, portletId);
175         document.addKeyword(Field.GROUP_ID, groupId);
176         document.addKeyword(Field.SCOPE_GROUP_ID, scopeGroupId);
177         document.addKeyword(Field.USER_ID, userId);
178 
179         try {
180             document.addFile(Field.CONTENT, is, fileEntry.getTitle());
181         }
182         catch (IOException ioe) {
183             throw new SearchException(
184                 "Cannot extract text from file" + companyId + " " + portletId +
185                     " " + scopeGroupId + " " + repositoryId + " " + fileName);
186         }
187 
188         document.addText(Field.PROPERTIES, properties);
189         document.addKeyword(Field.ASSET_CATEGORY_IDS, assetCategoryIds);
190         document.addKeyword(Field.ASSET_TAG_NAMES, assetTagNames);
191 
192         document.addKeyword(Field.FOLDER_ID, folderId);
193         document.addKeyword("repositoryId", repositoryId);
194         document.addKeyword("path", fileName);
195         document.addKeyword(
196             Field.ENTRY_CLASS_NAME, DLFileEntry.class.getName());
197         document.addKeyword(Field.ENTRY_CLASS_PK, fileEntryId);
198 
199         ExpandoBridge expandoBridge = ExpandoBridgeFactoryUtil.getExpandoBridge(
200             companyId, DLFileEntry.class.getName(), fileEntryId);
201 
202         ExpandoBridgeIndexerUtil.addAttributes(document, expandoBridge);
203 
204         if (_log.isDebugEnabled()) {
205             _log.debug(
206                 "Document " + companyId + " " + portletId + " " +
207                     scopeGroupId + " " + repositoryId + " " + fileName + " " +
208                         fileEntryId + " indexed successfully");
209         }
210 
211         return document;
212     }
213 
214     protected void doReindex(Object obj) throws Exception {
215         FileModel fileModel = (FileModel)obj;
216 
217         Document document = getDocument(fileModel);
218 
219         if (document != null) {
220             SearchEngineUtil.updateDocument(fileModel.getCompanyId(), document);
221         }
222     }
223 
224     protected void doReindex(String className, long classPK) throws Exception {
225     }
226 
227     protected void doReindex(String[] ids) throws Exception {
228         Hook hook = HookFactory.getInstance();
229 
230         hook.reindex(ids);
231     }
232 
233     protected String getPortletId(SearchContext searchContext) {
234         return (String)searchContext.getAttribute("portletId");
235     }
236 
237     private static Log _log = LogFactoryUtil.getLog(DLIndexer.class);
238 
239 }