001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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.impl.DLFileEntryImpl;
034    import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
035    import com.liferay.portlet.expando.model.ExpandoBridge;
036    import com.liferay.portlet.expando.util.ExpandoBridgeFactoryUtil;
037    import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
038    
039    import java.io.IOException;
040    import java.io.InputStream;
041    
042    import java.util.Date;
043    
044    import javax.portlet.PortletURL;
045    
046    /**
047     * @author Brian Wing Shun Chan
048     * @author Harry Mark
049     * @author Bruno Farache
050     * @author Raymond Augé
051     */
052    public class DLIndexer extends BaseIndexer {
053    
054            public static final String[] CLASS_NAMES = {FileModel.class.getName()};
055    
056            public String[] getClassNames() {
057                    return CLASS_NAMES;
058            }
059    
060            public Summary getSummary(
061                    Document document, String snippet, PortletURL portletURL) {
062    
063                    return null;
064            }
065    
066            protected void doDelete(Object obj) throws Exception {
067                    FileModel fileModel = (FileModel)obj;
068    
069                    Document document = new DocumentImpl();
070    
071                    document.addUID(
072                            fileModel.getPortletId(), fileModel.getRepositoryId(),
073                            fileModel.getFileName());
074    
075                    SearchEngineUtil.deleteDocument(
076                            fileModel.getCompanyId(), document.get(Field.UID));
077            }
078    
079            protected Document doGetDocument(Object obj) throws Exception {
080                    FileModel fileModel = (FileModel)obj;
081    
082                    long companyId = fileModel.getCompanyId();
083                    String portletId = fileModel.getPortletId();
084                    long groupId = getParentGroupId(fileModel.getGroupId());
085                    long scopeGroupId = fileModel.getGroupId();
086                    long userId = fileModel.getUserId();
087                    long folderId = DLFileEntryImpl.getFolderId(
088                            groupId, fileModel.getRepositoryId());
089                    long repositoryId = fileModel.getRepositoryId();
090                    String fileName = fileModel.getFileName();
091                    long fileEntryId = fileModel.getFileEntryId();
092                    String properties = fileModel.getProperties();
093                    Date modifiedDate = fileModel.getModifiedDate();
094                    long[] assetCategoryIds = fileModel.getAssetCategoryIds();
095                    String[] assetTagNames = fileModel.getAssetTagNames();
096    
097                    DLFileEntry fileEntry = null;
098    
099                    try {
100                            if (fileEntryId > 0) {
101                                    fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
102                                            fileEntryId);
103                            }
104                            else {
105                                    fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
106                                            groupId, folderId, fileName);
107                            }
108                    }
109                    catch (NoSuchFileEntryException nsfe) {
110                            if (_log.isDebugEnabled()) {
111                                    _log.debug(
112                                            "Not indexing document " + companyId + " " + portletId +
113                                                    " " + scopeGroupId + " " + repositoryId + " " +
114                                                            fileName + " " + fileEntryId);
115                            }
116    
117                            return null;
118                    }
119    
120                    if (userId == 0) {
121                            userId = fileEntry.getUserId();
122                    }
123    
124                    if (properties == null) {
125                            properties = fileEntry.getLuceneProperties();
126                    }
127    
128                    if (modifiedDate == null) {
129                            modifiedDate = fileEntry.getModifiedDate();
130                    }
131    
132                    if (assetCategoryIds == null) {
133                            assetCategoryIds = AssetCategoryLocalServiceUtil.getCategoryIds(
134                                    DLFileEntry.class.getName(), fileEntry.getFileEntryId());
135                    }
136    
137                    if (assetTagNames == null) {
138                            assetTagNames = AssetTagLocalServiceUtil.getTagNames(
139                                    DLFileEntry.class.getName(), fileEntry.getFileEntryId());
140                    }
141    
142                    if (_log.isDebugEnabled()) {
143                            _log.debug(
144                                    "Indexing document " + companyId + " " + portletId + " " +
145                                            scopeGroupId + " " + repositoryId + " " + fileName + " " +
146                                                    fileEntry.getFileEntryId());
147                    }
148    
149                    boolean indexContent = true;
150    
151                    InputStream is = null;
152    
153                    try {
154                            Hook hook = HookFactory.getInstance();
155    
156                            if (PropsValues.DL_FILE_INDEXING_MAX_SIZE == 0) {
157                                    indexContent = false;
158                            }
159                            else if (PropsValues.DL_FILE_INDEXING_MAX_SIZE != -1) {
160                                    long size = hook.getFileSize(companyId, repositoryId, fileName);
161    
162                                    if (size > PropsValues.DL_FILE_INDEXING_MAX_SIZE) {
163                                            indexContent = false;
164                                    }
165                            }
166    
167                            if (indexContent) {
168                                    is = hook.getFileAsStream(companyId, repositoryId, fileName);
169                            }
170                    }
171                    catch (Exception e) {
172                    }
173    
174                    if (indexContent && (is == null)) {
175                            if (_log.isDebugEnabled()) {
176                                    _log.debug(
177                                            "Document " + companyId + " " + portletId + " " +
178                                                    scopeGroupId + " " + repositoryId + " " + fileName +
179                                                            " " + fileEntry.getFileEntryId() + " does not " +
180                                                                    "have any content");
181                            }
182    
183                            return null;
184                    }
185    
186                    Document document = new DocumentImpl();
187    
188                    document.addUID(portletId, repositoryId, fileName);
189    
190                    document.addModifiedDate(modifiedDate);
191    
192                    document.addKeyword(Field.COMPANY_ID, companyId);
193                    document.addKeyword(Field.PORTLET_ID, portletId);
194                    document.addKeyword(Field.GROUP_ID, groupId);
195                    document.addKeyword(Field.SCOPE_GROUP_ID, scopeGroupId);
196                    document.addKeyword(Field.USER_ID, userId);
197    
198                    if (indexContent) {
199                            try {
200                                    document.addFile(Field.CONTENT, is, fileEntry.getTitle());
201                            }
202                            catch (IOException ioe) {
203                                    throw new SearchException(
204                                            "Cannot extract text from file" + companyId + " " +
205                                                    portletId +     " " + scopeGroupId + " " + repositoryId +
206                                                    " " + fileName);
207                            }
208                    }
209    
210                    document.addText(Field.PROPERTIES, properties);
211                    document.addKeyword(Field.ASSET_CATEGORY_IDS, assetCategoryIds);
212                    document.addKeyword(Field.ASSET_TAG_NAMES, assetTagNames);
213    
214                    document.addKeyword(Field.FOLDER_ID, folderId);
215                    document.addKeyword("repositoryId", repositoryId);
216                    document.addKeyword("path", fileName);
217                    document.addKeyword(
218                            Field.ENTRY_CLASS_NAME, DLFileEntry.class.getName());
219                    document.addKeyword(Field.ENTRY_CLASS_PK, fileEntry.getFileEntryId());
220    
221                    ExpandoBridge expandoBridge = ExpandoBridgeFactoryUtil.getExpandoBridge(
222                            companyId, DLFileEntry.class.getName(), fileEntry.getFileEntryId());
223    
224                    ExpandoBridgeIndexerUtil.addAttributes(document, expandoBridge);
225    
226                    if (_log.isDebugEnabled()) {
227                            _log.debug(
228                                    "Document " + companyId + " " + portletId + " " +
229                                            scopeGroupId + " " + repositoryId + " " + fileName + " " +
230                                                    fileEntry.getFileEntryId() + " indexed successfully");
231                    }
232    
233                    return document;
234            }
235    
236            protected void doReindex(Object obj) throws Exception {
237                    FileModel fileModel = (FileModel)obj;
238    
239                    Document document = getDocument(fileModel);
240    
241                    if (document != null) {
242                            SearchEngineUtil.updateDocument(fileModel.getCompanyId(), document);
243                    }
244            }
245    
246            protected void doReindex(String className, long classPK) throws Exception {
247            }
248    
249            protected void doReindex(String[] ids) throws Exception {
250                    Hook hook = HookFactory.getInstance();
251    
252                    hook.reindex(ids);
253            }
254    
255            protected String getPortletId(SearchContext searchContext) {
256                    return (String)searchContext.getAttribute("portletId");
257            }
258    
259            private static Log _log = LogFactoryUtil.getLog(DLIndexer.class);
260    
261    }