atg.droplet
Class CurrencyConversionTagConverter

java.lang.Object
  extended by atg.droplet.CurrencyTagConverter
      extended by atg.droplet.CurrencyConversionTagConverter
All Implemented Interfaces:
TagConverter

public class CurrencyConversionTagConverter
extends CurrencyTagConverter

A tag converter which is used to parse, format currency amounts and to convert them between an default currency and a locale-specific currency.

This converter is a subclass of CurrencyTagConverter, and relies upon CurrencyTagConverter to do parsing and formatting.

The exchange rates are specified in a properties file that uses 2-letter country codes as the property names. The corresponding value is the number in that country's local currency that corresponds to one unit of the default currency.

If the country of the user's locale (or of the locale specified as an attribute with this TagConverter) does not have a configured exchange rate, then this TagConverter returns null, which causes a <VALUEOF> tag to use its default value.

An example of the use of this converter is as follows:

<valueof param="priceInfo.amount" currencyConversion>no price</valueof>
You can also specify a locale as follows:
<valueof param="priceInfo.amount" currencyConversion locale="en_US">no price</valueof>
As with CurrencyTagConverter, if no locale is specified then the converter first looks for a request parameter named locale. This parameter can either be a java.util.Locale object or a String which names a locale. If this cannot be found then we fetch the locale from the RequestLocale.


Field Summary
static java.lang.String CLASS_VERSION
          Class version string
protected  java.util.Map mExchangeRates
           
static java.lang.String NAME
           
 
Constructor Summary
CurrencyConversionTagConverter()
          Constructs an instance of CurrencyConversionTagConverter
 
Method Summary
 java.lang.Number convertCurrency(java.lang.Number pOriginalValue, java.util.Locale pLocale, boolean toLocale)
          Convert an amount of money between the default currency and the currency of a specified locale
Returns null if the locale does not have a configured exchange rate
 java.lang.String convertObjectToString(DynamoHttpServletRequest pRequest, java.lang.Object pValue, java.util.Properties pAttributes)
          Converts the supplied Object value to a String value.
 java.lang.Object convertStringToObject(DynamoHttpServletRequest pRequest, java.lang.String pValue, java.util.Properties pAttributes)
          Converts the supplied String value to an Object value.
 java.util.Map getExchangeRates()
          Returns property exchangeRates, and, as a side-effect, initializes it if it has not already been initialized.
 java.lang.String getLookupKey(java.util.Locale pLocale)
          Obtain the string to use as a look-up key to determine the exchange rate.
 java.lang.String getName()
          Returns the unique name for the TagConverter.
 TagAttributeDescriptor[] getTagAttributeDescriptors()
          Returns the list of TagAttributeDescriptors which are used by this converter.
 void initializeConversionMap()
          Constructs a Map of locale names to currency exchange rates, based on the entries in a ResourceBundle
protected  void initializeIfNecessary()
          Do the initialization in initializeConversionMap on a just-in-time basis.
protected static java.lang.String replaceEURO(java.lang.String pCurrency, java.lang.String pReplacement)
          Returns a string with the Euro character replaced with the supplied string.
 void setExchangeRates(java.util.Map pExchangeRates)
          Sets property exchangeRates
 
Methods inherited from class atg.droplet.CurrencyTagConverter
formatCurrency, getCurrencyFormat, getDefaultLocale, getNumberFormat, isUseRequestLocale, setDefaultLocale, setUseRequestLocale
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CLASS_VERSION

public static java.lang.String CLASS_VERSION
Class version string


NAME

public static final java.lang.String NAME
See Also:
Constant Field Values

mExchangeRates

protected java.util.Map mExchangeRates
Constructor Detail

CurrencyConversionTagConverter

public CurrencyConversionTagConverter()
Constructs an instance of CurrencyConversionTagConverter

Method Detail

setExchangeRates

public void setExchangeRates(java.util.Map pExchangeRates)
Sets property exchangeRates

