In the previous post we saw how to a create validator with a policy and filter data read from an excel.
Lets create another validator to check for discount types of a sales order and validate the sales data using the two validators and filter the data.
What we need is a
DiscountTypeValidator to check for discount types allowed(
here is the updated
in line 12 we create another anonymous class for
DiscountTypeValidator with a
DiscountTypeAllowed with a list
allowedDiscountTypes that holds a list of discount type codes.
DiscountTypeValidator takes column index of the discount type as Int parameter. The
validator method checks whether the discount type mentioned in the excel is allowed or not and then returns a list of booleans.
So now we have list of data
worksheet from excel and lists of booleans
discTypeValidationResult that represents the validation result of
Validators for each row in the excel.
In line 15 we create a list of tuple containing the row object and one of the validation result,say
zip method of
validationResult looks like this
We have to zip the
discTypeValidationResult also with
There is no convenient method in scala to zip a tuple say (1,2) with another value say 3 so that we get (1,2,3) instead of ((1,2),3).
zipResults(validationResult, discTypeValidationResult) combines the validation results and gives a simple tuple containing the row object the validation result on the object.
validationResult.filter(_._2).map(_._1) filters the sales data based on the validation result and returns the valid data.
Scala mixin and compostion is very comprehensive and convenient compared to Spring IoC.