Java tutorial
/* * Copyright (c) 2007 NTT DATA Corporation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jp.terasoluna.fw.web.codelist; import java.util.List; import java.util.Locale; import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * ??{@link jp.terasoluna.fw.web.codelist.CodeListLoader} * ?NX?B * * <p> * NX??R?[hXg??P?[ * R?[hXg}bv`??B * ?A}bv?P?[w * R?[hXg?\bh?B * <br> * R?[hXg???ANXp?TuNX?B * * @see DBCodeListLoader * @see MappedCodeListLoader * */ public abstract class AbstractMultilingualCodeListLoader implements CodeListLoader { /** * ?ONX?B */ private Log log = LogFactory.getLog(AbstractMultilingualCodeListLoader.class); /** * ??R?[hXg?}bv * @see jp.terasoluna.fw.web.codelist.CodeBean */ protected Map<Locale, List<CodeBean>> localeMap = null; /** * ?P?[w??ftHg?P?[?B R?[hXg * ?P?[w???A ?P?[??B * ftHgT?[o?[JVMR?[h?P?[gp?B */ protected Locale defaultLocale = new Locale(Locale.getDefault().getLanguage()); /** * ftHg?P?[??B * * @param defaultLocale ftHg?P?[ */ public void setDefaultLocale(Locale defaultLocale) { this.defaultLocale = defaultLocale; } /** * R?[hXg?B * * R?[hXg {@link CodeBean} z?B<br> * ?R?[hXgAvP?[V??B * ?\bhI?[o?[Ch???A?WbN * R?[hXge?WeKv?B * * @return R?[hXg */ public CodeBean[] getCodeBeans() { return createCodeBeans(defaultLocale); } /** * ?P?[wR?[hXg?B * <p> * R?[hXg {@link CodeBean} z?B<br> * ?w?P?[R?[hXg???A * ftHg?P?[R?[hXg?B * * ?R?[hXgAvP?[V??B * ?\bhI?[o?[Ch???A?WbN * R?[hXge?WeKv?B * * @param locale ?P?[ * @return R?[hXg */ public CodeBean[] getCodeBeans(Locale locale) { if (log.isDebugEnabled()) { log.debug("getCodeBeans(" + locale + ") called."); } CodeBean[] result = createCodeBeans(locale); // R?[hXg???AftHg?P?[??? if (locale != null && result.length == 0) { result = createCodeBeans(defaultLocale); } return result; } /** * ?P?[R?[hXg???B * <p> * ?w?P?[R?[hXg????A * ??P?[R?[hXg???B * * @param locale ?P?[ * @return R?[hXg */ protected CodeBean[] createCodeBeans(Locale locale) { if (log.isDebugEnabled()) { log.debug("createCodeBeans(" + locale + ") called."); } if (localeMap == null) { if (log.isDebugEnabled()) { log.debug("field codeListsMap is null."); } // codeListsMap?z?B return new CodeBean[0]; } if (locale == null) { if (log.isDebugEnabled()) { log.debug("arg locale is null. replace locale default : " + defaultLocale); } if (defaultLocale == null) { throw new IllegalStateException("Default locale is null."); } locale = defaultLocale; } List<CodeBean> codeLists = localeMap.get(locale); // R?[hXg???A??P?[??? if (codeLists == null) { if (locale.getVariant().length() > 0) { return createCodeBeans(new Locale(locale.getLanguage(), locale.getCountry())); } else if (locale.getCountry().length() > 0) { return createCodeBeans(new Locale(locale.getLanguage())); } // codeLists?z?B return new CodeBean[0]; } CodeBean[] cb = new CodeBean[codeLists.size()]; for (int i = 0; i < codeLists.size(); i++) { cb[i] = new CodeBean(); cb[i].setId(codeLists.get(i).getId()); cb[i].setName(codeLists.get(i).getName()); } return cb; } /** * R?[h?A?R?[h?AoAgR?[h?P?[???B * <p> * R?[hnull??ftHg?P?[p?B * * @param language R?[h * @param country ?R?[h * @param variant oAgR?[h * @return ?P?[ */ protected Locale createLocale(String language, String country, String variant) { // R?[h????AftHg?P?[p if (language == null || language.length() == 0) { return defaultLocale; } // ?R?[h????AR?[h???P?[p if (country == null || country.length() == 0) { return new Locale(language); } // oAgR?[h????A // R?[h?A?R?[h???P?[p if (variant == null || variant.length() == 0) { return new Locale(language, country); } return new Locale(language, country, variant); } }