com.kenshoo.pl.entity.internal.DefaultFieldValueEnricher 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;
import com.kenshoo.pl.entity.*;
import com.kenshoo.pl.entity.spi.PostFetchCommandEnricher;
import com.kenshoo.pl.entity.spi.helpers.CommandsFieldMatcher;
import java.util.Collection;
import java.util.stream.Stream;
/**
* Created by yuvalr on 2/1/16.
*/
public class DefaultFieldValueEnricher, T> implements PostFetchCommandEnricher {
private final EntityField field;
private final T defaultValue;
public DefaultFieldValueEnricher(EntityField field, T defaultValue) {
this.field = field;
this.defaultValue = defaultValue;
}
@Override
public void enrich(Collection extends ChangeEntityCommand> changeEntityCommands, ChangeOperation changeOperation, ChangeContext changeContext) {
changeEntityCommands.stream()
.filter(command -> !command.isFieldChanged(field))
.forEach(command -> command.set(field, defaultValue));
}
@Override
public Stream> fieldsToEnrich() {
return Stream.of(field);
}
@Override
public boolean shouldRun(Collection extends EntityChange> changeEntityCommands) {
return CommandsFieldMatcher.isAnyFieldMissingInAnyCommand(changeEntityCommands, field);
}
@Override
public SupportedChangeOperation getSupportedChangeOperation() {
return SupportedChangeOperation.CREATE;
}
}