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;
025
026import org.ametys.cms.search.solr.SearcherFactory.Searcher;
027import org.ametys.web.frontoffice.search.metamodel.FacetDefinition;
028import org.ametys.web.frontoffice.search.requesttime.SearchComponent;
029import org.ametys.web.frontoffice.search.requesttime.SearchComponentArguments;
030
031/**
032 * {@link SearchComponent} for executing the search with facets.
033 */
034public class FacetSearchComponent implements SearchComponent
035{
036    @Override
037    public int priority()
038    {
039        return SEARCH_PRIORITY - 7000;
040    }
041
042    @Override
043    public boolean supports(SearchComponentArguments args)
044    {
045        return args.launchSearch();
046    }
047
048    @Override
049    public void execute(SearchComponentArguments args) throws Exception
050    {
051        Searcher searcher = args.searcher();
052        Collection<FacetDefinition> serviceFacets = args.serviceInstance().getFacets();
053        Map<String, List<String>> userFacets = args.userInputs().facets();
054        
055        checkValidInputs(serviceFacets, userFacets);
056        
057        searcher.withFacets(serviceFacets.stream()
058                .map(FacetDefinition::getSearchField)
059                .collect(Collectors.toList()));
060        
061        searcher.withFacetValues(userFacets);
062    }
063    
064    /**
065     * Checks the user inputs are valid
066     * @param serviceFacets The facets of the service instance
067     * @param userFacets The user input facets
068     * @throws InvalidUserInputException if at least user one input is invalid
069     */
070    protected void checkValidInputs(Collection<FacetDefinition> serviceFacets, Map<String, List<String>> userFacets) throws InvalidUserInputException
071    {
072        Collection<String> facetIds = serviceFacets
073                .stream()
074                .map(FacetDefinition::getId)
075                .collect(Collectors.toList());
076        Set<String> userFacetIds = userFacets.keySet();
077        
078        if (!CollectionUtils.containsAll(facetIds, userFacetIds))
079        {
080            throw new InvalidUserInputException("At least one of the user input facets is invalid because it was not declared by the service instance.");
081        }
082    }
083}