All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.kenshoo.pl.entity.internal.validators.FieldsCombinationValidationAdapter Maven / Gradle / Ivy

Go to download

A Java persistence layer based on JOOQ for high performance and business flow support.

There is a newer version: 0.1.121-jooq-3.16.3
Show newest version
package com.kenshoo.pl.entity.internal.validators;

import com.kenshoo.pl.entity.ChangeOperation;
import com.kenshoo.pl.entity.Entity;
import com.kenshoo.pl.entity.EntityChange;
import com.kenshoo.pl.entity.EntityField;
import com.kenshoo.pl.entity.EntityType;
import com.kenshoo.pl.entity.SupportedChangeOperation;
import com.kenshoo.pl.entity.ValidationError;
import com.kenshoo.pl.entity.internal.OverrideFieldsCombination;
import com.kenshoo.pl.entity.internal.ResultingFieldsCombination;
import com.kenshoo.pl.entity.spi.FieldsCombinationValidator;

import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class FieldsCombinationValidationAdapter> implements EntityChangeValidator {

    private final FieldsCombinationValidator validator;

    public FieldsCombinationValidationAdapter(FieldsCombinationValidator validator) {
        this.validator = validator;
    }

    @Override
    public Stream> getValidatedFields() {
        return validator.validatedFields();
    }

    @Override
    public SupportedChangeOperation getSupportedChangeOperation() {
        return SupportedChangeOperation.CREATE_AND_UPDATE;
    }

    @Override
    public Stream> getFieldsToFetch(ChangeOperation changeOperation) {
        Stream> substitutionsFields = validator.fetchFields();
        if (changeOperation == ChangeOperation.UPDATE) {
            return Stream.concat(validator.validatedFields(), substitutionsFields);
        } else {
            return substitutionsFields;
        }
    }

    @Override
    public ValidationError validate(EntityChange entityChange, Entity entity, ChangeOperation changeOperation) {
        if(validator.validateWhen().test(entity)) {
            ResultingFieldsCombination resultingFieldsCombination = new ResultingFieldsCombination<>(entityChange, entity, validator.validatedFields(), changeOperation);
            if (hasSubstitutions()) {
                return validator.validate(new OverrideFieldsCombination<>(entity, resultingFieldsCombination, mapFieldToOverrideFunction()));
            } else {
                return validator.validate(resultingFieldsCombination);
            }
        } else {
            return null;
        }
    }

    private boolean hasSubstitutions() {
        return validator.substitutions().findAny() != null;
    }

    private Map, FieldsCombinationValidator.Substitution> mapFieldToOverrideFunction() {
        return validator.substitutions().
                collect(Collectors.toMap(FieldsCombinationValidator.Substitution::overrideField, Function.identity()));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy