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.Collection;
019import java.util.List;
020import java.util.Map;
021import java.util.Set;
022import java.util.stream.Collectors;
023
024import org.apache.commons.collections4.CollectionUtils;
025import org.slf4j.Logger;
026
027import org.ametys.cms.search.solr.SearcherFactory.Searcher;
028import org.ametys.web.frontoffice.search.metamodel.FacetDefinition;
029import org.ametys.web.frontoffice.search.requesttime.SearchComponent;
030import org.ametys.web.frontoffice.search.requesttime.SearchComponentArguments;
031import org.ametys.web.frontoffice.search.requesttime.input.SearchUserInputs;
032import org.ametys.web.frontoffice.search.requesttime.input.impl.UserPrefsSearchUserInputs;
033
034/**
035 * {@link SearchComponent} for executing the search with facets.
036 */
037public class FacetSearchComponent implements SearchComponent
038{
039    private static final String __DISABLE_FACET_PARAMETER_NAME = "disableFacet";
040    
041    @Override
042    public int priority()
043    {
044        return SEARCH_PRIORITY - 7000;
045    }
046
047    @Override
048    public boolean supports(SearchComponentArguments args)
049    {
050        return args.launchSearch() && !args.generatorParameters().getParameterAsBoolean(__DISABLE_FACET_PARAMETER_NAME, false);
051    }
052
053    @Override
054    public void execute(SearchComponentArguments args) throws Exception
055    {
056        Searcher searcher = args.searcher();
057        Collection<FacetDefinition> serviceFacets = args.serviceInstance().getFacets();
058        SearchUserInputs userInputs = args.userInputs();
059        Map<String, List<String>> userFacets = checkValidInputs(serviceFacets, userInputs.facets(), userInputs instanceof UserPrefsSearchUserInputs, args.logger());
060        
061        searcher.withFacets(serviceFacets.stream()
062                .map(FacetDefinition::getSearchField)
063                .collect(Collectors.toList()));
064        
065        searcher.withFacetValues(userFacets);
066    }
067    
068    /**
069     * Checks the user inputs are valid
070     * @param serviceFacets The facets of the service instance
071     * @param userFacets The user input facets
072     * @param isFromUserPref <code>true</code> if the facets are from user pref. Then return only the valid input, otherwise throw an exception
073     * @param logger The logger
074     * @return the filtered user facets
075     * @throws InvalidUserInputException if at least user one input is invalid
076     */
077    protected Map<String, List<String>> checkValidInputs(Collection<FacetDefinition> serviceFacets, Map<String, List<String>> userFacets, boolean isFromUserPref, Logger logger) throws InvalidUserInputException
078    {
079        Map<String, List<String>> filteredUserFacets = userFacets;
080        
081        Collection<String> facetIds = serviceFacets
082                .stream()
083                .map(FacetDefinition::getId)
084                .collect(Collectors.toList());
085        Set<String> userFacetIds = userFacets.keySet();
086        
087        if (!CollectionUtils.containsAll(facetIds, userFacetIds))
088        {
089            throw new InvalidUserInputException("At least one of the user input facets is invalid because it was not declared by the service instance.");
090        }
091        
092        // If the user input is from user pref, just ignore the invalid input
093        if (isFromUserPref)
094        {
095            filteredUserFacets = filteredUserFacets.entrySet()
096                                .stream()
097                                .filter(f -> this._isFromFacets(userFacetIds, f.getKey(), logger))
098                                .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));
099        }
100        else if (!CollectionUtils.containsAll(facetIds, userFacetIds))
101        {
102            throw new InvalidUserInputException("At least one of the user input criterion is invalid because it was not declared by the service instance.");
103        }
104        
105        return filteredUserFacets;
106        
107    }
108    
109    private boolean _isFromFacets(Set<String> userFacetIds, String facetId, Logger logger)
110    {
111        boolean isFromCriteria = userFacetIds.contains(facetId);
112        if (!isFromCriteria)
113        {
114            if (logger.isDebugEnabled())
115            {
116                logger.debug("The user input criterion ({}) from user preferences is invalid because it was not declared by the service instance.", facetId);
117            }
118        }
119        return isFromCriteria;
120    }
121}