org.enhydra.barracuda.core.forms
Class DefaultFormMap

java.lang.Object
  |
  +--org.enhydra.barracuda.core.forms.DefaultFormMap
All Implemented Interfaces:
FormMap, StateMap

public class DefaultFormMap
extends java.lang.Object
implements FormMap

This class provides the default implementation of a FormMap.

A FormMap is used to provide a virtual representation of a form. It can contain any number of unique FormElements, and it can also be associated with FormValidators. The primary function of a form map is to:

Since:
1.0
Version:
%I%, %G%
Author:
Christian Cryder
, Diez B. Roggisch , Jacob Kjome

Field Summary
protected static java.util.Locale defaultLoc
           
protected  java.util.Map elements
           
protected static org.apache.log4j.Logger localLogger
           
protected  StateMap statemap
           
protected  java.util.List validators
           
 
Constructor Summary
DefaultFormMap()
           
 
Method Summary
 void defineElement(FormElement element)
          This defines an element to be mapped by this form, using the key from the FormElement.
 void defineElement(java.lang.String key, FormElement element)
          This defines an element to be mapped by this form.
 void defineValidator(FormValidator validator)
          This defines a validator for the entire form.
 boolean exists(java.lang.String key)
          Return true if an element exists and its value is not null
 java.lang.Boolean getBooleanVal(java.lang.String key)
          Get an Boolean value from the map
 java.lang.Boolean getBooleanVal(java.lang.String key, java.lang.Boolean dflt)
          Get the value for a given key, defaulting accordingly if the value is null.
 java.util.Date getDateVal(java.lang.String key)
          Get an Date value from the map
 java.lang.Double getDoubleVal(java.lang.String key)
          Get an Double value from the map
 java.lang.Double getDoubleVal(java.lang.String key, java.lang.Double dflt)
          Get the value for a given key, defaulting accordingly if the value is null.
 FormElement getElement(java.lang.String key)
          Get an element by key
 java.util.Map getElements()
          return a map with containing all the elements in this form map
 java.lang.Float getFloatVal(java.lang.String key)
          Get an Float value from the map
 java.lang.Float getFloatVal(java.lang.String key, java.lang.Float dflt)
          Get the value for a given key, defaulting accordingly if the value is null.
 java.lang.Integer getIntegerVal(java.lang.String key)
          Get an Integer value from the map
 java.lang.Integer getIntegerVal(java.lang.String key, java.lang.Integer dflt)
          Get the value for a given key, defaulting accordingly if the value is null.
 java.lang.Long getLongVal(java.lang.String key)
          Get an Long value from the map
 java.lang.Long getLongVal(java.lang.String key, java.lang.Long dflt)
          Get the value for a given key, defaulting accordingly if the value is null.
 java.lang.Short getShortVal(java.lang.String key)
          Get an Short value from the map
 java.lang.Short getShortVal(java.lang.String key, java.lang.Short dflt)
          Get the value for a given key, defaulting accordingly if the value is null.
 java.lang.Object getState(java.lang.Object key)
          get a property in this StateMap
 java.util.List getStateKeys()
          get a list of the keys for this StateMap
 java.util.Map getStateValues()
          get a copy of the underlying Map
 java.lang.String getStringVal(java.lang.String key)
          Get a String value from the map
 java.util.Date getStringVal(java.lang.String key, java.util.Date dflt)
          Get the value for a given key, defaulting accordingly if the value is null.
 java.lang.String getStringVal(java.lang.String key, java.lang.String dflt)
          Get the value for a given key, defaulting accordingly if the value is null.
 java.lang.Object getVal(java.lang.String key)
          Get the value for a given key.
 java.util.Map getVals()
          Get the a map of all the values that back this form map
 FormMap map(javax.servlet.ServletRequest req)
          This is where we actually take an incoming form (in the form of a ServletRequest) and map it using the definitions supplied by all the FormElements.
 FormMap map(javax.servlet.ServletRequest req, java.util.Locale loc)
          This is where we actually take an incoming form (in the form of a ServletRequest) and map it using the definitions supplied by all the FormElements.
 FormMap map(javax.servlet.ServletRequest req, java.lang.String prefix)
          This is where we actually take an incoming form (in the form of a ServletRequest) and map it using the definitions supplied by all the FormElements.
 FormMap map(javax.servlet.ServletRequest req, java.lang.String prefix, java.util.Locale loc)
          This is where we actually take an incoming form (in the form of a ServletRequest) and map it using the definitions supplied by all the FormElements.
 FormMap map(StateMap map)
          This is where we actually take an incoming form (in the form of a StateMap) and map it using the definitions supplied by all the FormElements.
 FormMap map(StateMap map, java.util.Locale loc)
          This is where we actually take an incoming form (in the form of a StateMap) and map it using the definitions supplied by all the FormElements.
 FormMap map(StateMap map, java.lang.String prefix)
          This is where we actually take an incoming form (in the form of a StateMap) and map it using the definitions supplied by all the FormElements.
 FormMap map(StateMap map, java.lang.String prefix, java.util.Locale loc)
          This is where we actually take an incoming form (in the form of a StateMap) and map it using the definitions supplied by all the FormElements.
 FormElement mapElement(java.lang.String key, java.lang.Object origVal)
          This allows you to map a single value (as opposed to passing in a whole statemap or request).
 FormElement mapElement(java.lang.String key, java.lang.Object origVal, java.util.Locale loc)
          This allows you to map a single value (as opposed to passing in a whole statemap or request).
 void putState(java.lang.Object key, java.lang.Object val)
          set a property in this StateMap
 java.lang.Object removeState(java.lang.Object key)
          remove a property in this StateMap
 void setVal(java.lang.String key, java.lang.Object val)
          Manually set the value of an element.
 FormMap validate(boolean deferExceptions)
          Validate the entire form (both form level and elements).
 FormMap validateElements(boolean deferExceptions)
          Validate just the elements (not the form)
 FormMap validateForm(boolean deferExceptions)
          Validate just the form (not the individual elements)
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

