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