com.kenshoo.pl.entity.spi.helpers.ParentConditionCompositeValidator 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.ImmutableListMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
import com.kenshoo.pl.entity.ChangeContext;
import com.kenshoo.pl.entity.ChangeOperation;
import com.kenshoo.pl.entity.EntityChange;
import com.kenshoo.pl.entity.EntityField;
import com.kenshoo.pl.entity.EntityType;
import com.kenshoo.pl.entity.ValidationError;
import com.kenshoo.pl.entity.spi.ChangesValidator;
import com.kenshoo.pl.entity.spi.ParentConditionValidator;
import java.util.Collection;
import java.util.stream.Stream;
public class ParentConditionCompositeValidator> implements ChangesValidator {
private final Multimap, ParentConditionValidator>> parentValidators = HashMultimap.create();
public void register(ParentConditionValidator> validator) {
parentValidators.put(validator.parentIdField(), validator);
}
@Override
public void validate(Collection extends EntityChange> entityChanges, ChangeOperation changeOperation, final ChangeContext changeContext) {
for (final EntityField, ?> field : parentValidators.keySet()) {
validateForField(entityChanges, changeContext, field);
}
}
@Override
public Stream extends EntityField, ?>> requiredFields(Collection extends EntityField> fieldsToUpdate, ChangeOperation changeOperation) {
return parentValidators.values().stream().map(ParentConditionValidator::parentIdField);
}
private void validateForField(Collection extends EntityChange> entityChanges, final ChangeContext changeContext, final EntityField, T> field) {
//noinspection unchecked
Collection> validators = (Collection>) (Collection) parentValidators.get(field);
if (!validators.isEmpty()) {
validateEntityChanges(entityChanges, changeContext, field, validators);
}
}
private void validateEntityChanges(Collection extends EntityChange> entityChanges, final ChangeContext changeContext, final EntityField, T> field, Collection> validators) {
ImmutableListMultimap> changesByParentId =
Multimaps.index(entityChanges, entityChange -> changeContext.getEntity(entityChange).get(field));
for (T parentId : changesByParentId.keySet()) {
for (ParentConditionValidator validator : validators) {
ValidationError error = validator.validate(parentId);
if (error != null) {
for (EntityChange entityChange : changesByParentId.get(parentId)) {
changeContext.addValidationError(entityChange, error);
}
}
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy