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