001/*
002 *  Copyright 2019 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.metamodel.impl;
017
018import java.util.List;
019import java.util.Map;
020import java.util.function.Function;
021
022import org.ametys.core.util.LambdaUtils;
023import org.ametys.runtime.i18n.I18nizableText;
024import org.ametys.runtime.parameter.Enumerator;
025import org.ametys.web.frontoffice.search.metamodel.EnumeratedValues;
026
027class EnumeratorBasedEnumeratedValues implements EnumeratedValues
028{
029    private Enumerator _enumerator;
030    private String _criterionDefinitionId;
031    
032    EnumeratorBasedEnumeratedValues(Enumerator enumerator, String criterionDefinitionId)
033    {
034        _enumerator = enumerator;
035        _criterionDefinitionId = criterionDefinitionId;
036    }
037    
038    @Override
039    public Map<Object, I18nizableText> getAllValues()
040    {
041        try
042        {
043            return _enumerator.getEntries();
044        }
045        catch (Exception e)
046        {
047            throw new IllegalArgumentException("An error occured with the Enumerator of the criterion definition '" + _criterionDefinitionId + "'", e);
048        }
049    }
050
051    @Override
052    public RestrictedValues getRestrictedValuesFor(List<Object> objs)
053    {
054        return new EnumeratorBasedRestrictedValues(this, objs);
055    }
056    
057    static class EnumeratorBasedRestrictedValues implements RestrictedValues
058    {
059        private EnumeratedValues _enumeratedValues;
060        private List<Object> _forObjs;
061
062        EnumeratorBasedRestrictedValues(EnumeratedValues enumeratedValues, List<Object> forObjs)
063        {
064            _enumeratedValues = enumeratedValues;
065            _forObjs = forObjs;
066        }
067        
068        @Override
069        public Map<Object, I18nizableText> values()
070        {
071            Map<Object, I18nizableText> allEntries = _enumeratedValues.getAllValues();
072            return _forObjs
073                    .stream()
074                    .filter(v -> _isContainedInAllEntries(v, allEntries))
075                    .collect(LambdaUtils.Collectors.toLinkedHashMap(
076                            Function.identity(), 
077                            allEntries::get));
078        }
079        
080        private boolean _isContainedInAllEntries(Object desiredValue, Map<Object, I18nizableText> allEntries)
081        {
082            return allEntries.containsKey(desiredValue);
083        }
084    }
085}
086