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

com.infusers.core.audit.AuditRecord Maven / Gradle / Ivy

There is a newer version: 2024.12.0008
Show newest version
package com.infusers.core.audit;

import java.io.Serializable;
import java.util.Date;

import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.PrePersist;
import jakarta.persistence.SequenceGenerator;

@Entity
public class AuditRecord extends JdkSerializationRedisSerializer implements Serializable {	
	private static final long serialVersionUID = 1L;
	@Id
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "INFUSERS-AUDIT")
	@SequenceGenerator(name = "INFUSERS-AUDIT", sequenceName = "AUDIT_SEQ", allocationSize = 1)
	private Long id;
	private Date createdAt;
	
	private String userEmailId;
	private String entityName;	
	private String action;	
	private String entityId;
	
	private static final int COMMENTS_MAX_LEN = 1000;
	
    @Column(length = 1000)
	private String comments;

	@PrePersist
	void createdAt() {
		this.createdAt = new Date();
	}

	private AuditRecord() {
		super();
		this.createdAt();
		this.trimComments();		
	}
	
	public AuditRecord(AuditRecord record) {
		super();
		this.id = record.id;
		
		this.userEmailId = record.userEmailId;
		this.entityName = record.entityName;
		this.entityId = record.entityId;		
		this.action = record.action;
		this.comments = record.comments;
		this.createdAt = record.createdAt;	
		
		this.trimComments();		
	}	
	
	public AuditRecord(String userEmailId, String entityName, String action,String entityId,  String comments) {
		super();
		this.userEmailId = userEmailId;
		this.entityName = entityName;
		this.action = action;
		this.entityId = entityId;
		this.comments = comments;
		this.createdAt();
		
		this.trimComments();		
	}
	
	@Override
	public String toString() {
		return "AuditRecord [id=" + id + ", createdAt=" + createdAt + ", userEmailId=" + userEmailId + ", entityName="
				+ entityName + ", action=" + action + ", entityId=" + entityId + ", comments=" + comments + "]";
	}

	public Long getId() {
		return id;
	}
	
	public String getUserEmailId() {
		return userEmailId;
	}

	public void setUserEmailId(String userEmailId) {
		this.userEmailId = userEmailId;
	}

	public Date getCreatedAt() {
		return createdAt;
	}

	public void setCreatedAt(Date createdAt) {
		this.createdAt = createdAt;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}
	
	public String getEntityName() {
		return entityName;
	}

	public void setEntityName(String entityName) {
		this.entityName = entityName;
	}

	public String getEntityId() {
		return entityId;
	}

	public void setEntityId(String entityId) {
		this.entityId = entityId;
	}

	public String getComments() {
		return comments;
	}

	public void setComments(String comments) {
		this.comments = comments;
		this.trimComments();
	}
	
	private void trimComments() {
		if(comments!=null && comments.length() > COMMENTS_MAX_LEN) {
            comments = comments.substring(0, COMMENTS_MAX_LEN - 3) + "...";
		}
	}
	
    // Common validation rules
    private boolean isNotNullOrEmpty(String value) {
        return value != null && !value.trim().isEmpty();
    }

    private boolean isCommentsValidLength(String value, int maxLength) {
    	if(value==null) {
    		return true;
    	}
        return value.length() <= maxLength;
    }
    
    private boolean isValidDate(Date date) {
        return date != null;
    }

    public boolean isValid() {
        return isValidId(id) &&
        		isValidExcludingId();
    }
    
    public boolean isValidExcludingId() {
        return 
               isValidDate(createdAt) &&
               isNotNullOrEmpty(userEmailId) &&
               isNotNullOrEmpty(entityName) &&
               isNotNullOrEmpty(action) &&
               //isNotNullOrEmpty(entityId) &&
               isCommentsValidLength(comments, COMMENTS_MAX_LEN);
    }    

    // Example validation for Long id
    private boolean isValidId(Long id) {
        return id != null && id > 0;
    }	
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy