All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.kenshoo.pl.entity.spi.helpers.ParentConditionCompositeValidator Maven / Gradle / Ivy

Go to download

A Java persistence layer based on JOOQ for high performance and business flow support.

There is a newer version: 0.1.121-jooq-3.16.3
Show newest version
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> entityChanges, ChangeOperation changeOperation, final ChangeContext changeContext) {
        for (final EntityField field : parentValidators.keySet()) {
            validateForField(entityChanges, changeContext, field);
        }
    }

    @Override
    public Stream> requiredFields(Collection> fieldsToUpdate, ChangeOperation changeOperation) {
        return parentValidators.values().stream().map(ParentConditionValidator::parentIdField);
    }


    private  void validateForField(Collection> entityChanges, final ChangeContext changeContext, final EntityField field) {
        //noinspection unchecked
        Collection> validators = (Collection>) (Collection) parentValidators.get(field);
        if (!validators.isEmpty()) {
            validateEntityChanges(entityChanges, changeContext, field, validators);
        }
    }

    private  void validateEntityChanges(Collection> entityChanges, final ChangeContext changeContext, final EntityField 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