From 88d250989300afb84f98e0bd0bbd07156b93d3c8 Mon Sep 17 00:00:00 2001 From: Joern Muehlencord Date: Thu, 20 Jun 2019 13:11:07 +0200 Subject: [PATCH] started to introduce embeded audit entity support --- .../shared/db/AbstractController.java | 33 ++++--- .../db/AbstractEnddateableController.java | 8 +- .../java/de/muehlencord/shared/db/Audit.java | 88 +++++++++++++++++++ .../muehlencord/shared/db/AuditListener.java | 67 ++++++++++++++ .../de/muehlencord/shared/db/Auditable.java | 27 ++++++ 5 files changed, 202 insertions(+), 21 deletions(-) create mode 100644 db/src/main/java/de/muehlencord/shared/db/Audit.java create mode 100644 db/src/main/java/de/muehlencord/shared/db/AuditListener.java create mode 100644 db/src/main/java/de/muehlencord/shared/db/Auditable.java diff --git a/db/src/main/java/de/muehlencord/shared/db/AbstractController.java b/db/src/main/java/de/muehlencord/shared/db/AbstractController.java index 1112d53..b545046 100644 --- a/db/src/main/java/de/muehlencord/shared/db/AbstractController.java +++ b/db/src/main/java/de/muehlencord/shared/db/AbstractController.java @@ -18,7 +18,6 @@ package de.muehlencord.shared.db; import de.muehlencord.shared.util.DateUtil; import java.util.ArrayList; import java.util.Arrays; -import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Locale; @@ -191,14 +190,14 @@ public abstract class AbstractController { return path; } - public void applyUpdateableChanges(Updateable updateable, boolean onCreate, String updatedBy) throws ControllerException { - if (onCreate) { - updateable.setCreatedBy(updatedBy); - updateable.setCreatedOn(new Date()); - } - updateable.setLastUpdatedBy(updatedBy); - updateable.setLastUpdatedOn(new Date()); - } +// public void applyUpdateableChanges(Updateable updateable, boolean onCreate, String updatedBy) throws ControllerException { +// if (onCreate) { +// updateable.setCreatedBy(updatedBy); +// updateable.setCreatedOn(new Date()); +// } +// updateable.setLastUpdatedBy(updatedBy); +// updateable.setLastUpdatedOn(new Date()); +// } public T attach(T entity) { return em.merge(entity); @@ -208,10 +207,10 @@ public abstract class AbstractController { @Transactional @Lock(LockType.WRITE) public T create(T entity, String createdBy) throws ControllerException { - if (Updateable.class.isAssignableFrom(entity.getClass())) { - Updateable updateable = (Updateable) entity; - applyUpdateableChanges(updateable, true, createdBy); - } +// if (Updateable.class.isAssignableFrom(entity.getClass())) { +// Updateable updateable = (Updateable) entity; +// applyUpdateableChanges(updateable, true, createdBy); +// } if (EndDateable.class.isAssignableFrom(entity.getClass())) { EndDateable endDateable = (EndDateable) entity; endDateable.setValidFrom(DateUtil.getCurrentTimeInUTC()); @@ -224,10 +223,10 @@ public abstract class AbstractController { @Transactional @Lock(LockType.WRITE) public T update(T entity, String updatedBy) throws ControllerException { - if (Updateable.class.isAssignableFrom(entity.getClass())) { - Updateable updateable = (Updateable) entity; - applyUpdateableChanges(updateable, false, updatedBy); - } +// if (Updateable.class.isAssignableFrom(entity.getClass())) { +// Updateable updateable = (Updateable) entity; +// applyUpdateableChanges(updateable, false, updatedBy); +// } return em.merge(entity); } diff --git a/db/src/main/java/de/muehlencord/shared/db/AbstractEnddateableController.java b/db/src/main/java/de/muehlencord/shared/db/AbstractEnddateableController.java index 031678d..4f4dca5 100644 --- a/db/src/main/java/de/muehlencord/shared/db/AbstractEnddateableController.java +++ b/db/src/main/java/de/muehlencord/shared/db/AbstractEnddateableController.java @@ -51,10 +51,10 @@ public abstract class AbstractEnddateableController> ex @Override public void delete(T entity, String deletedBy) throws ControllerException { T entityToUpdate = attach(entity); - if (Updateable.class.isAssignableFrom(entityToUpdate.getClass())) { - Updateable updateable = (Updateable) entityToUpdate; - applyUpdateableChanges(updateable, false, deletedBy); - } +// if (Updateable.class.isAssignableFrom(entityToUpdate.getClass())) { +// Updateable updateable = (Updateable) entityToUpdate; +// applyUpdateableChanges(updateable, false, deletedBy); +// } entityToUpdate.setValidTo(DateUtil.getCurrentTimeInUTC()); em.merge(entityToUpdate); } diff --git a/db/src/main/java/de/muehlencord/shared/db/Audit.java b/db/src/main/java/de/muehlencord/shared/db/Audit.java new file mode 100644 index 0000000..5ed5564 --- /dev/null +++ b/db/src/main/java/de/muehlencord/shared/db/Audit.java @@ -0,0 +1,88 @@ +/* + * Copyright 2019 Joern Muehlencord . + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package de.muehlencord.shared.db; + +import java.util.Date; +import javax.persistence.Basic; +import javax.persistence.Column; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; +import javax.validation.constraints.NotNull; + +/** + * + * @author joern.muehlencord + */ +public class Audit { + + private static final long serialVersionUID = 971294035102346924L; + + @Basic(optional = false) + @NotNull + @Column(name = "created_on") + @Temporal(TemporalType.TIMESTAMP) + private Date createdOn; + + @Basic(optional = false) + @NotNull + @Column(name = "created_by") + private String createdBy; + + @Basic(optional = false) + @NotNull + @Column(name = "last_updated_on") + @Temporal(TemporalType.TIMESTAMP) + private Date lastUpdatedOn; + + @Basic(optional = false) + @NotNull + @Column(name = "last_updated_by") + private String lastUpdatedBy; + + /* getter / setter */ + public Date getCreatedOn() { + return createdOn; + } + + public void setCreatedOn(Date createdOn) { + this.createdOn = createdOn; + } + + public String getCreatedBy() { + return createdBy; + } + + public void setCreatedBy(String createdBy) { + this.createdBy = createdBy; + } + + public Date getLastUpdatedOn() { + return lastUpdatedOn; + } + + public void setLastUpdatedOn(Date lastUpdatedOn) { + this.lastUpdatedOn = lastUpdatedOn; + } + + public String getLastUpdatedBy() { + return lastUpdatedBy; + } + + public void setLastUpdatedBy(String lastUpdatedBy) { + this.lastUpdatedBy = lastUpdatedBy; + } + +} diff --git a/db/src/main/java/de/muehlencord/shared/db/AuditListener.java b/db/src/main/java/de/muehlencord/shared/db/AuditListener.java new file mode 100644 index 0000000..797cf5f --- /dev/null +++ b/db/src/main/java/de/muehlencord/shared/db/AuditListener.java @@ -0,0 +1,67 @@ +/* + * Copyright 2019 joern.muehlencord. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package de.muehlencord.shared.db; + +import de.muehlencord.shared.util.DateUtil; +import java.util.Date; +import javax.persistence.PrePersist; +import javax.persistence.PreUpdate; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + * @author joern.muehlencord + */ +public class AuditListener { + + private static final Logger LOGGER = LoggerFactory.getLogger(AuditListener.class); + + @PrePersist + public void prePersist(Auditable auditable) { + Audit audit = auditable.getAudit(); + + if (audit == null) { + audit = new Audit(); + auditable.setAudit(audit); + } + +// LocalDateTime now = LocalDateTime.now(); + Date now = DateUtil.getCurrentTimeInUTC(); + audit.setCreatedOn(now); + audit.setLastUpdatedOn(now); +// audit.setCreatedBy(LoggedUser.get()); // TODO where to get the user from + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("prePersist executed"); + } + + } + + @PreUpdate + public void preUpdate(Auditable auditable) { + Audit audit = auditable.getAudit(); + +// LocalDateTime now = LocalDateTime.now(); + Date now = DateUtil.getCurrentTimeInUTC(); + audit.setLastUpdatedOn(now); +// audit.setUpdatedBy(LoggedUser.get()); // TODO where to get the user from + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("preUpdate executed"); + } + + } + +} diff --git a/db/src/main/java/de/muehlencord/shared/db/Auditable.java b/db/src/main/java/de/muehlencord/shared/db/Auditable.java new file mode 100644 index 0000000..7fda0da --- /dev/null +++ b/db/src/main/java/de/muehlencord/shared/db/Auditable.java @@ -0,0 +1,27 @@ +/* + * Copyright 2019 Joern Muehlencord . + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package de.muehlencord.shared.db; + +/** + * + * @author joern.muehlencord + */ +public interface Auditable { + + Audit getAudit(); + + void setAudit(Audit audit); +}