improved Audit

This commit is contained in:
2020-09-30 20:01:20 +02:00
parent c983bde031
commit 57907d05e6
6 changed files with 224 additions and 123 deletions

View File

@ -15,67 +15,84 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
--> -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<modelVersion>4.0.0</modelVersion> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.muehlencord.shared</groupId>
<artifactId>shared-db</artifactId> <groupId>de.muehlencord.shared</groupId>
<packaging>ejb</packaging> <artifactId>shared-db</artifactId>
<packaging>ejb</packaging>
<parent>
<artifactId>shared</artifactId> <parent>
<groupId>de.muehlencord</groupId> <artifactId>shared</artifactId>
<version>1.2-SNAPSHOT</version> <groupId>de.muehlencord</groupId>
</parent> <version>1.2-SNAPSHOT</version>
</parent>
<name>shared-db</name>
<dependencies> <name>shared-db</name>
<dependency> <dependencies>
<groupId>org.apache.commons</groupId> <dependency>
<artifactId>commons-lang3</artifactId> <groupId>org.apache.commons</groupId>
<type>jar</type> <artifactId>commons-lang3</artifactId>
</dependency> <type>jar</type>
<dependency> </dependency>
<groupId>${project.groupId}</groupId> <dependency>
<artifactId>shared-util</artifactId> <groupId>${project.groupId}</groupId>
</dependency> <artifactId>shared-util</artifactId>
<!-- Testing --> </dependency>
<dependency> <dependency>
<groupId>org.junit.jupiter</groupId> <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>junit-jupiter-engine</artifactId> <artifactId>jackson-databind</artifactId>
<scope>test</scope> </dependency>
</dependency> <dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<!-- Logging --> <artifactId>jackson-datatype-jsr310</artifactId>
<dependency> </dependency>
<groupId>org.slf4j</groupId> <!-- Testing -->
<artifactId>slf4j-api</artifactId> <dependency>
</dependency> <groupId>org.junit.jupiter</groupId>
<dependency> <artifactId>junit-jupiter-engine</artifactId>
<groupId>org.slf4j</groupId> <scope>test</scope>
<artifactId>slf4j-log4j12</artifactId> </dependency>
<scope>test</scope>
</dependency> <!-- Logging -->
<dependency> <dependency>
<groupId>javax</groupId> <groupId>org.slf4j</groupId>
<artifactId>javaee-web-api</artifactId> <artifactId>slf4j-api</artifactId>
<scope>provided</scope> </dependency>
</dependency> <dependency>
</dependencies> <groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<build> <scope>test</scope>
<plugins> </dependency>
<!-- create EJB version 3.1 --> <dependency>
<plugin> <groupId>javax</groupId>
<groupId>org.apache.maven.plugins</groupId> <artifactId>javaee-web-api</artifactId>
<artifactId>maven-ejb-plugin</artifactId> <scope>provided</scope>
<configuration> </dependency>
<ejbVersion>3.1</ejbVersion> <dependency>
<excludes>
<exclude>**/persistence.xml</exclude> <!-- Dev Tools -->
</excludes> <groupId>org.projectlombok</groupId>
</configuration> <artifactId>lombok</artifactId>
</plugin> <scope>provided</scope>
</plugins> <version>1.18.12</version>
</build> </dependency>
</dependencies>
<build>
<plugins>
<!-- create EJB version 3.1 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ejb-plugin</artifactId>
<configuration>
<ejbVersion>3.1</ejbVersion>
<excludes>
<exclude>**/persistence.xml</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project> </project>

View File

