001/*
002 *  Copyright 2017 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.restrictions;
017
018import java.util.HashMap;
019import java.util.Map;
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;
027import org.apache.commons.lang.StringUtils;
028
029import org.ametys.runtime.i18n.I18nizableText;
030import org.ametys.runtime.parameter.Enumerator;
031
032/**
033 * Enumerator for available {@link OdfProgramRestriction}s
034 */
035public class OrgProgramRestrictionEnumerator implements Enumerator, Serviceable, Configurable
036{
037    /** Odf program restriction manager */
038    protected OdfProgramRestrictionManager _odfProgramRestrictionManager;
039    
040    /** True if a value must be selected, false to allow the empty value. */
041    protected boolean _mandatory;
042    
043    @Override
044    public void configure(Configuration configuration) throws ConfigurationException
045    {
046        Configuration mandatoryConf = configuration.getChild("enumeration").getChild("custom-enumerator").getChild("mandatory", false);
047        _mandatory = mandatoryConf != null ? mandatoryConf.getValueAsBoolean(true) : false;
048    }
049    
050    @Override
051    public void service(ServiceManager smanager) throws ServiceException
052    {
053        _odfProgramRestrictionManager = (OdfProgramRestrictionManager) smanager.lookup(OdfProgramRestrictionManager.ROLE);
054    }
055
056    @Override
057    public Map<Object, I18nizableText> getEntries() throws Exception
058    {
059        Map<Object, I18nizableText> entries = new HashMap<>();
060        
061        if (!_mandatory)
062        {
063            entries.put(StringUtils.EMPTY, new I18nizableText("plugin.odf-web", "PLUGINS_ODFWEB_NO_RESTRICTION"));
064        }
065        
066        for (OdfProgramRestriction restriction : _odfProgramRestrictionManager.getRestrictions().values())
067        {
068            entries.put(restriction.getId(), restriction.getLabel());
069        }
070        
071        return entries;
072    }
073    
074    @Override
075    public I18nizableText getEntry(String value) throws Exception
076    {
077        I18nizableText entry = null;
078        
079        if (StringUtils.isEmpty(value))
080        {
081            entry = new I18nizableText("plugin.odf-web", "PLUGINS_ODFWEB_NO_RESTRICTION");
082        }
083        else
084        {
085            OdfProgramRestriction restriction = _odfProgramRestrictionManager.getRestrictions().get(value);
086            if (restriction != null)
087            {
088                entry = restriction.getLabel();
089            }
090        }
091        
092        return entry;
093    }
094
095}