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