localLogger

protected static org.apache.log4j.Logger localLogger

elements

protected java.util.Map elements

validators

protected java.util.List validators

statemap

protected StateMap statemap

defaultLoc

protected static final java.util.Locale defaultLoc
Constructor Detail

DefaultFormMap

public DefaultFormMap()
Method Detail

defineElement

public void defineElement(FormElement element)
This defines an element to be mapped by this form, using the key from the FormElement. You would invoke this method for each element in the form.

Specified by:
defineElement in interface FormMap
Parameters:
element - a FormElement to be mapped by this form

defineElement

public void defineElement(java.lang.String key,
                          FormElement element)
This defines an element to be mapped by this form. You would invoke this method for each element in the form.

Specified by:
defineElement in interface FormMap
Parameters:
key - the key which uniquely identifies this FormElement
element - a FormElement to be mapped by this form

defineValidator

public void defineValidator(FormValidator validator)
This defines a validator for the entire form. This validator will be invoked prior to validating specific form element validators. Calling this method multiple times will result in multiple form validators being added to the form (they will be invoked in the order they were added)

Specified by:
defineValidator in interface FormMap
Parameters:
validator - a form validator to be applied to the entire form

map

public FormMap map(javax.servlet.ServletRequest req)
This is where we actually take an incoming form (in the form of a ServletRequest) and map it using the definitions supplied by all the FormElements. If there are multiple parameters for a given key, the values will be mapped into List structures

Specified by:
map in interface FormMap
Parameters:
req - the ServletRequest to map paramters from based on all defined FormElements
Returns:
a reference to the FormMap (we do this so you can inline map/validate requests if you desire: form.map(req).validate())
Throws:
MappingException - if for some reason the value cannot be mapped successfully

map

public FormMap map(javax.servlet.ServletRequest req,
                   java.util.Locale loc)
This is where we actually take an incoming form (in the form of a ServletRequest) and map it using the definitions supplied by all the FormElements. If there are multiple parameters for a given key, the values will be mapped into List structures

Specified by:
map in interface FormMap
Parameters:
req - the ServletRequest to map paramters from based on all defined FormElements
loc - the locale to use when parsing Dates and other locale dependend values.
Returns:
a reference to the FormMap (we do this so you can inline map/validate requests if you desire: form.map(req).validate())
Throws:
MappingException - if for some reason the value cannot be mapped successfully

