001/*
002 *  Copyright 2018 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.enumeration;
017
018import java.util.LinkedHashMap;
019import java.util.Map;
020import java.util.Optional;
021
022import org.apache.avalon.framework.component.Component;
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.lang3.StringUtils;
027
028import org.ametys.core.util.I18nUtils;
029import org.ametys.runtime.i18n.I18nizableText;
030import org.ametys.runtime.parameter.Enumerator;
031
032/**
033 * Month enumerator.
034 */
035public class MonthEnumerator implements Enumerator, Serviceable, Component
036{
037    /** Avalon Role */
038    public static final String ROLE = MonthEnumerator.class.getName();
039    
040    private static final String __CATALOGUE = "plugin.odf";
041    private static final Map<Object, I18nizableText> __MONTHS;
042    
043    static
044    {
045        __MONTHS = new LinkedHashMap<>(3);
046        __MONTHS.put("1", new I18nizableText(__CATALOGUE, "PLUGINS_ODF_MONTH_ENUMERATOR_JANUARY"));
047        __MONTHS.put("2", new I18nizableText(__CATALOGUE, "PLUGINS_ODF_MONTH_ENUMERATOR_FEBRUARY"));
048        __MONTHS.put("3", new I18nizableText(__CATALOGUE, "PLUGINS_ODF_MONTH_ENUMERATOR_MARCH"));
049        __MONTHS.put("4", new I18nizableText(__CATALOGUE, "PLUGINS_ODF_MONTH_ENUMERATOR_APRIL"));
050        __MONTHS.put("5", new I18nizableText(__CATALOGUE, "PLUGINS_ODF_MONTH_ENUMERATOR_MAY"));
051        __MONTHS.put("6", new I18nizableText(__CATALOGUE, "PLUGINS_ODF_MONTH_ENUMERATOR_JUNE"));
052        __MONTHS.put("7", new I18nizableText(__CATALOGUE, "PLUGINS_ODF_MONTH_ENUMERATOR_JULY"));
053        __MONTHS.put("8", new I18nizableText(__CATALOGUE, "PLUGINS_ODF_MONTH_ENUMERATOR_AUGUST"));
054        __MONTHS.put("9", new I18nizableText(__CATALOGUE, "PLUGINS_ODF_MONTH_ENUMERATOR_SEPTEMBER"));
055        __MONTHS.put("10", new I18nizableText(__CATALOGUE, "PLUGINS_ODF_MONTH_ENUMERATOR_OCTOBER"));
056        __MONTHS.put("11", new I18nizableText(__CATALOGUE, "PLUGINS_ODF_MONTH_ENUMERATOR_NOVEMBER"));
057        __MONTHS.put("12", new I18nizableText(__CATALOGUE, "PLUGINS_ODF_MONTH_ENUMERATOR_DECEMBER"));
058    }
059
060    /**
061     * i18n utils
062     */
063    protected I18nUtils _i18nUtils;
064    
065    public void service(ServiceManager manager) throws ServiceException
066    {
067        _i18nUtils = (I18nUtils) manager.lookup(I18nUtils.ROLE);
068    }
069    
070    @Override
071    public Map<Object, I18nizableText> getEntries()
072    {
073        return __MONTHS;
074    }
075
076    @Override
077    public I18nizableText getEntry(String value)
078    {
079        return __MONTHS.get(value);
080    }
081    
082    /**
083     * Get the translated label for the given code
084     * @param code The code to retrieve
085     * @param language The language for the translation
086     * @return The translated label or an empty string
087     */
088    public String getLabel(String code, String language)
089    {
090        return Optional.ofNullable(code)
091                .map(this::getEntry)
092                .map(key -> _i18nUtils.translate(key, language))
093                .orElse(StringUtils.EMPTY);
094    }
095}