diff --git a/db/src/main/java/de/muehlencord/shared/db/CommonAbstractController.java b/db/src/main/java/de/muehlencord/shared/db/CommonAbstractController.java index 2dae84b..b3854e3 100644 --- a/db/src/main/java/de/muehlencord/shared/db/CommonAbstractController.java +++ b/db/src/main/java/de/muehlencord/shared/db/CommonAbstractController.java @@ -103,23 +103,31 @@ public abstract class CommonAbstractController { return entity; } + private 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 update(T entity, String updatedBy) throws ControllerException { - T currentEntity = attach(entity); + public 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 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 Predicate getFilterCondition(Predicate filterCondition, CriteriaBuilder cb, Root root, Map 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