/**
 * $Id: LocalizationBundleFactory.java 38320 2008-04-05 20:41:47Z lgestrin $
 *
 * Unpublished Work Copyright 2007 MarketTools, Inc. All Rights Reserved.
 *
 * Last changed by $Author: lgestrin $
 * Last changed at $Date: 2008-04-05 13:41:47 -0700 (Sat, 05 Apr 2008) $
 */
package com.markettools.platform.webapp.view.admin;

import com.markettools.platform.webapp.view.ViewConstants;
import com.markettools.platform.webutil.MultipleResourceBundleFactory;

import net.sourceforge.stripes.config.Configuration;

import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;


/**
 * custom implementation for localizaiton bundle factory for stripes.
 *
 * @author  Leonard Gestrin (lgestrin)
 * @since   1.0, (Mar 27, 2008)
 */
public class LocalizationBundleFactory
    implements net.sourceforge.stripes.localization.LocalizationBundleFactory {

    /** resource bundle * */
    private MultipleResourceBundleFactory bundleFactory;

    /**
     * Uses the BootstrapPropertyResolver attached to the Configuration in order to
     * look for configured bundle names in the servlet init parameters etc. If those
     * can't be found then the default bundle names are put in place.
     */
    public void init(Configuration configuration) throws Exception {
        bundleFactory = (MultipleResourceBundleFactory)
            configuration.getServletContext().getAttribute(
                ViewConstants.CXT_I18N_BUNDLE_FACTORY);
    }

    /**
     * Looks for a bundle called StripesResources with the supplied locale if one is
     * provided, or with the default locale if the locale provided is null.
     *
     * @param   locale  an optional locale, may be null.
     *
     * @return  ResourceBundle a bundle in which to look for localized error messages
     *
     * @throws  java.util.MissingResourceException  if a suitable bundle cannot be
     *                                              found
     */
    public ResourceBundle getErrorMessageBundle(Locale locale)
        throws MissingResourceException {

        try {

            if (locale == null) {
                return bundleFactory.getBundle();
            } else {
                return bundleFactory.getBundle(locale);
            }
        } catch (MissingResourceException mre) {
            MissingResourceException mre2 = new MissingResourceException(
                    "Could not find the error message resource bundle needed by Stripes. This "
                    + "almost certainly means that a properties file called '"
                    + bundleFactory.getBaseNamesAsString()
                    + ".properties' could not be found in the classpath. "
                    + "This properties file is needed to lookup validation error messages. Please "
                    + "ensure the file exists in WEB-INF/classes or elsewhere in your classpath.",
                    bundleFactory.getBaseNamesAsString(), "");
            mre2.setStackTrace(mre.getStackTrace());
            throw mre2;
        }
    }

    /**
     * Looks for a bundle called StripesResources with the supplied locale if one is
     * provided, or with the default locale if the locale provided is null.
     *
     * @param   locale  an optional locale, may be null.
     *
     * @return  ResourceBundle a bundle in which to look for localized field names
     *
     * @throws  MissingResourceException  if a suitable bundle cannot be found
     */
    public ResourceBundle getFormFieldBundle(Locale locale)
        throws MissingResourceException {

        return getErrorMessageBundle(locale);
    }
}
