com.kenshoo.pl.entity.spi.helpers.EntityChangeCompositeValidator 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.spi.helpers;
import com.kenshoo.pl.entity.*;
import com.kenshoo.pl.entity.internal.EntityTypeReflectionUtil;
import com.kenshoo.pl.entity.internal.validators.*;
import com.kenshoo.pl.entity.spi.*;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class EntityChangeCompositeValidator> implements ChangesValidator {
private final List> triggeredChangeValidators = new ArrayList<>();
public void register(FieldsCombinationValidator validator) {
register(new FieldsCombinationValidationAdapter<>(validator));
}
public void register(FieldValidator validator) {
register(new FieldValidationAdapter<>(validator));
}
public void register(FieldComplexValidator validator) {
register(new FieldComplexValidationAdapter<>(validator));
}
public void register(ImmutableFieldValidator validator) {
register(new ImmutableFieldValidationAdapter<>(validator));
}
public void register(RequiredFieldValidator validator) {
register(new RequiredFieldValidationAdapter<>(validator));
}
public void register(AncestorsValidator validator) {
register(new AncestorsValidationAdapter<>(validator));
}
public void register(EntityChangeValidator validator) {
register(new EntityChangeValidatorAdapter<>(validator));
}
public void register(E entityType, PrototypeFieldValidator validator) {
Set> entityFields = EntityTypeReflectionUtil.getFieldsByPrototype(entityType, validator.getPrototype());
if (entityFields.isEmpty()) {
throw new IllegalArgumentException("Can not find entity field by prototype: " + validator.getPrototype());
}
for (EntityField entityField : entityFields) {
register(new PrototypeFieldValidationAdapter<>(entityField, validator));
}
}
public void register(E entityType, PrototypeFieldsCombinationValidator validator) {
Map, EntityField> fieldMapping = EntityTypeReflectionUtil.getFieldMappingByPrototype(entityType, validator.getPrototypes());
if (fieldMapping.size() == validator.getPrototypes().size()) {
register(new PrototypeFieldsCombinationValidationAdapter<>(validator, fieldMapping));
} else {
for (EntityFieldPrototype> entityFieldPrototype : validator.getPrototypes()) {
if (!fieldMapping.containsKey(entityFieldPrototype)) {
throw new IllegalArgumentException("Can not find entity field by prototype: " + entityFieldPrototype);
}
}
}
}
public void register(E entityType, PrototypeFieldComplexValidator validator) {
Set> entityFields = EntityTypeReflectionUtil.getFieldsByPrototype(entityType, validator.getPrototype());
if (entityFields.isEmpty()) {
throw new IllegalArgumentException("Can not find entity field by prototype: " + validator.getPrototype());
}
for (EntityField entityField : entityFields) {
register(new PrototypeFieldComplexValidationAdapter<>(entityField, validator));
}
}
@Override
public void validate(Collection extends EntityChange> entityChanges, ChangeOperation changeOperation, ChangeContext changeContext) {
entityChanges.forEach(entityChange -> {
CurrentEntityState currentState = changeContext.getEntity(entityChange);
FinalEntityState finalState = FinalEntityState.merge(currentState, entityChange);
Collection extends EntityField> fieldsToUpdate = entityChange.getChangedFields().collect(Collectors.toList());
findValidatorsTriggeredByFields(fieldsToUpdate, changeOperation)
.map(validator -> validator.validate(entityChange, currentState, finalState))
.filter(Objects::nonNull)
.forEach(validationError -> changeContext.addValidationError(entityChange, validationError));
});
}
@Override
public Stream> requiredFields(Collection extends EntityField> fieldsToUpdate, ChangeOperation changeOperation) {
return findValidatorsTriggeredByFields(fieldsToUpdate, changeOperation)
.flatMap(ChangeValidatorAdapter::fieldsToFetch);
}
public void register(ChangeValidatorAdapter validatorAdapter) {
triggeredChangeValidators.add(validatorAdapter);
}
private Stream> findValidatorsTriggeredByFields(Collection extends EntityField> entityFields, ChangeOperation changeOperation) {
return triggeredChangeValidators.stream().
filter(triggeredValidator -> triggeredValidator.trigger().triggeredByFields(entityFields)).
filter(triggeredValidator -> triggeredValidator.getSupportedChangeOperation().supports(changeOperation));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy