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

com.kenshoo.pl.entity.internal.audit.AuditedFieldResolver 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.EntityField;
import com.kenshoo.pl.entity.EntityType;
import com.kenshoo.pl.entity.annotation.audit.Audited;
import com.kenshoo.pl.entity.annotation.audit.NotAudited;
import com.kenshoo.pl.entity.audit.AuditTrigger;

import java.util.Optional;

import static com.kenshoo.pl.entity.internal.EntityTypeReflectionUtil.getFieldAnnotation;
import static com.kenshoo.pl.entity.internal.EntityTypeReflectionUtil.isAnnotatedWith;
import static com.kenshoo.pl.entity.internal.audit.AuditIndicator.AUDITED;
import static java.util.Objects.requireNonNull;
import static java.util.function.Predicate.not;

public class AuditedFieldResolver {

    public static final AuditedFieldResolver INSTANCE = new AuditedFieldResolver(AuditFieldNameResolver.INSTANCE);

    private final AuditFieldNameResolver auditFieldNameResolver;

    @VisibleForTesting
    AuditedFieldResolver(final AuditFieldNameResolver auditFieldNameResolver) {
        this.auditFieldNameResolver = auditFieldNameResolver;
    }

    public > Optional> resolve(final EntityField field,
                                                                          final AuditIndicator entityAuditIndicator) {
        requireNonNull(field, "field is required");
        requireNonNull(entityAuditIndicator, "entityAuditIndicator is required");

        return Optional.of(field)
                       .filter(not(EntityField::isVirtual))
                       .filter(f -> !isAnnotatedWith(f.getEntityType(), NotAudited.class, f))
                       .filter(f -> isAnnotatedWith(f.getEntityType(), Audited.class, f) || entityAuditIndicator == AUDITED)
                       .map(this::toAuditedField);
    }

    private > AuditedField toAuditedField(EntityField field) {
        final var auditedFieldBuilder = AuditedField.builder(field)
                                                    .withName(auditFieldNameResolver.resolve(field));
        resolveFieldTrigger(field).ifPresent(auditedFieldBuilder::withTrigger);
        return auditedFieldBuilder.build();
    }

    private > Optional resolveFieldTrigger(final EntityField field) {
        return Optional.ofNullable(getFieldAnnotation(field.getEntityType(), field, Audited.class))
                       .map(Audited::trigger);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy