package com.sdm.core.hibernate.entity;

import com.fasterxml.jackson.annotation.JsonGetter;
import com.sdm.core.hibernate.audit.AuditListener;
import java.io.Serializable;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Table;
import org.hibernate.envers.ModifiedEntityNames;
import org.hibernate.envers.RevisionEntity;
import org.hibernate.envers.RevisionNumber;
import org.hibernate.envers.RevisionTimestamp;

@Entity(name = "AuditEntity")
@Table(name = "tbl_audit_info")
@RevisionEntity(AuditListener.class)
public class AuditEntity implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = -4287015242983266529L;

    @Id
    @GeneratedValue
    @RevisionNumber
    private long version;

    @RevisionTimestamp
    private long timestamp;

    private int userId;

    @ElementCollection
    @JoinTable(name = "tbl_audit_changes", joinColumns = @JoinColumn(name = "version"))
    @Column(name = "entity")
    @ModifiedEntityNames
    private Set<String> modifiedEntityNames;

    public long getVersion() {
        return version;
    }

    public void setVersion(long version) {
        this.version = version;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    @JsonGetter("modified_count")
    public int getModifiedCount() {
        if (modifiedEntityNames == null) {
            return 0;
        }

        return modifiedEntityNames.size();
    }

    public Set<String> getModifiedEntityNames() {
        return modifiedEntityNames;
    }

    public void setModifiedEntityNames(Set<String> modifiedEntityNames) {
        this.modifiedEntityNames = modifiedEntityNames;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + (int) (version ^ (version >>> 32));
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        AuditEntity other = (AuditEntity) obj;
        if (version != other.version) {
            return false;
        }
        return true;
    }

}

@@ -37,60 +37,8 @@
 y;%0A%0A
-import com.fasterxml.jackson.annotation.JsonGetter;%0A
 impo
@@ -117,107 +117,8 @@
 le;%0A
-import java.util.Set;%0Aimport javax.persistence.Column;%0Aimport javax.persistence.ElementCollection;%0A
 impo
@@ -245,135 +245,13 @@
 nce.
-JoinColumn;%0Aimport javax.persistence.JoinTable;%0Aimport javax.persistence.Table;%0Aimport org.hibernate.envers.ModifiedEntityNames
+Table
 ;%0Aim
@@ -787,219 +787,8 @@
 d;%0A%0A
-    @ElementCollection%0A    @JoinTable(name = %22tbl_audit_changes%22, joinColumns = @JoinColumn(name = %22version%22))%0A    @Column(name = %22entity%22)%0A    @ModifiedEntityNames%0A    private Set%3CString%3E modifiedEntityNames;%0A%0A
     
@@ -1223,434 +1223,8 @@
  %7D%0A%0A
-    @JsonGetter(%22modified_count%22)%0A    public int getModifiedCount() %7B%0A        if (modifiedEntityNames == null) %7B%0A            return 0;%0A        %7D%0A%0A        return modifiedEntityNames.size();%0A    %7D%0A%0A    public Set%3CString%3E getModifiedEntityNames() %7B%0A        return modifiedEntityNames;%0A    %7D%0A%0A    public void setModifiedEntityNames(Set%3CString%3E modifiedEntityNames) %7B%0A        this.modifiedEntityNames = modifiedEntityNames;%0A    %7D%0A%0A
     

Remove changed entity from audit table