Skip to content

Commit

Permalink
Several changes to enable Envers plus some other fixes
Browse files Browse the repository at this point in the history
- Add necessary auditing tables in Liquibase
- Add some missing foreign key constraints
- Add SQL to Liquibase to initialize the first revision for existing data
- Rename AbstractAuditingEntity to AbstractEntity
- AbstractEntity fields will be populated by AbstractEntityListener on PostLoad
- Add Envers settings to application.yml in main and test
- Add an AuditReader bean
- CustomRevisionEntity no longer inherits DefaultRevisionEntity, this is done to customize the field types, e.g. the timestamp is a Date instead of a Long, the id is a Long instead of an Integer, and we use a custom sequence for this entity's primary key so as not to interfere with our existing sequences
- Add a repository for CustomRevisionEntity
- Delete EventPublisherEntityListener, we don't need to audit entity events in this way anymore
- Add some commented lines in liquibase.gradle, a different config for using another Postgres DB as reference instead of the hibernate reference. When using the hibernate reference it seems the settings for envers in application.yml are not picked up. So if you need to create a diffChangelog, it's best to have a postgres with the 'old' schema and a postgres with the 'new' schema to use as reference and then run gradlew liquibaseDiffChangelog
- Revert all changes to the PK sequences for our own entities, these are no longer necessary since we have a custom sequence in CustomRevisionEntity
- Remove the created_at, created_by, last_modified_at and last_modified_by columns from all our entity tables. These are no longer necessary as this information is in the audit log.
- Remove the `AndCreatedDateBefore` part of the UserRepository method, since we don't have that column anymore
- Update the test for deleting non-activated users
  • Loading branch information
dennyverbeeck committed Mar 14, 2018
1 parent a527f58 commit e32b381
Show file tree
Hide file tree
Showing 27 changed files with 966 additions and 655 deletions.
4 changes: 4 additions & 0 deletions gradle/liquibase.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ task liquibaseDiffChangelog(dependsOn: compileJava, type: JavaExec) {

args "--changeLogFile=src/main/resources/config/liquibase/changelog/" + buildTimestamp() +"_changelog.xml"
args "--referenceUrl=hibernate:spring:org.radarcns.management.domain?dialect=org.hibernate.dialect.PostgreSQLDialect&hibernate.physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy&hibernate.implicit_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy"
// comment out line above and uncomment lines below if you want to compare against another postgres instance
//args "--referenceUrl=jdbc:postgresql://localhost:5433/managementportal"
//args "--referenceUsername=postgres"
//args "--referencePassword="
args "--username=postgres"
args "--password="
args "--url=jdbc:postgresql://localhost:5432/managementportal"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.radarcns.management.config.audit;

import org.hibernate.envers.AuditReader;
import org.hibernate.envers.AuditReaderFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.persistence.EntityManager;

@Configuration
public class AuditReaderConfiguration {

@Autowired
private EntityManager entityManager;

@Bean
public AuditReader auditReader() {
return AuditReaderFactory.get(entityManager);
}
}

This file was deleted.

59 changes: 59 additions & 0 deletions src/main/java/org/radarcns/management/domain/AbstractEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.radarcns.management.domain;

import org.radarcns.management.domain.support.AbstractEntityListener;

import java.io.Serializable;
import java.time.ZonedDateTime;

/**
* Base abstract class for entities which will hold definitions for created, last modified by and
* created, last modified by date. These will be populated by {@link AbstractEntityListener} on
* the {@code PostLoad} trigger. Since this class is not an Entity or a MappedSuperClass, we need
* to define the entitylistener on each of the subclasses.
*/
public abstract class AbstractEntity implements Serializable {

private static final long serialVersionUID = 1L;

private String createdBy;

private ZonedDateTime createdDate;

private String lastModifiedBy;

private ZonedDateTime lastModifiedDate;

public String getCreatedBy() {
return createdBy;
}

public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}

public ZonedDateTime getCreatedDate() {
return createdDate;
}

public void setCreatedDate(ZonedDateTime createdDate) {
this.createdDate = createdDate;
}

public String getLastModifiedBy() {
return lastModifiedBy;
}

public void setLastModifiedBy(String lastModifiedBy) {
this.lastModifiedBy = lastModifiedBy;
}

public ZonedDateTime getLastModifiedDate() {
return lastModifiedDate;
}

public void setLastModifiedDate(ZonedDateTime lastModifiedDate) {
this.lastModifiedDate = lastModifiedDate;
}

public abstract Long getId();
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class PersistentAuditEvent implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator", initialValue = 1, allocationSize = 1)
@SequenceGenerator(name = "sequenceGenerator", initialValue = 1000)
@Column(name = "event_id")
private Long id;

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/radarcns/management/domain/Project.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@
@Audited
@Table(name = "project")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Project extends AbstractAuditingEntity implements Serializable {
public class Project extends AbstractEntity implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator", initialValue = 1, allocationSize = 1)
@SequenceGenerator(name = "sequenceGenerator", initialValue = 1000)
private Long id;

@NotNull
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/org/radarcns/management/domain/Role.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.envers.Audited;
import org.hibernate.envers.RelationTargetAuditMode;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
Expand All @@ -34,18 +35,19 @@
@Audited
@Table(name = "radar_role")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Role extends AbstractAuditingEntity implements Serializable {
public class Role extends AbstractEntity implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator", initialValue = 1, allocationSize = 1)
@SequenceGenerator(name = "sequenceGenerator", initialValue = 1000)
private Long id;

@ManyToMany(mappedBy = "roles")
@JsonIgnore
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
private Set<User> users = new HashSet<>();

@ManyToOne
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/radarcns/management/domain/Source.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@
@Audited
@Table(name = "radar_source")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Source extends AbstractAuditingEntity implements Serializable {
public class Source extends AbstractEntity implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator", initialValue = 1, allocationSize = 1)
@SequenceGenerator(name = "sequenceGenerator", initialValue = 1000)
private Long id;

@NotNull
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/radarcns/management/domain/SourceData.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@
@Audited
@Table(name = "source_data")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class SourceData extends AbstractAuditingEntity implements Serializable {
public class SourceData extends AbstractEntity implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator", initialValue = 1, allocationSize = 1)
@SequenceGenerator(name = "sequenceGenerator", initialValue = 1000)
private Long id;

//SourceData type e.g. ACCELEROMETER, TEMPERATURE.
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/radarcns/management/domain/SourceType.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@
@Audited
@Table(name = "source_type")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class SourceType extends AbstractAuditingEntity implements Serializable {
public class SourceType extends AbstractEntity implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator", initialValue = 1, allocationSize = 1)
@SequenceGenerator(name = "sequenceGenerator", initialValue = 1000)
private Long id;

@NotNull
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/radarcns/management/domain/Subject.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@
@Audited
@Table(name = "subject")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Subject extends AbstractAuditingEntity implements Serializable {
public class Subject extends AbstractEntity implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator", initialValue = 1, allocationSize = 1)
@SequenceGenerator(name = "sequenceGenerator", initialValue = 1000)
private Long id;

@Column(name = "external_link")
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/org/radarcns/management/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
import org.hibernate.envers.Audited;
import org.hibernate.validator.constraints.Email;
import org.radarcns.auth.config.Constants;
import org.radarcns.management.domain.support.AbstractEntityListener;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
Expand All @@ -38,13 +40,14 @@
@Audited
@Table(name = "radar_user")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class User extends AbstractAuditingEntity implements Serializable {
@EntityListeners({AbstractEntityListener.class})
public class User extends AbstractEntity implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator", initialValue = 1, allocationSize = 1)
@SequenceGenerator(name = "sequenceGenerator", initialValue = 1000)
private Long id;

@NotNull
Expand Down
Loading

0 comments on commit e32b381

Please sign in to comment.