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