001/*
002 *  Copyright 2013 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.cms.languages;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.avalon.framework.service.Serviceable;
024
025import org.ametys.runtime.i18n.I18nizableText;
026import org.ametys.runtime.parameter.Enumerator;
027
028/**
029 * Languages enumerator
030 *
031 */
032public class LanguageEnumerator implements Enumerator, Serviceable
033{
034    private LanguagesManager _languageManager;
035    
036    @Override
037    public void service(ServiceManager manager) throws ServiceException
038    {
039        _languageManager = (LanguagesManager) manager.lookup(LanguagesManager.ROLE);
040    }
041    
042    @Override
043    public I18nizableText getEntry(String value) throws Exception
044    {
045        Map<String, Language> languages = _languageManager.getAvailableLanguages();
046        
047        if (languages.containsKey(value))
048        {
049            return languages.get(value).getLabel();
050        }
051        else
052        {
053            return new I18nizableText("");
054        }
055    }
056
057    @Override
058    public Map<Object, I18nizableText> getEntries() throws Exception
059    {
060        Map<Object, I18nizableText> entries = new HashMap<>();
061        
062        Map<String, Language> languages = _languageManager.getAvailableLanguages();
063        for (Language lang : languages.values())
064        {
065            entries.put(lang.getCode(), lang.getLabel());
066        }
067        
068        return entries;
069    }
070
071}