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