001/*
002 *  Copyright 2010 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.odf.orgunit;
017
018import java.util.Collections;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.avalon.framework.configuration.Configurable;
024import org.apache.avalon.framework.configuration.Configuration;
025import org.apache.avalon.framework.configuration.ConfigurationException;
026import org.apache.avalon.framework.service.ServiceException;
027import org.apache.avalon.framework.service.ServiceManager;
028import org.apache.avalon.framework.service.Serviceable;
029import org.apache.commons.lang.StringUtils;
030
031import org.ametys.plugins.repository.AmetysObjectResolver;
032import org.ametys.plugins.repository.UnknownAmetysObjectException;
033import org.ametys.runtime.i18n.I18nizableText;
034import org.ametys.runtime.model.Enumerator;
035
036/**
037 * Enumerator for {@link OrgUnit}s
038 *
039 */
040public class OrgUnitEnumerator implements Enumerator<String>, Serviceable, Configurable, org.ametys.runtime.parameter.Enumerator
041{
042    /** The Ametys object resolver */
043    protected AmetysObjectResolver _resolver;
044    /** The OrgUnit root provider */
045    protected RootOrgUnitProvider _orgUnitProvider;
046    
047    /** True if a value must be selected, false to allow the empty value. */
048    protected boolean _mandatory;
049    /** All option : 'disabled' to disable the 'all' option, 
050     * 'blank' to use a blank value for 'all' option,
051     * or 'concat' to concat all content types' ids for 'all' option
052     **/
053    protected String _allOption;
054    
055    @Override
056    public void configure(Configuration configuration) throws ConfigurationException
057    {
058        _mandatory = configuration.getChild("enumeration").getChild("custom-enumerator").getChild("mandatory").getValueAsBoolean(true);
059        Configuration allOptConf = configuration.getChild("all-option", false);
060        if (allOptConf == null)
061        {
062            allOptConf = configuration.getChild("enumeration").getChild("custom-enumerator").getChild("all-option", false);
063        }
064        _allOption = allOptConf != null ? allOptConf.getValue("blank") : "";
065    }
066    
067    @Override
068    public void service(ServiceManager smanager) throws ServiceException
069    {
070        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
071        _orgUnitProvider = (RootOrgUnitProvider) smanager.lookup(RootOrgUnitProvider.ROLE);
072    }
073
074    @Override
075    public Map<Object, I18nizableText> getEntries() throws Exception
076    {
077        return (Map<Object, I18nizableText>) (Object) getTypedEntries();
078    }
079
080    @Override
081    public Map<String, I18nizableText> getTypedEntries() throws Exception
082    {
083        Map<String, I18nizableText> entries = new HashMap<>();
084        
085        if (!_mandatory)
086        {
087            entries.put("", new I18nizableText("plugin.odf", "PLUGINS_ODF_ORGUNIT_ENUMERATOR_VALUE_ALL"));
088        }
089        
090        OrgUnit orgUnit = _orgUnitProvider.getRoot();
091        entries.putAll(_getSubOrgUnits(orgUnit));
092        
093        // All orgunits
094        if (_allOption.equals("blank"))
095        {
096            entries.put("", new I18nizableText("plugin.cms", "WIDGET_COMBOBOX_ALL_OPTIONS"));
097        }
098        else if (_allOption.equals("concat"))
099        {
100            entries.put(StringUtils.join(entries.keySet(), ','), new I18nizableText("plugin.cms", "WIDGET_COMBOBOX_ALL_OPTIONS"));
101        }
102        
103        return entries;
104    }
105    
106    private Map<String, I18nizableText> _getSubOrgUnits (OrgUnit orgUnit)
107    {
108        Map<String, I18nizableText> entries = new HashMap<>();
109        
110        List<String> ids = orgUnit.getSubOrgUnits();
111        for (String id : ids)
112        {
113            try
114            {
115                OrgUnit child = _resolver.resolveById(id);
116                String title = child.getTitle();
117                
118                entries.put(id, new I18nizableText(title));
119                
120                entries.putAll(_getSubOrgUnits (child));
121            }
122            catch (UnknownAmetysObjectException e)
123            {
124                // Ignore.
125            }
126        }
127        
128        return entries;
129    }
130
131    @Override
132    public I18nizableText getEntry(String value) throws Exception
133    {
134        I18nizableText entry = null;
135        
136        if (StringUtils.isEmpty(value))
137        {
138            entry = new I18nizableText("plugin.odf", "PLUGINS_ODF_ORGUNIT_ENUMERATOR_VALUE_ALL");
139        }
140        else
141        {
142            try
143            {
144                OrgUnit orgunit = _resolver.resolveById(value);
145                entry = new I18nizableText(orgunit.getTitle());
146            }
147            catch (UnknownAmetysObjectException e)
148            {
149                // Ignore.
150            }
151        }
152        
153        return entry;
154    }
155    
156    @Override
157    // TODO NEWATTRIBUTEAPI: remove this method when org.ametys.runtime.parameter.Enumerator will be removed
158    public Map<String, Object> getConfiguration()
159    {
160        return Collections.EMPTY_MAP;
161    }
162}