@ -1,4 +1,4 @@
/* /*
* Copyright 2019 Joern Muehlencord (joern@muehlencord.de). * Copyright 2019 Joern Muehlencord (joern@muehlencord.de).
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
@ -15,77 +15,119 @@
*/ */
package de.muehlencord.shared.db; package de.muehlencord.shared.db;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import java.io.Serializable; import java.io.Serializable;
import java.util.Date; import java.time.LocalDateTime;
import java.time.ZoneOffset;
import javax.persistence.Basic; import javax.persistence.Basic;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Embeddable; import javax.persistence.Embeddable;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;
/** /**
*
* @author Joern Muehlencord (joern@muehlencord.de) * @author Joern Muehlencord (joern@muehlencord.de)
*/ */
@Embeddable @Embeddable
@Getter
@Setter
public class Audit implements Serializable { public class Audit implements Serializable {
private static final long serialVersionUID = -955765069412891842L; private static final long serialVersionUID = -955765069412891842L;
@Basic(optional = false) @Basic(optional = false)
@NotNull @NotNull
@Column(name = "created_on") @Column(name = "valid_from")
@Temporal(TemporalType.TIMESTAMP) @JsonDeserialize(using = LocalDateTimeDeserializer.class)
private Date createdOn; @JsonSerialize(using = LocalDateTimeSerializer.class)
private LocalDateTime validFrom;
@Basic(optional = false) @Column(name = "valid_to")
@NotNull @JsonDeserialize(using = LocalDateTimeDeserializer.class)
@Column(name = "created_by") @JsonSerialize(using = LocalDateTimeSerializer.class)
private String createdBy; private LocalDateTime validTo;
@Basic(optional = false) @Basic(optional = false)
@NotNull @NotNull
@Column(name = "last_updated_on") @Column(name = "created_on")
@Temporal(TemporalType.TIMESTAMP) @JsonDeserialize(using = LocalDateTimeDeserializer.class)
private Date lastUpdatedOn; @JsonSerialize(using = LocalDateTimeSerializer.class)
private LocalDateTime createdOn;
@Basic(optional = false) @Basic(optional = false)
@NotNull @NotNull
@Column(name = "last_updated_by") @Column(name = "created_by")
private String lastUpdatedBy; private String createdBy;
/* getter / setter */ @Basic(optional = false)
public Date getCreatedOn() { @NotNull
return createdOn; @Column(name = "last_updated_on")
} @JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonSerialize(using = LocalDateTimeSerializer.class)
private LocalDateTime lastUpdatedOn;
public void setCreatedOn(Date createdOn) { @Basic(optional = false)
this.createdOn = createdOn; @NotNull
} @Column(name = "last_updated_by")
private String lastUpdatedBy;
public String getCreatedBy() { public Audit() {
return createdBy; this.validFrom = LocalDateTime.now(ZoneOffset.UTC);
} this.lastUpdatedOn = LocalDateTime.now(ZoneOffset.UTC);
this.createdOn = LocalDateTime.now(ZoneOffset.UTC);
}
public void setCreatedBy(String createdBy) { public Audit withNewAudit(String userName) {
this.createdBy = createdBy; LocalDateTime now = LocalDateTime.now(ZoneOffset.UTC);
} this.setCreatedBy(userName);
this.setCreatedOn(now);
this.setLastUpdatedBy(userName);
this.setLastUpdatedOn(now);
this.setValidFrom(now);
return this;
}
public Date getLastUpdatedOn() { public Audit withEndDate() {
return lastUpdatedOn; return withEndDate (LocalDateTime.now(ZoneOffset.UTC));
} }
public void setLastUpdatedOn(Date lastUpdatedOn) { private Audit withEndDate(LocalDateTime endDate) {
this.lastUpdatedOn = lastUpdatedOn; this.setValidTo(endDate);
} return this;
}
public String getLastUpdatedBy() { public Audit withValidFrom(final LocalDateTime validFrom) {
return lastUpdatedBy; this.validFrom = validFrom;
} return this;
}
public void setLastUpdatedBy(String lastUpdatedBy) { public Audit withValidTo(final LocalDateTime validTo) {
this.lastUpdatedBy = lastUpdatedBy; this.validTo = validTo;
} return this;
}
public Audit withCreatedOn(final LocalDateTime createdOn) {
this.createdOn = createdOn;
return this;
}
public Audit withCreatedBy(final String createdBy) {
this.createdBy = createdBy;
return this;
}
public Audit withLastUpdatedOn(final LocalDateTime lastUpdatedOn) {
this.lastUpdatedOn = lastUpdatedOn;
return this;
}
public Audit withLastUpdatedBy(final String lastUpdatedBy) {
this.lastUpdatedBy = lastUpdatedBy;
return this;
}
} }

View File

