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