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<Map.Entry<Object, Object>> itr =
156                 extraSettingsProps.entrySet().iterator();
157 
158             while (itr.hasNext()) {
159                 Map.Entry<Object, Object> entry = itr.next();
160 
161                 String value = GetterUtil.getString((String)entry.getValue());
162 
163                 sm.append(value);
164             }
165 
166             String properties = sm.toString();
167 
168             String[] tagsEntries = TagsEntryLocalServiceUtil.getEntryNames(
169                 DLFileEntry.class.getName(), fileEntry.getFileEntryId());
170 
171             return getAddFileDocument(
172                 companyId, portletId, groupId, repositoryId, fileName,
173                 properties, tagsEntries);
174         }
175         catch (PortalException pe) {
176             throw new IOException(pe.getMessage());
177         }
178         catch (SystemException se) {
179             throw new IOException(se.getMessage());
180         }
181     }
182 
183     public static Document getAddFileDocument(
184             long companyId, String portletId, long groupId, long repositoryId,
185             String fileName, String properties, String[] tagsEntries)
186         throws IOException {
187 
188         if (_log.isDebugEnabled()) {
189             _log.debug(
190                 "Indexing document " + companyId + " " + portletId + " " +
191                     groupId + " " + repositoryId + " " + fileName);
192         }
193 
194         String fileExt = StringPool.BLANK;
195 
196         int fileExtVersionPos = fileName.indexOf(DLServiceImpl.VERSION);
197 
198         if (fileExtVersionPos != -1) {
199             int fileExtPos = fileName.lastIndexOf(
200                 StringPool.PERIOD, fileExtVersionPos);
201 
202             if (fileExtPos != -1) {
203                 fileExt = fileName.substring(fileExtPos, fileExtVersionPos);
204             }
205         }
206         else {
207             int fileExtPos = fileName.lastIndexOf(StringPool.PERIOD);
208 
209             if (fileExtPos != -1) {
210                 fileExt = fileName.substring(fileExtPos, fileName.length());
211             }
212         }
213 
214         InputStream is = null;
215 
216         try {
217             Hook hook = HookFactory.getInstance();
218 
219             is = hook.getFileAsStream(companyId, repositoryId, fileName);
220         }
221         catch (Exception e) {
222         }
223 
224         if (is == null) {
225             if (_log.isDebugEnabled()) {
226                 _log.debug(
227                     "Document " + companyId + " " + portletId + " " + groupId +
228                         " " + repositoryId + " " + fileName +
229                             " does not have any content");
230             }
231 
232             return null;
233         }
234 
235         Document doc = new Document();
236 
237         LuceneUtil.addKeyword(
238             doc, LuceneFields.UID,
239             LuceneFields.getUID(portletId, repositoryId, fileName));
240 
241         LuceneUtil.addKeyword(doc, LuceneFields.COMPANY_ID, companyId);
242         LuceneUtil.addKeyword(doc, LuceneFields.PORTLET_ID, portletId);
243         LuceneUtil.addKeyword(doc, LuceneFields.GROUP_ID, groupId);
244 
245         doc.add(LuceneFields.getFile(LuceneFields.CONTENT, is, fileExt));
246 
247         if (Validator.isNotNull(properties)) {
248             LuceneUtil.addText(doc, LuceneFields.PROPERTIES, properties);
249         }
250 
251         LuceneUtil.addModifiedDate(doc);
252 
253         LuceneUtil.addKeyword(doc, "repositoryId", repositoryId);
254         LuceneUtil.addKeyword(doc, "path", fileName);
255 
256         LuceneUtil.addKeyword(doc, LuceneFields.TAG_ENTRY, tagsEntries);
257 
258         if (_log.isDebugEnabled()) {
259             _log.debug(
260                 "Document " + companyId + " " + portletId + " " + groupId +
261                     " " + repositoryId + " " + fileName +
262                         " indexed successfully");
263         }
264 
265         return doc;
266     }
267 
268     public static void updateFile(
269             long companyId, String portletId, long groupId, long repositoryId,
270             String fileName, String properties, String[] tagsEntries)
271         throws IOException {
272 
273         try {
274             deleteFile(companyId, portletId, repositoryId, fileName);
275         }
276         catch (IOException ioe) {
277         }
278 
279         addFile(
280             companyId, portletId, groupId, repositoryId, fileName, properties,
281             tagsEntries);
282     }
283 
284     public DocumentSummary getDocumentSummary(
285         com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
286 
287         return null;
288     }
289 
290     public void reIndex(String[] ids) throws SearchException {
291         if (LuceneUtil.INDEX_READ_ONLY) {
292             return;
293         }
294 
295         Hook hook = HookFactory.getInstance();
296 
297         hook.reIndex(ids);
298     }
299 
300     private static Log _log = LogFactory.getLog(Indexer.class);
301 
302 }