AuditLog.java

/*
 * Copyright 2018 Global Crop Diversity Trust
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.genesys.blocks.auditlog.model;

import java.time.Instant;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Index;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.genesys.blocks.auditlog.annotations.NotAudited;
import org.genesys.blocks.model.ClassPK;
import org.genesys.blocks.model.EmptyModel;
import org.genesys.blocks.util.JsonSidConverter;
import org.hibernate.annotations.Type;
import org.springframework.data.annotation.CreatedBy;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 * The Class AuditLog.
 */
@Entity
@Table(name = "auditlog", indexes = { @Index(unique = false, columnList = "entityId, classPk, prop") })
@NotAudited
@Getter
@Setter
@ToString(callSuper = true)
public class AuditLog extends EmptyModel {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = -2254427722756061411L;
	/** Constant used to indicate that a property is not audited and values should not be persisted */
	public static final String FIELD_VALUE_NOT_AUDITED = "__FIELD_VALUE_NOT_AUDITED__";

	/** The id. */
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(nullable = false, length = 20)
	protected Long id;

	/** The created by. */
	@CreatedBy
	@JsonSerialize(converter = JsonSidConverter.class)
	private Long createdBy;

	/** The log date. */
	@Column(name = "logdate", nullable = false)
	private Instant logDate;

	/** Class name of the referenced entity. */
	@ManyToOne(optional = false)
	@JoinColumn(name = "classPk")
	private ClassPK classPk;

	/** ID of the referenced entity. */
	@Column(name = "entityId")
	private long entityId;

	/** The name of the property modified. */
	@Column(nullable = false, name = "prop", length = 50)
	private String propertyName;

	/** The type of entity referenced in the changed property. */
	@ManyToOne(optional = true)
	@JoinColumn(name = "entityClassPk")
	private ClassPK referencedEntity;

	/** String representation of the previous state. */
	@Lob
	@Column(nullable = true)
	@Type(type = "org.hibernate.type.TextType")
	private String previousState;

	/** String representation of the updated state. */
	@Lob
	@Column(nullable = true)
	@Type(type = "org.hibernate.type.TextType")
	private String newState;

	/** The action. */
	@Enumerated(EnumType.STRING)
	@Column(length = 10, nullable = false, updatable = false)
	private AuditAction action;

	/** The previous entity. */
	@Transient
	@JsonProperty
	private Object previousEntity;

	/** The next entity. */
	@Transient
	@JsonProperty
	private Object newEntity;

	/**
	 * Makes a deep copy of an existing AuditLog object.
	 * 
	 * @param source The AuditLog to copy
	 * @return a deep copy of {@code this}
	 */
	public static AuditLog copy(AuditLog source) {
		var other = new AuditLog();
		other.id = source.getId();
		other.createdBy = source.getCreatedBy();
		other.logDate = source.getLogDate();
		other.classPk = source.getClassPk();
		other.entityId = source.getEntityId();
		other.propertyName = source.getPropertyName();
		other.referencedEntity = source.getReferencedEntity();
		other.previousState = source.getPreviousState();
		other.newState = source.getNewState();
		other.action = source.getAction();
		other.previousEntity = source.getPreviousEntity();
		other.newEntity = source.getNewEntity();
		return other;
	}

	@Override
	public boolean canEqual(Object other) {
		return other instanceof AuditLog;
	}

}