From eb3bf3b71aaea80e5ed44cf220211d1a966d909b Mon Sep 17 00:00:00 2001 From: Joern Muehlencord Date: Mon, 19 Aug 2019 13:04:24 +0200 Subject: [PATCH] introduced IdentifiableEntity --- .../shared/db/CommonAbstractController.java | 40 ++++++++++++++----- .../shared/db/IdentifiableEntity.java | 30 ++++++++++++++ 2 files changed, 61 insertions(+), 9 deletions(-) create mode 100644 db/src/main/java/de/muehlencord/shared/db/IdentifiableEntity.java 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 c5b6131..9c4d2e5 100644 --- a/db/src/main/java/de/muehlencord/shared/db/CommonAbstractController.java +++ b/db/src/main/java/de/muehlencord/shared/db/CommonAbstractController.java @@ -87,6 +87,18 @@ public abstract class CommonAbstractController { return audit; } + @TransactionAttribute(TransactionAttributeType.REQUIRED) + @Transactional + @Lock(LockType.WRITE) + public T createOrUpdate(T entity, String createdBy) throws ControllerException { + if (entity.getId() == null) { + create(entity, createdBy); + return entity; + } else { + return update(entity, createdBy); + } + } + @TransactionAttribute(TransactionAttributeType.REQUIRED) @Transactional @Lock(LockType.WRITE) @@ -137,7 +149,7 @@ public abstract class CommonAbstractController { @Transactional @Lock(LockType.WRITE) public T update(T entity, String updatedBy) throws ControllerException { - T currentEntity = executeUpdate(entity, updatedBy); + T currentEntity = executeUpdate(entity, updatedBy); if (EndDateable.class.isAssignableFrom(entity.getClass())) { T newEntity = EntityUtil.cloneToNewEntity(currentEntity); em.merge(currentEntity); @@ -173,10 +185,22 @@ public abstract class CommonAbstractController { } protected Predicate getFilterCondition(CriteriaBuilder cb, Root root, Map filters, Map excludeFilters) { - Predicate filterCondition = null; - filterCondition = getFilterCondition(filterCondition, cb, root, filters, true); - filterCondition = getFilterCondition(filterCondition, cb, root, excludeFilters, false); - return filterCondition; +// Predicate filterCondition = null; +// filterCondition = getFilterCondition(filterCondition, cb, root, filters, true); +// filterCondition = getFilterCondition(filterCondition, cb, root, excludeFilters, false); +// return filterCondition; + Predicate includeFilterCondition = getFilterCondition(null, cb, root, filters, true); + Predicate excludeFilterCondition = getFilterCondition(null, cb, root, excludeFilters, false); + + if ((includeFilterCondition == null) && (excludeFilterCondition == null)) { + return null; + } else if (includeFilterCondition == null) { + return excludeFilterCondition; + } else if (excludeFilterCondition == null) { + return includeFilterCondition; + } else { + return cb.and(includeFilterCondition, excludeFilterCondition); + } } /** @@ -186,8 +210,7 @@ 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) { @@ -300,8 +323,7 @@ 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 diff --git a/db/src/main/java/de/muehlencord/shared/db/IdentifiableEntity.java b/db/src/main/java/de/muehlencord/shared/db/IdentifiableEntity.java new file mode 100644 index 0000000..2c76d40 --- /dev/null +++ b/db/src/main/java/de/muehlencord/shared/db/IdentifiableEntity.java @@ -0,0 +1,30 @@ +/* + * Copyright 2019 joern.muehlencord. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package de.muehlencord.shared.db; + +import java.io.Serializable; + +/** + * + * @author joern.muehlencord + */ +public interface IdentifiableEntity extends Serializable{ + + Object getId(); + + String getIdString(); + +}