atg.droplet
Class CurrencyConversionFormatter

java.lang.Object
  extended by atg.nucleus.logging.VariableArgumentApplicationLoggingImpl
      extended by atg.nucleus.GenericService
          extended by atg.nucleus.TimedOperationService
              extended by atg.nucleus.servlet.ServletService
                  extended by atg.nucleus.servlet.HttpServletService
                      extended by atg.servlet.DynamoServlet
                          extended by atg.droplet.CurrencyFormatter
                              extended by atg.droplet.CurrencyConversionFormatter
All Implemented Interfaces:
NameContextBindingListener, NameContextElement, NameResolver, AdminableService, ApplicationLogging, atg.nucleus.logging.ApplicationLoggingSender, atg.nucleus.logging.TraceApplicationLogging, atg.nucleus.logging.VariableArgumentApplicationLogging, ComponentNameResolver, Service, ServiceListener, ParameterServlet, java.util.EventListener, javax.servlet.Servlet

public class CurrencyConversionFormatter
extends CurrencyFormatter

This droplet can be used to convert and format a numeric amount, within a particular Locale, from a specific currency into a specific currency.

Example:

 <importbean bean="/atg/dynamo/droplet/CurrencyConversionFormatter">
 <droplet bean="CurrencyConversionFormatter">
   <param name="currency" value="param:amount">
   <param name="locale" value="en_US">
   <param name="targetLocale" value="bean:/OriginatingRequest.requestLocale.locale">
   <param name="euroSymbol" value="&euro">
   <oparam name="output">
     <valueof param="formattedCurrency" valueishtml>no price</valueof>
   </oparam>
 </droplet>
 
Usage:
currency
A number, expressed as either a java.lang.Number or String, which is the value to format
locale
The locale that "currency" is currently accurate for. For example, if sourceLocale is "en_US" then currency is expressed in US dollars. This value can be either a java.util.Locale object or a String which represents a locale. (e.g. en_US) If you want this to use Euro's, the variant of the locale must be EURO. This parameter is optional, if it is not provided then the default locale is assumed.
targetLocale
The locale which should be used to convert format the currency amount. This value can If you want this to use Euro's, the variant of the locale must be EURO. be either a java.util.Locale object or a String which represents a locale. (e.g. en_US)
euroSymbol
This optional parameter will be used if the target locale is using Euro's. If this is provided, it will be used as the symbol for Euros. This is useful because the commonly-used character set ISO Latin-1 (ISO 8859-1) does not include this character.
output
This parameter is serviced if the instance of this class is used as a droplet
formattedCurrency
Available from within the output parameter, this is the formatted currency


Field Summary
static java.lang.String CLASS_VERSION
          Class version string
protected  java.util.Map mExchangeRates
           
 
Fields inherited from class atg.servlet.DynamoServlet
mAllowCleanupRequest, mParameters
 
Fields inherited from class atg.nucleus.servlet.ServletService
SERVLET_INFO_KEY
 
Fields inherited from class atg.nucleus.GenericService
SERVICE_INFO_KEY
 
Fields inherited from interface atg.nucleus.logging.TraceApplicationLogging
DEFAULT_LOG_TRACE_STATUS
 
Fields inherited from interface atg.nucleus.logging.ApplicationLogging
DEFAULT_LOG_DEBUG_STATUS, DEFAULT_LOG_ERROR_STATUS, DEFAULT_LOG_INFO_STATUS, DEFAULT_LOG_WARNING_STATUS
 
Constructor Summary
CurrencyConversionFormatter()
          Constructs an instanceof CurrencyConversionFormatter
 
Method Summary
 java.lang.Number convertCurrency(java.lang.Number pOriginalValue, java.util.Locale pSourceLocale, java.util.Locale pTargetLocale)
          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
protected  java.lang.String formatCurrency(DynamoHttpServletRequest pRequest, DynamoHttpServletResponse pResponse)
          Return the currency formatted in the given Locale as a String.
protected  java.util.Locale getCurrencyTargetLocale(DynamoHttpServletRequest pRequest, DynamoHttpServletResponse pResponse)
          Returns the locale associated with the request.
protected  java.lang.String getEuroSymbol(DynamoHttpServletRequest pRequest, DynamoHttpServletResponse pResponse)
          Returns the symbol to use when formatting a price in Euros.
 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.
protected  java.lang.String getYenSymbol(DynamoHttpServletRequest pRequest, DynamoHttpServletResponse pResponse)
          Returns the symbol to use when formatting a price in Yen.
 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.
 void service(DynamoHttpServletRequest pRequest, DynamoHttpServletResponse pResponse)
          Takes the formatted currency String and sets a request parameter named formattedCurrency, then services the output parameter.
 void setExchangeRates(java.util.Map pExchangeRates)
          Sets property exchangeRates
 
Methods inherited from class atg.droplet.CurrencyFormatter
getCurrency, getCurrencyLocale, getDefaultLocale, isUseRequestLocale, setDefaultLocale, setUseRequestLocale
 
