introduced validity

This commit is contained in:
2019-06-23 17:10:30 +02:00
parent 1e5e15cda0
commit bf590223b8
6 changed files with 89 additions and 46 deletions

View File

@ -19,6 +19,7 @@ import java.io.Serializable;
import java.util.Date;
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;
@ -27,6 +28,7 @@ import javax.validation.constraints.NotNull;
*
* @author joern.muehlencord
*/
@Embeddable
public class Audit implements Serializable {
private static final long serialVersionUID = -955765069412891842L;

View File

@ -97,14 +97,20 @@ public abstract class CommonAbstractController {
}
if (EndDateable.class.isAssignableFrom(entity.getClass())) {
EndDateable endDateable = (EndDateable) entity;
endDateable.setValidFrom(DateUtil.getCurrentTimeInUTC());
if (endDateable.getValidity() == null) {
Validity validity = new Validity();
endDateable.setValidity(validity);
}
if (endDateable.getValidity().getValidFrom() == null) {
endDateable.getValidity().setValidFrom(DateUtil.getCurrentTimeInUTC());
}
}
em.persist(entity);
return entity;
}
private <T extends Serializable> T executeUpdate(T entity, String updatedBy) throws ControllerException {
T currentEntity = attach(entity);
private <T extends Serializable> T executeUpdate(T entity, String updatedBy) throws ControllerException {
T currentEntity = attach(entity);
if (Auditable.class.isAssignableFrom(currentEntity.getClass())) {
Audit audit = ((Auditable) currentEntity).getAudit();
((Auditable) currentEntity).setAudit(applyAuditChanges(audit, false, updatedBy));
@ -113,21 +119,28 @@ public abstract class CommonAbstractController {
if (EndDateable.class.isAssignableFrom(currentEntity.getClass())) {
// end date existing entity
EndDateable endDateable = (EndDateable) currentEntity;
endDateable.setValidTo(DateUtil.getCurrentTimeInUTC());
Validity validity = endDateable.getValidity();
if (validity == null) {
validity = new Validity();
validity.setValidFrom(DateUtil.getCurrentTimeInUTC());
}
validity.setValidTo(DateUtil.getCurrentTimeInUTC());
endDateable.setValidity(validity);
// and create new entity instead
}
return currentEntity;
}
@TransactionAttribute(TransactionAttributeType.REQUIRED)
@Transactional
@Lock(LockType.WRITE)
public <T extends Serializable> T update(T entity, String updatedBy) throws ControllerException {
T currentEntity = executeUpdate(entity,updatedBy);
public <T extends Serializable> T update(T entity, String updatedBy) throws ControllerException {
T currentEntity = executeUpdate(entity, updatedBy);
T newEntity = EntityUtil.cloneToNewEntity(currentEntity);
if (EndDateable.class.isAssignableFrom(entity.getClass())) {
em.merge (currentEntity);
em.merge(currentEntity);
return create(newEntity, updatedBy);
} else {
@ -144,7 +157,7 @@ public abstract class CommonAbstractController {
// if the entity is endDateable just set the validToDate to now intead of executing the deletion
if (EndDateable.class.isAssignableFrom(entity.getClass())) {
T currentEntity = executeUpdate(entity, deletedBy);
em.merge (currentEntity);
em.merge(currentEntity);
} else {
em.remove(attach(entity));
}

View File

@ -15,21 +15,15 @@
*/
package de.muehlencord.shared.db;
import java.util.Date;
/**
* Enddateable entities are not deleted but an enddate is set to "now"
*
* @author Joern Muehlencord <joern at muehlencord.de>
*/
public interface EndDateable<T> {
Date getValidFrom();
Date getValidTo();
void setValidity(Validity v);
void setValidFrom(Date validFrom);
void setValidTo(Date validTo);
Validity getValidity();
}

View File

@ -82,7 +82,8 @@ public class EntityUtil {
}
/**
* clones the given entity and updates related fields so the entity appears as new.The following changes are executed
* clones the given entity and updates related fields so the entity appears as new.The following changes are
* executed
* <ul>
* <li>the Id field of the entity is set to null</li>
* <li>if the entity is auditable, the audit is set to null.</li>
@ -107,8 +108,10 @@ public class EntityUtil {
// set new valid dates if class is enddateable
if (EndDateable.class.isAssignableFrom(newEntity.getClass())) {
((EndDateable) newEntity).setValidFrom(DateUtil.getCurrentTimeInUTC());
((EndDateable) newEntity).setValidTo(null);
Validity validity = new Validity();
validity.setValidFrom(DateUtil.getCurrentTimeInUTC());
validity.setValidTo(null);
((EndDateable) newEntity).setValidity(validity);
}
return newEntity;
}

View File

@ -0,0 +1,49 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package de.muehlencord.shared.db;
import java.io.Serializable;
import java.util.Date;
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;
/**
*
* @author Joern Muehlencord <joern at muehlencord.de>
*/
@Embeddable
public class Validity implements Serializable {
@Basic(optional = false)
@NotNull
@Column(name = "valid_from")
@Temporal(TemporalType.TIMESTAMP)
private Date validFrom;
@Column(name = "valid_to")
@Temporal(TemporalType.TIMESTAMP)
private Date validTo;
public Date getValidFrom() {
return validFrom;
}
public void setValidFrom(Date validFrom) {
this.validFrom = validFrom;
}
public Date getValidTo() {
return validTo;
}
public void setValidTo(Date validTo) {
this.validTo = validTo;
}
}