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

com.kenshoo.pl.entity.spi.helpers.EntityChangeCompositeValidator 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.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.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> entityChanges, ChangeOperation changeOperation, ChangeContext changeContext) {
        entityChanges.forEach(entityChange -> {
            CurrentEntityState currentState = changeContext.getEntity(entityChange);
            Collection> fieldsToUpdate = entityChange.getChangedFields().collect(Collectors.toList());
            findValidatorsTriggeredByFields(fieldsToUpdate, changeOperation)
                    .map(validator -> validator.validate(entityChange, currentState))
                    .filter(Objects::nonNull)
                    .forEach(validationError -> changeContext.addValidationError(entityChange, validationError));
        });
    }

    @Override
    public Stream> requiredFields(Collection> fieldsToUpdate, ChangeOperation changeOperation) {
        return findValidatorsTriggeredByFields(fieldsToUpdate, changeOperation)
                .flatMap(ChangeValidatorAdapter::fieldsToFetch);
    }

    public void register(ChangeValidatorAdapter validatorAdapter) {
        triggeredChangeValidators.add(validatorAdapter);
    }

    private Stream> findValidatorsTriggeredByFields(Collection> 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