Methods inherited from class atg.servlet.DynamoServlet
doDelete, doGet, doPost, doPut, getParameter, getParameters, getServletContext, service, service, setParameter
 
Methods inherited from class atg.nucleus.servlet.ServletService
destroy, getServletConfig, getServletInfo, init, setServletInfo
 
Methods inherited from class atg.nucleus.TimedOperationService
getAverageRequestHandlingTime, getHandledRequestCount, getRequestStartTime, getTotalRequestHandlingTime, isKeepingStatistics, notifyHandledRequest, resetStatistics, setKeepingStatistics
 
Methods inherited from class atg.nucleus.GenericService
addLogListener, createAdminServlet, doStartService, doStopService, getAbsoluteName, getAdminServlet, getLoggingForVlogging, getLogListenerCount, getLogListeners, getName, getNameContext, getNucleus, getRoot, getServiceConfiguration, getServiceInfo, isLoggingDebug, isLoggingError, isLoggingInfo, isLoggingTrace, isLoggingWarning, isRunning, logDebug, logDebug, logDebug, logError, logError, logError, logInfo, logInfo, logInfo, logTrace, logTrace, logTrace, logWarning, logWarning, logWarning, nameContextElementBound, nameContextElementUnbound, removeLogListener, resolveName, resolveName, resolveName, resolveName, sendLogEvent, setLoggingDebug, setLoggingError, setLoggingInfo, setLoggingTrace, setLoggingWarning, setNucleus, setServiceInfo, startService, stopService
 
Methods inherited from class atg.nucleus.logging.VariableArgumentApplicationLoggingImpl
vlogDebug, vlogDebug, vlogDebug, vlogDebug, vlogError, vlogError, vlogError, vlogError, vlogInfo, vlogInfo, vlogInfo, vlogInfo, vlogTrace, vlogTrace, vlogTrace, vlogTrace, vlogWarning, vlogWarning, vlogWarning, vlogWarning
 
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


mExchangeRates

protected java.util.Map mExchangeRates
Constructor Detail

CurrencyConversionFormatter

public CurrencyConversionFormatter()
Constructs an instanceof CurrencyConversionFormatter

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

getCurrencyTargetLocale

protected java.util.Locale getCurrencyTargetLocale(DynamoHttpServletRequest pRequest,
                                                   DynamoHttpServletResponse pResponse)
                                            throws javax.servlet.ServletException,
                                                   java.io.IOException
Returns the locale associated with the request. The method first searches for a request paramater named sourceLocale. This value can be either a java.util.Locale object or a String which represents the locale. Next if the useRequestLocale property is true, then the locale of the request will be returned. Finally, if the locale cannot be determined, the the defaultLocale property is used.

Throws:
javax.servlet.ServletException
java.io.IOException

getEuroSymbol

protected java.lang.String getEuroSymbol(DynamoHttpServletRequest pRequest,
                                         DynamoHttpServletResponse pResponse)
                                  throws javax.servlet.ServletException,
                                         java.io.IOException
Returns the symbol to use when formatting a price in Euros. This is optional.

Overrides:
getEuroSymbol in class CurrencyFormatter
Throws:
javax.servlet.ServletException
java.io.IOException

getYenSymbol

protected java.lang.String getYenSymbol(DynamoHttpServletRequest pRequest,
                                        DynamoHttpServletResponse pResponse)
                                 throws javax.servlet.ServletException,
                                        java.io.IOException
Returns the symbol to use when formatting a price in Yen.

Throws:
javax.servlet.ServletException
java.io.IOException

formatCurrency

protected java.lang.String formatCurrency(DynamoHttpServletRequest pRequest,
                                          DynamoHttpServletResponse pResponse)
                                   throws javax.servlet.ServletException,
                                          java.io.IOException
Return the currency formatted in the given Locale as a String. The currency and locales are extracted from the request using the getCurrency(request, response) getCurrencyLocale(request, response) and getCurrencyTargetLocale(request, response) methods.

Overrides:
formatCurrency in class CurrencyFormatter
Throws:
javax.servlet.ServletException
java.io.IOException

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

Throws:
ServiceException - if an entry could not be parsed as a Double

convertCurrency

public java.lang.Number convertCurrency(java.lang.Number pOriginalValue,
                                        java.util.Locale pSourceLocale,
                                        java.util.Locale pTargetLocale)
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
pSourceLocale - the locale indicating the currency from which to convert
pTargetLocale - the locale indicating the currency to which to convert

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

service

public void service(DynamoHttpServletRequest pRequest,
                    DynamoHttpServletResponse pResponse)
             throws javax.servlet.ServletException,
                    java.io.IOException
Takes the formatted currency String and sets a request parameter named formattedCurrency, then services the output parameter.

Overrides:
service in class CurrencyFormatter
Parameters:
pRequest - the request to be processed
pResponse - the response object for this request
Throws:
javax.servlet.ServletException - an application specific error occurred processing this request
java.io.IOException - an error occurred reading data from the request or writing data to the response.