introduced validity
This commit is contained in:
@ -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;
|
||||
|
||||
@ -97,7 +97,13 @@ 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;
|
||||
@ -113,7 +119,14 @@ 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
|
||||
}
|
||||
|
||||
|
||||
@ -15,8 +15,6 @@
|
||||
*/
|
||||
package de.muehlencord.shared.db;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Enddateable entities are not deleted but an enddate is set to "now"
|
||||
*
|
||||
@ -24,12 +22,8 @@ import java.util.Date;
|
||||
*/
|
||||
public interface EndDateable<T> {
|
||||
|
||||
Date getValidFrom();
|
||||
void setValidity(Validity v);
|
||||
|
||||
Date getValidTo();
|
||||
|
||||
void setValidFrom(Date validFrom);
|
||||
|
||||
void setValidTo(Date validTo);
|
||||
Validity getValidity();
|
||||
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
import javax.persistence.Basic;
|
||||
import javax.persistence.Column;
|
||||
@ -12,8 +11,6 @@ import javax.persistence.Id;
|
||||
import javax.persistence.NamedQueries;
|
||||
import javax.persistence.NamedQuery;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.Temporal;
|
||||
import javax.persistence.TemporalType;
|
||||
import javax.validation.constraints.Max;
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.NotNull;
|
||||
@ -69,12 +66,9 @@ public class TestEntity implements Serializable, Auditable, EndDateable<TestEnti
|
||||
@Max(value = 180)
|
||||
@Column(name = "longitude")
|
||||
private Double longitude;
|
||||
@Column(name = "valid_from")
|
||||
@Temporal(TemporalType.TIMESTAMP)
|
||||
private Date validFrom;
|
||||
@Column(name = "valid_to")
|
||||
@Temporal(TemporalType.TIMESTAMP)
|
||||
private Date validTo;
|
||||
|
||||
@Embedded
|
||||
private Validity validity;
|
||||
@Embedded
|
||||
private Audit audit;
|
||||
|
||||
@ -155,23 +149,13 @@ public class TestEntity implements Serializable, Auditable, EndDateable<TestEnti
|
||||
}
|
||||
|
||||
@Override
|
||||
public Date getValidFrom() {
|
||||
return validFrom;
|
||||
public Validity getValidity() {
|
||||
return validity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setValidFrom(Date validFrom) {
|
||||
this.validFrom = validFrom;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Date getValidTo() {
|
||||
return validTo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setValidTo(Date validTo) {
|
||||
this.validTo = validTo;
|
||||
public void setValidity(Validity validity) {
|
||||
this.validity = validity;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -184,7 +168,6 @@ public class TestEntity implements Serializable, Auditable, EndDateable<TestEnti
|
||||
this.audit = audit;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int hash = 0;
|
||||
@ -210,5 +193,4 @@ public class TestEntity implements Serializable, Auditable, EndDateable<TestEnti
|
||||
return "de.muehlencord.office.entity.party.AddressEntity[ id=" + id + " ]";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user