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.portal.kernel.search;
016    
017    import java.util.ArrayList;
018    import java.util.List;
019    
020    /**
021     * @author Eduardo Garcia
022     */
023    public class FolderSearcher extends BaseSearcher {
024    
025            public static Indexer getInstance() {
026                    return new FolderSearcher();
027            }
028    
029            public FolderSearcher() {
030                    setDefaultSelectedFieldNames(Field.TITLE, Field.UID);
031                    setFilterSearch(true);
032                    setPermissionAware(true);
033    
034                    List<String> folderClassNames = new ArrayList<String>();
035    
036                    for (Indexer indexer : IndexerRegistryUtil.getIndexers()) {
037                            if (indexer instanceof FolderIndexer) {
038                                    FolderIndexer folderIndexer = (FolderIndexer)indexer;
039    
040                                    for (String folderClassName :
041                                                    folderIndexer.getFolderClassNames()) {
042    
043                                            folderClassNames.add(folderClassName);
044                                    }
045                            }
046                    }
047    
048                    _classNames = folderClassNames.toArray(
049                            new String[folderClassNames.size()]);
050            }
051    
052            @Override
053            public String[] getClassNames() {
054                    return _classNames;
055            }
056    
057            @Override
058            protected BooleanQuery createFullQuery(
059                            BooleanQuery contextQuery, SearchContext searchContext)
060                    throws Exception {
061    
062                    long[] folderIds = searchContext.getFolderIds();
063    
064                    BooleanQuery entryClassPKQuery = BooleanQueryFactoryUtil.create(
065                            searchContext);
066    
067                    for (long folderId : folderIds) {
068                            entryClassPKQuery.addTerm(Field.ENTRY_CLASS_PK, folderId);
069                    }
070    
071                    contextQuery.add(entryClassPKQuery, BooleanClauseOccur.MUST);
072    
073                    return super.createFullQuery(contextQuery, searchContext);
074            }
075    
076            private final String[] _classNames;
077    
078    }