com.kenshoo.pl.entity.internal.validators.ImmutableFieldValidationAdapter 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.internal.validators;
import com.google.common.collect.ImmutableMap;
import com.kenshoo.pl.entity.CurrentEntityState;
import com.kenshoo.pl.entity.EntityChange;
import com.kenshoo.pl.entity.EntityField;
import com.kenshoo.pl.entity.EntityType;
import com.kenshoo.pl.entity.SupportedChangeOperation;
import com.kenshoo.pl.entity.ValidationError;
import com.kenshoo.pl.entity.spi.ImmutableFieldValidator;
import java.util.stream.Stream;
public class ImmutableFieldValidationAdapter, T> implements ChangeValidatorAdapter {
private final ImmutableFieldValidator validator;
private final ValidationTrigger trigger;
public ImmutableFieldValidationAdapter(ImmutableFieldValidator validator) {
this.validator = validator;
this.trigger = new FieldTrigger<>(validator.immutableField());
}
@Override
public ValidationTrigger trigger() {
return trigger;
}
@Override
public SupportedChangeOperation getSupportedChangeOperation() {
return SupportedChangeOperation.UPDATE;
}
@Override
public Stream extends EntityField, ?>> fieldsToFetch() {
Stream> fetchFields = validator.fetchFields();
if(fetchFields != null) {
return Stream.concat(Stream.of(validator.immutableField()), validator.fetchFields());
} else {
return Stream.of(validator.immutableField());
}
}
@Override
public ValidationError validate(EntityChange entityChange, CurrentEntityState currentState) {
if (entityChange.isFieldChanged(validator.immutableField()) && validator.immutableWhen().test(currentState)) {
return new ValidationError(validator.getErrorCode(), validator.immutableField(), ImmutableMap.of("field", validator.immutableField().toString()));
}
return null;
}
}