1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.documentlibrary.util;
16  
17  import com.liferay.documentlibrary.service.impl.DLServiceImpl;
18  import com.liferay.portal.PortalException;
19  import com.liferay.portal.SystemException;
20  import com.liferay.portal.kernel.log.Log;
21  import com.liferay.portal.kernel.log.LogFactoryUtil;
22  import com.liferay.portal.kernel.search.Document;
23  import com.liferay.portal.kernel.search.DocumentImpl;
24  import com.liferay.portal.kernel.search.Field;
25  import com.liferay.portal.kernel.search.SearchEngineUtil;
26  import com.liferay.portal.kernel.search.SearchException;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.model.Group;
29  import com.liferay.portal.service.GroupLocalServiceUtil;
30  import com.liferay.portal.util.PropsValues;
31  import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
32  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
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  import com.liferay.portlet.tags.model.TagsEntryConstants;
38  import com.liferay.portlet.tags.service.TagsEntryLocalServiceUtil;
39  
40  import java.io.IOException;
41  import java.io.InputStream;
42  
43  import java.util.Date;
44  
45  /**
46   * <a href="DLIndexerImpl.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   */
50  public class DLIndexerImpl implements DLIndexer {
51  
52      public void addFile(
53              long companyId, String portletId, long groupId, long userId,
54              long repositoryId, String fileName)
55          throws SearchException {
56  
57          Document doc = getFileDocument(
58              companyId, portletId, groupId, userId, repositoryId, fileName);
59  
60          if (doc != null) {
61              SearchEngineUtil.addDocument(companyId, doc);
62          }
63      }
64  
65      public void addFile(
66              long companyId, String portletId, long groupId, long userId,
67              long repositoryId, String fileName, long fileEntryId,
68              String properties, Date modifiedDate, String[] tagsCategories,
69              String[] tagsEntries)
70          throws SearchException {
71  
72          Document doc = getFileDocument(
73              companyId, portletId, groupId, userId, repositoryId, fileName,
74              fileEntryId, properties, modifiedDate, tagsCategories, tagsEntries);
75  
76          if (doc != null) {
77              SearchEngineUtil.addDocument(companyId, doc);
78          }
79      }
80  
81      /**
82       * @deprecated
83       */
84      public void addFile(
85              long companyId, String portletId, long groupId, long repositoryId,
86              String fileName)
87          throws SearchException {
88  
89          addFile(companyId, portletId, groupId, 0, repositoryId, fileName);
90      }
91  
92      /**
93       * @deprecated
94       */
95      public void addFile(
96              long companyId, String portletId, long groupId, long repositoryId,
97              String fileName, long fileEntryId, String properties,
98              Date modifiedDate, String[] tagsCategories, String[] tagsEntries)
99          throws SearchException {
100 
101         addFile(
102             companyId, portletId, groupId, 0, repositoryId, fileName,
103             fileEntryId, properties, modifiedDate, tagsCategories, tagsEntries);
104     }
105 
106     public void deleteFile(
107             long companyId, String portletId, long repositoryId,
108             String fileName)
109         throws SearchException {
110 
111         SearchEngineUtil.deleteDocument(
112             companyId, getFileUID(portletId, repositoryId, fileName));
113     }
114 
115     public Document getFileDocument(
116             long companyId, String portletId, long groupId, long userId,
117             long repositoryId, String fileName)
118         throws SearchException {
119 
120         try {
121             DLFileEntry fileEntry = null;
122 
123             try {
124                 fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
125                     repositoryId, fileName);
126             }
127             catch (NoSuchFileEntryException nsfe) {
128                 if (_log.isWarnEnabled()) {
129                     _log.warn(
130                         "File " + fileName + " in repository " +
131                             repositoryId + " exists in the JCR but does " +
132                                 "not exist in the database");
133                 }
134 
135                 return null;
136             }
137 
138             if (userId == 0) {
139                 userId = fileEntry.getUserId();
140             }
141 
142             String properties = fileEntry.getLuceneProperties();
143 
144             String[] tagsCategories = TagsEntryLocalServiceUtil.getEntryNames(
145                 DLFileEntry.class.getName(), fileEntry.getFileEntryId(),
146                 TagsEntryConstants.FOLKSONOMY_CATEGORY);
147             String[] tagsEntries = TagsEntryLocalServiceUtil.getEntryNames(
148                 DLFileEntry.class.getName(), fileEntry.getFileEntryId());
149 
150             return getFileDocument(
151                 companyId, portletId, groupId, userId, repositoryId, fileName,
152                 fileEntry.getFileEntryId(), properties,
153                 fileEntry.getModifiedDate(), tagsCategories, tagsEntries);
154         }
155         catch (PortalException pe) {
156             throw new SearchException(pe.getMessage());
157         }
158         catch (SystemException se) {
159             throw new SearchException(se.getMessage());
160         }
161     }
162 
163     public Document getFileDocument(
164             long companyId, String portletId, long groupId, long userId,
165             long repositoryId, String fileName, long fileEntryId,
166             String properties, Date modifiedDate, String[] tagsCategories,
167             String[] tagsEntries)
168         throws SearchException {
169 
170         long scopeGroupId = groupId;
171 
172         try {
173             Group group = GroupLocalServiceUtil.getGroup(groupId);
174 
175             if (group.isLayout()) {
176                 groupId = group.getParentGroupId();
177             }
178         }
179         catch (Exception e) {
180         }
181 
182         if (fileEntryId <= 0) {
183             _log.debug(
184                 "Not indexing document " + companyId + " " + portletId + " " +
185                     scopeGroupId + " " + repositoryId + " " + fileName + " " +
186                         fileEntryId);
187 
188             return null;
189         }
190 
191         if (userId == 0) {
192             try {
193                 DLFileEntry fileEntry =
194                     DLFileEntryLocalServiceUtil.getFileEntry(fileEntryId);
195 
196                 userId = fileEntry.getUserId();
197             }
198             catch (Exception e) {
199             }
200         }
201 
202         if (_log.isDebugEnabled()) {
203             _log.debug(
204                 "Indexing document " + companyId + " " + portletId + " " +
205                     scopeGroupId + " " + repositoryId + " " + fileName + " " +
206                         fileEntryId);
207         }
208 
209         String fileExt = StringPool.BLANK;
210 
211         int fileExtVersionPos = fileName.indexOf(DLServiceImpl.VERSION);
212 
213         if (fileExtVersionPos != -1) {
214             int fileExtPos = fileName.lastIndexOf(
215                 StringPool.PERIOD, fileExtVersionPos);
216 
217             if (fileExtPos != -1) {
218                 fileExt = fileName.substring(fileExtPos, fileExtVersionPos);
219             }
220         }
221         else {
222             int fileExtPos = fileName.lastIndexOf(StringPool.PERIOD);
223 
224             if (fileExtPos != -1) {
225                 fileExt = fileName.substring(fileExtPos, fileName.length());
226             }
227         }
228 
229         boolean indexContent = true;
230 
231         InputStream is = null;
232 
233         try {
234             Hook hook = HookFactory.getInstance();
235 
236             if (PropsValues.DL_FILE_INDEXING_MAX_SIZE == 0) {
237                 indexContent = false;
238             }
239             else if (PropsValues.DL_FILE_INDEXING_MAX_SIZE != -1) {
240                 long size = hook.getFileSize(companyId, repositoryId, fileName);
241 
242                 if (size > PropsValues.DL_FILE_INDEXING_MAX_SIZE) {
243                     indexContent = false;
244                 }
245             }
246 
247             if (indexContent) {
248                 is = hook.getFileAsStream(companyId, repositoryId, fileName);
249             }
250         }
251         catch (Exception e) {
252         }
253 
254         if (indexContent && (is == null)) {
255             if (_log.isDebugEnabled()) {
256                 _log.debug(
257                     "Document " + companyId + " " + portletId + " " +
258                         scopeGroupId + " " + repositoryId + " " + fileName +
259                             " " + fileEntryId + " does not have any content");
260             }
261 
262             return null;
263         }
264 
265         Document doc = new DocumentImpl();
266 
267         doc.addUID(portletId, repositoryId, fileName);
268 
269         doc.addModifiedDate(modifiedDate);
270 
271         doc.addKeyword(Field.COMPANY_ID, companyId);
272         doc.addKeyword(Field.PORTLET_ID, portletId);
273         doc.addKeyword(Field.GROUP_ID, groupId);
274         doc.addKeyword(Field.SCOPE_GROUP_ID, scopeGroupId);
275         doc.addKeyword(Field.USER_ID, userId);
276 
277         if (indexContent) {
278             try {
279                 doc.addFile(Field.CONTENT, is, fileExt);
280             }
281             catch (IOException ioe) {
282                 throw new SearchException(
283                     "Cannot extract text from file" + companyId + " " +
284                         portletId + " " + scopeGroupId + " " + repositoryId +
285                             " " + fileName);
286             }
287         }
288 
289         doc.addText(Field.PROPERTIES, properties);
290         doc.addKeyword(Field.TAGS_CATEGORIES, tagsCategories);
291         doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
292 
293         doc.addKeyword("repositoryId", repositoryId);
294         doc.addKeyword("path", fileName);
295         doc.addKeyword(Field.ENTRY_CLASS_NAME, DLFileEntry.class.getName());
296         doc.addKeyword(Field.ENTRY_CLASS_PK, fileEntryId);
297 
298         ExpandoBridge expandoBridge = ExpandoBridgeFactoryUtil.getExpandoBridge(
299             DLFileEntry.class.getName(), fileEntryId);
300 
301         ExpandoBridgeIndexerUtil.addAttributes(doc, expandoBridge);
302 
303         if (_log.isDebugEnabled()) {
304             _log.debug(
305                 "Document " + companyId + " " + portletId + " " +
306                     scopeGroupId + " " + repositoryId + " " + fileName + " " +
307                         fileEntryId + " indexed successfully");
308         }
309 
310         return doc;
311     }
312 
313     /**
314      * @deprecated
315      */
316     public Document getFileDocument(
317             long companyId, String portletId, long groupId, long repositoryId,
318             String fileName)
319         throws SearchException {
320 
321         return getFileDocument(
322             companyId, portletId, groupId, 0, repositoryId, fileName);
323     }
324 
325     /**
326      * @deprecated
327      */
328     public Document getFileDocument(
329             long companyId, String portletId, long groupId, long repositoryId,
330             String fileName, long fileEntryId, String properties,
331             Date modifiedDate, String[] tagsCategories, String[] tagsEntries)
332         throws SearchException {
333 
334         return getFileDocument(
335             companyId, portletId, groupId, 0, repositoryId, fileName,
336             fileEntryId, properties, modifiedDate, tagsCategories, tagsEntries);
337     }
338 
339     public String getFileUID(
340         String portletId, long repositoryId, String fileName) {
341 
342         Document doc = new DocumentImpl();
343 
344         doc.addUID(portletId, repositoryId, fileName);
345 
346         return doc.get(Field.UID);
347     }
348 
349     public void updateFile(
350             long companyId, String portletId, long groupId, long userId,
351             long repositoryId, String fileName, long fileEntryId,
352             String properties, Date modifiedDate, String[] tagsCategories,
353             String[] tagsEntries)
354         throws SearchException {
355 
356         Document doc = getFileDocument(
357             companyId, portletId, groupId, userId, repositoryId, fileName,
358             fileEntryId, properties, modifiedDate, tagsCategories, tagsEntries);
359 
360         if (doc != null) {
361             SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
362         }
363     }
364 
365     /**
366      * @deprecated
367      */
368     public void updateFile(
369             long companyId, String portletId, long groupId, long repositoryId,
370             String fileName, long fileEntryId, String properties,
371             Date modifiedDate, String[] tagsCategories, String[] tagsEntries)
372         throws SearchException {
373 
374         updateFile(
375             companyId, portletId, groupId, 0, repositoryId, fileName,
376             fileEntryId, properties, modifiedDate, tagsCategories, tagsEntries);
377     }
378 
379     private static Log _log = LogFactoryUtil.getLog(DLIndexerImpl.class);
380 
381 }