001/*
002 *  Copyright 2018 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.web.frontoffice.search.requesttime.impl;
017
018import java.util.List;
019import java.util.stream.Collectors;
020
021import org.ametys.cms.search.query.OrQuery;
022import org.ametys.cms.search.query.Query;
023import org.ametys.web.frontoffice.search.instance.SearchServiceInstance;
024import org.ametys.web.frontoffice.search.instance.model.SearchContext;
025import org.ametys.web.frontoffice.search.metamodel.AdditionalParameterValueMap;
026import org.ametys.web.frontoffice.search.metamodel.Returnable;
027import org.ametys.web.frontoffice.search.metamodel.context.ContextQueriesWrapper;
028import org.ametys.web.frontoffice.search.requesttime.SearchComponent;
029import org.ametys.web.frontoffice.search.requesttime.SearchComponentArguments;
030import org.ametys.web.repository.page.Page;
031import org.ametys.web.repository.site.Site;
032
033/**
034 * {@link SearchComponent} for restricting the results to the {@link Returnable}s
035 */
036public class ReturnableSearchComponent implements SearchComponent
037{
038    @Override
039    public int priority()
040    {
041        return SEARCH_PRIORITY - 10000;
042    }
043
044    @Override
045    public boolean supports(SearchComponentArguments args)
046    {
047        return args.launchSearch();
048    }
049
050    @Override
051    public void execute(SearchComponentArguments args) throws Exception
052    {
053        Page currentPage = args.currentPage();
054        Site currentSite = args.currentSite();
055        String currentLang = args.currentLang();
056        SearchServiceInstance serviceInstance = args.serviceInstance();
057        AdditionalParameterValueMap additionalParameterValues = serviceInstance.getAdditionalParameterValues();
058        
059        List<ContextQueriesWrapper> contextQueriesWrappers = serviceInstance.getContexts()
060            .stream()
061            .map(searchCtx -> _createContextQueriesWrapper(searchCtx, currentSite, currentPage, currentLang))
062            .collect(Collectors.toList());
063        
064        Query filterQuery = args.serviceInstance().getReturnables()
065                .stream()
066                .map(returnable -> returnable.filterReturnedDocumentQuery(contextQueriesWrappers, additionalParameterValues))
067                .collect(OrQuery.collector());
068        args.searcher().addFilterQuery(filterQuery);
069    }
070    
071    /**
072     * Creates a {@link ContextQueriesWrapper} given one {@link SearchContext} and the current site, page and lang.
073     * @param searchContext The search context
074     * @param currentSite The current site
075     * @param currentPage The current page
076     * @param currentLang The current lang
077     * @return The created wrapper of queries of a {@link SearchContext}
078     */
079    protected ContextQueriesWrapper _createContextQueriesWrapper(SearchContext searchContext, Site currentSite, Page currentPage, String currentLang)
080    {
081        return new ContextQueriesWrapper(
082                searchContext.getSiteQuery(currentSite),
083                searchContext.getSitemapQuery(currentPage),
084                searchContext.getContextLang(currentLang),
085                searchContext.getTagQuery()
086        );
087    }
088}