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