introduced IdentifiableEntity

This commit is contained in:
Joern Muehlencord
2019-08-19 13:04:24 +02:00
parent b138841faa
commit eb3bf3b71a
2 changed files with 61 additions and 9 deletions

View File

@ -87,6 +87,18 @@ public abstract class CommonAbstractController {
return audit;
}
@TransactionAttribute(TransactionAttributeType.REQUIRED)
@Transactional
@Lock(LockType.WRITE)
public <T extends IdentifiableEntity> 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 extends Serializable> 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 <T extends Serializable> Predicate getFilterCondition(CriteriaBuilder cb, Root<T> root, Map<String, Object> filters, Map<String, Object> 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 <T extends Serializable> Predicate getFilterCondition(Predicate filterCondition, CriteriaBuilder cb, Root<T> root, Map<String, Object> 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

View File

@ -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();
}