started to introduce embeded audit entity support
This commit is contained in:
@ -18,7 +18,6 @@ package de.muehlencord.shared.db;
|
|||||||
import de.muehlencord.shared.util.DateUtil;
|
import de.muehlencord.shared.util.DateUtil;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Date;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
@ -191,14 +190,14 @@ public abstract class AbstractController<T> {
|
|||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void applyUpdateableChanges(Updateable updateable, boolean onCreate, String updatedBy) throws ControllerException {
|
// public void applyUpdateableChanges(Updateable updateable, boolean onCreate, String updatedBy) throws ControllerException {
|
||||||
if (onCreate) {
|
// if (onCreate) {
|
||||||
updateable.setCreatedBy(updatedBy);
|
// updateable.setCreatedBy(updatedBy);
|
||||||
updateable.setCreatedOn(new Date());
|
// updateable.setCreatedOn(new Date());
|
||||||
}
|
// }
|
||||||
updateable.setLastUpdatedBy(updatedBy);
|
// updateable.setLastUpdatedBy(updatedBy);
|
||||||
updateable.setLastUpdatedOn(new Date());
|
// updateable.setLastUpdatedOn(new Date());
|
||||||
}
|
// }
|
||||||
|
|
||||||
public T attach(T entity) {
|
public T attach(T entity) {
|
||||||
return em.merge(entity);
|
return em.merge(entity);
|
||||||
@ -208,10 +207,10 @@ public abstract class AbstractController<T> {
|
|||||||
@Transactional
|
@Transactional
|
||||||
@Lock(LockType.WRITE)
|
@Lock(LockType.WRITE)
|
||||||
public T create(T entity, String createdBy) throws ControllerException {
|
public T create(T entity, String createdBy) throws ControllerException {
|
||||||
if (Updateable.class.isAssignableFrom(entity.getClass())) {
|
// if (Updateable.class.isAssignableFrom(entity.getClass())) {
|
||||||
Updateable updateable = (Updateable) entity;
|
// Updateable updateable = (Updateable) entity;
|
||||||
applyUpdateableChanges(updateable, true, createdBy);
|
// applyUpdateableChanges(updateable, true, createdBy);
|
||||||
}
|
// }
|
||||||
if (EndDateable.class.isAssignableFrom(entity.getClass())) {
|
if (EndDateable.class.isAssignableFrom(entity.getClass())) {
|
||||||
EndDateable endDateable = (EndDateable) entity;
|
EndDateable endDateable = (EndDateable) entity;
|
||||||
endDateable.setValidFrom(DateUtil.getCurrentTimeInUTC());
|
endDateable.setValidFrom(DateUtil.getCurrentTimeInUTC());
|
||||||
@ -224,10 +223,10 @@ public abstract class AbstractController<T> {
|
|||||||
@Transactional
|
@Transactional
|
||||||
@Lock(LockType.WRITE)
|
@Lock(LockType.WRITE)
|
||||||
public T update(T entity, String updatedBy) throws ControllerException {
|
public T update(T entity, String updatedBy) throws ControllerException {
|
||||||
if (Updateable.class.isAssignableFrom(entity.getClass())) {
|
// if (Updateable.class.isAssignableFrom(entity.getClass())) {
|
||||||
Updateable updateable = (Updateable) entity;
|
// Updateable updateable = (Updateable) entity;
|
||||||
applyUpdateableChanges(updateable, false, updatedBy);
|
// applyUpdateableChanges(updateable, false, updatedBy);
|
||||||
}
|
// }
|
||||||
return em.merge(entity);
|
return em.merge(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -51,10 +51,10 @@ public abstract class AbstractEnddateableController<T extends EndDateable<T>> ex
|
|||||||
@Override
|
@Override
|
||||||
public void delete(T entity, String deletedBy) throws ControllerException {
|
public void delete(T entity, String deletedBy) throws ControllerException {
|
||||||
T entityToUpdate = attach(entity);
|
T entityToUpdate = attach(entity);
|
||||||
if (Updateable.class.isAssignableFrom(entityToUpdate.getClass())) {
|
// if (Updateable.class.isAssignableFrom(entityToUpdate.getClass())) {
|
||||||
Updateable updateable = (Updateable) entityToUpdate;
|
// Updateable updateable = (Updateable) entityToUpdate;
|
||||||
applyUpdateableChanges(updateable, false, deletedBy);
|
// applyUpdateableChanges(updateable, false, deletedBy);
|
||||||
}
|
// }
|
||||||
entityToUpdate.setValidTo(DateUtil.getCurrentTimeInUTC());
|
entityToUpdate.setValidTo(DateUtil.getCurrentTimeInUTC());
|
||||||
em.merge(entityToUpdate);
|
em.merge(entityToUpdate);
|
||||||
}
|
}
|
||||||
|
|||||||
88
db/src/main/java/de/muehlencord/shared/db/Audit.java
Normal file
88
db/src/main/java/de/muehlencord/shared/db/Audit.java
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2019 Joern Muehlencord <joern at muehlencord.de>.
|
||||||
|
*
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
67
db/src/main/java/de/muehlencord/shared/db/AuditListener.java
Normal file
67
db/src/main/java/de/muehlencord/shared/db/AuditListener.java
Normal file
@ -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");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
27
db/src/main/java/de/muehlencord/shared/db/Auditable.java
Normal file
27
db/src/main/java/de/muehlencord/shared/db/Auditable.java
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2019 Joern Muehlencord <joern at muehlencord.de>.
|
||||||
|
*
|
||||||
|
* 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);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user