Generic API

See the details of the extensible cart calculation generic API.

The generic API of the extensible cart calculation is based on an interface, and a class that implements it. They are:

  • the FindPDTValueInfoStrategy<VALUE extends PDTValue, INFO extends PDTInformation, CRITERIA extends PDTCriteria> interface

  • the GenericPDTFindValueInfoStrategy<VALUE extends PDTValue, INFO extends PDTInformation, CRITERIA extends PDTCriteria, MODEL extends PDTRowModel> class

The FindPDTValueInfoStrategy interface provides two generic methods that are used to resolve prices, taxes and discounts:

  • List<VALUE> getPDTValues(CRITERIA) resolves price, discount, and tax values for given criteria. The method is used during cart (re)calculation.

  • List<INFO> getPDTInformation(CRITERIA) resolves price, discount, and tax information for given criteria.

The generic GenericPDTFindValueInfoStrategy implementation is based on the FindPDTValueInfoStrategy interface. The implementation executes the shared flow of retrieving and matching prices, discounts, and taxes to resolve values and information. It splits the task responsible for resolving values and information into multiple parts, and executes methods from the dedicated single-method interfaces in the following order:

Order

Method

Interface

Method Description

1

List<MODEL> matchRows(final CRITERIA criteria)

PDTModelMatcher<CRITERIA extends PDTCriteria, MODEL extends PDTRowModel> (*)

Matches rows (including querying repository, filtering rows, and sorting rows)

 

1.1

Collection<MODEL> findRows(CRITERIA criteria)

PDTRowRepository<CRITERIA extends PDTCriteria, MODEL extends PDTRowModel>

Queries repository

1.2

Collection<MODEL> filter(Collection<MODEL> collection, CRITERIA criteria)

PDTRowFilter<CRITERIA extends PDTCriteria, MODEL extends PDTRowModel>

Filters rows

1.3

Comparator<MODEL> comparator(CRITERIA criteria)PDTRowFilter<CRITERIA extends PDTCriteria, MODEL extends PDTRowModel>

PDTRowMatchComparatorProvider<CRITERIA extends PDTCriteria, MODEL extends PDTRowModel>

Compares rows

2

Collection<MODEL> process(Collection<MODEL> models, CRITERIA criteria)

PDTPostMatcher<CRITERIA extends PDTCriteria, MODEL extends PDTRowModel>

Posts matching rows

3

TARGET convert(SOURCE source, CONTEXT context)

PDTConverter<SOURCE, TARGET, CONTEXT>

Converts rows into value/information

(*) For the PDTModelMatcher interface, the generic GenericPDTModelMatcher implementation is also provided. The implemented matchRows() method finds rows using PDTRowRepository, filters them using PDTRowFilter, and sorts them using PDTRowMatchComparatorProvider.

The following code snippet shows the implementation of the getPDTValues() method of GenericPDTFindValueInfoStrategy:

private PDTModelMatcher<CRITERIA, MODEL> valueModelMatcher;
private PDTPostMatcher<CRITERIA, MODEL> valuePostMatcher;
private PDTConverter<MODEL, VALUE, CRITERIA> valueConverter;
 
 
public List<VALUE> getPDTValues(final CRITERIA criteria) throws CalculationException{
    Collection<MODEL> rowModels = valueModelMatcher.matchRows(criteria);
    rowModels = valuePostMatcher.process(rowModels, criteria);
    return valueConverter.convertAll(rowModels, criteria);
}

The following code snippet shows the implementation of the getPDTInformation() method:

private PDTModelMatcher<CRITERIA, MODEL> informationModelMatcher;
private PDTPostMatcher<CRITERIA, MODEL> informationPostMatcher;
private PDTConverter<MODEL, INFO, CRITERIA> informationConverter;
 
public List<INFO> getPDTInformation(final CRITERIA criteria) throws CalculationException{
    Collection<MODEL> rowModels = informationModelMatcher.matchRows(criteria);
    rowModels = informationPostMatcher.process(rowModels, criteria);
    return informationConverter.convertAll(rowModels, criteria);
}

The GenericPDTFindValueInfoStrategy class is a base class for default implementations responsible for resolving price/tax/discounts values and information in the de.hybris.platform.order.strategies.calculation.pdt.impl package as follows:

  • DefaultFindPriceValueInfoStrategy extends GenericPDTFindValueInfoStrategy<PriceValue, PriceInformation, PriceValueInfoCriteria, PriceRowModel>

  • DefaultFindDiscountValueInfoStrategy extends GenericPDTFindValueInfoStrategy<DiscountValue, DiscountInformation, DiscountValueInfoCriteria, DiscountRowModel>

  • DefaultFindTaxValueInfoStrategy extends GenericPDTFindValueInfoStrategy<TaxValue, TaxInformation, TaxValueInfoCriteria, TaxRowModel>

The following XML configuration snippet shows part of a Spring configuration for the default DefaultFindDiscountValueInfoStrategy discount strategy (for DefaultFindPriceValueInfoStrategy, the DefaultFindTaxValueInfoStrategy XML configuration looks similar):

<alias alias="discountValueModelMatcher" name="defaultDiscountValueModelMatcher"/>       
 
<bean id="defaultDiscountValueModelMatcher" class="de.hybris.platform.order.strategies.calculation.pdt.matcher.impl.GenericPDTModelMatcher" >
    <property name="rowRepository" ref="discountRowValueRepository"/>
    <property name="rowFilter" ref="discountRowValueFilter"/>
    <property name="rowMatchComparatorProvider" ref="discountRowValueMatchComparatorProvider"/>
</bean>
 
<alias alias="discountInfoModelMatcher" name="defaultDiscountInfoModelMatcher"/>
<bean id="defaultDiscountInfoModelMatcher" class="de.hybris.platform.order.strategies.calculation.pdt.matcher.impl.GenericPDTModelMatcher" >
    <property name="rowRepository" ref="discountRowInfoRepository"/>
    <property name="rowFilter" ref="discountRowInfoFilter"/>
    <property name="rowMatchComparatorProvider" ref="discountRowInfoMatchComparatorProvider"/>
</bean>
 
<alias alias="findDiscountValueInfoStrategy" name="defaultFindDiscountValueInfoStrategy"/>
<bean id="defaultFindDiscountValueInfoStrategy" class="de.hybris.platform.order.strategies.calculation.pdt.impl.DefaultFindDiscountValueInfoStrategy">
    <property name="valueModelMatcher" ref="discountValueModelMatcher"/>
    <property name="informationModelMatcher" ref="discountInfoModelMatcher"/>
    <property name="valuePostMatcher" ref="discountValuePostMatcher"/>
    <property name="informationPostMatcher" ref="discountInfoPostMatcher"/>  
    <property name="valueConverter" ref="discountValueConverter"/>
    <property name="informationConverter" ref="discountInfoConverter"/>
</bean>   

The configuration is used for resolving discount values and information. DefaultFindDiscountValueInfoStrategy has these two sets of bean properties:

  • The following set is used when the getPDTInformation() method is called:

    <property name="valueModelMatcher" ref="discountValueModelMatcher"/>
    <property name="valuePostMatcher" ref="discountValuePostMatcher"/>
    <property name="valueConverter" ref="discountValueConverter"/>
  • The following set it used when the getPDTValues() method is called:

    <property name="informationModelMatcher" ref="discountInfoModelMatcher"/>
    <property name="informationPostMatcher" ref="discountInfoPostMatcher"/>
    <property name="informationConverter" ref="discountInfoConverter"/>