com.kenshoo.pl.entity.internal.audit.AuditFieldChangeGenerator 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.audit;
import com.google.common.annotations.VisibleForTesting;
import com.kenshoo.pl.entity.CurrentEntityState;
import com.kenshoo.pl.entity.EntityType;
import com.kenshoo.pl.entity.FinalEntityState;
import com.kenshoo.pl.entity.audit.FieldAuditRecord;
import java.util.Optional;
import static java.util.Objects.requireNonNull;
public class AuditFieldChangeGenerator {
public static final AuditFieldChangeGenerator INSTANCE = new AuditFieldChangeGenerator(AuditFieldValueResolver.INSTANCE);
private final AuditFieldValueResolver auditFieldValueResolver;
AuditFieldChangeGenerator() {
this(AuditFieldValueResolver.INSTANCE);
}
@VisibleForTesting
AuditFieldChangeGenerator(final AuditFieldValueResolver auditFieldValueResolver) {
// singleton
this.auditFieldValueResolver = auditFieldValueResolver;
}
> Optional generate(final CurrentEntityState currentState,
final FinalEntityState finalState,
final AuditedField field) {
requireNonNull(currentState, "A current state is required");
requireNonNull(finalState, "A final state is required");
requireNonNull(field, "A field is required");
return Optional.of(field)
.filter(f -> fieldWasChanged(currentState, finalState, f))
.map(f -> buildFieldRecord(currentState, finalState, f));
}
private , T> boolean fieldWasChanged(final CurrentEntityState currentState,
final FinalEntityState finalState,
final AuditedField field) {
return !fieldStayedTheSame(currentState, finalState, field);
}
private , T> boolean fieldStayedTheSame(final CurrentEntityState currentState,
final FinalEntityState finalState,
final AuditedField field) {
final var triptionalCurrentValue = auditFieldValueResolver.resolve(field, currentState);
final var triptionalFinalValue = auditFieldValueResolver.resolve(field, finalState);
return triptionalCurrentValue.equalsAsOptional(triptionalFinalValue, field::valuesEqual);
}
private > FieldAuditRecord buildFieldRecord(final CurrentEntityState currentState,
final FinalEntityState finalState,
final AuditedField field) {
final FieldAuditRecord.Builder fieldRecordBuilder = FieldAuditRecord.builder(field.getName());
auditFieldValueResolver.resolveToString(field, currentState).ifNotNull(fieldRecordBuilder::oldValue);
auditFieldValueResolver.resolveToString(field, finalState).ifNotNull(fieldRecordBuilder::newValue);
return fieldRecordBuilder.build();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy