001/*
002 *  Copyright 2021 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.plugins.odfweb.service.search;
017
018import java.util.HashMap;
019import java.util.List;
020import java.util.Map;
021import java.util.Map.Entry;
022import java.util.stream.Collectors;
023
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.avalon.framework.service.Serviceable;
027
028import org.ametys.plugins.odfweb.service.search.criterion.DegreeUniversityAttributeContentSearchCriterionDefinition;
029import org.ametys.plugins.odfweb.service.search.helper.DegreeUniversityHelper;
030import org.ametys.plugins.repository.AmetysObject;
031import org.ametys.web.frontoffice.search.requesttime.SearchComponent;
032import org.ametys.web.frontoffice.search.requesttime.SearchComponentArguments;
033import org.ametys.web.frontoffice.search.requesttime.input.SearchUserInputs;
034
035/**
036 * {@link SearchComponent} for handle degree university inputs
037 */
038public class DegreeUniversitySearchComponent implements SearchComponent, Serviceable
039{
040    /** The degree university helper */
041    protected DegreeUniversityHelper _degreeUniversityHelper;
042    
043    public void service(ServiceManager manager) throws ServiceException
044    {
045        _degreeUniversityHelper = (DegreeUniversityHelper) manager.lookup(DegreeUniversityHelper.ROLE);
046    }
047    
048    @Override
049    public int priority()
050    {
051        return MAX_PRIORITY;
052    }
053
054    @Override
055    public boolean supports(SearchComponentArguments args)
056    {
057        return args.launchSearch();
058    }
059
060    @Override
061    public void execute(SearchComponentArguments args) throws Exception
062    {
063        SearchUserInputs userInputs = args.userInputs();
064        
065        Map<String, List<String>> facets = new HashMap<>();
066        for (Entry<String, List<String>> entry : userInputs.facets().entrySet())
067        {
068            if (entry.getKey().equals("ContentReturnable$" + DegreeUniversityAttributeContentSearchCriterionDefinition.DEGREE_UNIVERSITY_SEARCH_CRITERION_ID))
069            {
070                facets.put(_getCriterionKey(args), _updatedFacetsValues(entry.getValue()));
071            }
072            else
073            {
074                facets.put(entry.getKey(), entry.getValue());
075            }
076        }
077        userInputs.setFacets(facets);
078        
079        args.updateUserInputs(userInputs);
080    }
081    
082    private String _getCriterionKey(SearchComponentArguments args)
083    {
084        String key = "ContentReturnable$" + DegreeUniversityAttributeContentSearchCriterionDefinition.DEGREE_SEARCH_CRITERION_ID;
085        boolean present = args.serviceInstance()
086            .getFacets()
087            .stream()
088            .anyMatch(f -> f.getId().equals(key));
089        
090        return present ? key : "ContentReturnable$" + DegreeUniversityAttributeContentSearchCriterionDefinition.DEGREE_UNIVERSITY_SEARCH_CRITERION_ID;
091    }
092    
093    private List<String> _updatedFacetsValues(List<String> facets)
094    {
095        return facets.stream()
096            .map(this::_getDegreeValues)
097            .flatMap(List::stream)
098            .collect(Collectors.toList());
099    }
100    
101    private List<String> _getDegreeValues(String value)
102    {
103        if (DegreeUniversityAttributeContentSearchCriterionDefinition.SEARCH_CRITERION_ALL_DU_VALUE.equals(value))
104        {
105            return _degreeUniversityHelper.getDegrees(true)
106                .stream()
107                .map(AmetysObject::getId)
108                .collect(Collectors.toList());
109        }
110        
111        return List.of(value);
112    }
113}