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

com.kenshoo.pl.entity.validators.RequiredAtLeastOneChildValidator 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.validators;

import com.kenshoo.pl.entity.*;
import com.kenshoo.pl.entity.spi.ChangesValidator;

import java.util.Collection;
import java.util.Map;

/**
 * A validator that force given a child in command
 *
 * @param      entity type
 * @param  entity child type
 */
public class RequiredAtLeastOneChildValidator, CHILD extends EntityType> implements ChangesValidator {

    private final String errorCode;
    private final CHILD childType;

    public RequiredAtLeastOneChildValidator(CHILD childType, String errorCode) {
        this.errorCode = errorCode;
        this.childType = childType;
    }

    @Override
    public void validate(Collection> entityChanges, ChangeOperation changeOperation, ChangeContext changeContext) {
        entityChanges.forEach(entityChange -> {
            long childrenAmount = entityChange.getChildren(childType).count();
            if (childrenAmount == 0) {
                changeContext.addValidationError(entityChange,
                        new ValidationError(errorCode, Map.of("childType", childType.getName())));
            }
        });
    }

    @Override
    public SupportedChangeOperation getSupportedChangeOperation() {
        return SupportedChangeOperation.CREATE;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy