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