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