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.QueryFilter;
019    import com.liferay.portal.kernel.search.generic.BooleanQueryImpl;
020    import com.liferay.portal.kernel.util.ArrayUtil;
021    import com.liferay.portal.util.PortalUtil;
022    
023    /**
024     * @author Michael C. Han
025     */
026    public class BaseRelatedEntryIndexer implements RelatedEntryIndexer {
027    
028            @Override
029            public void addRelatedClassNames(
030                            BooleanFilter contextBooleanFilter, SearchContext searchContext)
031                    throws Exception {
032    
033                    searchContext.setAttribute("relatedClassName", Boolean.TRUE);
034    
035                    String[] relatedEntryClassNames = (String[])searchContext.getAttribute(
036                            "relatedEntryClassNames");
037    
038                    if (ArrayUtil.isEmpty(relatedEntryClassNames)) {
039                            return;
040                    }
041    
042                    BooleanFilter relatedBooleanFilters = new BooleanFilter();
043    
044                    for (String relatedEntryClassName : relatedEntryClassNames) {
045                            Indexer<?> indexer = IndexerRegistryUtil.getIndexer(
046                                    relatedEntryClassName);
047    
048                            if (indexer == null) {
049                                    continue;
050                            }
051    
052                            BooleanFilter relatedBooleanFilter = new BooleanFilter();
053    
054                            indexer.postProcessContextBooleanFilter(
055                                    relatedBooleanFilter, searchContext);
056    
057                            for (IndexerPostProcessor indexerPostProcessor :
058                                            indexer.getIndexerPostProcessors()) {
059    
060                                    indexerPostProcessor.postProcessContextBooleanFilter(
061                                            relatedBooleanFilter, searchContext);
062                            }
063    
064                            postProcessContextQuery(
065                                    relatedBooleanFilter, searchContext, indexer);
066    
067                            relatedBooleanFilter.addRequiredTerm(
068                                    Field.CLASS_NAME_ID,
069                                    PortalUtil.getClassNameId(relatedEntryClassName));
070    
071                            relatedBooleanFilters.add(
072                                    relatedBooleanFilter, BooleanClauseOccur.SHOULD);
073                    }
074    
075                    if (relatedBooleanFilters.hasClauses()) {
076                            contextBooleanFilter.add(
077                                    relatedBooleanFilters, BooleanClauseOccur.MUST);
078                    }
079    
080                    searchContext.setAttribute("relatedClassName", Boolean.FALSE);
081            }
082    
083            @Override
084            public void addRelatedEntryFields(Document document, Object obj)
085                    throws Exception {
086            }
087    
088            @Override
089            public void updateFullQuery(SearchContext searchContext) {
090            }
091    
092            /**
093             * @deprecated As of 7.0.0, added strictly to support backwards
094             *             compatibility of {@link
095             *             Indexer#postProcessContextQuery(BooleanQuery, SearchContext)}
096             */
097            @Deprecated
098            protected void postProcessContextQuery(
099                            BooleanFilter relatedBooleanFilter, SearchContext searchContext,
100                            Indexer<?> indexer)
101                    throws Exception {
102    
103                    BooleanQuery entityQuery = new BooleanQueryImpl();
104    
105                    indexer.postProcessContextQuery(entityQuery, searchContext);
106    
107                    for (IndexerPostProcessor indexerPostProcessor :
108                                    indexer.getIndexerPostProcessors()) {
109    
110                            indexerPostProcessor.postProcessContextQuery(
111                                    entityQuery, searchContext);
112                    }
113    
114                    if (entityQuery.hasClauses()) {
115                            QueryFilter queryFilter = new QueryFilter(entityQuery);
116    
117                            relatedBooleanFilter.add(queryFilter, BooleanClauseOccur.MUST);
118                    }
119            }
120    
121    }