@ -16,12 +16,14 @@
package de.muehlencord.shared.db; package de.muehlencord.shared.db;
import de.muehlencord.shared.util.DateUtil; import de.muehlencord.shared.util.DateUtil;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date; import java.util.Date;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
/** /**
* Does not work, cannot Spring independet inject current username - implemented in AbstractController manually * Does not work, cannot Spring independent inject current username - implemented in AbstractController manually
* @author Joern Muehlencord (joern@muehlencord.de) * @author Joern Muehlencord (joern@muehlencord.de)
*/ */
public class AuditListener { public class AuditListener {
@ -37,8 +39,7 @@ public class AuditListener {
auditable.setAudit(audit); auditable.setAudit(audit);
} }
// LocalDateTime now = LocalDateTime.now(); LocalDateTime now = LocalDateTime.now(ZoneOffset.UTC);
Date now = DateUtil.getCurrentTimeInUTC();
audit.setCreatedOn(now); audit.setCreatedOn(now);
audit.setLastUpdatedOn(now); audit.setLastUpdatedOn(now);
// audit.setCreatedBy(LoggedUser.get()); // TODO where to get the user from // audit.setCreatedBy(LoggedUser.get()); // TODO where to get the user from
@ -52,8 +53,8 @@ public class AuditListener {
public void preUpdate(Auditable auditable) { public void preUpdate(Auditable auditable) {
Audit audit = auditable.getAudit(); Audit audit = auditable.getAudit();
// LocalDateTime now = LocalDateTime.now(); LocalDateTime now = LocalDateTime.now(ZoneOffset.UTC);
Date now = DateUtil.getCurrentTimeInUTC(); // Date now = DateUtil.getCurrentTimeInUTC();
audit.setLastUpdatedOn(now); audit.setLastUpdatedOn(now);
// audit.setUpdatedBy(LoggedUser.get()); // TODO where to get the user from // audit.setUpdatedBy(LoggedUser.get()); // TODO where to get the user from
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {

View File

@ -17,6 +17,9 @@ package de.muehlencord.shared.db;
import de.muehlencord.shared.util.DateUtil; import de.muehlencord.shared.util.DateUtil;
import java.io.Serializable; import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date; import java.util.Date;
@ -219,10 +222,10 @@ public abstract class CommonAbstractController {
} }
if (onCreate) { if (onCreate) {
audit.setCreatedBy(changedBy); audit.setCreatedBy(changedBy);
audit.setCreatedOn(DateUtil.getCurrentTimeInUTC()); audit.setCreatedOn(LocalDateTime.now(ZoneOffset.UTC));
} }
audit.setLastUpdatedBy(changedBy); audit.setLastUpdatedBy(changedBy);
audit.setLastUpdatedOn(DateUtil.getCurrentTimeInUTC()); audit.setLastUpdatedOn(LocalDateTime.now(ZoneOffset.UTC));
return audit; return audit;
} }

View File

@ -0,0 +1,25 @@
package de.muehlencord.shared.db;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
@MappedSuperclass
public abstract class NumericallyIdentifiedEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}

21
pom.xml
View File

@ -155,15 +155,20 @@ limitations under the License.
<dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId> <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId> <artifactId>jackson-annotations</artifactId>
<version>2.9.9</version> <version>2.11.2</version>
<type>jar</type> <type>jar</type>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId> <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId> <artifactId>jackson-databind</artifactId>
<version>2.9.9.1</version> <version>2.11.2</version>
<type>jar</type> <type>jar</type>
</dependency> </dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.11.2</version>
</dependency>
<dependency> <dependency>
<groupId>org.apache.shiro</groupId> <groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId> <artifactId>shiro-core</artifactId>
@ -222,12 +227,20 @@ limitations under the License.
<artifactId>poi-ooxml</artifactId> <artifactId>poi-ooxml</artifactId>
<version>4.0.1</version> <version>4.0.1</version>
<!-- do not upgrade to 4.1.0, see bug https://bz.apache.org/bugzilla/show_bug.cgi?id=63845 --> <!-- do not upgrade to 4.1.0, see bug https://bz.apache.org/bugzilla/show_bug.cgi?id=63845 -->
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.inversoft</groupId> <groupId>com.inversoft</groupId>
<artifactId>prime-jwt</artifactId> <artifactId>prime-jwt</artifactId>
<version>2.0.0</version> <version>2.0.0</version>
</dependency> </dependency>
<!-- Dev Tools -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
<version>1.18.12</version>
</dependency>
<!-- Testing --> <!-- Testing -->
<dependency> <dependency>