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.kenshoo.pl.entity.CurrentEntityState;
import com.kenshoo.pl.entity.EntityField;
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 {

    > Optional> generate(final CurrentEntityState currentState,
                                                                     final FinalEntityState finalState,
                                                                     final EntityField 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 EntityField field) {
        return !fieldStayedTheSame(currentState, finalState, field);
    }

    private , T> boolean fieldStayedTheSame(final CurrentEntityState currentState,
                                                                    final FinalEntityState finalState,
                                                                    final EntityField field) {
        return currentState.safeGet(field).equals(finalState.safeGet(field), field::valuesEqual);
    }

    private > FieldAuditRecord buildFieldRecord(final CurrentEntityState currentState,
                                                                           final FinalEntityState finalState,
                                                                           final EntityField field) {
        final FieldAuditRecord.Builder fieldRecordBuilder = FieldAuditRecord.builder(field);
        currentState.safeGet(field).ifNotNull(fieldRecordBuilder::oldValue);
        finalState.safeGet(field).ifNotNull(fieldRecordBuilder::newValue);
        return fieldRecordBuilder.build();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy