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