com.kenshoo.pl.entity.internal.SingleFieldEnricher 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 java.util.Collection;
import java.util.function.BiPredicate;
import java.util.function.Predicate;
import java.util.stream.Stream;
abstract public class SingleFieldEnricher, T> implements PostFetchCommandEnricher {
@Override
final public void enrich(Collection extends ChangeEntityCommand> changeEntityCommands, ChangeOperation changeOperation, ChangeContext changeContext) {
changeEntityCommands.stream()
.filter(this::shouldRunForCommand)
.filter(additionalCommandFilter())
.filter(command-> additionalPostFetchCommandFilter().test(command, changeContext.getEntity(command)))
.forEach(command -> command.set(enrichedField(), enrichedValue(command, changeContext.getEntity(command))));
}
@Override
final public Stream> fieldsToEnrich() {
return Stream.of(enrichedField());
}
@Override
final public boolean shouldRun(Collection extends EntityChange> commands) {
return commands.stream().filter(additionalCommandFilter()).anyMatch(this::shouldRunForCommand);
}
abstract protected EntityField enrichedField();
abstract protected T enrichedValue(EntityChange entityChange, Entity entity);
protected Predicate> additionalCommandFilter() {
return entityChange -> true;
}
protected BiPredicate, Entity> additionalPostFetchCommandFilter() {
return (entityChange, entity) -> true;
}
protected boolean shouldRunForCommand(EntityChange entityChange) {
return enrichedFieldIsMissing(entityChange);
}
private boolean enrichedFieldIsMissing(EntityChange entityChange) {
return !entityChange.isFieldChanged(enrichedField());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy