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.Document;
21 import com.liferay.portal.kernel.search.DocumentImpl;
22 import com.liferay.portal.kernel.search.Field;
23 import com.liferay.portal.kernel.search.SearchContext;
24 import com.liferay.portal.kernel.search.SearchEngineUtil;
25 import com.liferay.portal.kernel.search.SearchException;
26 import com.liferay.portal.kernel.search.Summary;
27 import com.liferay.portal.search.BaseIndexer;
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.DLFolderConstants;
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 folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
88 long repositoryId = fileModel.getRepositoryId();
89 String fileName = fileModel.getFileName();
90 long fileEntryId = fileModel.getFileEntryId();
91 String properties = fileModel.getProperties();
92 Date modifiedDate = fileModel.getModifiedDate();
93 long[] assetCategoryIds = fileModel.getAssetCategoryIds();
94 String[] assetTagNames = fileModel.getAssetTagNames();
95
96 DLFileEntry fileEntry = null;
97
98 try {
99 if (fileEntryId > 0) {
100 fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
101 fileEntryId);
102 }
103 else {
104 fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
105 groupId, folderId, fileName);
106 }
107 }
108 catch (NoSuchFileEntryException nsfe) {
109 if (_log.isDebugEnabled()) {
110 _log.debug(
111 "Not indexing document " + companyId + " " + portletId +
112 " " + scopeGroupId + " " + repositoryId + " " +
113 fileName + " " + fileEntryId);
114 }
115
116 return null;
117 }
118
119 folderId = fileEntry.getFolderId();
120
121 if (properties == null) {
122 properties = fileEntry.getLuceneProperties();
123 }
124
125 if (assetCategoryIds == null) {
126 assetCategoryIds = AssetCategoryLocalServiceUtil.getCategoryIds(
127 DLFileEntry.class.getName(), fileEntry.getFileEntryId());
128 }
129
130 if (assetTagNames == null) {
131 assetTagNames = AssetTagLocalServiceUtil.getTagNames(
132 DLFileEntry.class.getName(), fileEntry.getFileEntryId());
133 }
134
135 if (_log.isDebugEnabled()) {
136 _log.debug(
137 "Indexing document " + companyId + " " + portletId + " " +
138 scopeGroupId + " " + repositoryId + " " + fileName + " " +
139 fileEntryId);
140 }
141
142 InputStream is = null;
143
144 try {
145 Hook hook = HookFactory.getInstance();
146
147 is = hook.getFileAsStream(companyId, repositoryId, fileName);
148 }
149 catch (Exception e) {
150 }
151
152 if (is == null) {
153 if (_log.isDebugEnabled()) {
154 _log.debug(
155 "Document " + companyId + " " + portletId + " " +
156 scopeGroupId + " " + repositoryId + " " + fileName +
157 " " + fileEntryId + " does not have any content");
158 }
159
160 return null;
161 }
162
163 Document document = new DocumentImpl();
164
165 document.addUID(portletId, repositoryId, fileName);
166
167 document.addModifiedDate(modifiedDate);
168
169 document.addKeyword(Field.COMPANY_ID, companyId);
170 document.addKeyword(Field.PORTLET_ID, portletId);
171 document.addKeyword(Field.GROUP_ID, groupId);
172 document.addKeyword(Field.SCOPE_GROUP_ID, scopeGroupId);
173
174 try {
175 document.addFile(Field.CONTENT, is, fileEntry.getTitle());
176 }
177 catch (IOException ioe) {
178 throw new SearchException(
179 "Cannot extract text from file" + companyId + " " + portletId +
180 " " + scopeGroupId + " " + repositoryId + " " + fileName);
181 }
182
183 document.addText(Field.PROPERTIES, properties);
184 document.addKeyword(Field.ASSET_CATEGORY_IDS, assetCategoryIds);
185 document.addKeyword(Field.ASSET_TAG_NAMES, assetTagNames);
186
187 document.addKeyword(Field.FOLDER_ID, folderId);
188 document.addKeyword("repositoryId", repositoryId);
189 document.addKeyword("path", fileName);
190 document.addKeyword(
191 Field.ENTRY_CLASS_NAME, DLFileEntry.class.getName());
192 document.addKeyword(Field.ENTRY_CLASS_PK, fileEntryId);
193
194 ExpandoBridge expandoBridge = ExpandoBridgeFactoryUtil.getExpandoBridge(
195 companyId, DLFileEntry.class.getName(), fileEntryId);
196
197 ExpandoBridgeIndexerUtil.addAttributes(document, expandoBridge);
198
199 if (_log.isDebugEnabled()) {
200 _log.debug(
201 "Document " + companyId + " " + portletId + " " +
202 scopeGroupId + " " + repositoryId + " " + fileName + " " +
203 fileEntryId + " indexed successfully");
204 }
205
206 return document;
207 }
208
209 protected void doReindex(Object obj) throws Exception {
210 FileModel fileModel = (FileModel)obj;
211
212 Document document = getDocument(fileModel);
213
214 if (document != null) {
215 SearchEngineUtil.updateDocument(
216 fileModel.getCompanyId(), document.get(Field.UID), document);
217 }
218 }
219
220 protected void doReindex(String className, long classPK) throws Exception {
221 }
222
223 protected void doReindex(String[] ids) throws Exception {
224 Hook hook = HookFactory.getInstance();
225
226 hook.reindex(ids);
227 }
228
229 protected String getPortletId(SearchContext searchContext) {
230 return (String)searchContext.getAttribute("portletId");
231 }
232
233 private static Log _log = LogFactoryUtil.getLog(DLIndexer.class);
234
235 }