com.kenshoo.pl.entity.internal.validators.FieldsCombinationValidationAdapter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of persistence-layer Show documentation
Show all versions of persistence-layer Show documentation
A Java persistence layer based on JOOQ for high performance and business flow support.
package com.kenshoo.pl.entity.internal.validators;
import com.kenshoo.pl.entity.CurrentEntityState;
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 ChangeValidatorAdapter {
private final FieldsCombinationValidator validator;
private final ValidationTrigger trigger;
public FieldsCombinationValidationAdapter(FieldsCombinationValidator validator) {
this.validator = validator;
this.trigger = new AnyFieldsTrigger<>(validator.validatedFields());
}
@Override
public ValidationTrigger trigger() {
return trigger;
}
@Override
public SupportedChangeOperation getSupportedChangeOperation() {
return SupportedChangeOperation.CREATE_AND_UPDATE;
}
@Override
public Stream extends EntityField, ?>> fieldsToFetch() {
return Stream.concat(validator.validatedFields(), validator.fetchFields());
}
@Override
public ValidationError validate(EntityChange entityChange, CurrentEntityState currentState) {
if(validator.validateWhen().test(currentState)) {
ResultingFieldsCombination resultingFieldsCombination = new ResultingFieldsCombination<>(entityChange, currentState, validator.validatedFields(), entityChange.getChangeOperation());
if (hasSubstitutions()) {
return validator.validate(new OverrideFieldsCombination<>(currentState, resultingFieldsCombination, mapFieldToOverrideFunction()));
} else {
return validator.validate(resultingFieldsCombination);
}
} else {
return null;
}
}
private boolean hasSubstitutions() {
return validator.substitutions().findAny().isPresent();
}
private Map, FieldsCombinationValidator.Substitution> mapFieldToOverrideFunction() {
return validator.substitutions().
collect(Collectors.toMap(FieldsCombinationValidator.Substitution::overrideField, Function.identity()));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy