1   /**
2    * Copyright (c) 2000-2008 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.search.Document;
29  import com.liferay.portal.kernel.search.DocumentImpl;
30  import com.liferay.portal.kernel.search.DocumentSummary;
31  import com.liferay.portal.kernel.search.Field;
32  import com.liferay.portal.kernel.search.SearchEngineUtil;
33  import com.liferay.portal.kernel.search.SearchException;
34  import com.liferay.portal.kernel.util.GetterUtil;
35  import com.liferay.portal.kernel.util.StringPool;
36  import com.liferay.portal.kernel.util.Validator;
37  import com.liferay.portal.util.PropsValues;
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.tags.service.TagsEntryLocalServiceUtil;
42  
43  import java.io.IOException;
44  import java.io.InputStream;
45  
46  import java.util.Iterator;
47  import java.util.Map;
48  import java.util.Properties;
49  
50  import javax.portlet.PortletURL;
51  
52  import org.apache.commons.logging.Log;
53  import org.apache.commons.logging.LogFactory;
54  
55  /**
56   * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
57   *
58   * @author Brian Wing Shun Chan
59   * @author Harry Mark
60   * @author Bruno Farache
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          SearchEngineUtil.addDocument(companyId, doc);
74      }
75  
76      public static void addFile(
77              long companyId, String portletId, long groupId, long repositoryId,
78              String fileName, String properties, String[] tagsEntries)
79          throws SearchException {
80  
81          Document doc = getFileDocument(
82              companyId, portletId, groupId, repositoryId, fileName, properties,
83              tagsEntries);
84  
85          SearchEngineUtil.addDocument(companyId, doc);
86      }
87  
88      public static void deleteFile(
89              long companyId, String portletId, long repositoryId,
90              String fileName)
91          throws SearchException {
92  
93          SearchEngineUtil.deleteDocument(companyId, getFileUID(
94              portletId, repositoryId, fileName));
95      }
96  
97      public static Document getFileDocument(
98              long companyId, String portletId, long groupId, long repositoryId,
99              String fileName)
100         throws SearchException {
101 
102         try {
103             DLFileEntry fileEntry = null;
104 
105             try {
106                 fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
107                     repositoryId, fileName);
108             }
109             catch (NoSuchFileEntryException nsfe) {
110                 if (_log.isWarnEnabled()) {
111                     _log.warn(
112                         "File " + fileName + " in repository " +
113                             repositoryId + " exists in the JCR but does " +
114                                 "not exist in the database");
115                 }
116 
117                 return null;
118             }
119 
120             StringBuilder sb = new StringBuilder();
121 
122             sb.append(fileEntry.getTitle());
123             sb.append(StringPool.SPACE);
124             sb.append(fileEntry.getDescription());
125             sb.append(StringPool.SPACE);
126 
127             Properties extraSettingsProps =
128                 fileEntry.getExtraSettingsProperties();
129 
130             Iterator<Map.Entry<Object, Object>> itr =
131                 extraSettingsProps.entrySet().iterator();
132 
133             while (itr.hasNext()) {
134                 Map.Entry<Object, Object> entry = itr.next();
135 
136                 String value = GetterUtil.getString((String)entry.getValue());
137 
138                 sb.append(value);
139             }
140 
141             String properties = sb.toString();
142 
143             String[] tagsEntries = TagsEntryLocalServiceUtil.getEntryNames(
144                 DLFileEntry.class.getName(), fileEntry.getFileEntryId());
145 
146             return getFileDocument(
147                 companyId, portletId, groupId, repositoryId, fileName,
148                 properties, tagsEntries);
149         }
150         catch (PortalException pe) {
151             throw new SearchException(pe.getMessage());
152         }
153         catch (SystemException se) {
154             throw new SearchException(se.getMessage());
155         }
156     }
157 
158     public static Document getFileDocument(
159             long companyId, String portletId, long groupId, long repositoryId,
160             String fileName, String properties, String[] tagsEntries)
161         throws SearchException {
162 
163         if (_log.isDebugEnabled()) {
164             _log.debug(
165                 "Indexing document " + companyId + " " + portletId + " " +
166                     groupId + " " + repositoryId + " " + fileName);
167         }
168 
169         String fileExt = StringPool.BLANK;
170 
171         int fileExtVersionPos = fileName.indexOf(DLServiceImpl.VERSION);
172 
173         if (fileExtVersionPos != -1) {
174             int fileExtPos = fileName.lastIndexOf(
175                 StringPool.PERIOD, fileExtVersionPos);
176 
177             if (fileExtPos != -1) {
178                 fileExt = fileName.substring(fileExtPos, fileExtVersionPos);
179             }
180         }
181         else {
182             int fileExtPos = fileName.lastIndexOf(StringPool.PERIOD);
183 
184             if (fileExtPos != -1) {
185                 fileExt = fileName.substring(fileExtPos, fileName.length());
186             }
187         }
188 
189         InputStream is = null;
190 
191         try {
192             Hook hook = HookFactory.getInstance();
193 
194             is = hook.getFileAsStream(companyId, repositoryId, fileName);
195         }
196         catch (Exception e) {
197         }
198 
199         if (is == null) {
200             if (_log.isDebugEnabled()) {
201                 _log.debug(
202                     "Document " + companyId + " " + portletId + " " + groupId +
203                         " " + repositoryId + " " + fileName +
204                             " does not have any content");
205             }
206 
207             return null;
208         }
209 
210         Document doc = new DocumentImpl();
211 
212         doc.addUID(portletId, repositoryId, fileName);
213 
214         doc.addKeyword(Field.COMPANY_ID, companyId);
215         doc.addKeyword(Field.PORTLET_ID, portletId);
216         doc.addKeyword(Field.GROUP_ID, groupId);
217 
218         try {
219             doc.addFile(Field.CONTENT, is, fileExt);
220         }
221         catch (IOException ioe) {
222             throw new SearchException(
223                 "Cannot extract text from file" + companyId + " " + portletId +
224                     " " + groupId + " " + repositoryId + " " + fileName);
225         }
226 
227         if (Validator.isNotNull(properties)) {
228             doc.addText(Field.PROPERTIES, properties);
229         }
230 
231         doc.addModifiedDate();
232 
233         doc.addKeyword("repositoryId", repositoryId);
234         doc.addKeyword("path", fileName);
235 
236         doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
237 
238         if (_log.isDebugEnabled()) {
239             _log.debug(
240                 "Document " + companyId + " " + portletId + " " + groupId +
241                     " " + repositoryId + " " + fileName +
242                         " indexed successfully");
243         }
244 
245         return doc;
246     }
247 
248     public static String getFileUID(
249             String portletId, long repositoryId, String fileName) {
250         Document doc = new DocumentImpl();
251 
252         doc.addUID(portletId, repositoryId, fileName);
253 
254         return doc.get(Field.UID);
255     }
256 
257     public static void updateFile(
258             long companyId, String portletId, long groupId, long repositoryId,
259             String fileName, String properties, String[] tagsEntries)
260         throws SearchException {
261 
262         Document doc = getFileDocument(
263             companyId, portletId, groupId, repositoryId, fileName, properties,
264             tagsEntries);
265 
266         SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
267     }
268 
269     public DocumentSummary getDocumentSummary(
270         com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
271 
272         return null;
273     }
274 
275     public void reIndex(String[] ids) throws SearchException {
276         if (PropsValues.INDEX_READ_ONLY) {
277             return;
278         }
279 
280         Hook hook = HookFactory.getInstance();
281 
282         hook.reIndex(ids);
283     }
284 
285     private static Log _log = LogFactory.getLog(Indexer.class);
286 
287 }