package gw.rating.rtm.valueprovider
uses gw.api.domain.covterm.CovTerm
uses gw.api.locale.DisplayKey
uses gw.api.productmodel.CovTermPattern
uses gw.api.productmodel.PolicyLinePattern
uses gw.api.productmodel.ProductModelClass
uses gw.rating.flow.util.InScopeUsage
uses gw.rating.flow.util.RatingEntityGraphTraverser
uses gw.rating.rtm.domain.PersistenceAdapter
uses gw.rating.rtm.domain.table.RateTableCell
uses gw.rating.rtm.matchop.CoverageParam
uses gw.rating.rtm.util.AvailableAttributePresenter
/**
*
* Value provider implementation for coverage terms of a specific policy line
* coverage configured in PC product model.
*
* This value provider should be used for rate table columns satisfying one of
* the following conditions:
*
* - A rate table column depends on another rate table column that contains coverage code.
* - An argument is specified for this value provider that is the coverage code.
*
* In both cases value provider will retrieve coverage terms of a specific coverage,
* either from another column or an argument.
*
* Example 1: When a value provider is configured with an argument -
* gw.rating.rtm.valueprovider.CovTermValueProvider(PACollisionCov),
* rate table column does not need to depend on any other column in the rate table and
* will return all coverage terms under the coverage identified by PACollisionCov code.
*
* Example 2: When a value provider is configured without arguments -
* gw.rating.rtm.valueprovider.CovTermValueProvider,
* rate table column must depend on another column in the rate table that defines
* coverage code.
*
* This value provider should be usually used together with
* {@link gw.rating.rtm.valueprovider.CoverageValueProvider CoverageValueProvider}.
* In that case, a column that uses CovTermValueProvider should depend on a column that
* uses CoverageValueProvider.
*
*/
class CovTermValueProvider extends AbstractProductModelValueProvider {
private var _cache : Map> = new HashMap>()
private var _covColumn : RateTableColumn
construct(column : RateTableColumn) {
super(column)
_covColumn = ColumnDefinition.DependsOn
}
/**
* Returns an array of coverage term codes for a specific coverage. If rate table column
* is defined as dependent on another rate table column, the code of the coverage for
* which coverage terms are returned is retrieved from the bean parameter
* according to column definition of the referenced column.
* If an optional argument is provided in the value provider configuration the value of
* that argument will be used as a coverage code ahead of the one in the bean.
*
* @param bean physical bean that stores rate table row (a set of rate table
* parameters and factor(s).
* @returns an array of coverage term codes.
*/
override function getValueMap(args : String[]) : Map {
var covCode = args.single()
var values = _cache.get(covCode)
if (values == null) {
values = new LinkedHashMap()
// get coverage pattern
var cov = PolicyLinePattern.getCoveragePatternByPublicId(covCode)
if (cov == null) return {}
// process all cov terms
cov.CovTerms.each(\ covTerm -> values.put(covTerm.PublicID, covTerm.Name))
_cache.put(covCode, values)
}
return values
}
override function getDependentValues(dependencies : String[]) : Map {
return dependencies.length > 0 ? getValueMap(dependencies): {}
}
override function getValues(adapter : PersistenceAdapter) : String[] {
var covCode = getCovCode(adapter)
if (covCode == null) return {}
return getValueMap({covCode}).Keys.toTypedArray()
}
function containsCodeFor(cell : RateTableCell) : boolean {
var covCode = getCovCode(cell.Adapter)
return cell.cellValueContainsCode(getValueMap({covCode}).keySet())
}
/**
* Returns a coverage term name for a supplied coverage term code.
*
* @param code coverage term code
* @returns coverage term name
*/
override function valueByCode(adapter : PersistenceAdapter, code : String) : String {
var covCode = getCovCode(adapter)
if (covCode == null) return code
if (!_cache.containsKey(covCode)) {
getValues(adapter)
}
return _cache.get(covCode).get(code)
}
protected function createDefaultAttributePresenter(cov : entity.CalcRoutineParameter) : AvailableAttributePresenter {
return new AvailableAttributePresenter(cov.Code, "CovTerm", false, "CovTerm")
}
override function availableAttributes(line : PolicyLinePattern, paramSet : CalcRoutineParameterSet) : List {
if (paramSet == null) {
return {}
}
// If we have something in Arguments[0], we are intending to match only a coverage with that pattern.
var covParams = (Arguments == null or Arguments.length == 0)
? paramSet.Parameters.where(\ p -> p.ParamType == "entity.Coverage")
: paramSet.Parameters.where(\ p -> coveragePatternMatchesCoverageArgument(p))
var ret : List = {}
covParams.each(\ cov -> {
if (cov.CoveragePattern == null and not cov.UseWrapper) {
ret.add(createDefaultAttributePresenter(cov))
} else {
var results = new RatingEntityGraphTraverser(paramSet).getImmutableTargetableElementsForParameterSet(paramSet)
var usages : List = {}
results.asMap().eachKeyAndValue(\ k, l -> usages.addAll(l.where(\ v -> v.Root == cov.Code.Code)))
usages = usages.where(\ v -> gw.api.util.TypeUtil.isNominallyOrStructurallyAssignable(CovTerm.Type, v.FeatureType))
ret.addAll(usages.toTypedArray().map(\ u -> new AvailableAttributePresenter(u)).toList())
}
})
return ret
}
private function getCovCode(adapter : PersistenceAdapter) : String {
if (Arguments != null and Arguments.length > 0) return Arguments[0]
if (adapter == null) return null
if (_covColumn == null) return null
var covCode = adapter.getFieldValue(_covColumn.PhysicalColumnName)
if (covCode == null) return null
return covCode as String
}
override function getQueryParamValue(cov : Coverage) : String {
return cov.CovTerms.first().PatternCode
}
override function getQueryParam(cov : Coverage) : CoverageParam {
var patternCode = getQueryParamValue(cov)
if (patternCode == null) return null // don't wrap null
return new CoverageParam(:codeIdentifier = cov.CovTerms.first().PatternCodeIdentifier,
:patternCode = patternCode)
}
override property get ArgumentsHelpText() : String {
return DisplayKey.get("Web.Rating.RateTableDefinition.ValueProvider.HelpText.CovTermValueProvider")
}
override property get ProductModelType(): Type {
return CovTermPattern.Type
}
}