1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.documentlibrary.util;
21  
22  import com.liferay.documentlibrary.service.impl.DLServiceImpl;
23  import com.liferay.portal.PortalException;
24  import com.liferay.portal.SystemException;
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.search.Document;
28  import com.liferay.portal.kernel.search.DocumentImpl;
29  import com.liferay.portal.kernel.search.DocumentSummary;
30  import com.liferay.portal.kernel.search.Field;
31  import com.liferay.portal.kernel.search.SearchEngineUtil;
32  import com.liferay.portal.kernel.search.SearchException;
33  import com.liferay.portal.kernel.util.GetterUtil;
34  import com.liferay.portal.kernel.util.StringPool;
35  import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
36  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
37  import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
38  import com.liferay.portlet.expando.model.ExpandoBridge;
39  import com.liferay.portlet.expando.model.impl.ExpandoBridgeImpl;
40  import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
41  import com.liferay.portlet.tags.model.TagsEntryConstants;
42  import com.liferay.portlet.tags.service.TagsEntryLocalServiceUtil;
43  
44  import java.io.IOException;
45  import java.io.InputStream;
46  
47  import java.util.Date;
48  import java.util.Iterator;
49  import java.util.Map;
50  import java.util.Properties;
51  
52  import javax.portlet.PortletURL;
53  
54  /**
55   * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
56   *
57   * @author Brian Wing Shun Chan
58   * @author Harry Mark
59   * @author Bruno Farache
60   * @author Raymond Augé
61   *
62   */
63  public class Indexer implements com.liferay.portal.kernel.search.Indexer {
64  
65      public static void addFile(
66              long companyId, String portletId, long groupId, long repositoryId,
67              String fileName)
68          throws SearchException {
69  
70          Document doc = getFileDocument(
71              companyId, portletId, groupId, repositoryId, fileName);
72  
73          if (doc != null) {
74              SearchEngineUtil.addDocument(companyId, doc);
75          }
76      }
77  
78      public static void addFile(
79              long companyId, String portletId, long groupId, long repositoryId,
80              String fileName, long fileEntryId, String properties,
81              Date modifiedDate, String[] tagsCategories, String[] tagsEntries)
82          throws SearchException {
83  
84          Document doc = getFileDocument(
85              companyId, portletId, groupId, repositoryId, fileName, fileEntryId,
86              properties, modifiedDate, tagsCategories, tagsEntries);
87  
88          if (doc != null) {
89              SearchEngineUtil.addDocument(companyId, doc);
90          }
91      }
92  
93      public static void deleteFile(
94              long companyId, String portletId, long repositoryId,
95              String fileName)
96          throws SearchException {
97  
98          SearchEngineUtil.deleteDocument(
99              companyId, getFileUID(portletId, repositoryId, fileName));
100     }
101 
102     public static Document getFileDocument(
103             long companyId, String portletId, long groupId, long repositoryId,
104             String fileName)
105         throws SearchException {
106 
107         try {
108             DLFileEntry fileEntry = null;
109 
110             try {
111                 fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
112                     repositoryId, fileName);
113             }
114             catch (NoSuchFileEntryException nsfe) {
115                 if (_log.isWarnEnabled()) {
116                     _log.warn(
117                         "File " + fileName + " in repository " +
118                             repositoryId + " exists in the JCR but does " +
119                                 "not exist in the database");
120                 }
121 
122                 return null;
123             }
124 
125             StringBuilder sb = new StringBuilder();
126 
127             sb.append(fileEntry.getTitle());
128             sb.append(StringPool.SPACE);
129             sb.append(fileEntry.getDescription());
130             sb.append(StringPool.SPACE);
131 
132             Properties extraSettingsProps =
133                 fileEntry.getExtraSettingsProperties();
134 
135             Iterator<Map.Entry<Object, Object>> itr =
136                 extraSettingsProps.entrySet().iterator();
137 
138             while (itr.hasNext()) {
139                 Map.Entry<Object, Object> entry = itr.next();
140 
141                 String value = GetterUtil.getString((String)entry.getValue());
142 
143                 sb.append(value);
144             }
145 
146             String properties = sb.toString();
147 
148             String[] tagsCategories = TagsEntryLocalServiceUtil.getEntryNames(
149                 DLFileEntry.class.getName(), fileEntry.getFileEntryId(),
150                 TagsEntryConstants.FOLKSONOMY_CATEGORY);
151             String[] tagsEntries = TagsEntryLocalServiceUtil.getEntryNames(
152                 DLFileEntry.class.getName(), fileEntry.getFileEntryId());
153 
154             return getFileDocument(
155                 companyId, portletId, groupId, repositoryId, fileName,
156                 fileEntry.getFileEntryId(), properties,
157                 fileEntry.getModifiedDate(), tagsCategories, tagsEntries);
158         }
159         catch (PortalException pe) {
160             throw new SearchException(pe.getMessage());
161         }
162         catch (SystemException se) {
163             throw new SearchException(se.getMessage());
164         }
165     }
166 
167     public static Document getFileDocument(
168             long companyId, String portletId, long groupId, long repositoryId,
169             String fileName, long fileEntryId, String properties,
170             Date modifiedDate, String[] tagsCategories, String[] tagsEntries)
171         throws SearchException {
172 
173         if (fileEntryId <= 0) {
174             _log.debug(
175                 "Not indexing document " + companyId + " " + portletId + " " +
176                     groupId + " " + repositoryId + " " + fileName + " " +
177                         fileEntryId);
178 
179             return null;
180         }
181 
182         if (_log.isDebugEnabled()) {
183             _log.debug(
184                 "Indexing document " + companyId + " " + portletId + " " +
185                     groupId + " " + repositoryId + " " + fileName + " " +
186                         fileEntryId);
187         }
188 
189         String fileExt = StringPool.BLANK;
190 
191         int fileExtVersionPos = fileName.indexOf(DLServiceImpl.VERSION);
192 
193         if (fileExtVersionPos != -1) {
194             int fileExtPos = fileName.lastIndexOf(
195                 StringPool.PERIOD, fileExtVersionPos);
196 
197             if (fileExtPos != -1) {
198                 fileExt = fileName.substring(fileExtPos, fileExtVersionPos);
199             }
200         }
201         else {
202             int fileExtPos = fileName.lastIndexOf(StringPool.PERIOD);
203 
204             if (fileExtPos != -1) {
205                 fileExt = fileName.substring(fileExtPos, fileName.length());
206             }
207         }
208 
209         InputStream is = null;
210 
211         try {
212             Hook hook = HookFactory.getInstance();
213 
214             is = hook.getFileAsStream(companyId, repositoryId, fileName);
215         }
216         catch (Exception e) {
217         }
218 
219         if (is == null) {
220             if (_log.isDebugEnabled()) {
221                 _log.debug(
222                     "Document " + companyId + " " + portletId + " " + groupId +
223                         " " + repositoryId + " " + fileName + " " +
224                             fileEntryId + " does not have any content");
225             }
226 
227             return null;
228         }
229 
230         Document doc = new DocumentImpl();
231 
232         doc.addUID(portletId, repositoryId, fileName);
233 
234         doc.addModifiedDate(modifiedDate);
235 
236         doc.addKeyword(Field.COMPANY_ID, companyId);
237         doc.addKeyword(Field.PORTLET_ID, portletId);
238         doc.addKeyword(Field.GROUP_ID, groupId);
239 
240         try {
241             doc.addFile(Field.CONTENT, is, fileExt);
242         }
243         catch (IOException ioe) {
244             throw new SearchException(
245                 "Cannot extract text from file" + companyId + " " + portletId +
246                     " " + groupId + " " + repositoryId + " " + fileName);
247         }
248 
249         doc.addText(Field.PROPERTIES, properties);
250         doc.addKeyword(Field.TAGS_CATEGORIES, tagsCategories);
251         doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
252 
253         doc.addKeyword("repositoryId", repositoryId);
254         doc.addKeyword("path", fileName);
255         doc.addKeyword(Field.ENTRY_CLASS_NAME, DLFileEntry.class.getName());
256         doc.addKeyword(Field.ENTRY_CLASS_PK, fileEntryId);
257 
258         ExpandoBridge expandoBridge = new ExpandoBridgeImpl(
259             DLFileEntry.class.getName(), fileEntryId);
260 
261         ExpandoBridgeIndexerUtil.addAttributes(doc, expandoBridge);
262 
263         if (_log.isDebugEnabled()) {
264             _log.debug(
265                 "Document " + companyId + " " + portletId + " " + groupId +
266                     " " + repositoryId + " " + fileName + " " + fileEntryId +
267                         " indexed successfully");
268         }
269 
270         return doc;
271     }
272 
273     public static String getFileUID(
274             String portletId, long repositoryId, String fileName) {
275         Document doc = new DocumentImpl();
276 
277         doc.addUID(portletId, repositoryId, fileName);
278 
279         return doc.get(Field.UID);
280     }
281 
282     public static void updateFile(
283             long companyId, String portletId, long groupId, long repositoryId,
284             String fileName, long fileEntryId, String properties,
285             Date modifiedDate, String[] tagsCategories, String[] tagsEntries)
286         throws SearchException {
287 
288         Document doc = getFileDocument(
289             companyId, portletId, groupId, repositoryId, fileName, fileEntryId,
290             properties, modifiedDate, tagsCategories, tagsEntries);
291 
292         if (doc != null) {
293             SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
294         }
295     }
296 
297     public String[] getClassNames() {
298         return _CLASS_NAMES;
299     }
300 
301     public DocumentSummary getDocumentSummary(
302         com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
303 
304         return null;
305     }
306 
307     public void reIndex(String className, long classPK) {
308     }
309 
310     public void reIndex(String[] ids) throws SearchException {
311         if (SearchEngineUtil.isIndexReadOnly()) {
312             return;
313         }
314 
315         Hook hook = HookFactory.getInstance();
316 
317         hook.reIndex(ids);
318     }
319 
320     private static final String[] _CLASS_NAMES = new String[0];
321 
322     private static Log _log = LogFactoryUtil.getLog(Indexer.class);
323 
324 }