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