Listing 22-14. The RuleFactoryImpl Class
import org.joda.time.DateTime
import org.joda.time.Years;
import com.apress.prospring3.ch22.rule.domain.Rule
import com.apress.prospring3.ch22.rule.factory.RuleFactory
class RuleFactoryImpl implements RuleFactory {
Closure age = { birthDate -> return Years.yearsBetween(birthDate, new
DateTime()).getYears() }
public Rule getAgeCategoryRule() {
// Rule definition
Rule rule = new Rule()
// ****************** CONFIGURATION ******************
rule.singlehit=true
// ***************************************************
rule.conditions=[
// ****************** CONDITIONS *********************
{object, param -> age(object.birthDate) >= param}, {object, param ->
age(object.birthDate) <= param}
// ***************************************************
]
rule.actions=[
// ******************* ACTIONS ***********************
{object, param -> object.ageCategory = param}
// ***************************************************
]
rule.parameters=[
// **************** PARAMETERSETS ********************
// Min age, Max age, ageCategory
[0,10,'Kid'],
[11,20,'Youth'],
[21,40,'Adult'],
[41,60,'Middle-aged'],
[61,120,'Old']
// ***************************************************
]
return rule
}
}
In Listing 22-14, the class implements the RuleFactory interface, and the getAgeCategoryRule()
method is implemented to provide the rule. Within the rule, a Closure called age is defined to calculate
the age based on the birthDate property (which is of JodaTime's DateTime type) of a Contact object.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home