1
19
20 package com.liferay.documentlibrary.util;
21
22 import com.liferay.documentlibrary.service.impl.DLServiceImpl;
23 import com.liferay.portal.PortalException;
24 import com.liferay.portal.SystemException;
25 import com.liferay.portal.kernel.log.Log;
26 import com.liferay.portal.kernel.log.LogFactoryUtil;
27 import com.liferay.portal.kernel.search.Document;
28 import com.liferay.portal.kernel.search.DocumentImpl;
29 import com.liferay.portal.kernel.search.DocumentSummary;
30 import com.liferay.portal.kernel.search.Field;
31 import com.liferay.portal.kernel.search.SearchEngineUtil;
32 import com.liferay.portal.kernel.search.SearchException;
33 import com.liferay.portal.kernel.util.GetterUtil;
34 import com.liferay.portal.kernel.util.StringPool;
35 import com.liferay.portal.util.PropsValues;
36 import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
37 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
38 import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
39 import com.liferay.portlet.tags.service.TagsEntryLocalServiceUtil;
40
41 import java.io.IOException;
42 import java.io.InputStream;
43
44 import java.util.Date;
45 import java.util.Iterator;
46 import java.util.Map;
47 import java.util.Properties;
48
49 import javax.portlet.PortletURL;
50
51
59 public class Indexer implements com.liferay.portal.kernel.search.Indexer {
60
61 public static void addFile(
62 long companyId, String portletId, long groupId, long repositoryId,
63 String fileName)
64 throws SearchException {
65
66 Document doc = getFileDocument(
67 companyId, portletId, groupId, repositoryId, fileName);
68
69 if (doc != null) {
70 SearchEngineUtil.addDocument(companyId, doc);
71 }
72 }
73
74 public static void addFile(
75 long companyId, String portletId, long groupId, long repositoryId,
76 String fileName, String properties, Date modifiedDate,
77 String[] tagsEntries)
78 throws SearchException {
79
80 Document doc = getFileDocument(
81 companyId, portletId, groupId, repositoryId, fileName, properties,
82 modifiedDate, tagsEntries);
83
84 if (doc != null) {
85 SearchEngineUtil.addDocument(companyId, doc);
86 }
87 }
88
89 public static void deleteFile(
90 long companyId, String portletId, long repositoryId,
91 String fileName)
92 throws SearchException {
93
94 SearchEngineUtil.deleteDocument(
95 companyId, getFileUID(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 properties, fileEntry.getModifiedDate(), tagsEntries);
150 }
151 catch (PortalException pe) {
152 throw new SearchException(pe.getMessage());
153 }
154 catch (SystemException se) {
155 throw new SearchException(se.getMessage());
156 }
157 }
158
159 public static Document getFileDocument(
160 long companyId, String portletId, long groupId, long repositoryId,
161 String fileName, String properties, Date modifiedDate,
162 String[] tagsEntries)
163 throws SearchException {
164
165 if (_log.isDebugEnabled()) {
166 _log.debug(
167 "Indexing document " + companyId + " " + portletId + " " +
168 groupId + " " + repositoryId + " " + fileName);
169 }
170
171 String fileExt = StringPool.BLANK;
172
173 int fileExtVersionPos = fileName.indexOf(DLServiceImpl.VERSION);
174
175 if (fileExtVersionPos != -1) {
176 int fileExtPos = fileName.lastIndexOf(
177 StringPool.PERIOD, fileExtVersionPos);
178
179 if (fileExtPos != -1) {
180 fileExt = fileName.substring(fileExtPos, fileExtVersionPos);
181 }
182 }
183 else {
184 int fileExtPos = fileName.lastIndexOf(StringPool.PERIOD);
185
186 if (fileExtPos != -1) {
187 fileExt = fileName.substring(fileExtPos, fileName.length());
188 }
189 }
190
191 InputStream is = null;
192
193 try {
194 Hook hook = HookFactory.getInstance();
195
196 is = hook.getFileAsStream(companyId, repositoryId, fileName);
197 }
198 catch (Exception e) {
199 }
200
201 if (is == null) {
202 if (_log.isDebugEnabled()) {
203 _log.debug(
204 "Document " + companyId + " " + portletId + " " + groupId +
205 " " + repositoryId + " " + fileName +
206 " does not have any content");
207 }
208
209 return null;
210 }
211
212 Document doc = new DocumentImpl();
213
214 doc.addUID(portletId, repositoryId, fileName);
215
216 doc.addModifiedDate(modifiedDate);
217
218 doc.addKeyword(Field.COMPANY_ID, companyId);
219 doc.addKeyword(Field.PORTLET_ID, portletId);
220 doc.addKeyword(Field.GROUP_ID, groupId);
221
222 try {
223 doc.addFile(Field.CONTENT, is, fileExt);
224 }
225 catch (IOException ioe) {
226 throw new SearchException(
227 "Cannot extract text from file" + companyId + " " + portletId +
228 " " + groupId + " " + repositoryId + " " + fileName);
229 }
230
231 doc.addText(Field.PROPERTIES, properties);
232 doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
233
234 doc.addKeyword("repositoryId", repositoryId);
235 doc.addKeyword("path", fileName);
236
237 if (_log.isDebugEnabled()) {
238 _log.debug(
239 "Document " + companyId + " " + portletId + " " + groupId +
240 " " + repositoryId + " " + fileName +
241 " indexed successfully");
242 }
243
244 return doc;
245 }
246
247 public static String getFileUID(
248 String portletId, long repositoryId, String fileName) {
249 Document doc = new DocumentImpl();
250
251 doc.addUID(portletId, repositoryId, fileName);
252
253 return doc.get(Field.UID);
254 }
255
256 public static void updateFile(
257 long companyId, String portletId, long groupId, long repositoryId,
258 String fileName, String properties, Date modifiedDate,
259 String[] tagsEntries)
260 throws SearchException {
261
262 Document doc = getFileDocument(
263 companyId, portletId, groupId, repositoryId, fileName, properties,
264 modifiedDate, tagsEntries);
265
266 if (doc != null) {
267 SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
268 }
269 }
270
271 public DocumentSummary getDocumentSummary(
272 com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
273
274 return null;
275 }
276
277 public void reIndex(String[] ids) throws SearchException {
278 if (PropsValues.INDEX_READ_ONLY) {
279 return;
280 }
281
282 Hook hook = HookFactory.getInstance();
283
284 hook.reIndex(ids);
285 }
286
287 private static Log _log = LogFactoryUtil.getLog(Indexer.class);
288
289 }