map

public FormMap map(javax.servlet.ServletRequest req,
                   java.lang.String prefix)
This is where we actually take an incoming form (in the form of a ServletRequest) and map it using the definitions supplied by all the FormElements. If there are multiple parameters for a given key, the values will be mapped into List structures

Specified by:
map in interface FormMap
Parameters:
req - the ServletRequest to map paramters from based on all defined FormElements
prefix - the prefix to use when mapping parameters
Returns:
a reference to the FormMap (we do this so you can inline map/validate requests if you desire: form.map(req).validate())
Throws:
MappingException - if for some reason the value cannot be mapped successfully

map

public FormMap map(javax.servlet.ServletRequest req,
                   java.lang.String prefix,
                   java.util.Locale loc)
This is where we actually take an incoming form (in the form of a ServletRequest) and map it using the definitions supplied by all the FormElements. If there are multiple parameters for a given key, the values will be mapped into List structures if a prefix is given, only the parameters which start with that prefix are mapped. Important: They are mapped to their name without that prefix.

Specified by:
map in interface FormMap
Parameters:
req - the ServletRequest to map paramters from based on all defined FormElements
prefix - the prefix to use when mapping parameters
loc - the locale to use when parsing Dates and other locale dependend values.
Returns:
a reference to the FormMap (we do this so you can inline map/validate requests if you desire: form.map(req).validate())
Throws:
MappingException - if for some reason the value cannot be mapped successfully

map

public FormMap map(StateMap map)
This is where we actually take an incoming form (in the form of a StateMap) and map it using the definitions supplied by all the FormElements.

Specified by:
map in interface FormMap
Parameters:
map - the StateMap to map properties from based on all defined FormElements
Returns:
a reference to the FormMap (we do this so you can inline map/validate requests if you desire: form.map(req).validate())
Throws:
MappingException - if for some reason the value cannot be mapped successfully

map

public FormMap map(StateMap map,
                   java.lang.String prefix)
This is where we actually take an incoming form (in the form of a StateMap) and map it using the definitions supplied by all the FormElements.

Specified by:
map in interface FormMap
Parameters:
map - the StateMap to map properties from based on all defined FormElements
prefix - the prefix to use when mapping parameters
Returns:
a reference to the FormMap (we do this so you can inline map/validate requests if you desire: form.map(req).validate())
Throws:
MappingException - if for some reason the value cannot be mapped successfully

map

public FormMap map(StateMap map,
                   java.util.Locale loc)
This is where we actually take an incoming form (in the form of a StateMap) and map it using the definitions supplied by all the FormElements.

Specified by:
map in interface FormMap
Parameters:
map - the StateMap to map properties from based on all defined FormElements
loc - the locale to use when parsing Dates and other locale dependend values.
Returns:
a reference to the FormMap (we do this so you can inline map/validate requests if you desire: form.map(req).validate())
Throws:
MappingException - if for some reason the value cannot be mapped successfully

map

public FormMap map(StateMap map,
                   java.lang.String prefix,
                   java.util.Locale loc)
This is where we actually take an incoming form (in the form of a StateMap) and map it using the definitions supplied by all the FormElements.

Specified by:
map in interface FormMap
Parameters:
map - the StateMap to map properties from based on all defined FormElements
prefix - the prefix to use when mapping parameters
loc - the locale to use when parsing Dates and other locale dependend values.
Returns:
a reference to the FormMap (we do this so you can inline map/validate requests if you desire: form.map(req).validate())
Throws:
MappingException - if for some reason the value cannot be mapped successfully

mapElement

public FormElement mapElement(java.lang.String key,
                              java.lang.Object origVal)
This allows you to map a single value (as opposed to passing in a whole statemap or request).

Specified by:
mapElement in interface FormMap
Parameters:
key - the name of the element we wish to map to
origVal - the original value to be mapped
Returns:
the newly mapped form element

mapElement

public FormElement mapElement(java.lang.String key,
                              java.lang.Object origVal,
                              java.util.Locale loc)
This allows you to map a single value (as opposed to passing in a whole statemap or request).

