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: 1 for January, 12 for December. 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 @SuppressWarnings("unchecked") 073 public Map<Object, I18nizableText> getEntries() throws Exception 074 { 075 return (Map<Object, I18nizableText>) (Object) getTypedEntries(); 076 } 077 078 @Override 079 public Map<String, I18nizableText> getTypedEntries() throws Exception 080 { 081 return __MONTHS; 082 } 083 084 @Override 085 public I18nizableText getEntry(String value) 086 { 087 return __MONTHS.get(value); 088 } 089 090 /** 091 * Get the translated label for the given code 092 * @param code The code to retrieve 093 * @param language The language for the translation 094 * @return The translated label or an empty string 095 */ 096 public String getLabel(String code, String language) 097 { 098 return Optional.ofNullable(code) 099 .map(this::getEntry) 100 .map(key -> _i18nUtils.translate(key, language)) 101 .orElse(StringUtils.EMPTY); 102 } 103 104 @Override 105 // TODO NEWATTRIBUTEAPI: remove this method when org.ametys.runtime.parameter.Enumerator will be removed 106 public Map<String, Object> getConfiguration() 107 { 108 return Collections.EMPTY_MAP; 109 } 110}