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.plugins.odfpilotage.report.consistency;
017
018import java.util.Map;
019import java.util.stream.Collectors;
020
021import org.apache.avalon.framework.configuration.Configurable;
022import org.apache.avalon.framework.configuration.Configuration;
023import org.apache.avalon.framework.configuration.ConfigurationException;
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.runtime.i18n.I18nizableText;
029import org.ametys.runtime.model.Enumerator;
030
031/**
032 * Enumerator for {@link ConsistencyAnalysis}
033 */
034public class AnalysisEnumerator implements Enumerator<String>, Serviceable, Configurable
035{
036    /** The analysis extension point */
037    protected AnalysisExtensionPoint _analysisEP;
038    
039    /**
040     * <code>true</code> to have the all option.
041     * Default value: <code>true</code>
042     **/
043    protected boolean _allOption;
044    
045    @Override
046    public void service(ServiceManager smanager) throws ServiceException
047    {
048        _analysisEP = (AnalysisExtensionPoint) smanager.lookup(AnalysisExtensionPoint.ROLE);
049    }
050    
051    @Override
052    public void configure(Configuration configuration) throws ConfigurationException
053    {
054        _allOption = configuration.getChild("enumeration").getChild("custom-enumerator").getChild("allOption").getValueAsBoolean(true);
055    }
056    
057    public Map<String, I18nizableText> getTypedEntries() throws Exception
058    {
059        Map<String, I18nizableText> entries = _analysisEP.getExtensionsIds()
060            .stream()
061            .map(_analysisEP::getExtension)
062            .collect(Collectors.toMap(ConsistencyAnalysis::getId, ConsistencyAnalysis::getLabel));
063        
064        // All analyses
065        _handleAllOptionEntry(entries);
066        
067        return entries;
068    }
069    
070    /**
071     * Add the all option entry to the entry list if necessary
072     * @param entries The enumerator entries
073     */
074    protected void _handleAllOptionEntry(Map<String, I18nizableText> entries)
075    {
076        if (_allOption)
077        {
078            entries.put("", new I18nizableText("plugin.cms", "WIDGET_COMBOBOX_ALL_OPTIONS"));
079        }
080    }
081
082    @Override
083    public I18nizableText getEntry(String value) throws Exception
084    {
085        ConsistencyAnalysis analysis = _analysisEP.getExtension(value);
086        return analysis.getLabel();
087    }
088}