All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.kenshoo.pl.entity.internal.audit.AuditFieldChangeGenerator Maven / Gradle / Ivy

Go to download

A Java persistence layer based on JOOQ for high performance and business flow support.

There is a newer version: 0.1.121-jooq-3.16.3
Show newest version
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