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