Specified by:
mapElement in interface FormMap
Parameters:
key - the name of the element we wish to map to
origVal - the original value to be mapped
loc - the locale to use when parsing Dates and other locale dependant values.
Returns:
the newly mapped form element

validate

public FormMap validate(boolean deferExceptions)
                 throws ValidationException
Validate the entire form (both form level and elements). We start by invoking form validators which apply to individual form elements, then we invoke any which apply to the entire form

Specified by:
validate in interface FormMap
Parameters:
deferExceptions - do we want to deferValidation exceptions and attempt to validate all elements so that we can process all the exceptions at once
Returns:
a reference to the FormMap (we do this so you can inline map/validate requests if you desire: form.map(req).validate())
Throws:
ValidationException - if the form (or any element within it) is invalid

validateElements

public FormMap validateElements(boolean deferExceptions)
                         throws ValidationException
Validate just the elements (not the form)

Specified by:
validateElements in interface FormMap
Parameters:
deferExceptions - do we want to deferValidation exceptions and attempt to validate all elements so that we can process all the exceptions at once
Returns:
a reference to the FormMap (we do this so you can inline map/validate requests if you desire: form.map(req).validate())
Throws:
ValidationException - if the form (or any element within it) is invalid

validateForm

public FormMap validateForm(boolean deferExceptions)
                     throws ValidationException
Validate just the form (not the individual elements)

Specified by:
validateForm in interface FormMap
Parameters:
deferExceptions - do we want to deferValidation exceptions and attempt to validate all elements so that we can process all the exceptions at once
Returns:
a reference to the FormMap (we do this so you can inline map/validate requests if you desire: form.map(req).validate())
Throws:
ValidationException - if the form (or any element within it) is invalid

putState

public void putState(java.lang.Object key,
                     java.lang.Object val)
set a property in this StateMap

Specified by:
putState in interface StateMap
Parameters:
key - the state key object
val - the state value object

getState

public java.lang.Object getState(java.lang.Object key)
get a property in this StateMap

Specified by:
getState in interface StateMap
Parameters:
key - the state key object
Returns:
the value for the given key

removeState

public java.lang.Object removeState(java.lang.Object key)
remove a property in this StateMap

Specified by:
removeState in interface StateMap
Parameters:
key - the key object
Returns:
the object which was removed

getStateKeys

public java.util.List getStateKeys()
get a list of the keys for this StateMap

Specified by:
getStateKeys in interface StateMap
Returns:
a list the keys for this StateMap

getStateValues

public java.util.Map getStateValues()
get a copy of the underlying Map

Specified by:
getStateValues in interface StateMap
Returns:
a copy of the underlying state Map

exists

public boolean exists(java.lang.String key)
Return true if an element exists and its value is not null

Specified by:
exists in interface FormMap
Parameters:
key - the key which uniquely identifies this FormElement
Returns:
true if an element exists (not null)

getElement

public FormElement getElement(java.lang.String key)
Get an element by key

Specified by:
getElement in interface FormMap
Parameters:
key - the key which uniquely identifies this FormElement
Returns:
the FormElement for this key (may be null)

getElements

public java.util.Map getElements()
return a map with containing all the elements in this form map

Specified by:
getElements in interface FormMap
Returns:
a copy of the Map that backs this FormMap

setVal

public void setVal(java.lang.String key,
                   java.lang.Object val)
Manually set the value of an element. If an element does not exist for this key one will be created.

Specified by:
setVal in interface FormMap
Parameters:
key - the key
val - the value for the key

getVal

public java.lang.Object getVal(java.lang.String key)
Get the value for a given key. This is basically a convenience method. You could manually grab the FormElement using getElement and then retrieve the value that way as well.

Specified by:
getVal in interface FormMap
Parameters:
key - the key which uniquely identifies this FormElement
Returns:
the value for this key (may be null)

getVals

public java.util.Map getVals()
Get the a map of all the values that back this form map

Returns:
the value for this key (may be null)

getStringVal

public java.lang.String getStringVal(java.lang.String key)
Get a String value from the map

Parameters:
key - the form element key
Returns:
the value for the given key (may be null if the value is not set or the key does not match a known form element)

getStringVal

