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