package gw.rating.rtm.valueprovider
uses gw.api.locale.DisplayKey
uses gw.api.util.DisplayableException
uses gw.lang.reflect.TypeSystem
uses gw.lang.reflect.IType
uses gw.rating.rtm.domain.table.RateTableCell
uses java.lang.Exception
uses java.util.Map
uses java.util.HashMap
/**
* A factory class that provides an instance of an appropriate value provider for a
* particular rate table column.
*
* It is expected that every instance of RateTableModel receives its own instance of
* RateTableCellValueProviderFactory. Using the same factory instance for multiple rate
* tables may have undesired behavior if the tables have any column names in common.
*/
class RateTableCellValueProviderFactory {
var _valueProviders : Map as readonly ValueProviders
construct() {
_valueProviders = new HashMap ()
}
/**
* Returns a value provider instance for a specific rate table cell.
* @param cell rte table cell wrapper that provides access to rate table column definition.
*/
function getValueProviderFor(cell: RateTableCell) : RateTableCellValueProvider {
var column = cell.ColumnDefinition
var columnName = cell.ColumnName
if (!_valueProviders.containsKey(columnName)) {
var valueProvider = getValueProvider(column)
if (valueProvider != null) {
_valueProviders.put(columnName, valueProvider)
}
}
return _valueProviders.get(columnName)
}
/**
* Returns a value provider instance for a specific rate table column.
* If value provider is defined for a rate table column, creates and initializes an instance
* of value provider.
*
* - For columns of boolean type returns an instance of
BooleanValueProvider.
* - If the implClass does not resolve to anything in the type system, or if a value provider
* is not defined for a column, the
UnboundValueProvider is returned.
* - For columns defining typelist value provider returns an instance of
TypeListValueProvider.
* - For all other types an instance of specified value provider is created and arguments are set if specified.
*
*/
static function getValueProvider(column : RateTableColumn) : RateTableCellValueProvider {
var parser = Parser.parse(column.ValueProvider)
var implClass = parser.ClassName
var args = parser.Arguments
if (implClass == null) {
return column.ColumnType == RateTableDataType.TC_BOOLEAN
? new BooleanValueProvider(column)
: new UnboundValueProvider(column)
}
var valueProvider : RateTableCellValueProvider
var t = TypeSystem.getByFullNameIfValid(implClass)
if (t == null) {
return new UnboundValueProvider(column)
}
try {
valueProvider = getValueProviderInstance(t, column)
valueProvider.Arguments = args
} catch (ex : Exception) {
throw new DisplayableException(DisplayKey.get("Web.Rating.Errors.InvalidValueProvider", column.ValueProvider,
ex.LocalizedMessage))
}
return valueProvider
}
static private function getValueProviderInstance(type : IType, column : RateTableColumn) : RateTableCellValueProvider {
var constructor = type.TypeInfo.getCallableConstructor({RateTableColumn})
return constructor.Constructor.newInstance({column}) as RateTableCellValueProvider
}
}