net.sf.chainedoptions
Class AbstractChainedOption

java.lang.Object
  extended by net.sf.chainedoptions.AbstractChainedOption
All Implemented Interfaces:
ChainedOption, org.springframework.beans.factory.InitializingBean

public abstract class AbstractChainedOption
extends java.lang.Object
implements ChainedOption, org.springframework.beans.factory.InitializingBean

Abstract class that implements the ChainedOption interface and provides an implementation of updateValue(Object, List, Object). This implementation loops through the list of options and checks if the commandProperty in the command object matches any item in the list. If it doesn't, a default value is set in the command object. The default value is provided by the ChainedOptionStrategy retrieved from the getStrategy(Object)method.

Subclasses must implement the method retrieveOptions(Object, Object) method. Below is an example of such an implementation:

 package com.acme.valuehandler;
 
 import java.util.List;
 import com.acme.valuehandler.config.RegionConfig;
 
 public class CountryChainedOption extends AbstractChainedOption {
 
     private RegionConfig regionConfig;
 
     private String regionProperty;
 
     public List retrieveOptions(Object command) {
         String region = getProperty(command, regionProperty);
         List countryBeans = regionConfig.getCountries(region);
         List countries = getConverter().convert(countryBeans);
         return getStrategy().adjustAndSort(countries);
     }
 }
 

Author:
Mattias Arthursson, Ulrik Sandberg

Constructor Summary
AbstractChainedOption()
           
 
Method Summary
 void afterPropertiesSet()
          Checks if all necessary properties have been set.
 java.lang.String getCommandProperty()
           
 BeanConverter getConverter()
           
 java.lang.String getOptionsKey()
          Get the key that identifies the list of options corresponding to this ChainedOption.
protected  java.lang.Object getProperty(java.lang.Object bean, java.lang.String property)
          Utility method that gets a named property from a given object.
 ChainedOptionStrategy getStrategy(java.lang.Object command)
          Override this if the implementation has several strategies.
protected  void initChainedOption()
          Template method that subclasses may implement to ensure proper initialization.
protected  boolean matches(LabelValueBean bean, java.lang.String value)
          Utility method that matches the value of the given bean with the specified value.
abstract  java.util.List retrieveOptions(java.lang.Object command, java.lang.Object context)
          Retrieve a value List for this ChainedOption corresponding to the current values in the command that this ChainedOption depends on.
 void setCommandProperty(java.lang.String commandProperty)
          Set the property on the target command object managed by this instance.
 void setConverter(BeanConverter converter)
          Set the BeanConverter that should be used for translating to LabelValueBeans.
 void setDefaultStrategy(ChainedOptionStrategy defaultStrategy)
          Set the default strategy to use.
 void setOptionsKey(java.lang.String optionsKey)
          Set the key that should identify the option list managed by this instance.
protected  void setProperty(java.lang.Object bean, java.lang.String propertyName, java.lang.Object value)
          Utility method that sets a named property on a given object.
 void updateValue(java.lang.Object command, java.util.List options, java.lang.Object context)
          Default implementation of updateValue, which loops through the list of available options and compares them to the current value of the managed attribute in the command object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractChainedOption

public AbstractChainedOption()
Method Detail

retrieveOptions

public abstract java.util.List retrieveOptions(java.lang.Object command,
                                               java.lang.Object context)
Description copied from interface: ChainedOption
Retrieve a value List for this ChainedOption corresponding to the current values in the command that this ChainedOption depends on. This method may use a BeanConverterto translate Java Beans to LabelValueBeanobjects and an ChainedOptionStrategyto perform additional modifications to the option list.

Specified by:
retrieveOptions in interface ChainedOption
Parameters:
command - Command object to use when extracting values that the ChainedOption depends on.
context - may contain any context that might be interesting for retreiving valid options. E.g. a HttpServletRequest object can be supplied as context for a Strategy to perform filtering based on user access.
Returns:
A list of LabelValueBeanobjects.

updateValue

public void updateValue(java.lang.Object command,
                        java.util.List options,
                        java.lang.Object context)
Default implementation of updateValue, which loops through the list of available options and compares them to the current value of the managed attribute in the command object.

If the selected value is present in the list, no modifications are done in the command, otherwise a default value is retrieved by calling ChainedOptionStrategy.getDefaultValue(List, Object), and the commandProperty in the command is set to this value.

Specified by:
updateValue in interface ChainedOption
Parameters:
command - the object which will possibly be updated with a new object.
options - the list of options with which the managed value in the command will be compared.
context - a context that will be supplied to the ChainedOptionStrategy for selecting an appropriate default value.
See Also:
ChainedOptionStrategy.getDefaultValue(List, Object)

matches

protected boolean matches(LabelValueBean bean,
                          java.lang.String value)
Utility method that matches the value of the given bean with the specified value.

Parameters:
bean - The LabelValueBeanto match the value against.
value - The value to match.
Returns:
true if the values match.

afterPropertiesSet

public final void afterPropertiesSet()
                              throws java.lang.Exception
Checks if all necessary properties have been set. Subclasses should override initChainedOption()if additional integrity checks are needed on initialization.

Specified by:
afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
Throws:
java.lang.IllegalArgumentException - if any mandatory property has not been set.
java.lang.Exception

initChainedOption

protected void initChainedOption()
Template method that subclasses may implement to ensure proper initialization. This method is called after all properties has been set.


setProperty

protected void setProperty(java.lang.Object bean,
                           java.lang.String propertyName,
                           java.lang.Object value)
Utility method that sets a named property on a given object.

Parameters:
bean - The object to set the property on.
propertyName - The name of the property to set.
value - The value that the property will be set to.

getProperty

protected java.lang.Object getProperty(java.lang.Object bean,
                                       java.lang.String property)
Utility method that gets a named property from a given object.

Parameters:
bean - The object to get the property from.
property - The name of the property to get.
Returns:
The property value.

getCommandProperty

public java.lang.String getCommandProperty()
Returns:
Returns the commandProperty.

setCommandProperty

public void setCommandProperty(java.lang.String commandProperty)
Set the property on the target command object managed by this instance.

Parameters:
commandProperty - The commandProperty to set.

getOptionsKey

public java.lang.String getOptionsKey()
Description copied from interface: ChainedOption
Get the key that identifies the list of options corresponding to this ChainedOption.

Specified by:
getOptionsKey in interface ChainedOption
Returns:
Returns the optionsKey.

setOptionsKey

public void setOptionsKey(java.lang.String optionsKey)
Set the key that should identify the option list managed by this instance.

Parameters:
optionsKey - The optionsKey to set.

getConverter

public BeanConverter getConverter()
Returns:
Returns the converter.

setConverter

public void setConverter(BeanConverter converter)
Set the BeanConverter that should be used for translating to LabelValueBeans.

Parameters:
converter - The converter to set.

getStrategy

public ChainedOptionStrategy getStrategy(java.lang.Object command)
Override this if the implementation has several strategies.

Specified by:
getStrategy in interface ChainedOption
Parameters:
command - the command to use for selecting the correct Strategy.
Returns:
this implementation returns the defaultStrategy.

setDefaultStrategy

public void setDefaultStrategy(ChainedOptionStrategy defaultStrategy)
Set the default strategy to use.

Parameters:
defaultStrategy - the default strategy to set.


Copyright ? 2005. All Rights Reserved.