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.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.kenshoo.pl.entity.ChangeContext;
import com.kenshoo.pl.entity.ChangeOperation;
import com.kenshoo.pl.entity.CurrentEntityState;
import com.kenshoo.pl.entity.EntityChange;
import com.kenshoo.pl.entity.EntityField;
import com.kenshoo.pl.entity.EntityFieldPrototype;
import com.kenshoo.pl.entity.EntityType;
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.Stream;
import static java.util.stream.Collectors.toSet;
public class EntityChangeCompositeValidator> implements ChangesValidator {
private final ValidationTrigger ANY_FIELD_TRIGGER = entityField -> true;
private final Multimap, EntityChangeValidator> changesValidators = HashMultimap.create();
public void register(E entityType, ChangeValidator validator) {
if (validator instanceof FieldComplexValidator) {
//noinspection unchecked
register((FieldComplexValidator) validator);
} else if (validator instanceof FieldValidator) {
//noinspection unchecked
register((FieldValidator) validator);
} else if (validator instanceof FieldsCombinationValidator) {
//noinspection unchecked
register((FieldsCombinationValidator) validator);
} else if (validator instanceof ImmutableFieldValidator) {
//noinspection unchecked
register((ImmutableFieldValidator) validator);
} else if (validator instanceof AncestorsValidator) {
//noinspection unchecked
register((AncestorsValidator) validator);
} else if (validator instanceof PrototypeFieldValidator) {
//noinspection unchecked
register(entityType, (PrototypeFieldValidator) validator);
} else if ((validator instanceof PrototypeFieldsCombinationValidator)) {
register(entityType, (PrototypeFieldsCombinationValidator) validator);
} else if (validator instanceof PrototypeFieldComplexValidator) {
//noinspection unchecked
register(entityType, (PrototypeFieldComplexValidator) validator);
} else if (validator instanceof RequiredFieldValidator) {
//noinspection unchecked
register((RequiredFieldValidator) validator);
}
}
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) {
changesValidators.put(ANY_FIELD_TRIGGER, new AncestorsValidationAdapter<>(validator));
}
public void register(EntityChangeValidator validator) {
validator.validatedFields().forEach(field -> changesValidators.put(new FieldTrigger<>(field), 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) {
changesValidators.put(new FieldTrigger<>(entityField), 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) {
changesValidators.put(new FieldTrigger<>(entityField), new PrototypeFieldComplexValidationAdapter<>(entityField, validator));
}
}
@Override
public void validate(Collection extends EntityChange> entityChanges, ChangeOperation changeOperation, ChangeContext changeContext) {
entityChanges.forEach(entityChange -> {
CurrentEntityState currentState = changeContext.getEntity(entityChange);
Collection> validators = findValidators(entityChange, changeOperation);
validators.stream()
.filter(validator -> validator.getSupportedChangeOperation().supports(changeOperation))
.map(validator -> validator.validate(entityChange, currentState))
.filter(Objects::nonNull)
.forEach(validationError -> changeContext.addValidationError(entityChange, validationError));
});
}
@Override
public Stream> requiredFields(Collection extends EntityField> fieldsToUpdate, ChangeOperation changeOperation) {
return fieldsToUpdate.stream()
.flatMap(this::findValidatorsTriggeredByField)
.filter(validator -> validator.getSupportedChangeOperation().supports(changeOperation))
.flatMap(EntityChangeValidator::fetchFields);
}
private Collection> findValidators(EntityChange entityChange, ChangeOperation changeOperation) {
return entityChange.getChangedFields()
.flatMap(this::findValidatorsTriggeredByField)
.filter(validator -> validator.getSupportedChangeOperation().supports(changeOperation))
.collect(toSet());
}
private Stream> findValidatorsTriggeredByField(EntityField entityField) {
return changesValidators.keySet().stream().
filter(trigger -> trigger.triggeredByField(entityField)).
flatMap(trigger -> changesValidators.get(trigger).stream());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy