Skip to content

Commit

Permalink
Add changed entities tracking
Browse files Browse the repository at this point in the history
Enables to get a list of changed entities per revision
  • Loading branch information
dennyverbeeck committed Mar 19, 2018
1 parent f7a45be commit b44b67c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
package org.radarcns.management.domain.audit;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.envers.ModifiedEntityNames;
import org.hibernate.envers.RevisionEntity;
import org.hibernate.envers.RevisionNumber;
import org.hibernate.envers.RevisionTimestamp;
import org.radarcns.management.config.audit.CustomRevisionListener;

import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import java.io.Serializable;
import java.text.DateFormat;
import java.util.Date;
import java.util.Objects;
import java.util.Set;

@Entity
@RevisionEntity(CustomRevisionListener.class)
Expand All @@ -36,6 +44,13 @@ public class CustomRevisionEntity implements Serializable {

private String auditor;

@ElementCollection(fetch = FetchType.EAGER)
@JoinTable(name = "REVCHANGES", joinColumns = @JoinColumn(name = "REV"))
@Column(name = "ENTITYNAME")
@Fetch(FetchMode.JOIN)
@ModifiedEntityNames
private Set<String> modifiedEntityNames;

public long getId() {
return id;
}
Expand Down Expand Up @@ -63,19 +78,23 @@ public boolean equals(Object o) {
}
CustomRevisionEntity that = (CustomRevisionEntity) o;
return id == that.id && Objects.equals(timestamp, that.timestamp) && Objects
.equals(auditor, that.auditor);
.equals(auditor, that.auditor) && Objects
.equals(modifiedEntityNames, that.modifiedEntityNames);
}

@Override
public int hashCode() {
return Objects.hash(id, timestamp, auditor);
return Objects.hash(id, timestamp, auditor, modifiedEntityNames);
}

@Override
public String toString() {
return "CustomRevisionEntity(id = " + id
+ ", revisionDate = " + DateFormat.getDateTimeInstance().format( timestamp ) + ", "
+ "auditor = " + auditor + ")";
return "CustomRevisionEntity{"
+ "id=" + id
+ ", timestamp=" + timestamp
+ ", auditor='" + auditor + '\''
+ ", modifiedEntityNames=" + modifiedEntityNames
+ '}';
}

public String getAuditor() {
Expand All @@ -85,4 +104,12 @@ public String getAuditor() {
public void setAuditor(String auditor) {
this.auditor = auditor;
}

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

public void setModifiedEntityNames(Set<String> modifiedEntityNames) {
this.modifiedEntityNames = modifiedEntityNames;
}
}
2 changes: 2 additions & 0 deletions src/main/resources/config/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ spring:
physical-strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
implicit-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
properties:
hibernate.hbm2ddl.auto: update
org.hibernate.envers:
store_data_at_delete: true
audit_strategy: org.hibernate.envers.strategy.ValidityAuditStrategy
audit_strategy_validity_store_revend_timestamp: true
global_with_modified_flag: true
track_entities_changed_in_revision: true
messages:
basename: i18n/messages
mvc:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,4 +542,17 @@
<dropColumn tableName="subject" columnName="last_modified_by"/>
<dropColumn tableName="subject" columnName="last_modified_date"/>
</changeSet>

<changeSet id="1520933868142-71" author="[email protected]">
<createTable tableName="revchanges">
<column name="rev" type="BIGINT">
<constraints nullable="false"/>
</column>
<column name="entityname" type="varchar(255)"/>
</createTable>
</changeSet>

<changeSet author="[email protected]" id="1520933868142-72">
<addForeignKeyConstraint baseColumnNames="rev" baseTableName="revchanges" constraintName="FK6ccsoae5dclnhlrltuq7d2grq" deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION" referencedColumnNames="id" referencedTableName="_revisions_info"/>
</changeSet>
</databaseChangeLog>
1 change: 1 addition & 0 deletions src/test/resources/config/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ spring:
audit_strategy: org.hibernate.envers.strategy.ValidityAuditStrategy
audit_strategy_validity_store_revend_timestamp: true
global_with_modified_flag: true
track_entities_changed_in_revision: true
mail:
host: localhost
messages:
Expand Down

0 comments on commit b44b67c

Please sign in to comment.