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