001    /**
002     * Copyright (c) 2000-present 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.portlet.documentlibrary.util;
016    
017    import com.liferay.portal.kernel.comment.Comment;
018    import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
019    import com.liferay.portal.kernel.dao.orm.DynamicQuery;
020    import com.liferay.portal.kernel.dao.orm.Property;
021    import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
022    import com.liferay.portal.kernel.exception.PortalException;
023    import com.liferay.portal.kernel.log.Log;
024    import com.liferay.portal.kernel.log.LogFactoryUtil;
025    import com.liferay.portal.kernel.repository.model.FileEntry;
026    import com.liferay.portal.kernel.repository.model.FileVersion;
027    import com.liferay.portal.kernel.search.BaseIndexer;
028    import com.liferay.portal.kernel.search.BaseRelatedEntryIndexer;
029    import com.liferay.portal.kernel.search.BooleanClauseOccur;
030    import com.liferay.portal.kernel.search.BooleanQuery;
031    import com.liferay.portal.kernel.search.DDMStructureIndexer;
032    import com.liferay.portal.kernel.search.Document;
033    import com.liferay.portal.kernel.search.DocumentHelper;
034    import com.liferay.portal.kernel.search.DocumentImpl;
035    import com.liferay.portal.kernel.search.Field;
036    import com.liferay.portal.kernel.search.Indexer;
037    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
038    import com.liferay.portal.kernel.search.RelatedEntryIndexer;
039    import com.liferay.portal.kernel.search.SearchContext;
040    import com.liferay.portal.kernel.search.SearchEngineUtil;
041    import com.liferay.portal.kernel.search.SearchException;
042    import com.liferay.portal.kernel.search.Summary;
043    import com.liferay.portal.kernel.search.filter.BooleanFilter;
044    import com.liferay.portal.kernel.search.filter.QueryFilter;
045    import com.liferay.portal.kernel.search.generic.BooleanQueryImpl;
046    import com.liferay.portal.kernel.spring.osgi.OSGiBeanProperties;
047    import com.liferay.portal.kernel.util.ArrayUtil;
048    import com.liferay.portal.kernel.util.CharPool;
049    import com.liferay.portal.kernel.util.GetterUtil;
050    import com.liferay.portal.kernel.util.LocaleUtil;
051    import com.liferay.portal.kernel.util.PropsKeys;
052    import com.liferay.portal.kernel.util.StringBundler;
053    import com.liferay.portal.kernel.util.StringPool;
054    import com.liferay.portal.kernel.util.StringUtil;
055    import com.liferay.portal.kernel.util.Validator;
056    import com.liferay.portal.model.Group;
057    import com.liferay.portal.repository.liferayrepository.model.LiferayFileEntry;
058    import com.liferay.portal.security.permission.ActionKeys;
059    import com.liferay.portal.security.permission.PermissionChecker;
060    import com.liferay.portal.service.GroupLocalServiceUtil;
061    import com.liferay.portal.util.PortalUtil;
062    import com.liferay.portal.util.PortletKeys;
063    import com.liferay.portal.util.PrefsPropsUtil;
064    import com.liferay.portal.util.PropsValues;
065    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
066    import com.liferay.portlet.documentlibrary.model.DLFileEntryMetadata;
067    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
068    import com.liferay.portlet.documentlibrary.model.DLFolder;
069    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
070    import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
071    import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
072    import com.liferay.portlet.documentlibrary.service.DLFileEntryMetadataLocalServiceUtil;
073    import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
074    import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
075    import com.liferay.portlet.dynamicdatamapping.StructureFieldException;
076    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
077    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
078    import com.liferay.portlet.dynamicdatamapping.storage.DDMFormValues;
079    import com.liferay.portlet.dynamicdatamapping.storage.StorageEngineUtil;
080    import com.liferay.portlet.dynamicdatamapping.util.DDMIndexer;
081    import com.liferay.portlet.dynamicdatamapping.util.DDMIndexerUtil;
082    import com.liferay.portlet.dynamicdatamapping.util.DDMUtil;
083    import com.liferay.portlet.expando.model.ExpandoBridge;
084    import com.liferay.portlet.expando.util.ExpandoBridgeFactoryUtil;
085    import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
086    
087    import java.io.IOException;
088    import java.io.InputStream;
089    import java.io.Serializable;
090    
091    import java.util.LinkedHashMap;
092    import java.util.List;
093    import java.util.Locale;
094    
095    import javax.portlet.PortletRequest;
096    import javax.portlet.PortletResponse;
097    
098    /**
099     * @author Brian Wing Shun Chan
100     * @author Raymond Aug??
101     * @author Alexander Chow
102     */
103    @OSGiBeanProperties
104    public class DLFileEntryIndexer
105            extends BaseIndexer implements DDMStructureIndexer, RelatedEntryIndexer {
106    
107            public static final String CLASS_NAME = DLFileEntry.class.getName();
108    
109            public DLFileEntryIndexer() {
110                    setDefaultSelectedFieldNames(
111                            Field.ASSET_TAG_NAMES, Field.COMPANY_ID, Field.CONTENT,
112                            Field.ENTRY_CLASS_NAME, Field.ENTRY_CLASS_PK, Field.GROUP_ID,
113                            Field.MODIFIED_DATE, Field.SCOPE_GROUP_ID, Field.TITLE, Field.UID);
114                    setFilterSearch(true);
115                    setPermissionAware(true);
116            }
117    
118            @Override
119            public void addRelatedClassNames(
120                            BooleanFilter contextBooleanFilter, SearchContext searchContext)
121                    throws Exception {
122    
123                    _relatedEntryIndexer.addRelatedClassNames(
124                            contextBooleanFilter, searchContext);
125            }
126    
127            @Override
128            public void addRelatedEntryFields(Document document, Object obj)
129                    throws Exception {
130    
131                    Comment comment = (Comment)obj;
132    
133                    FileEntry fileEntry = null;
134    
135                    try {
136                            fileEntry = DLAppLocalServiceUtil.getFileEntry(
137                                    comment.getClassPK());
138                    }
139                    catch (Exception e) {
140                            return;
141                    }
142    
143                    if (fileEntry instanceof LiferayFileEntry) {
144                            DLFileEntry dlFileEntry = (DLFileEntry)fileEntry.getModel();
145    
146                            document.addKeyword(Field.FOLDER_ID, dlFileEntry.getFolderId());
147                            document.addKeyword(Field.HIDDEN, dlFileEntry.isInHiddenFolder());
148                            document.addKeyword(
149                                    Field.TREE_PATH,
150                                    StringUtil.split(dlFileEntry.getTreePath(), CharPool.SLASH));
151                    }
152            }
153    
154            @Override
155            public String getClassName() {
156                    return CLASS_NAME;
157            }
158    
159            @Override
160            public boolean hasPermission(
161                            PermissionChecker permissionChecker, String entryClassName,
162                            long entryClassPK, String actionId)
163                    throws Exception {
164    
165                    return DLFileEntryPermission.contains(
166                            permissionChecker, entryClassPK, ActionKeys.VIEW);
167            }
168    
169            @Override
170            public boolean isVisible(long classPK, int status) throws Exception {
171                    FileEntry fileEntry = DLAppLocalServiceUtil.getFileEntry(classPK);
172    
173                    FileVersion fileVersion = fileEntry.getFileVersion();
174    
175                    return isVisible(fileVersion.getStatus(), status);
176            }
177    
178            @Override
179            public boolean isVisibleRelatedEntry(long classPK, int status)
180                    throws Exception {
181    
182                    FileEntry fileEntry = DLAppLocalServiceUtil.getFileEntry(classPK);
183    
184                    if (fileEntry instanceof LiferayFileEntry) {
185                            DLFileEntry dlFileEntry = (DLFileEntry)fileEntry.getModel();
186    
187                            if (dlFileEntry.isInHiddenFolder()) {
188                                    Indexer indexer = IndexerRegistryUtil.getIndexer(
189                                            dlFileEntry.getClassName());
190    
191                                    return indexer.isVisible(dlFileEntry.getClassPK(), status);
192                            }
193                    }
194    
195                    return true;
196            }
197    
198            @Override
199            public void postProcessContextBooleanFilter(
200                            BooleanFilter contextBooleanFilter, SearchContext searchContext)
201                    throws Exception {
202    
203                    addStatus(contextBooleanFilter, searchContext);
204    
205                    if (searchContext.isIncludeAttachments()) {
206                            addRelatedClassNames(contextBooleanFilter, searchContext);
207                    }
208    
209                    contextBooleanFilter.addRequiredTerm(
210                            Field.HIDDEN, searchContext.isIncludeAttachments());
211    
212                    addSearchClassTypeIds(contextBooleanFilter, searchContext);
213    
214                    String ddmStructureFieldName = (String)searchContext.getAttribute(
215                            "ddmStructureFieldName");
216                    Serializable ddmStructureFieldValue = searchContext.getAttribute(
217                            "ddmStructureFieldValue");
218    
219                    if (Validator.isNotNull(ddmStructureFieldName) &&
220                            Validator.isNotNull(ddmStructureFieldValue)) {
221    
222                            String[] ddmStructureFieldNameParts = StringUtil.split(
223                                    ddmStructureFieldName, DDMIndexer.DDM_FIELD_SEPARATOR);
224    
225                            DDMStructure structure = DDMStructureLocalServiceUtil.getStructure(
226                                    GetterUtil.getLong(ddmStructureFieldNameParts[1]));
227    
228                            String fieldName = StringUtil.replaceLast(
229                                    ddmStructureFieldNameParts[2],
230                                    StringPool.UNDERLINE.concat(
231                                            LocaleUtil.toLanguageId(searchContext.getLocale())),
232                                    StringPool.BLANK);
233    
234                            try {
235                                    ddmStructureFieldValue = DDMUtil.getIndexedFieldValue(
236                                            ddmStructureFieldValue, structure.getFieldType(fieldName));
237                            }
238                            catch (StructureFieldException sfe) {
239                                    if (_log.isDebugEnabled()) {
240                                            _log.debug(sfe, sfe);
241                                    }
242                            }
243    
244                            BooleanQuery booleanQuery = new BooleanQueryImpl();
245    
246                            booleanQuery.addRequiredTerm(
247                                    ddmStructureFieldName,
248                                    StringPool.QUOTE + ddmStructureFieldValue + StringPool.QUOTE);
249    
250                            contextBooleanFilter.add(new QueryFilter(booleanQuery));
251                    }
252    
253                    String[] mimeTypes = (String[])searchContext.getAttribute("mimeTypes");
254    
255                    if (ArrayUtil.isNotEmpty(mimeTypes)) {
256                            BooleanFilter mimeTypesBooleanFilter = new BooleanFilter();
257    
258                            for (String mimeType : mimeTypes) {
259                                    mimeTypesBooleanFilter.addTerm(
260                                            "mimeType",
261                                            StringUtil.replace(
262                                                    mimeType, CharPool.FORWARD_SLASH, CharPool.UNDERLINE));
263                            }
264    
265                            contextBooleanFilter.add(
266                                    mimeTypesBooleanFilter, BooleanClauseOccur.MUST);
267                    }
268            }
269    
270            @Override
271            public void postProcessSearchQuery(
272                            BooleanQuery searchQuery, BooleanFilter fullQueryBooleanFilter,
273                            SearchContext searchContext)
274                    throws Exception {
275    
276                    String keywords = searchContext.getKeywords();
277    
278                    if (Validator.isNull(keywords)) {
279                            addSearchTerm(searchQuery, searchContext, Field.DESCRIPTION, false);
280                            addSearchTerm(searchQuery, searchContext, Field.TITLE, false);
281                            addSearchTerm(searchQuery, searchContext, Field.USER_NAME, false);
282                    }
283    
284                    addSearchTerm(searchQuery, searchContext, "ddmContent", false);
285                    addSearchTerm(searchQuery, searchContext, "extension", false);
286                    addSearchTerm(searchQuery, searchContext, "fileEntryTypeId", false);
287                    addSearchTerm(searchQuery, searchContext, "path", false);
288    
289                    LinkedHashMap<String, Object> params =
290                            (LinkedHashMap<String, Object>)searchContext.getAttribute("params");
291    
292                    if (params != null) {
293                            String expandoAttributes = (String)params.get("expandoAttributes");
294    
295                            if (Validator.isNotNull(expandoAttributes)) {
296                                    addSearchExpando(searchQuery, searchContext, expandoAttributes);
297                            }
298                    }
299            }
300    
301            @Override
302            public void reindexDDMStructures(List<Long> ddmStructureIds)
303                    throws SearchException {
304    
305                    if (SearchEngineUtil.isIndexReadOnly() || !isIndexerEnabled()) {
306                            return;
307                    }
308    
309                    try {
310                            List<DLFileEntry> dlFileEntries =
311                                    DLFileEntryLocalServiceUtil.getDDMStructureFileEntries(
312                                            ArrayUtil.toLongArray(ddmStructureIds));
313    
314                            for (DLFileEntry dlFileEntry : dlFileEntries) {
315                                    doReindex(dlFileEntry);
316                            }
317                    }
318                    catch (Exception e) {
319                            throw new SearchException(e);
320                    }
321            }
322    
323            @Override
324            public void updateFullQuery(SearchContext searchContext) {
325                    if (searchContext.isIncludeAttachments()) {
326                            searchContext.addFullQueryEntryClassName(
327                                    DLFileEntry.class.getName());
328                    }
329            }
330    
331            protected void addFileEntryTypeAttributes(
332                            Document document, DLFileVersion dlFileVersion)
333                    throws PortalException {
334    
335                    List<DLFileEntryMetadata> dlFileEntryMetadatas =
336                            DLFileEntryMetadataLocalServiceUtil.
337                                    getFileVersionFileEntryMetadatas(
338                                            dlFileVersion.getFileVersionId());
339    
340                    for (DLFileEntryMetadata dlFileEntryMetadata : dlFileEntryMetadatas) {
341                            DDMFormValues ddmFormValues = null;
342    
343                            try {
344                                    ddmFormValues = StorageEngineUtil.getDDMFormValues(
345                                            dlFileEntryMetadata.getDDMStorageId());
346                            }
347                            catch (Exception e) {
348                            }
349    
350                            if (ddmFormValues != null) {
351                                    DDMStructure ddmStructure =
352                                            DDMStructureLocalServiceUtil.getStructure(
353                                                    dlFileEntryMetadata.getDDMStructureId());
354    
355                                    DDMIndexerUtil.addAttributes(
356                                            document, ddmStructure, ddmFormValues);
357                            }
358                    }
359            }
360    
361            @Override
362            protected void doDelete(Object obj) throws Exception {
363                    DLFileEntry dlFileEntry = (DLFileEntry)obj;
364    
365                    Document document = new DocumentImpl();
366    
367                    document.addUID(CLASS_NAME, dlFileEntry.getFileEntryId());
368    
369                    SearchEngineUtil.deleteDocument(
370                            getSearchEngineId(), dlFileEntry.getCompanyId(),
371                            document.get(Field.UID), isCommitImmediately());
372            }
373    
374            @Override
375            protected Document doGetDocument(Object obj) throws Exception {
376                    DLFileEntry dlFileEntry = (DLFileEntry)obj;
377    
378                    if (_log.isDebugEnabled()) {
379                            _log.debug("Indexing document " + dlFileEntry);
380                    }
381    
382                    boolean indexContent = true;
383    
384                    InputStream is = null;
385    
386                    try {
387                            String[] ignoreExtensions = PrefsPropsUtil.getStringArray(
388                                    PropsKeys.DL_FILE_INDEXING_IGNORE_EXTENSIONS, StringPool.COMMA);
389    
390                            if (ArrayUtil.contains(
391                                            ignoreExtensions,
392                                            StringPool.PERIOD + dlFileEntry.getExtension())) {
393    
394                                    indexContent = false;
395                            }
396    
397                            if (indexContent) {
398                                    is = dlFileEntry.getFileVersion().getContentStream(false);
399                            }
400                    }
401                    catch (Exception e) {
402                    }
403    
404                    DLFileVersion dlFileVersion = dlFileEntry.getFileVersion();
405    
406                    try {
407                            Document document = getBaseModelDocument(
408                                    CLASS_NAME, dlFileEntry, dlFileVersion);
409    
410                            if (indexContent) {
411                                    if (is != null) {
412                                            try {
413                                                    document.addFile(
414                                                            Field.CONTENT, is, dlFileEntry.getTitle(),
415                                                            PropsValues.DL_FILE_INDEXING_MAX_SIZE);
416                                            }
417                                            catch (IOException ioe) {
418                                                    throw new SearchException(
419                                                            "Cannot extract text from file" + dlFileEntry);
420                                            }
421                                    }
422                                    else if (_log.isDebugEnabled()) {
423                                            _log.debug(
424                                                    "Document " + dlFileEntry +
425                                                            " does not have any content");
426                                    }
427                            }
428    
429                            document.addKeyword(
430                                    Field.CLASS_TYPE_ID, dlFileEntry.getFileEntryTypeId());
431                            document.addText(Field.DESCRIPTION, dlFileEntry.getDescription());
432                            document.addKeyword(Field.FOLDER_ID, dlFileEntry.getFolderId());
433                            document.addKeyword(Field.HIDDEN, dlFileEntry.isInHiddenFolder());
434                            document.addText(
435                                    Field.PROPERTIES, dlFileEntry.getLuceneProperties());
436                            document.addText(Field.TITLE, dlFileEntry.getTitle());
437                            document.addKeyword(
438                                    Field.TREE_PATH,
439                                    StringUtil.split(dlFileEntry.getTreePath(), CharPool.SLASH));
440    
441                            document.addKeyword(
442                                    "dataRepositoryId", dlFileEntry.getDataRepositoryId());
443                            document.addText(
444                                    "ddmContent",
445                                    extractDDMContent(dlFileVersion, LocaleUtil.getSiteDefault()));
446                            document.addKeyword("extension", dlFileEntry.getExtension());
447                            document.addKeyword(
448                                    "fileEntryTypeId", dlFileEntry.getFileEntryTypeId());
449                            document.addKeyword(
450                                    "mimeType",
451                                    StringUtil.replace(
452                                            dlFileEntry.getMimeType(), CharPool.FORWARD_SLASH,
453                                            CharPool.UNDERLINE));
454                            document.addKeyword("path", dlFileEntry.getTitle());
455                            document.addKeyword("readCount", dlFileEntry.getReadCount());
456                            document.addKeyword("size", dlFileEntry.getSize());
457    
458                            ExpandoBridge expandoBridge =
459                                    ExpandoBridgeFactoryUtil.getExpandoBridge(
460                                            dlFileEntry.getCompanyId(), DLFileEntry.class.getName(),
461                                            dlFileVersion.getFileVersionId());
462    
463                            ExpandoBridgeIndexerUtil.addAttributes(document, expandoBridge);
464    
465                            addFileEntryTypeAttributes(document, dlFileVersion);
466    
467                            if (dlFileEntry.isInHiddenFolder()) {
468                                    Indexer indexer = IndexerRegistryUtil.getIndexer(
469                                            dlFileEntry.getClassName());
470    
471                                    if ((indexer != null) &&
472                                            (indexer instanceof RelatedEntryIndexer)) {
473    
474                                            RelatedEntryIndexer relatedEntryIndexer =
475                                                    (RelatedEntryIndexer)indexer;
476    
477                                            relatedEntryIndexer.addRelatedEntryFields(
478                                                    document, new LiferayFileEntry(dlFileEntry));
479    
480                                            DocumentHelper documentHelper = new DocumentHelper(
481                                                    document);
482    
483                                            documentHelper.setAttachmentOwnerKey(
484                                                    PortalUtil.getClassNameId(dlFileEntry.getClassName()),
485                                                    dlFileEntry.getClassPK());
486    
487                                            document.addKeyword(Field.RELATED_ENTRY, true);
488                                    }
489                            }
490    
491                            if (_log.isDebugEnabled()) {
492                                    _log.debug("Document " + dlFileEntry + " indexed successfully");
493                            }
494    
495                            return document;
496                    }
497                    finally {
498                            if (is != null) {
499                                    try {
500                                            is.close();
501                                    }
502                                    catch (IOException ioe) {
503                                    }
504                            }
505                    }
506            }
507    
508            @Override
509            protected Summary doGetSummary(
510                    Document document, Locale locale, String snippet,
511                    PortletRequest portletRequest, PortletResponse portletResponse) {
512    
513                    Summary summary = createSummary(document, Field.TITLE, Field.CONTENT);
514    
515                    summary.setMaxContentLength(200);
516    
517                    return summary;
518            }
519    
520            @Override
521            protected void doReindex(Object obj) throws Exception {
522                    DLFileEntry dlFileEntry = (DLFileEntry)obj;
523    
524                    DLFileVersion dlFileVersion = dlFileEntry.getFileVersion();
525    
526                    if (!dlFileVersion.isApproved() && !dlFileEntry.isInTrash()) {
527                            return;
528                    }
529    
530                    Document document = getDocument(dlFileEntry);
531    
532                    if (document != null) {
533                            SearchEngineUtil.updateDocument(
534                                    getSearchEngineId(), dlFileEntry.getCompanyId(), document,
535                                    isCommitImmediately());
536                    }
537            }
538    
539            @Override
540            protected void doReindex(String className, long classPK) throws Exception {
541                    DLFileEntry dlFileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
542                            classPK);
543    
544                    doReindex(dlFileEntry);
545            }
546    
547            @Override
548            protected void doReindex(String[] ids) throws Exception {
549                    if (ids.length == 1) {
550                            long companyId = GetterUtil.getLong(ids[0]);
551    
552                            reindexFolders(companyId);
553                            reindexRoot(companyId);
554                    }
555                    else {
556                            long companyId = GetterUtil.getLong(ids[0]);
557                            long groupId = GetterUtil.getLong(ids[2]);
558                            long dataRepositoryId = GetterUtil.getLong(ids[3]);
559    
560                            reindexFileEntries(companyId, groupId, dataRepositoryId);
561                    }
562            }
563    
564            protected String extractDDMContent(
565                            DLFileVersion dlFileVersion, Locale locale)
566                    throws Exception {
567    
568                    List<DLFileEntryMetadata> dlFileEntryMetadatas =
569                            DLFileEntryMetadataLocalServiceUtil.
570                                    getFileVersionFileEntryMetadatas(
571                                            dlFileVersion.getFileVersionId());
572    
573                    StringBundler sb = new StringBundler(dlFileEntryMetadatas.size());
574    
575                    for (DLFileEntryMetadata dlFileEntryMetadata : dlFileEntryMetadatas) {
576                            DDMFormValues ddmFormValues = null;
577    
578                            try {
579                                    ddmFormValues = StorageEngineUtil.getDDMFormValues(
580                                            dlFileEntryMetadata.getDDMStorageId());
581                            }
582                            catch (Exception e) {
583                            }
584    
585                            if (ddmFormValues != null) {
586                                    DDMStructure ddmStructure =
587                                            DDMStructureLocalServiceUtil.getStructure(
588                                                    dlFileEntryMetadata.getDDMStructureId());
589    
590                                    sb.append(
591                                            DDMIndexerUtil.extractAttributes(
592                                                    ddmStructure, ddmFormValues, locale));
593                            }
594                    }
595    
596                    return sb.toString();
597            }
598    
599            protected void reindexFileEntries(
600                            long companyId, final long groupId, final long dataRepositoryId)
601                    throws PortalException {
602    
603                    final ActionableDynamicQuery actionableDynamicQuery =
604                            DLFileEntryLocalServiceUtil.getActionableDynamicQuery();
605    
606                    actionableDynamicQuery.setAddCriteriaMethod(
607                            new ActionableDynamicQuery.AddCriteriaMethod() {
608    
609                                    @Override
610                                    public void addCriteria(DynamicQuery dynamicQuery) {
611                                            Property property = PropertyFactoryUtil.forName("folderId");
612    
613                                            long folderId = DLFolderConstants.getFolderId(
614                                                    groupId, dataRepositoryId);
615    
616                                            dynamicQuery.add(property.eq(folderId));
617                                    }
618    
619                            });
620                    actionableDynamicQuery.setCompanyId(companyId);
621                    actionableDynamicQuery.setGroupId(groupId);
622                    actionableDynamicQuery.setPerformActionMethod(
623                            new ActionableDynamicQuery.PerformActionMethod() {
624    
625                                    @Override
626                                    public void performAction(Object object) {
627                                            DLFileEntry dlFileEntry = (DLFileEntry)object;
628    
629                                            try {
630                                                    Document document = getDocument(dlFileEntry);
631    
632                                                    if (document != null) {
633                                                            actionableDynamicQuery.addDocument(document);
634                                                    }
635                                            }
636                                            catch (PortalException pe) {
637                                                    if (_log.isWarnEnabled()) {
638                                                            _log.warn(
639                                                                    "Unable to index document library file entry " +
640                                                                            dlFileEntry.getFileEntryId(),
641                                                                    pe);
642                                                    }
643                                            }
644                                    }
645    
646                            });
647                    actionableDynamicQuery.setSearchEngineId(getSearchEngineId());
648    
649                    actionableDynamicQuery.performActions();
650            }
651    
652            protected void reindexFolders(final long companyId) throws PortalException {
653                    ActionableDynamicQuery actionableDynamicQuery =
654                            DLFolderLocalServiceUtil.getActionableDynamicQuery();
655    
656                    actionableDynamicQuery.setCompanyId(companyId);
657                    actionableDynamicQuery.setPerformActionMethod(
658                            new ActionableDynamicQuery.PerformActionMethod() {
659    
660                                    @Override
661                                    public void performAction(Object object)
662                                            throws PortalException {
663    
664                                            DLFolder dlFolder = (DLFolder)object;
665    
666                                            String portletId = PortletKeys.DOCUMENT_LIBRARY;
667                                            long groupId = dlFolder.getGroupId();
668                                            long folderId = dlFolder.getFolderId();
669    
670                                            String[] newIds = {
671                                                    String.valueOf(companyId), portletId,
672                                                    String.valueOf(groupId), String.valueOf(folderId)
673                                            };
674    
675                                            reindex(newIds);
676                                    }
677    
678                            });
679    
680                    actionableDynamicQuery.performActions();
681            }
682    
683            protected void reindexRoot(final long companyId) throws PortalException {
684                    ActionableDynamicQuery actionableDynamicQuery =
685                            GroupLocalServiceUtil.getActionableDynamicQuery();
686    
687                    actionableDynamicQuery.setCompanyId(companyId);
688                    actionableDynamicQuery.setPerformActionMethod(
689                            new ActionableDynamicQuery.PerformActionMethod() {
690    
691                                    @Override
692                                    public void performAction(Object object)
693                                            throws PortalException {
694    
695                                            Group group = (Group)object;
696    
697                                            String portletId = PortletKeys.DOCUMENT_LIBRARY;
698                                            long groupId = group.getGroupId();
699                                            long folderId = groupId;
700    
701                                            String[] newIds = {
702                                                    String.valueOf(companyId), portletId,
703                                                    String.valueOf(groupId), String.valueOf(folderId)
704                                            };
705    
706                                            reindex(newIds);
707                                    }
708    
709                            });
710    
711                    actionableDynamicQuery.performActions();
712            }
713    
714            private static final Log _log = LogFactoryUtil.getLog(
715                    DLFileEntryIndexer.class);
716    
717            private final RelatedEntryIndexer _relatedEntryIndexer =
718                    new BaseRelatedEntryIndexer();
719    
720    }