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.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.portlet.documentlibrary.NoSuchFileEntryException;
29  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
30  import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
31  import com.liferay.portlet.tags.service.TagsEntryLocalServiceUtil;
32  
33  import java.io.IOException;
34  import java.io.InputStream;
35  
36  import java.util.Date;
37  
38  /**
39   * <a href="DLIndexerImpl.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   */
43  public class DLIndexerImpl implements DLIndexer {
44  
45      public void addFile(
46              long companyId, String portletId, long groupId, long repositoryId,
47              String fileName)
48          throws SearchException {
49  
50          Document doc = getFileDocument(
51              companyId, portletId, groupId, repositoryId, fileName);
52  
53          if (doc != null) {
54              SearchEngineUtil.addDocument(companyId, doc);
55          }
56      }
57  
58      public void addFile(
59              long companyId, String portletId, long groupId, long repositoryId,
60              String fileName, String properties, Date modifiedDate,
61              String[] tagsEntries)
62          throws SearchException {
63  
64          Document doc = getFileDocument(
65              companyId, portletId, groupId, repositoryId, fileName, properties,
66              modifiedDate, tagsEntries);
67  
68          if (doc != null) {
69              SearchEngineUtil.addDocument(companyId, doc);
70          }
71      }
72  
73      public void deleteFile(
74              long companyId, String portletId, long repositoryId,
75              String fileName)
76          throws SearchException {
77  
78          SearchEngineUtil.deleteDocument(
79              companyId, getFileUID(portletId, repositoryId, fileName));
80      }
81  
82      public Document getFileDocument(
83              long companyId, String portletId, long groupId, long repositoryId,
84              String fileName)
85          throws SearchException {
86  
87          try {
88              DLFileEntry fileEntry = null;
89  
90              try {
91                  fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
92                      repositoryId, fileName);
93              }
94              catch (NoSuchFileEntryException nsfe) {
95                  if (_log.isWarnEnabled()) {
96                      _log.warn(
97                          "File " + fileName + " in repository " +
98                              repositoryId + " exists in the JCR but does " +
99                                  "not exist in the database");
100                 }
101 
102                 return null;
103             }
104 
105             String properties = fileEntry.getLuceneProperties();
106 
107             String[] tagsEntries = TagsEntryLocalServiceUtil.getEntryNames(
108                 DLFileEntry.class.getName(), fileEntry.getFileEntryId());
109 
110             return getFileDocument(
111                 companyId, portletId, groupId, repositoryId, fileName,
112                 properties, fileEntry.getModifiedDate(), tagsEntries);
113         }
114         catch (PortalException pe) {
115             throw new SearchException(pe.getMessage());
116         }
117         catch (SystemException se) {
118             throw new SearchException(se.getMessage());
119         }
120     }
121 
122     public Document getFileDocument(
123             long companyId, String portletId, long groupId, long repositoryId,
124             String fileName, String properties, Date modifiedDate,
125             String[] tagsEntries)
126         throws SearchException {
127 
128         if (_log.isDebugEnabled()) {
129             _log.debug(
130                 "Indexing document " + companyId + " " + portletId + " " +
131                     groupId + " " + repositoryId + " " + fileName);
132         }
133 
134         String fileExt = StringPool.BLANK;
135 
136         int fileExtVersionPos = fileName.indexOf(DLServiceImpl.VERSION);
137 
138         if (fileExtVersionPos != -1) {
139             int fileExtPos = fileName.lastIndexOf(
140                 StringPool.PERIOD, fileExtVersionPos);
141 
142             if (fileExtPos != -1) {
143                 fileExt = fileName.substring(fileExtPos, fileExtVersionPos);
144             }
145         }
146         else {
147             int fileExtPos = fileName.lastIndexOf(StringPool.PERIOD);
148 
149             if (fileExtPos != -1) {
150                 fileExt = fileName.substring(fileExtPos, fileName.length());
151             }
152         }
153 
154         InputStream is = null;
155 
156         try {
157             Hook hook = HookFactory.getInstance();
158 
159             is = hook.getFileAsStream(companyId, repositoryId, fileName);
160         }
161         catch (Exception e) {
162         }
163 
164         if (is == null) {
165             if (_log.isDebugEnabled()) {
166                 _log.debug(
167                     "Document " + companyId + " " + portletId + " " + groupId +
168                         " " + repositoryId + " " + fileName +
169                             " does not have any content");
170             }
171 
172             return null;
173         }
174 
175         Document doc = new DocumentImpl();
176 
177         doc.addUID(portletId, repositoryId, fileName);
178 
179         doc.addModifiedDate(modifiedDate);
180 
181         doc.addKeyword(Field.COMPANY_ID, companyId);
182         doc.addKeyword(Field.PORTLET_ID, portletId);
183         doc.addKeyword(Field.GROUP_ID, groupId);
184 
185         try {
186             doc.addFile(Field.CONTENT, is, fileExt);
187         }
188         catch (IOException ioe) {
189             throw new SearchException(
190                 "Cannot extract text from file" + companyId + " " + portletId +
191                     " " + groupId + " " + repositoryId + " " + fileName);
192         }
193 
194         doc.addText(Field.PROPERTIES, properties);
195         doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
196 
197         doc.addKeyword("repositoryId", repositoryId);
198         doc.addKeyword("path", fileName);
199 
200         if (_log.isDebugEnabled()) {
201             _log.debug(
202                 "Document " + companyId + " " + portletId + " " + groupId +
203                     " " + repositoryId + " " + fileName +
204                         " indexed successfully");
205         }
206 
207         return doc;
208     }
209 
210     public String getFileUID(
211         String portletId, long repositoryId, String fileName) {
212 
213         Document doc = new DocumentImpl();
214 
215         doc.addUID(portletId, repositoryId, fileName);
216 
217         return doc.get(Field.UID);
218     }
219 
220     public void updateFile(
221             long companyId, String portletId, long groupId, long repositoryId,
222             String fileName, String properties, Date modifiedDate,
223             String[] tagsEntries)
224         throws SearchException {
225 
226         Document doc = getFileDocument(
227             companyId, portletId, groupId, repositoryId, fileName, properties,
228             modifiedDate, tagsEntries);
229 
230         if (doc != null) {
231             SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
232         }
233     }
234 
235     private static Log _log = LogFactoryUtil.getLog(DLIndexerImpl.class);
236 
237 }