com.kenshoo.pl.entity.internal.audit.AuditedFieldResolver 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.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