/*
 *************************************************************************
 * Copyright (c) 2004, 2005 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Actuate Corporation  - initial API and implementation
 *  
 *************************************************************************
 */

package org.eclipse.birt.data.oda.util;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.LinkedList;
import java.util.Locale;

/**
 * A base class that manages retrieving localized messages from int-based message 
 * keys.  Subclasses should override init() to pass a localized message array via 
 * setResourceTable().
 * @deprecated	As of BIRT 2.0, replaced by
 * 		{@link org.eclipse.datatools.connectivity.oda.util.ResourceManager 
 * 				org.eclipse.datatools.connectivity.oda.util.ResourceManager} .
 * 		<br>The BIRT ODA run-time framework has been migrated to the 
 * 		Eclipse Data Tools Platform (DTP) project.
 */
public class ResourceManager
{
	/**
	 * All int-key resources for any subclassed instance are held in this object.
	 */
	private Object[] m_oaValues = null;

	/**
	 *
	 */
	protected ResourceManager( ) { init(); }

	protected void init( )
	{
		setResourceTable( new Object[] { } );
	}

	/**
	 * The keys and values are internally created using a utility.
	 * Hence, they must always match. No possibility of an exception out here.
	 * Returns the offset from which the new set of resources are appended in
	 * case htResources becomes a static in the future.
	 */
	protected final void setResourceTable( Object[] oaValues )
	{
		this.m_oaValues = oaValues;
	}

	/**
	 *
	 */
	public final Object get( int iKey ) throws ValueNotFoundException
	{
		if( iKey < 0 || iKey > m_oaValues.length ) 
        {
			throw new ValueNotFoundException("Unable to find resource for key=" + iKey);
        }
		return m_oaValues[iKey];
	}

	/**
	 *
	 */
	public final String getString( int iKey ) throws ValueNotFoundException
	{
		if( iKey < 0 || iKey > m_oaValues.length ) 
			throw new ValueNotFoundException( "Unable to find resource for key=" + iKey );
		return ( m_oaValues[iKey] != null ) ? m_oaValues[iKey].toString( ) : null;
	}

	/**
	 *
	 */
	public String[] getResourceKeyNames( )
	{
		Class cBundle = getClass( );
		Field fa[] = null;
		try 
        {
			fa = cBundle.getFields( );
		}
        catch ( SecurityException ex ) 
        {
            return null; 
        }
		
		int[] iaRequiredMods = { Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL, Modifier.TRANSIENT };

		LinkedList ll = new LinkedList( );
		for( int i = 0; i < fa.length; i++ )
		{
			int m = fa[i].getModifiers( );
			boolean bInclude = true;
			for ( int j = 0; j < iaRequiredMods.length; j++ )
            {
				if( ( m & iaRequiredMods[j]) != iaRequiredMods[j] )
                {
                    bInclude = false;
                }
            }
			if( bInclude == true )  
            {
                ll.add( fa[i].getName( ) );
            }
		}
		Object [] oa = ll.toArray( );
		String [] sa = new String[oa.length];

		for( int i = 0; i < oa.length; i++ )
        {
			sa[i] = (String) oa[i];
        }
		return sa;
	}

	/**
	 *
	 */
    public static final ResourceManager loadResources( String sFQClass, Locale l )
	{
		if( l == null ) 
        {
            l = Locale.getDefault( );
        }

		String sLanguage = l.getLanguage( );
		String sCountry = l.getCountry( );
		String sVariant = l.getVariant( );

		int n = ( sLanguage.length() > 0 ) ? 2 : 1;
		n += ( sCountry.length( ) > 0 ) ? 1 : 0;
		n += ( sVariant.length( ) > 0 ) ? 1 : 0;

		String[] saNames = new String[n];
		String[] sa = {sFQClass, "_" + sLanguage, "_" + sCountry, "__" + sVariant};
		saNames[0] = sa[0];
		for( int i = 1; i < n; i++ )
        {
			saNames[i] = saNames[i - 1] + sa[i];
        }

		Class c = null;
		boolean bFound = false;
		for( int i = n - 1; i >= 0; i-- )
		{
			try 
            {
				c = Class.forName( saNames[i] );
				bFound = true;
				break;
			}
            catch ( ClassNotFoundException cnfe )
			{
			}
		}
		if( !bFound )
		{
            return null;
		}
		
		try 
        {
			ResourceManager rm = ( ResourceManager ) c.newInstance( );
			return rm;
		} 
        catch ( InstantiationException ie )
		{
            // do nothing
		} 
        catch ( IllegalAccessException iae )
		{
            // do nothing
		}
		return null;
	}

	/**
	 *
	 */
	public static final String getMediumDateFormat( )
	{
		return ( ( SimpleDateFormat ) SimpleDateFormat.getDateInstance( DateFormat.MEDIUM ) ).toPattern( );
	}

	/**
	 *
	 */
	public static final String getNumberFormat( int iFractionDigits )
	{
		DecimalFormat nf = ( DecimalFormat ) DecimalFormat.getNumberInstance( );
		nf.setMinimumFractionDigits( iFractionDigits );
		nf.setMaximumFractionDigits( iFractionDigits );
		nf.setMinimumIntegerDigits( 1 );
		return nf.toPattern( );
	}

	/**
	 *
	 */
	public static final String getCurrencyFormat( int iFractionDigits )
	{
		DecimalFormat nf = (DecimalFormat) DecimalFormat.getCurrencyInstance( );
		nf.setMinimumFractionDigits( iFractionDigits );
		nf.setMaximumFractionDigits( iFractionDigits );
		nf.setMinimumIntegerDigits( 1 );
		return nf.toPattern( );
	}
}

class ValueNotFoundException extends RuntimeException
{
	public ValueNotFoundException( String s ) { super( s ); }
}

