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

com.kenshoo.pl.entity.internal.audit.AuditedFieldSet 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.collect.HashMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.SetMultimap;
import com.kenshoo.pl.entity.EntityField;
import com.kenshoo.pl.entity.EntityType;
import com.kenshoo.pl.entity.audit.AuditTrigger;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;

import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;

import static com.kenshoo.pl.entity.audit.AuditTrigger.*;
import static java.util.Collections.emptySet;
import static java.util.Collections.singleton;
import static java.util.Objects.requireNonNull;
import static org.apache.commons.lang3.builder.ToStringStyle.SHORT_PREFIX_STYLE;

public class AuditedFieldSet> {

    private final EntityField idField;

    // Fields from other entities which are included always in the audit record with their current values
    private final Set> externalFields;

    // Fields from current entity (besides id), keyed by audit trigger type
    private final SetMultimap> internalFields;

    private AuditedFieldSet(final EntityField idField,
                            final Set> externalFields,
                            final SetMultimap> internalFields) {
        this.idField = idField;
        this.externalFields = externalFields;
        this.internalFields = internalFields;
    }

    public EntityField getIdField() {
        return idField;
    }

    public Set> getExternalFields() {
        return externalFields;
    }

    public Stream> getMandatoryFields() {
        return Stream.of(externalFields, internalFields.get(ALWAYS))
                     .flatMap(Set::stream);
    }

    public Stream> getOnChangeFields() {
        return Stream.of(ON_CREATE_OR_UPDATE, ON_UPDATE)
                     .flatMap(trigger -> internalFields.get(trigger).stream());
    }

    public Stream> getInternalFields() {
        return internalFields.values().stream();
    }

    public boolean hasInternalFields() {
        return !internalFields.isEmpty();
    }

    public Stream> getAllFields() {
        return Stream.of(singleton(idField),
                         externalFields,
                         internalFields.values())
                     .flatMap(Collection::stream);
    }

    public static > Builder builder(final EntityField idField) {
        return new Builder<>(idField);
    }

    public static class Builder> {
        private final EntityField idField;
        private Set> externalFields = emptySet();
        private final SetMultimap> internalFields = HashMultimap.create();

        public Builder(final EntityField idField) {
            this.idField = requireNonNull(idField, "idField is required");
            Stream.of(ALWAYS, ON_CREATE_OR_UPDATE, ON_UPDATE)
                  .forEach(trigger -> internalFields.putAll(trigger, emptySet()));
        }

        public Builder withExternalFields(final EntityField... externalFields) {
            this.externalFields = externalFields == null ? emptySet() : ImmutableSet.copyOf(externalFields);
            return this;
        }

        public Builder withExternalFields(final Iterable> externalFields) {
            this.externalFields = externalFields == null ? emptySet() : ImmutableSet.copyOf(externalFields);
            return this;
        }

        public final Builder withInternalFields(final Map>> internalFields) {
            if (internalFields != null) {
                internalFields.forEach(this.internalFields::putAll);
            } else {
                this.internalFields.clear();
            }
            return this;
        }

        @SafeVarargs
        public final Builder withInternalFields(final AuditTrigger trigger,
                                                   final EntityField... internalMandatoryFields) {
            internalFields.putAll(trigger,
                                  internalMandatoryFields == null ? emptySet() : ImmutableSet.copyOf(internalMandatoryFields));
            return this;
        }

        public Builder withInternalFields(final AuditTrigger trigger,
                                             final Iterable> internalMandatoryFields) {
            internalFields.putAll(trigger,
                                  internalMandatoryFields == null ? emptySet() : ImmutableSet.copyOf(internalMandatoryFields));
            return this;
        }

        public AuditedFieldSet build() {
            return new AuditedFieldSet<>(idField,
                                         externalFields,
                                         internalFields);
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }

        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        final AuditedFieldSet that = (AuditedFieldSet) o;

        return new EqualsBuilder()
            .append(idField, that.idField)
            .append(externalFields, that.externalFields)
            .append(internalFields, that.internalFields)
            .isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 37)
            .append(idField)
            .append(externalFields)
            .append(internalFields)
            .toHashCode();
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this, SHORT_PREFIX_STYLE)
            .append("idField", idField)
            .append("externalFields", externalFields)
            .append("internalFields", internalFields)
            .toString();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy