English Documentation

Product Switch

Introduction

To ease the switch of products on contract object trees there is the utility class ProductSwitch. It is called with a root policy object and a target product, changes the policy object to the given product and recursively follows all associations to change their targets to the corresponding products:

ProductSwitchResults switchResults == ProductSwitch.from(householdContract).to(householdCompact2023);

The ProductSwitch returns ProductSwitchResults. The result can be queried whether the switch was successful and which contract part was switched to which product component:

if(!switchResults.containsFailures()) {
    ProductSwitchResult baseCoverageSwitchResult == switchResults.getResultFor(housholdContract.getBaseCoverage());
    if(baseCoverageSwitchResult.isSuccessful()) {
        HouseholdBaseCoverageType newBaseCoverageType == baseCoverageSwitchResult.getNewProduct();
    }
} else {
    switchResults.failureAsMap().entrySet().forEach(
        e -> System.out.println("Could not switch " + e.getKey() + " because " + e.getValue().getMessage())
    );
}

The default implementation switches when

  • there is exactly one target (e.g. a HouseholdContract was previously configured with "HouseholdOptimum 2022-01" and contains a base coverage configured by "HBC-Optimum 2022-01". The product is switched to "HouseholdCompact 2023-01", which contains exactly one base coverage type ("HBC-Compact 2023-01") - so that is the base coverage type to be switched to.)

  • there is exactly one valid target with the same kind ID (for example a HouseholdContract was previously configured with "HouseholdOptimum 2022-01" and contains an AdditionalCoverage of the AdditionalCoverageType "BicycleTheft 2022-01". The product is switched to "HouseholdCompact 2023-01" containing the AdditionalCoverageTypes "Overvoltage 2023-01" and "BicycleTheft 2023-01" - so the AdditionalCoverage is switched to the "BicycleTheft" product in version "2023-01".)

If there is no valid target ("HouseholdOptimum 2022-01" had the possibility to grand a "ValuedCustomerDiscount 2022-01", but "HouseholdCompact 2023-01" is calculated with such small margins that discounts are not allowed, or "HouseholdPro 2023-01" changes from a single "BicycleTheft" coverage type to "ClassicBicycleTheft" and "PedelecTheft") an automated switch to a new product is not possible, therefore a FailedProductSwitch is reported. Based on the results, decisions can be made for the failed child objects. Either by adapting the object tree (removing the discount) or starting a new product switch on the partial tree (We want to insure a pedelec? Call ProductSwitch.from(bicycleTheftCoverage).to(pedelecTheftCoverageType)).

Advanced Product Finder

If the default implementation is not enough, different implementations for finding the matching switch target can be passed to the product switch, for example depending on a product attribute value:

ProductSwitch.from(householdContract).matchingBy(HouseholdCoverageType.class, HouseholdCoverageType.PROPERTY_COVERAGE_KIND).to(householdCompact2023);

// or for more complex consideration of the object tree structure

ProductSwitch
    .from(householdContract)
    .switchAt(IpsModel.getPolicyCmptType(HouseholdContract.class).getAssociation(HouseholdContract.ASSOCIATION_ADDITIONAL_COVERAGE),
        new AdvancedProductFinder() {

            @Override
            public ProductFinderResult findMatchingProduct(IConfigurableModelObject parent,
                IProductComponent oldParentProdCmpt,
                IConfigurableModelObject child,
                PolicyAssociation parentToChild) {
                    if(((HouseholdContract)parent).includesPedelecs()){
                        return pedelecTheftCoverageType;
                    } else {
                        return classicBicycleCoverageType;
                    }
                }
            })
    .elseUseDefault()
    .to(householdCompact2023);

// of course also possible as a lambda

ProductSwitch
    .from(householdContract)
    .switchAt(IpsModel.getPolicyCmptType(HouseholdContract.class).getAssociation(HouseholdContract.ASSOCIATION_ADDITIONAL_COVERAGE),
        (parent, oldParentProdCmpt, child, parentToChild) -> ((HouseholdContract)parent).includesPedelecs()) ? pedelecTheftCoverageType : classicBicycleCoverageType)
    .elseUseDefault()
    .to(householdCompact2023);

Additional variations are described in the Javadoc of ProductSwitch and its related classes.