The value of this property is a Map that has an entry for each supported currency conversion. The name for each entry is a 2-letter country code. The value for each entry is a double that indicates how many of the locale-specific currency are equivalent to one (1) unit of the default currency.


getExchangeRates

public java.util.Map getExchangeRates()
Returns property exchangeRates, and, as a side-effect, initializes it if it has not already been initialized.

See Also:
initializeIfNecessary

getName

public java.lang.String getName()
Returns the unique name for the TagConverter. This name can be used by the TagConverterManager to get a handle to this converter. When you specify the use of a TagConverter using the converter attribute in a tag, you use this name to refer to the TagConverter. TagConverter names are case insensitive (as is the convention in HTML).

Specified by:
getName in interface TagConverter
Overrides:
getName in class CurrencyTagConverter

getTagAttributeDescriptors

public TagAttributeDescriptor[] getTagAttributeDescriptors()
Returns the list of TagAttributeDescriptors which are used by this converter.

Specified by:
getTagAttributeDescriptors in interface TagConverter
Overrides:
getTagAttributeDescriptors in class CurrencyTagConverter

convertStringToObject

public java.lang.Object convertStringToObject(DynamoHttpServletRequest pRequest,
                                              java.lang.String pValue,
                                              java.util.Properties pAttributes)
                                       throws TagConversionException
Converts the supplied String value to an Object value. This operation is performed if a converter is used with an input tag during the form submission process or if a converter attributes are specified in the param tag when defining a new parameter.

Specified by:
convertStringToObject in interface TagConverter
Overrides:
convertStringToObject in class CurrencyTagConverter
Parameters:
pRequest - the request object for this conversion. This object can be null if the conversion is not performed in the context of a request.
pAttributes - The list of attributes in the tag that is invoking this converter.
pValue - The original String value to be converted.
Returns:
the converted object value.
Throws:
TagConversionException - if the conversion failed.

convertObjectToString

public java.lang.String convertObjectToString(DynamoHttpServletRequest pRequest,
                                              java.lang.Object pValue,
                                              java.util.Properties pAttributes)
                                       throws TagConversionException
Converts the supplied Object value to a String value. This operation is performed when you are displaying a value using this converter. This occurs when the converter is used in the valueof tag or when rendering the value attribute of an input tag.

Specified by:
convertObjectToString in interface TagConverter
Overrides:
convertObjectToString in class CurrencyTagConverter
Parameters:
pRequest - the request this conversion applies to. You can use this request to obtain the RequestLocale to localize the conversion process. This can be null if the conversion is not performed in the context of a request.
pValue - the Object value to be converted to a String
pAttributes - the set of attributes supplied in this tag declaration.
Returns:
the converted object value.
Throws:
TagConversionException

initializeIfNecessary

protected void initializeIfNecessary()
Do the initialization in initializeConversionMap on a just-in-time basis. Formerly this was done in the constructor, causing a deferrable start-up cost for this class.

See Also:
initializeConversionMap, getExchangeRates

initializeConversionMap

public void initializeConversionMap()
Constructs a Map of locale names to currency exchange rates, based on the entries in a ResourceBundle


convertCurrency

public java.lang.Number convertCurrency(java.lang.Number pOriginalValue,
                                        java.util.Locale pLocale,
                                        boolean toLocale)
Convert an amount of money between the default currency and the currency of a specified locale
Returns null if the locale does not have a configured exchange rate

Parameters:
pOriginalValue - the amount of money to convert
pLocale - the locale indicating the currency from/to which to convert
toLocale - true to convert from default to locale-specific currency, false to convert from locale-specific currency to default currency

getLookupKey

public java.lang.String getLookupKey(java.util.Locale pLocale)
Obtain the string to use as a look-up key to determine the exchange rate. The implementation here uses the locale's country name.

Parameters:
pLocale - the user's locale

replaceEURO

protected static java.lang.String replaceEURO(java.lang.String pCurrency,
                                              java.lang.String pReplacement)
Returns a string with the Euro character replaced with the supplied string.