001
014
015 package com.liferay.documentlibrary.util;
016
017 import com.liferay.documentlibrary.model.FileModel;
018 import com.liferay.portal.kernel.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.search.BaseIndexer;
021 import com.liferay.portal.kernel.search.Document;
022 import com.liferay.portal.kernel.search.DocumentImpl;
023 import com.liferay.portal.kernel.search.Field;
024 import com.liferay.portal.kernel.search.SearchContext;
025 import com.liferay.portal.kernel.search.SearchEngineUtil;
026 import com.liferay.portal.kernel.search.SearchException;
027 import com.liferay.portal.kernel.search.Summary;
028 import com.liferay.portal.util.PropsValues;
029 import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
030 import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
031 import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
032 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
033 import com.liferay.portlet.documentlibrary.model.DLFileVersion;
034 import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryImpl;
035 import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
036 import com.liferay.portlet.expando.model.ExpandoBridge;
037 import com.liferay.portlet.expando.util.ExpandoBridgeFactoryUtil;
038 import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
039
040 import java.io.IOException;
041 import java.io.InputStream;
042
043 import java.util.Date;
044
045 import javax.portlet.PortletURL;
046
047
053 public class DLIndexer extends BaseIndexer {
054
055 public static final String[] CLASS_NAMES = {FileModel.class.getName()};
056
057 public String[] getClassNames() {
058 return CLASS_NAMES;
059 }
060
061 public Summary getSummary(
062 Document document, String snippet, PortletURL portletURL) {
063
064 return null;
065 }
066
067 protected void doDelete(Object obj) throws Exception {
068 FileModel fileModel = (FileModel)obj;
069
070 Document document = new DocumentImpl();
071
072 document.addUID(
073 fileModel.getPortletId(), fileModel.getRepositoryId(),
074 fileModel.getFileName());
075
076 SearchEngineUtil.deleteDocument(
077 fileModel.getCompanyId(), document.get(Field.UID));
078 }
079
080 protected Document doGetDocument(Object obj) throws Exception {
081 FileModel fileModel = (FileModel)obj;
082
083 long companyId = fileModel.getCompanyId();
084 String portletId = fileModel.getPortletId();
085 long groupId = getParentGroupId(fileModel.getGroupId());
086 long scopeGroupId = fileModel.getGroupId();
087 long userId = fileModel.getUserId();
088 long folderId = DLFileEntryImpl.getFolderId(
089 scopeGroupId, fileModel.getRepositoryId());
090 long repositoryId = fileModel.getRepositoryId();
091 String fileName = fileModel.getFileName();
092 long fileEntryId = fileModel.getFileEntryId();
093 String properties = fileModel.getProperties();
094 Date modifiedDate = fileModel.getModifiedDate();
095 long[] assetCategoryIds = fileModel.getAssetCategoryIds();
096 String[] assetCategoryNames = fileModel.getAssetCategoryNames();
097 String[] assetTagNames = fileModel.getAssetTagNames();
098
099 DLFileEntry fileEntry = null;
100
101 try {
102 if (fileEntryId > 0) {
103 fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
104 fileEntryId);
105 }
106 else {
107 fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
108 scopeGroupId, folderId, fileName);
109 }
110 }
111 catch (NoSuchFileEntryException nsfe) {
112 if (_log.isDebugEnabled()) {
113 _log.debug(
114 "Not indexing document " + companyId + " " + portletId +
115 " " + scopeGroupId + " " + repositoryId + " " +
116 fileName + " " + fileEntryId);
117 }
118
119 return null;
120 }
121
122 if (userId == 0) {
123 userId = fileEntry.getUserId();
124 }
125
126 if (properties == null) {
127 properties = fileEntry.getLuceneProperties();
128 }
129
130 if (modifiedDate == null) {
131 modifiedDate = fileEntry.getModifiedDate();
132 }
133
134 if (assetCategoryIds == null) {
135 assetCategoryIds = AssetCategoryLocalServiceUtil.getCategoryIds(
136 DLFileEntry.class.getName(), fileEntry.getFileEntryId());
137 }
138
139 if (assetTagNames == null) {
140 assetTagNames = AssetTagLocalServiceUtil.getTagNames(
141 DLFileEntry.class.getName(), fileEntry.getFileEntryId());
142 }
143
144 if (_log.isDebugEnabled()) {
145 _log.debug(
146 "Indexing document " + companyId + " " + portletId + " " +
147 scopeGroupId + " " + repositoryId + " " + fileName + " " +
148 fileEntry.getFileEntryId());
149 }
150
151 boolean indexContent = true;
152
153 InputStream is = null;
154
155 try {
156 Hook hook = HookFactory.getInstance();
157
158 if (PropsValues.DL_FILE_INDEXING_MAX_SIZE == 0) {
159 indexContent = false;
160 }
161 else if (PropsValues.DL_FILE_INDEXING_MAX_SIZE != -1) {
162 long size = hook.getFileSize(companyId, repositoryId, fileName);
163
164 if (size > PropsValues.DL_FILE_INDEXING_MAX_SIZE) {
165 indexContent = false;
166 }
167 }
168
169 if (indexContent) {
170 is = hook.getFileAsStream(companyId, repositoryId, fileName);
171 }
172 }
173 catch (Exception e) {
174 }
175
176 if (indexContent && (is == null)) {
177 if (_log.isDebugEnabled()) {
178 _log.debug(
179 "Document " + companyId + " " + portletId + " " +
180 scopeGroupId + " " + repositoryId + " " + fileName +
181 " " + fileEntry.getFileEntryId() + " does not " +
182 "have any content");
183 }
184
185 return null;
186 }
187
188 Document document = new DocumentImpl();
189
190 document.addUID(portletId, repositoryId, fileName);
191
192 document.addModifiedDate(modifiedDate);
193
194 document.addKeyword(Field.COMPANY_ID, companyId);
195 document.addKeyword(Field.PORTLET_ID, portletId);
196 document.addKeyword(Field.GROUP_ID, groupId);
197 document.addKeyword(Field.SCOPE_GROUP_ID, scopeGroupId);
198 document.addKeyword(Field.USER_ID, userId);
199
200 if (indexContent) {
201 try {
202 document.addFile(Field.CONTENT, is, fileEntry.getTitle());
203 }
204 catch (IOException ioe) {
205 throw new SearchException(
206 "Cannot extract text from file" + companyId + " " +
207 portletId + " " + scopeGroupId + " " + repositoryId +
208 " " + fileName);
209 }
210 }
211
212 document.addText(Field.PROPERTIES, properties);
213 document.addKeyword(Field.ASSET_CATEGORY_IDS, assetCategoryIds);
214 document.addKeyword(Field.ASSET_CATEGORY_NAMES, assetCategoryNames);
215 document.addKeyword(Field.ASSET_TAG_NAMES, assetTagNames);
216
217 document.addKeyword(Field.FOLDER_ID, folderId);
218 document.addKeyword("repositoryId", repositoryId);
219 document.addKeyword("path", fileName);
220 document.addKeyword(
221 Field.ENTRY_CLASS_NAME, DLFileEntry.class.getName());
222 document.addKeyword(Field.ENTRY_CLASS_PK, fileEntry.getFileEntryId());
223
224 DLFileVersion fileVersion = fileEntry.getFileVersion();
225
226 ExpandoBridge expandoBridge = ExpandoBridgeFactoryUtil.getExpandoBridge(
227 companyId, DLFileEntry.class.getName(),
228 fileVersion.getFileVersionId());
229
230 ExpandoBridgeIndexerUtil.addAttributes(document, expandoBridge);
231
232 if (_log.isDebugEnabled()) {
233 _log.debug(
234 "Document " + companyId + " " + portletId + " " +
235 scopeGroupId + " " + repositoryId + " " + fileName + " " +
236 fileEntry.getFileEntryId() + " indexed successfully");
237 }
238
239 return document;
240 }
241
242 protected void doReindex(Object obj) throws Exception {
243 FileModel fileModel = (FileModel)obj;
244
245 Document document = getDocument(fileModel);
246
247 if (document != null) {
248 SearchEngineUtil.updateDocument(fileModel.getCompanyId(), document);
249 }
250 }
251
252 protected void doReindex(String className, long classPK) throws Exception {
253 }
254
255 protected void doReindex(String[] ids) throws Exception {
256 Hook hook = HookFactory.getInstance();
257
258 hook.reindex(ids);
259 }
260
261 protected String getPortletId(SearchContext searchContext) {
262 return (String)searchContext.getAttribute("portletId");
263 }
264
265 private static Log _log = LogFactoryUtil.getLog(DLIndexer.class);
266
267 }