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