
com.kenshoo.pl.entity.audit.AuditRecord 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.audit;
import com.kenshoo.pl.entity.ChangeOperation;
import com.kenshoo.pl.entity.FieldValue;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import java.util.Collection;
import java.util.Optional;
import static java.util.Collections.emptyList;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toList;
public class AuditRecord {
private final String entityType;
private final String entityId;
private final Collection mandatoryFieldValues;
private final ChangeOperation operator;
private final String entityChangeDescription;
private final Collection fieldRecords;
private final Collection childRecords;
private AuditRecord(final String entityType,
final String entityId,
final Collection mandatoryFieldValues,
final ChangeOperation operator,
final String entityChangeDescription,
final Collection fieldRecords,
final Collection childRecords) {
this.entityType = requireNonNull(entityType, "entityType is required");
this.entityId = entityId;
this.mandatoryFieldValues = mandatoryFieldValues;
this.operator = requireNonNull(operator, "operator is required");
this.entityChangeDescription = entityChangeDescription;
this.fieldRecords = fieldRecords;
this.childRecords = childRecords;
}
public String getEntityType() {
return entityType;
}
public Optional getEntityId() {
return Optional.ofNullable(entityId);
}
public Collection getMandatoryFieldValues() {
return mandatoryFieldValues;
}
public ChangeOperation getOperator() {
return operator;
}
public Optional getEntityChangeDescription() {
return Optional.ofNullable(entityChangeDescription);
}
public Collection getFieldRecords() {
return fieldRecords;
}
public Collection getChildRecords() {
return childRecords;
}
public boolean hasNoChanges() {
return fieldRecords.isEmpty() && childRecords.isEmpty();
}
/**
* Generates a deep string representation of the entire hierarchy of records.
* WARNING: if there are many nested levels of child records, will have poor performance!
*/
@Override
public String toString() {
return toString(Integer.MAX_VALUE);
}
/**
* Generates a deep string representation limited to the given number of nested levels.
* @param maxDepth maximum depth of recursion, must be at least one (one means without child records).
*/
public String toString(final int maxDepth) {
if (maxDepth <= 0) {
return StringUtils.EMPTY;
}
return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
.append("entityType", entityType)
.append("entityId", entityId)
.append("mandatoryFieldValues", mandatoryFieldValues)
.append("operator", operator)
.append("entityChangeDescription", entityChangeDescription)
.append("fieldRecords", fieldRecords)
.append("childRecords", childRecordsToString(maxDepth))
.toString();
}
private String childRecordsToString(final int maxDepth) {
return childRecords.stream()
.map(childRecord -> childRecord.toString(maxDepth - 1))
.collect(toList())
.toString();
}
public static class Builder {
private String entityType;
private String entityId;
private Collection mandatoryFieldValues = emptyList();
private ChangeOperation operator;
private String entityChangeDescription;
private Collection fieldRecords = emptyList();
private Collection childRecords = emptyList();
public Builder withEntityType(String entityType) {
this.entityType = entityType;
return this;
}
public Builder withEntityId(String entityId) {
this.entityId = entityId;
return this;
}
public Builder withOperator(ChangeOperation operator) {
this.operator = operator;
return this;
}
public Builder withEntityChangeDescription(final String entityChangeDescription) {
this.entityChangeDescription = entityChangeDescription;
return this;
}
public Builder withMandatoryFieldValues(final Collection fieldValues) {
this.mandatoryFieldValues = fieldValues == null ? emptyList() : fieldValues;
return this;
}
public Builder withFieldRecords(Collection fieldRecords) {
this.fieldRecords = fieldRecords == null ? emptyList() : fieldRecords;
return this;
}
public Builder withChildRecords(Collection childRecords) {
this.childRecords = childRecords == null ? emptyList() : childRecords;
return this;
}
public AuditRecord build() {
return new AuditRecord(
entityType,
entityId,
mandatoryFieldValues,
operator,
entityChangeDescription,
fieldRecords,
childRecords);
}
}
}