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.ChangeEntityCommand;
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.EntityFieldPrototype;
import com.kenshoo.pl.entity.EntityType;
import com.kenshoo.pl.entity.internal.EntityTypeReflectionUtil;
import com.kenshoo.pl.entity.internal.validators.EntityChangeValidator;
import com.kenshoo.pl.entity.internal.validators.FieldComplexValidationAdapter;
import com.kenshoo.pl.entity.internal.validators.FieldValidationAdapter;
import com.kenshoo.pl.entity.internal.validators.FieldsCombinationValidationAdapter;
import com.kenshoo.pl.entity.internal.validators.ImmutableFieldValidationAdapter;
import com.kenshoo.pl.entity.internal.validators.PrototypeFieldComplexValidationAdapter;
import com.kenshoo.pl.entity.internal.validators.PrototypeFieldValidationAdapter;
import com.kenshoo.pl.entity.internal.validators.PrototypeFieldsCombinationValidationAdapter;
import com.kenshoo.pl.entity.internal.validators.RequiredFieldValidationAdapter;
import com.kenshoo.pl.entity.spi.ChangeValidator;
import com.kenshoo.pl.entity.spi.ChangesValidator;
import com.kenshoo.pl.entity.spi.FieldComplexValidator;
import com.kenshoo.pl.entity.spi.FieldValidator;
import com.kenshoo.pl.entity.spi.FieldsCombinationValidator;
import com.kenshoo.pl.entity.spi.ImmutableFieldValidator;
import com.kenshoo.pl.entity.spi.PrototypeFieldComplexValidator;
import com.kenshoo.pl.entity.spi.PrototypeFieldValidator;
import com.kenshoo.pl.entity.spi.PrototypeFieldsCombinationValidator;
import com.kenshoo.pl.entity.spi.RequiredFieldValidator;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Stream;
import static java.util.stream.Collectors.toSet;
public class EntityChangeCompositeValidator> implements ChangesValidator {
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 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(EntityChangeValidator validator) {
validator.getValidatedFields().forEach(field -> changesValidators.put(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(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(entityField, new PrototypeFieldComplexValidationAdapter<>(entityField, validator));
}
}
@Override
public void validate(Collection extends EntityChange> entityChanges, ChangeOperation changeOperation, ChangeContext changeContext) {
entityChanges.forEach(entityChange -> {
Entity entity = changeContext.getEntity(entityChange);
Collection> validators = findValidators(entityChange);
validators.stream()
.filter(validator -> validator.getSupportedChangeOperation().supports(changeOperation))
.map(validator -> validator.validate(entityChange, entity, changeOperation))
.filter(Objects::nonNull)
.forEach(validationError -> changeContext.addValidationError(entityChange, validationError));
});
}
@Override
public Stream> requiredFields(Collection extends EntityField> fieldsToUpdate, ChangeOperation changeOperation) {
return fieldsToUpdate.stream()
.flatMap(field -> changesValidators.get(field).stream())
.filter(validator -> validator.getSupportedChangeOperation().supports(changeOperation))
.flatMap(validator -> validator.getFieldsToFetch(changeOperation));
}
private Collection> findValidators(EntityChange entityChange) {
return entityChange.getChangedFields()
.flatMap(field -> changesValidators.get(field).stream())
.collect(toSet());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy