1
14
15 package com.liferay.documentlibrary.util;
16
17 import com.liferay.documentlibrary.model.FileModel;
18 import com.liferay.portal.kernel.log.Log;
19 import com.liferay.portal.kernel.log.LogFactoryUtil;
20 import com.liferay.portal.kernel.search.BaseIndexer;
21 import com.liferay.portal.kernel.search.Document;
22 import com.liferay.portal.kernel.search.DocumentImpl;
23 import com.liferay.portal.kernel.search.Field;
24 import com.liferay.portal.kernel.search.SearchContext;
25 import com.liferay.portal.kernel.search.SearchEngineUtil;
26 import com.liferay.portal.kernel.search.SearchException;
27 import com.liferay.portal.kernel.search.Summary;
28 import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
29 import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
30 import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
31 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
32 import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryImpl;
33 import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
34 import com.liferay.portlet.expando.model.ExpandoBridge;
35 import com.liferay.portlet.expando.util.ExpandoBridgeFactoryUtil;
36 import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
37
38 import java.io.IOException;
39 import java.io.InputStream;
40
41 import java.util.Date;
42
43 import javax.portlet.PortletURL;
44
45
53 public class DLIndexer extends BaseIndexer {
54
55 public static final String[] CLASS_NAMES = {FileModel.class.getName()};
56
57 public String[] getClassNames() {
58 return CLASS_NAMES;
59 }
60
61 public Summary getSummary(
62 Document document, String snippet, PortletURL portletURL) {
63
64 return null;
65 }
66
67 protected void doDelete(Object obj) throws Exception {
68 FileModel fileModel = (FileModel)obj;
69
70 Document document = new DocumentImpl();
71
72 document.addUID(
73 fileModel.getPortletId(), fileModel.getRepositoryId(),
74 fileModel.getFileName());
75
76 SearchEngineUtil.deleteDocument(
77 fileModel.getCompanyId(), document.get(Field.UID));
78 }
79
80 protected Document doGetDocument(Object obj) throws Exception {
81 FileModel fileModel = (FileModel)obj;
82
83 long companyId = fileModel.getCompanyId();
84 String portletId = fileModel.getPortletId();
85 long groupId = getParentGroupId(fileModel.getGroupId());
86 long scopeGroupId = fileModel.getGroupId();
87 long userId = fileModel.getUserId();
88 long folderId = DLFileEntryImpl.getFolderId(
89 groupId, fileModel.getRepositoryId());
90 long repositoryId = fileModel.getRepositoryId();
91 String fileName = fileModel.getFileName();
92 long fileEntryId = fileModel.getFileEntryId();
93 String properties = fileModel.getProperties();
94 Date modifiedDate = fileModel.getModifiedDate();
95 long[] assetCategoryIds = fileModel.getAssetCategoryIds();
96 String[] assetTagNames = fileModel.getAssetTagNames();
97
98 DLFileEntry fileEntry = null;
99
100 try {
101 if (fileEntryId > 0) {
102 fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
103 fileEntryId);
104 }
105 else {
106 fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
107 groupId, folderId, fileName);
108 }
109 }
110 catch (NoSuchFileEntryException nsfe) {
111 if (_log.isDebugEnabled()) {
112 _log.debug(
113 "Not indexing document " + companyId + " " + portletId +
114 " " + scopeGroupId + " " + repositoryId + " " +
115 fileName + " " + fileEntryId);
116 }
117
118 return null;
119 }
120
121 if (userId == 0) {
122 userId = fileEntry.getUserId();
123 }
124
125 if (properties == null) {
126 properties = fileEntry.getLuceneProperties();
127 }
128
129 if (modifiedDate == null) {
130 modifiedDate = fileEntry.getModifiedDate();
131 }
132
133 if (assetCategoryIds == null) {
134 assetCategoryIds = AssetCategoryLocalServiceUtil.getCategoryIds(
135 DLFileEntry.class.getName(), fileEntry.getFileEntryId());
136 }
137
138 if (assetTagNames == null) {
139 assetTagNames = AssetTagLocalServiceUtil.getTagNames(
140 DLFileEntry.class.getName(), fileEntry.getFileEntryId());
141 }
142
143 if (_log.isDebugEnabled()) {
144 _log.debug(
145 "Indexing document " + companyId + " " + portletId + " " +
146 scopeGroupId + " " + repositoryId + " " + fileName + " " +
147 fileEntryId);
148 }
149
150 InputStream is = null;
151
152 try {
153 Hook hook = HookFactory.getInstance();
154
155 is = hook.getFileAsStream(companyId, repositoryId, fileName);
156 }
157 catch (Exception e) {
158 }
159
160 if (is == null) {
161 if (_log.isDebugEnabled()) {
162 _log.debug(
163 "Document " + companyId + " " + portletId + " " +
164 scopeGroupId + " " + repositoryId + " " + fileName +
165 " " + fileEntryId + " does not have any content");
166 }
167
168 return null;
169 }
170
171 Document document = new DocumentImpl();
172
173 document.addUID(portletId, repositoryId, fileName);
174
175 document.addModifiedDate(modifiedDate);
176
177 document.addKeyword(Field.COMPANY_ID, companyId);
178 document.addKeyword(Field.PORTLET_ID, portletId);
179 document.addKeyword(Field.GROUP_ID, groupId);
180 document.addKeyword(Field.SCOPE_GROUP_ID, scopeGroupId);
181 document.addKeyword(Field.USER_ID, userId);
182
183 try {
184 document.addFile(Field.CONTENT, is, fileEntry.getTitle());
185 }
186 catch (IOException ioe) {
187 throw new SearchException(
188 "Cannot extract text from file" + companyId + " " + portletId +
189 " " + scopeGroupId + " " + repositoryId + " " + fileName);
190 }
191
192 document.addText(Field.PROPERTIES, properties);
193 document.addKeyword(Field.ASSET_CATEGORY_IDS, assetCategoryIds);
194 document.addKeyword(Field.ASSET_TAG_NAMES, assetTagNames);
195
196 document.addKeyword(Field.FOLDER_ID, folderId);
197 document.addKeyword("repositoryId", repositoryId);
198 document.addKeyword("path", fileName);
199 document.addKeyword(
200 Field.ENTRY_CLASS_NAME, DLFileEntry.class.getName());
201 document.addKeyword(Field.ENTRY_CLASS_PK, fileEntryId);
202
203 ExpandoBridge expandoBridge = ExpandoBridgeFactoryUtil.getExpandoBridge(
204 companyId, DLFileEntry.class.getName(), fileEntryId);
205
206 ExpandoBridgeIndexerUtil.addAttributes(document, expandoBridge);
207
208 if (_log.isDebugEnabled()) {
209 _log.debug(
210 "Document " + companyId + " " + portletId + " " +
211 scopeGroupId + " " + repositoryId + " " + fileName + " " +
212 fileEntryId + " indexed successfully");
213 }
214
215 return document;
216 }
217
218 protected void doReindex(Object obj) throws Exception {
219 FileModel fileModel = (FileModel)obj;
220
221 Document document = getDocument(fileModel);
222
223 if (document != null) {
224 SearchEngineUtil.updateDocument(fileModel.getCompanyId(), document);
225 }
226 }
227
228 protected void doReindex(String className, long classPK) throws Exception {
229 }
230
231 protected void doReindex(String[] ids) throws Exception {
232 Hook hook = HookFactory.getInstance();
233
234 hook.reindex(ids);
235 }
236
237 protected String getPortletId(SearchContext searchContext) {
238 return (String)searchContext.getAttribute("portletId");
239 }
240
241 private static Log _log = LogFactoryUtil.getLog(DLIndexer.class);
242
243 }