introduced validity
This commit is contained in:
@ -19,6 +19,7 @@ import java.io.Serializable;
|
|||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import javax.persistence.Basic;
|
import javax.persistence.Basic;
|
||||||
import javax.persistence.Column;
|
import javax.persistence.Column;
|
||||||
|
import javax.persistence.Embeddable;
|
||||||
import javax.persistence.Temporal;
|
import javax.persistence.Temporal;
|
||||||
import javax.persistence.TemporalType;
|
import javax.persistence.TemporalType;
|
||||||
import javax.validation.constraints.NotNull;
|
import javax.validation.constraints.NotNull;
|
||||||
@ -27,6 +28,7 @@ import javax.validation.constraints.NotNull;
|
|||||||
*
|
*
|
||||||
* @author joern.muehlencord
|
* @author joern.muehlencord
|
||||||
*/
|
*/
|
||||||
|
@Embeddable
|
||||||
public class Audit implements Serializable {
|
public class Audit implements Serializable {
|
||||||
|
|
||||||
private static final long serialVersionUID = -955765069412891842L;
|
private static final long serialVersionUID = -955765069412891842L;
|
||||||
|
|||||||
@ -97,7 +97,13 @@ public abstract class CommonAbstractController {
|
|||||||
}
|
}
|
||||||
if (EndDateable.class.isAssignableFrom(entity.getClass())) {
|
if (EndDateable.class.isAssignableFrom(entity.getClass())) {
|
||||||
EndDateable endDateable = (EndDateable) entity;
|
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);
|
em.persist(entity);
|
||||||
return entity;
|
return entity;
|
||||||
@ -113,7 +119,14 @@ public abstract class CommonAbstractController {
|
|||||||
if (EndDateable.class.isAssignableFrom(currentEntity.getClass())) {
|
if (EndDateable.class.isAssignableFrom(currentEntity.getClass())) {
|
||||||
// end date existing entity
|
// end date existing entity
|
||||||
EndDateable endDateable = (EndDateable) currentEntity;
|
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
|
// and create new entity instead
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,10 +137,10 @@ public abstract class CommonAbstractController {
|
|||||||
@Transactional
|
@Transactional
|
||||||
@Lock(LockType.WRITE)
|
@Lock(LockType.WRITE)
|
||||||
public <T extends Serializable> T update(T entity, String updatedBy) throws ControllerException {
|
public <T extends Serializable> T update(T entity, String updatedBy) throws ControllerException {
|
||||||
T currentEntity = executeUpdate(entity,updatedBy);
|
T currentEntity = executeUpdate(entity, updatedBy);
|
||||||
T newEntity = EntityUtil.cloneToNewEntity(currentEntity);
|
T newEntity = EntityUtil.cloneToNewEntity(currentEntity);
|
||||||
if (EndDateable.class.isAssignableFrom(entity.getClass())) {
|
if (EndDateable.class.isAssignableFrom(entity.getClass())) {
|
||||||
em.merge (currentEntity);
|
em.merge(currentEntity);
|
||||||
|
|
||||||
return create(newEntity, updatedBy);
|
return create(newEntity, updatedBy);
|
||||||
} else {
|
} 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 the entity is endDateable just set the validToDate to now intead of executing the deletion
|
||||||
if (EndDateable.class.isAssignableFrom(entity.getClass())) {
|
if (EndDateable.class.isAssignableFrom(entity.getClass())) {
|
||||||
T currentEntity = executeUpdate(entity, deletedBy);
|
T currentEntity = executeUpdate(entity, deletedBy);
|
||||||
em.merge (currentEntity);
|
em.merge(currentEntity);
|
||||||
} else {
|
} else {
|
||||||
em.remove(attach(entity));
|
em.remove(attach(entity));
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,8 +15,6 @@
|
|||||||
*/
|
*/
|
||||||
package de.muehlencord.shared.db;
|
package de.muehlencord.shared.db;
|
||||||
|
|
||||||
import java.util.Date;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enddateable entities are not deleted but an enddate is set to "now"
|
* Enddateable entities are not deleted but an enddate is set to "now"
|
||||||
*
|
*
|
||||||
@ -24,12 +22,8 @@ import java.util.Date;
|
|||||||
*/
|
*/
|
||||||
public interface EndDateable<T> {
|
public interface EndDateable<T> {
|
||||||
|
|
||||||
Date getValidFrom();
|
void setValidity(Validity v);
|
||||||
|
|
||||||
Date getValidTo();
|
Validity getValidity();
|
||||||
|
|
||||||
void setValidFrom(Date validFrom);
|
|
||||||
|
|
||||||
void setValidTo(Date validTo);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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>
|
* <ul>
|
||||||
* <li>the Id field of the entity is set to null</li>
|
* <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>
|
* <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
|
// set new valid dates if class is enddateable
|
||||||
if (EndDateable.class.isAssignableFrom(newEntity.getClass())) {
|
if (EndDateable.class.isAssignableFrom(newEntity.getClass())) {
|
||||||
((EndDateable) newEntity).setValidFrom(DateUtil.getCurrentTimeInUTC());
|
Validity validity = new Validity();
|
||||||
((EndDateable) newEntity).setValidTo(null);
|
validity.setValidFrom(DateUtil.getCurrentTimeInUTC());
|
||||||
|
validity.setValidTo(null);
|
||||||
|
((EndDateable) newEntity).setValidity(validity);
|
||||||
}
|
}
|
||||||
return newEntity;
|
return newEntity;
|
||||||
}
|
}
|
||||||
|
|||||||
49
db/src/main/java/de/muehlencord/shared/db/Validity.java
Normal file
49
db/src/main/java/de/muehlencord/shared/db/Validity.java
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,7 +1,6 @@
|
|||||||
package de.muehlencord.shared.db;
|
package de.muehlencord.shared.db;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.Date;
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import javax.persistence.Basic;
|
import javax.persistence.Basic;
|
||||||
import javax.persistence.Column;
|
import javax.persistence.Column;
|
||||||
@ -12,8 +11,6 @@ import javax.persistence.Id;
|
|||||||
import javax.persistence.NamedQueries;
|
import javax.persistence.NamedQueries;
|
||||||
import javax.persistence.NamedQuery;
|
import javax.persistence.NamedQuery;
|
||||||
import javax.persistence.Table;
|
import javax.persistence.Table;
|
||||||
import javax.persistence.Temporal;
|
|
||||||
import javax.persistence.TemporalType;
|
|
||||||
import javax.validation.constraints.Max;
|
import javax.validation.constraints.Max;
|
||||||
import javax.validation.constraints.Min;
|
import javax.validation.constraints.Min;
|
||||||
import javax.validation.constraints.NotNull;
|
import javax.validation.constraints.NotNull;
|
||||||
@ -69,12 +66,9 @@ public class TestEntity implements Serializable, Auditable, EndDateable<TestEnti
|
|||||||
@Max(value = 180)
|
@Max(value = 180)
|
||||||
@Column(name = "longitude")
|
@Column(name = "longitude")
|
||||||
private Double longitude;
|
private Double longitude;
|
||||||
@Column(name = "valid_from")
|
|
||||||
@Temporal(TemporalType.TIMESTAMP)
|
@Embedded
|
||||||
private Date validFrom;
|
private Validity validity;
|
||||||
@Column(name = "valid_to")
|
|
||||||
@Temporal(TemporalType.TIMESTAMP)
|
|
||||||
private Date validTo;
|
|
||||||
@Embedded
|
@Embedded
|
||||||
private Audit audit;
|
private Audit audit;
|
||||||
|
|
||||||
@ -155,23 +149,13 @@ public class TestEntity implements Serializable, Auditable, EndDateable<TestEnti
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Date getValidFrom() {
|
public Validity getValidity() {
|
||||||
return validFrom;
|
return validity;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setValidFrom(Date validFrom) {
|
public void setValidity(Validity validity) {
|
||||||
this.validFrom = validFrom;
|
this.validity = validity;
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Date getValidTo() {
|
|
||||||
return validTo;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setValidTo(Date validTo) {
|
|
||||||
this.validTo = validTo;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -184,7 +168,6 @@ public class TestEntity implements Serializable, Auditable, EndDateable<TestEnti
|
|||||||
this.audit = audit;
|
this.audit = audit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
int hash = 0;
|
int hash = 0;
|
||||||
@ -210,5 +193,4 @@ public class TestEntity implements Serializable, Auditable, EndDateable<TestEnti
|
|||||||
return "de.muehlencord.office.entity.party.AddressEntity[ id=" + id + " ]";
|
return "de.muehlencord.office.entity.party.AddressEntity[ id=" + id + " ]";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user