fixed entity duplication when trying to delete

This commit is contained in:
2019-06-22 16:22:29 +02:00
parent 40acbcd6ac
commit 1e5e15cda0

View File

@ -103,23 +103,31 @@ public abstract class CommonAbstractController {
return 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));
}
if (EndDateable.class.isAssignableFrom(currentEntity.getClass())) {
// end date existing entity
EndDateable endDateable = (EndDateable) currentEntity;
endDateable.setValidTo(DateUtil.getCurrentTimeInUTC());
// 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 = attach(entity);
public <T extends Serializable> T update(T entity, String updatedBy) throws ControllerException {
T currentEntity = executeUpdate(entity,updatedBy);
T newEntity = EntityUtil.cloneToNewEntity(currentEntity);
if (Auditable.class.isAssignableFrom(entity.getClass())) {
Audit audit = ((Auditable) entity).getAudit();
((Auditable) entity).setAudit(applyAuditChanges(audit, false, updatedBy));
}
if (EndDateable.class.isAssignableFrom(entity.getClass())) {
// end date existing entity
EndDateable endDateable = (EndDateable) entity;
endDateable.setValidTo(DateUtil.getCurrentTimeInUTC());
em.merge(entity);
// and create new entity instead
em.merge (currentEntity);
return create(newEntity, updatedBy);
} else {
@ -135,7 +143,8 @@ public abstract class CommonAbstractController {
public <T extends Serializable> void delete(T entity, String deletedBy) throws ControllerException {
// if the entity is endDateable just set the validToDate to now intead of executing the deletion
if (EndDateable.class.isAssignableFrom(entity.getClass())) {
update(entity, deletedBy);
T currentEntity = executeUpdate(entity, deletedBy);
em.merge (currentEntity);
} else {
em.remove(attach(entity));
}
@ -165,7 +174,8 @@ public abstract class CommonAbstractController {
* @param cb the criteria builder to use
* @param root the root of the object to search for
* @param filters the filters to apply
* @param include if set to true, the filter is used as include filter (equals, in). If set to false, the filter is inverted and used as exclude filter (not equals, not in etc)
* @param include if set to true, the filter is used as include filter (equals, in). If set to false, the filter is
* inverted and used as exclude filter (not equals, not in etc)
* @return
*/
protected <T extends Serializable> Predicate getFilterCondition(Predicate filterCondition, CriteriaBuilder cb, Root<T> root, Map<String, Object> filters, boolean include) {
@ -278,7 +288,8 @@ public abstract class CommonAbstractController {
}
/**
* returns null, if the list is empty or null itself. Returns the one element if there is exactly one element in the list. Otherwise an exception is thrown
* returns null, if the list is empty or null itself. Returns the one element if there is exactly one element in the
* list. Otherwise an exception is thrown
*
* @param resultList
* @return