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