improved Audit
This commit is contained in:
19
db/pom.xml
19
db/pom.xml
@ -15,7 +15,8 @@ See the License for the specific language governing permissions and
|
||||
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"
|
||||
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>
|
||||
@ -39,6 +40,14 @@ limitations under the License.
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>shared-util</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.datatype</groupId>
|
||||
<artifactId>jackson-datatype-jsr310</artifactId>
|
||||
</dependency>
|
||||
<!-- Testing -->
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
@ -61,6 +70,14 @@ limitations under the License.
|
||||
<artifactId>javaee-web-api</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
||||
<!-- Dev Tools -->
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<scope>provided</scope>
|
||||
<version>1.18.12</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@ -15,29 +15,48 @@
|
||||
*/
|
||||
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.util.Date;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
import javax.persistence.Basic;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Embeddable;
|
||||
import javax.persistence.Temporal;
|
||||
import javax.persistence.TemporalType;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Joern Muehlencord (joern@muehlencord.de)
|
||||
*/
|
||||
@Embeddable
|
||||
@Getter
|
||||
@Setter
|
||||
public class Audit implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = -955765069412891842L;
|
||||
|
||||
@Basic(optional = false)
|
||||
@NotNull
|
||||
@Column(name = "valid_from")
|
||||
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
|
||||
@JsonSerialize(using = LocalDateTimeSerializer.class)
|
||||
private LocalDateTime validFrom;
|
||||
|
||||
@Column(name = "valid_to")
|
||||
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
|
||||
@JsonSerialize(using = LocalDateTimeSerializer.class)
|
||||
private LocalDateTime validTo;
|
||||
|
||||
@Basic(optional = false)
|
||||
@NotNull
|
||||
@Column(name = "created_on")
|
||||
@Temporal(TemporalType.TIMESTAMP)
|
||||
private Date createdOn;
|
||||
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
|
||||
@JsonSerialize(using = LocalDateTimeSerializer.class)
|
||||
private LocalDateTime createdOn;
|
||||
|
||||
@Basic(optional = false)
|
||||
@NotNull
|
||||
@ -47,45 +66,68 @@ public class Audit implements Serializable {
|
||||
@Basic(optional = false)
|
||||
@NotNull
|
||||
@Column(name = "last_updated_on")
|
||||
@Temporal(TemporalType.TIMESTAMP)
|
||||
private Date lastUpdatedOn;
|
||||
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
|
||||
@JsonSerialize(using = LocalDateTimeSerializer.class)
|
||||
private LocalDateTime lastUpdatedOn;
|
||||
|
||||
@Basic(optional = false)
|
||||
@NotNull
|
||||
@Column(name = "last_updated_by")
|
||||
private String lastUpdatedBy;
|
||||
|
||||
/* getter / setter */
|
||||
public Date getCreatedOn() {
|
||||
return createdOn;
|
||||
public Audit() {
|
||||
this.validFrom = LocalDateTime.now(ZoneOffset.UTC);
|
||||
this.lastUpdatedOn = LocalDateTime.now(ZoneOffset.UTC);
|
||||
this.createdOn = LocalDateTime.now(ZoneOffset.UTC);
|
||||
}
|
||||
|
||||
public void setCreatedOn(Date createdOn) {
|
||||
public Audit withNewAudit(String userName) {
|
||||
LocalDateTime now = LocalDateTime.now(ZoneOffset.UTC);
|
||||
this.setCreatedBy(userName);
|
||||
this.setCreatedOn(now);
|
||||
this.setLastUpdatedBy(userName);
|
||||
this.setLastUpdatedOn(now);
|
||||
this.setValidFrom(now);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Audit withEndDate() {
|
||||
return withEndDate (LocalDateTime.now(ZoneOffset.UTC));
|
||||
}
|
||||
|
||||
private Audit withEndDate(LocalDateTime endDate) {
|
||||
this.setValidTo(endDate);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Audit withValidFrom(final LocalDateTime validFrom) {
|
||||
this.validFrom = validFrom;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Audit withValidTo(final LocalDateTime validTo) {
|
||||
this.validTo = validTo;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Audit withCreatedOn(final LocalDateTime createdOn) {
|
||||
this.createdOn = createdOn;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getCreatedBy() {
|
||||
return createdBy;
|
||||
}
|
||||
|
||||
public void setCreatedBy(String createdBy) {
|
||||
public Audit withCreatedBy(final String createdBy) {
|
||||
this.createdBy = createdBy;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Date getLastUpdatedOn() {
|
||||
return lastUpdatedOn;
|
||||
}
|
||||
|
||||
public void setLastUpdatedOn(Date lastUpdatedOn) {
|
||||
public Audit withLastUpdatedOn(final LocalDateTime lastUpdatedOn) {
|
||||
this.lastUpdatedOn = lastUpdatedOn;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getLastUpdatedBy() {
|
||||
return lastUpdatedBy;
|
||||
}
|
||||
|
||||
public void setLastUpdatedBy(String lastUpdatedBy) {
|
||||
public Audit withLastUpdatedBy(final String lastUpdatedBy) {
|
||||
this.lastUpdatedBy = lastUpdatedBy;
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -16,12 +16,14 @@
|
||||
package de.muehlencord.shared.db;
|
||||
|
||||
import de.muehlencord.shared.util.DateUtil;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.Date;
|
||||
import org.slf4j.Logger;
|
||||
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)
|
||||
*/
|
||||
public class AuditListener {
|
||||
@ -37,8 +39,7 @@ public class AuditListener {
|
||||
auditable.setAudit(audit);
|
||||
}
|
||||
|
||||
// LocalDateTime now = LocalDateTime.now();
|
||||
Date now = DateUtil.getCurrentTimeInUTC();
|
||||
LocalDateTime now = LocalDateTime.now(ZoneOffset.UTC);
|
||||
audit.setCreatedOn(now);
|
||||
audit.setLastUpdatedOn(now);
|
||||
// audit.setCreatedBy(LoggedUser.get()); // TODO where to get the user from
|
||||
@ -52,8 +53,8 @@ public class AuditListener {
|
||||
public void preUpdate(Auditable auditable) {
|
||||
Audit audit = auditable.getAudit();
|
||||
|
||||
// LocalDateTime now = LocalDateTime.now();
|
||||
Date now = DateUtil.getCurrentTimeInUTC();
|
||||
LocalDateTime now = LocalDateTime.now(ZoneOffset.UTC);
|
||||
// Date now = DateUtil.getCurrentTimeInUTC();
|
||||
audit.setLastUpdatedOn(now);
|
||||
// audit.setUpdatedBy(LoggedUser.get()); // TODO where to get the user from
|
||||
if (LOGGER.isDebugEnabled()) {
|
||||
|
||||
@ -17,6 +17,9 @@ package de.muehlencord.shared.db;
|
||||
|
||||
import de.muehlencord.shared.util.DateUtil;
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
@ -219,10 +222,10 @@ public abstract class CommonAbstractController {
|
||||
}
|
||||
if (onCreate) {
|
||||
audit.setCreatedBy(changedBy);
|
||||
audit.setCreatedOn(DateUtil.getCurrentTimeInUTC());
|
||||
audit.setCreatedOn(LocalDateTime.now(ZoneOffset.UTC));
|
||||
}
|
||||
audit.setLastUpdatedBy(changedBy);
|
||||
audit.setLastUpdatedOn(DateUtil.getCurrentTimeInUTC());
|
||||
audit.setLastUpdatedOn(LocalDateTime.now(ZoneOffset.UTC));
|
||||
|
||||
return audit;
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
17
pom.xml
17
pom.xml
@ -155,15 +155,20 @@ limitations under the License.
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-annotations</artifactId>
|
||||
<version>2.9.9</version>
|
||||
<version>2.11.2</version>
|
||||
<type>jar</type>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>2.9.9.1</version>
|
||||
<version>2.11.2</version>
|
||||
<type>jar</type>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.datatype</groupId>
|
||||
<artifactId>jackson-datatype-jsr310</artifactId>
|
||||
<version>2.11.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.shiro</groupId>
|
||||
<artifactId>shiro-core</artifactId>
|
||||
@ -229,6 +234,14 @@ limitations under the License.
|
||||
<version>2.0.0</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Dev Tools -->
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<scope>provided</scope>
|
||||
<version>1.18.12</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Testing -->
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
|
||||
Reference in New Issue
Block a user