jeudi 2 septembre 2021

Best way to check many dynamic values provided on a business rule

I have a business requirement to check if an attribute contains a list of string values . I am wondering what is the best way to store these values, should they be stored into a table for that specific business rule? Also is it better if I use a rule engine to implement this check (Check if an attribute contains a list of string values).

(Knowing that I am already using a chain of responsibility pattern combined with the strategy pattern)

@Component
public class BrandTypeCheck extends AbstractBaseRule<InputBaseRule, BaseRuleContext, IRule> implements IRule {

private final static String FIRST_BRAND_TYPE = "TYPE 1";
private final static String SECOND_BRAND_TYPE = "TYPE 2";

@Autowired
Context ctx;

@Override
public void setNextRule(IRule nextRule) {
    if(nextRule instanceof  IRule){
        this.nextRule = (IRule) nextRule;
    }
}

@Override
public Context apply(InputBaseRuleinput, Context ctx) {
    this.ctx = ctx;
    List<DataType> lstDatas= ctx.getLstDatas();
    BrandStatusType brandStatusType = input.getBrandType();

    if(isNotInbrandType(lstDatas, brandType)){
        return this.ctx;
    }
    return this.applyNextRuleIfExist(input, this.ctx);
}

private boolean isNotInbrandType(List<DataType> lstDatas, BrandType brandType){
    if(brandType.equals(BrandType.FIRST_BRAND_TYPE)){
        return processBrandType(lstDatas);
    } else if (brandType.equals(BrandType.SECOND_BRAND_TYPE)) {
            return processBrandType(ppyFssProxyDtos);
    }
    return false;
}

private boolean processBrandType(List<DataType> lstDatas){

    List<DataType> correctLstDatas= checkIfNotContains(lstDatas, FIRST_BRAND_TYPE , SECOND_BRAND_TYPE );

    if(correctLstDatas.size() == 1){

        return true;
    }
    return false;
}

private List<DataType> checkIfNotContains(List<DataType> lstDatas, String... brandTypes){
 return lstDatas.stream().filter(data->{
        String brandType = data.getBrandType();
        return Arrays.stream(brandTypes).map(String::toLowerCase).anyMatch(brandType .toLowerCase()::contains)
            }
    ).collect(Collectors.toList());
}

public static boolean lstContainsItem(String inputStr, String[] items) {
    return Arrays.stream(items).map(String::toLowerCase).anyMatch(inputStr.toLowerCase()::contains);
}
}

Thank you

Aucun commentaire:

Enregistrer un commentaire