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.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.service.Serviceable;
025import org.apache.commons.lang.StringUtils;
026
027import org.ametys.plugins.repository.AmetysObjectResolver;
028import org.ametys.plugins.repository.UnknownAmetysObjectException;
029import org.ametys.runtime.i18n.I18nizableText;
030import org.ametys.runtime.parameter.Enumerator;
031
032/**
033 * Enumerator for {@link OrgUnit}s using UAI code as unique identifier
034 *
035 */
036public class OrgUnitUAICodeEnumerator implements Enumerator, Serviceable
037{
038    /** The Ametys object resolver */
039    protected AmetysObjectResolver _resolver;
040    /** The OrgUnit root provider */
041    protected RootOrgUnitProvider _orgUnitProvider;
042    
043    
044    @Override
045    public void service(ServiceManager smanager) throws ServiceException
046    {
047        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
048        _orgUnitProvider = (RootOrgUnitProvider) smanager.lookup(RootOrgUnitProvider.ROLE);
049    }
050
051    @Override
052    public Map<Object, I18nizableText> getEntries() throws Exception
053    {
054        Map<Object, I18nizableText> entries = new HashMap<>();
055        
056        OrgUnit orgUnit = _orgUnitProvider.getRoot();
057        entries.putAll(_getSubOrgUnits(orgUnit));
058        
059        return entries;
060    }
061    
062    private Map<Object, I18nizableText> _getSubOrgUnits (OrgUnit orgUnit)
063    {
064        Map<Object, I18nizableText> entries = new HashMap<>();
065        
066        List<String> ids = orgUnit.getSubOrgUnits();
067        for (String id : ids)
068        {
069            try
070            {
071                OrgUnit child = _resolver.resolveById(id);
072                String title = child.getTitle();
073                
074                entries.put(child.getUAICode(), new I18nizableText(title));
075                
076                entries.putAll(_getSubOrgUnits (child));
077            }
078            catch (UnknownAmetysObjectException e)
079            {
080                // Ignore.
081            }
082        }
083        
084        return entries;
085    }
086
087    @Override
088    public I18nizableText getEntry(String value) throws Exception
089    {
090        I18nizableText entry = null;
091        
092        if (StringUtils.isEmpty(value))
093        {
094            entry = new I18nizableText("plugin.odf", "PLUGINS_ODF_ORGUNIT_ENUMERATOR_VALUE_ALL");
095        }
096        else
097        {
098            try
099            {
100                OrgUnit orgunit = _resolver.resolveById(value);
101                entry = new I18nizableText(orgunit.getTitle());
102            }
103            catch (UnknownAmetysObjectException e)
104            {
105                // Ignore.
106            }
107        }
108        
109        return entry;
110    }
111
112}