public java.lang.String getStringVal(java.lang.String key,
                                     java.lang.String dflt)
Get the value for a given key, defaulting accordingly if the value is null.

Parameters:
key - the key which uniquely identifies this FormElement
dflt - the default value to be used if the underlying value is null
Returns:
the value for this key (may be null)

getBooleanVal

public java.lang.Boolean getBooleanVal(java.lang.String key)
Get an Boolean value from the map

Parameters:
key - the form element key
Returns:
the value for the given key (may be null if the value is not set or the key does not match a known form element)

getBooleanVal

public java.lang.Boolean getBooleanVal(java.lang.String key,
                                       java.lang.Boolean dflt)
Get the value for a given key, defaulting accordingly if the value is null.

Parameters:
key - the key which uniquely identifies this FormElement
dflt - the default value to be used if the underlying value is null
Returns:
the value for this key (may be null)

getIntegerVal

public java.lang.Integer getIntegerVal(java.lang.String key)
Get an Integer value from the map

Parameters:
key - the form element key
Returns:
the value for the given key (may be null if the value is not set or the key does not match a known form element)

getIntegerVal

public java.lang.Integer getIntegerVal(java.lang.String key,
                                       java.lang.Integer dflt)
Get the value for a given key, defaulting accordingly if the value is null.

Parameters:
key - the key which uniquely identifies this FormElement
dflt - the default value to be used if the underlying value is null
Returns:
the value for this key (may be null)

getDateVal

public java.util.Date getDateVal(java.lang.String key)
Get an Date value from the map

Parameters:
key - the form element key
Returns:
the value for the given key (may be null if the value is not set or the key does not match a known form element)

getStringVal

public java.util.Date getStringVal(java.lang.String key,
                                   java.util.Date dflt)
Get the value for a given key, defaulting accordingly if the value is null.

Parameters:
key - the key which uniquely identifies this FormElement
dflt - the default value to be used if the underlying value is null
Returns:
the value for this key (may be null)

getLongVal

public java.lang.Long getLongVal(java.lang.String key)
Get an Long value from the map

Parameters:
key - the form element key
Returns:
the value for the given key (may be null if the value is not set or the key does not match a known form element)

getLongVal

public java.lang.Long getLongVal(java.lang.String key,
                                 java.lang.Long dflt)
Get the value for a given key, defaulting accordingly if the value is null.

Parameters:
key - the key which uniquely identifies this FormElement
dflt - the default value to be used if the underlying value is null
Returns:
the value for this key (may be null)

getShortVal

public java.lang.Short getShortVal(java.lang.String key)
Get an Short value from the map

Parameters:
key - the form element key
Returns:
the value for the given key (may be null if the value is not set or the key does not match a known form element)

getShortVal

public java.lang.Short getShortVal(java.lang.String key,
                                   java.lang.Short dflt)
Get the value for a given key, defaulting accordingly if the value is null.

Parameters:
key - the key which uniquely identifies this FormElement
dflt - the default value to be used if the underlying value is null
Returns:
the value for this key (may be null)

getDoubleVal

public java.lang.Double getDoubleVal(java.lang.String key)
Get an Double value from the map

Parameters:
key - the form element key
Returns:
the value for the given key (may be null if the value is not set or the key does not match a known form element)

getDoubleVal

public java.lang.Double getDoubleVal(java.lang.String key,
                                     java.lang.Double dflt)
Get the value for a given key, defaulting accordingly if the value is null.

Parameters:
key - the key which uniquely identifies this FormElement
dflt - the default value to be used if the underlying value is null
Returns:
the value for this key (may be null)

getFloatVal

public java.lang.Float getFloatVal(java.lang.String key)
Get an Float value from the map

Parameters:
key - the form element key
Returns:
the value for the given key (may be null if the value is not set or the key does not match a known form element)

getFloatVal

public java.lang.Float getFloatVal(java.lang.String key,
                                   java.lang.Float dflt)
Get the value for a given key, defaulting accordingly if the value is null.

Parameters:
key - the key which uniquely identifies this FormElement
dflt - the default value to be used if the underlying value is null
Returns:
the value for this key (may be null)


Copyright © 2001 Enhydra.org