1
14
15 package com.liferay.documentlibrary.util;
16
17 import com.liferay.documentlibrary.service.impl.DLServiceImpl;
18 import com.liferay.portal.PortalException;
19 import com.liferay.portal.SystemException;
20 import com.liferay.portal.kernel.log.Log;
21 import com.liferay.portal.kernel.log.LogFactoryUtil;
22 import com.liferay.portal.kernel.search.Document;
23 import com.liferay.portal.kernel.search.DocumentImpl;
24 import com.liferay.portal.kernel.search.Field;
25 import com.liferay.portal.kernel.search.SearchEngineUtil;
26 import com.liferay.portal.kernel.search.SearchException;
27 import com.liferay.portal.kernel.util.StringPool;
28 import com.liferay.portal.model.Group;
29 import com.liferay.portal.service.GroupLocalServiceUtil;
30 import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
31 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
32 import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
33 import com.liferay.portlet.expando.model.ExpandoBridge;
34 import com.liferay.portlet.expando.util.ExpandoBridgeFactoryUtil;
35 import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
36 import com.liferay.portlet.tags.model.TagsEntryConstants;
37 import com.liferay.portlet.tags.service.TagsEntryLocalServiceUtil;
38
39 import java.io.IOException;
40 import java.io.InputStream;
41
42 import java.util.Date;
43
44
49 public class DLIndexerImpl implements DLIndexer {
50
51 public void addFile(
52 long companyId, String portletId, long groupId, long userId,
53 long repositoryId, String fileName)
54 throws SearchException {
55
56 Document doc = getFileDocument(
57 companyId, portletId, groupId, userId, repositoryId, fileName);
58
59 if (doc != null) {
60 SearchEngineUtil.addDocument(companyId, doc);
61 }
62 }
63
64 public void addFile(
65 long companyId, String portletId, long groupId, long userId,
66 long repositoryId, String fileName, long fileEntryId,
67 String properties, Date modifiedDate, String[] tagsCategories,
68 String[] tagsEntries)
69 throws SearchException {
70
71 Document doc = getFileDocument(
72 companyId, portletId, groupId, userId, repositoryId, fileName,
73 fileEntryId, properties, modifiedDate, tagsCategories, tagsEntries);
74
75 if (doc != null) {
76 SearchEngineUtil.addDocument(companyId, doc);
77 }
78 }
79
80
83 public void addFile(
84 long companyId, String portletId, long groupId, long repositoryId,
85 String fileName)
86 throws SearchException {
87
88 addFile(companyId, portletId, groupId, 0, repositoryId, fileName);
89 }
90
91
94 public void addFile(
95 long companyId, String portletId, long groupId, long repositoryId,
96 String fileName, long fileEntryId, String properties,
97 Date modifiedDate, String[] tagsCategories, String[] tagsEntries)
98 throws SearchException {
99
100 addFile(
101 companyId, portletId, groupId, 0, repositoryId, fileName,
102 fileEntryId, properties, modifiedDate, tagsCategories, tagsEntries);
103 }
104
105 public void deleteFile(
106 long companyId, String portletId, long repositoryId,
107 String fileName)
108 throws SearchException {
109
110 SearchEngineUtil.deleteDocument(
111 companyId, getFileUID(portletId, repositoryId, fileName));
112 }
113
114 public Document getFileDocument(
115 long companyId, String portletId, long groupId, long userId,
116 long repositoryId, String fileName)
117 throws SearchException {
118
119 try {
120 DLFileEntry fileEntry = null;
121
122 try {
123 fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
124 repositoryId, fileName);
125 }
126 catch (NoSuchFileEntryException nsfe) {
127 if (_log.isWarnEnabled()) {
128 _log.warn(
129 "File " + fileName + " in repository " +
130 repositoryId + " exists in the JCR but does " +
131 "not exist in the database");
132 }
133
134 return null;
135 }
136
137 if (userId == 0) {
138 userId = fileEntry.getUserId();
139 }
140
141 String properties = fileEntry.getLuceneProperties();
142
143 String[] tagsCategories = TagsEntryLocalServiceUtil.getEntryNames(
144 DLFileEntry.class.getName(), fileEntry.getFileEntryId(),
145 TagsEntryConstants.FOLKSONOMY_CATEGORY);
146 String[] tagsEntries = TagsEntryLocalServiceUtil.getEntryNames(
147 DLFileEntry.class.getName(), fileEntry.getFileEntryId());
148
149 return getFileDocument(
150 companyId, portletId, groupId, userId, repositoryId, fileName,
151 fileEntry.getFileEntryId(), properties,
152 fileEntry.getModifiedDate(), tagsCategories, tagsEntries);
153 }
154 catch (PortalException pe) {
155 throw new SearchException(pe.getMessage());
156 }
157 catch (SystemException se) {
158 throw new SearchException(se.getMessage());
159 }
160 }
161
162 public Document getFileDocument(
163 long companyId, String portletId, long groupId, long userId,
164 long repositoryId, String fileName, long fileEntryId,
165 String properties, Date modifiedDate, String[] tagsCategories,
166 String[] tagsEntries)
167 throws SearchException {
168
169 long scopeGroupId = groupId;
170
171 try {
172 Group group = GroupLocalServiceUtil.getGroup(groupId);
173
174 if (group.isLayout()) {
175 groupId = group.getParentGroupId();
176 }
177 }
178 catch (Exception e) {
179 }
180
181 if (fileEntryId <= 0) {
182 _log.debug(
183 "Not indexing document " + companyId + " " + portletId + " " +
184 scopeGroupId + " " + repositoryId + " " + fileName + " " +
185 fileEntryId);
186
187 return null;
188 }
189
190 if (userId == 0) {
191 try {
192 DLFileEntry fileEntry =
193 DLFileEntryLocalServiceUtil.getFileEntry(fileEntryId);
194
195 userId = fileEntry.getUserId();
196 }
197 catch (Exception e) {
198 }
199 }
200
201 if (_log.isDebugEnabled()) {
202 _log.debug(
203 "Indexing document " + companyId + " " + portletId + " " +
204 scopeGroupId + " " + repositoryId + " " + fileName + " " +
205 fileEntryId);
206 }
207
208 String fileExt = StringPool.BLANK;
209
210 int fileExtVersionPos = fileName.indexOf(DLServiceImpl.VERSION);
211
212 if (fileExtVersionPos != -1) {
213 int fileExtPos = fileName.lastIndexOf(
214 StringPool.PERIOD, fileExtVersionPos);
215
216 if (fileExtPos != -1) {
217 fileExt = fileName.substring(fileExtPos, fileExtVersionPos);
218 }
219 }
220 else {
221 int fileExtPos = fileName.lastIndexOf(StringPool.PERIOD);
222
223 if (fileExtPos != -1) {
224 fileExt = fileName.substring(fileExtPos, fileName.length());
225 }
226 }
227
228 InputStream is = null;
229
230 try {
231 Hook hook = HookFactory.getInstance();
232
233 is = hook.getFileAsStream(companyId, repositoryId, fileName);
234 }
235 catch (Exception e) {
236 }
237
238 if (is == null) {
239 if (_log.isDebugEnabled()) {
240 _log.debug(
241 "Document " + companyId + " " + portletId + " " +
242 scopeGroupId + " " + repositoryId + " " + fileName +
243 " " + fileEntryId + " does not have any content");
244 }
245
246 return null;
247 }
248
249 Document doc = new DocumentImpl();
250
251 doc.addUID(portletId, repositoryId, fileName);
252
253 doc.addModifiedDate(modifiedDate);
254
255 doc.addKeyword(Field.COMPANY_ID, companyId);
256 doc.addKeyword(Field.PORTLET_ID, portletId);
257 doc.addKeyword(Field.GROUP_ID, groupId);
258 doc.addKeyword(Field.SCOPE_GROUP_ID, scopeGroupId);
259 doc.addKeyword(Field.USER_ID, userId);
260
261 try {
262 doc.addFile(Field.CONTENT, is, fileExt);
263 }
264 catch (IOException ioe) {
265 throw new SearchException(
266 "Cannot extract text from file" + companyId + " " + portletId +
267 " " + scopeGroupId + " " + repositoryId + " " + fileName);
268 }
269
270 doc.addText(Field.PROPERTIES, properties);
271 doc.addKeyword(Field.TAGS_CATEGORIES, tagsCategories);
272 doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
273
274 doc.addKeyword("repositoryId", repositoryId);
275 doc.addKeyword("path", fileName);
276 doc.addKeyword(Field.ENTRY_CLASS_NAME, DLFileEntry.class.getName());
277 doc.addKeyword(Field.ENTRY_CLASS_PK, fileEntryId);
278
279 ExpandoBridge expandoBridge = ExpandoBridgeFactoryUtil.getExpandoBridge(
280 DLFileEntry.class.getName(), fileEntryId);
281
282 ExpandoBridgeIndexerUtil.addAttributes(doc, expandoBridge);
283
284 if (_log.isDebugEnabled()) {
285 _log.debug(
286 "Document " + companyId + " " + portletId + " " +
287 scopeGroupId + " " + repositoryId + " " + fileName + " " +
288 fileEntryId + " indexed successfully");
289 }
290
291 return doc;
292 }
293
294
297 public Document getFileDocument(
298 long companyId, String portletId, long groupId, long repositoryId,
299 String fileName)
300 throws SearchException {
301
302 return getFileDocument(
303 companyId, portletId, groupId, 0, repositoryId, fileName);
304 }
305
306
309 public Document getFileDocument(
310 long companyId, String portletId, long groupId, long repositoryId,
311 String fileName, long fileEntryId, String properties,
312 Date modifiedDate, String[] tagsCategories, String[] tagsEntries)
313 throws SearchException {
314
315 return getFileDocument(
316 companyId, portletId, groupId, 0, repositoryId, fileName,
317 fileEntryId, properties, modifiedDate, tagsCategories, tagsEntries);
318 }
319
320 public String getFileUID(
321 String portletId, long repositoryId, String fileName) {
322
323 Document doc = new DocumentImpl();
324
325 doc.addUID(portletId, repositoryId, fileName);
326
327 return doc.get(Field.UID);
328 }
329
330 public void updateFile(
331 long companyId, String portletId, long groupId, long userId,
332 long repositoryId, String fileName, long fileEntryId,
333 String properties, Date modifiedDate, String[] tagsCategories,
334 String[] tagsEntries)
335 throws SearchException {
336
337 Document doc = getFileDocument(
338 companyId, portletId, groupId, userId, repositoryId, fileName,
339 fileEntryId, properties, modifiedDate, tagsCategories, tagsEntries);
340
341 if (doc != null) {
342 SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
343 }
344 }
345
346
349 public void updateFile(
350 long companyId, String portletId, long groupId, long repositoryId,
351 String fileName, long fileEntryId, String properties,
352 Date modifiedDate, String[] tagsCategories, String[] tagsEntries)
353 throws SearchException {
354
355 updateFile(
356 companyId, portletId, groupId, 0, repositoryId, fileName,
357 fileEntryId, properties, modifiedDate, tagsCategories, tagsEntries);
358 }
359
360 private static Log _log = LogFactoryUtil.getLog(DLIndexerImpl.class);
361
362 }