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