diff --git a/account/src/main/java/de/muehlencord/shared/account/business/AbstractController.java b/account/src/main/java/de/muehlencord/shared/account/business/AbstractController.java index 55a7bfb..92926aa 100644 --- a/account/src/main/java/de/muehlencord/shared/account/business/AbstractController.java +++ b/account/src/main/java/de/muehlencord/shared/account/business/AbstractController.java @@ -33,6 +33,9 @@ import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Order; import javax.persistence.criteria.Root; +import javax.persistence.metamodel.IdentifiableType; +import javax.persistence.metamodel.Metamodel; +import javax.persistence.metamodel.SingularAttribute; import javax.transaction.Transactional; /** @@ -59,7 +62,7 @@ public abstract class AbstractController { return em.merge(entity); } - @TransactionAttribute (TransactionAttributeType.REQUIRED) + @TransactionAttribute(TransactionAttributeType.REQUIRED) @Transactional @Lock(LockType.WRITE) public void create(T entity) { @@ -70,10 +73,10 @@ public abstract class AbstractController { updateable.setLastUpdatedBy(account.getUsername()); updateable.setLastUpdatedOn(new Date()); } - em.persist(entity); + em.persist(entity); } - @TransactionAttribute (TransactionAttributeType.REQUIRED) + @TransactionAttribute(TransactionAttributeType.REQUIRED) @Transactional @Lock(LockType.WRITE) public T update(T entity) { @@ -85,7 +88,7 @@ public abstract class AbstractController { return em.merge(entity); } - @TransactionAttribute (TransactionAttributeType.REQUIRED) + @TransactionAttribute(TransactionAttributeType.REQUIRED) @Transactional @Lock(LockType.WRITE) public void delete(T entity) { @@ -116,4 +119,31 @@ public abstract class AbstractController { return query.getResultList(); } + /** + * returnns 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 + * @throws ControllerException + */ + public T ensureSingleElement(List resultList) throws ControllerException { + if ((resultList == null) || (resultList.isEmpty())) { + return null; + } + if (resultList.size() > 1) { + throw new ControllerException(ControllerException.CAUSE_TOO_MANY_ROWS, "More than one element found in list - expected exactly one"); + } + return resultList.get(0); + } + + private SingularAttribute getIdAttribute() { + Metamodel m = em.getEntityManagerFactory().getMetamodel(); + IdentifiableType of = (IdentifiableType) m.managedType(entityClass); +// of.getDeclaredId(entityClass).getJavaMember(). + return of.getId(of.getIdType().getJavaType()); + } + + } diff --git a/account/src/main/java/de/muehlencord/shared/account/business/ControllerException.java b/account/src/main/java/de/muehlencord/shared/account/business/ControllerException.java new file mode 100644 index 0000000..dc17cf2 --- /dev/null +++ b/account/src/main/java/de/muehlencord/shared/account/business/ControllerException.java @@ -0,0 +1,58 @@ +/* + * 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.account.business; + +/** + * + * @author joern.muehlencord + */ +public class ControllerException extends Exception { + + private static final long serialVersionUID = 5190280225284514859L; + public static final int CAUSE_ALREADY_EXISTS = 1; + public static final int CAUSE_NOT_FOUND = 2; + public static final int CAUSE_CANNOT_PERSIST = 3; + public static final int CAUSE_TOO_MANY_ROWS = 4; + + private final int causeCode; + + /** + * Creates a new instance of ControllerException without detail + * message. + * + * @param cause the reason code + * @param message an explanation + */ + public ControllerException(int cause, String message) { + super(message); + this.causeCode = cause; + } + + /** + * + * @param causeCode + * @param message + * @param cause + */ + public ControllerException(int causeCode, String message, Throwable cause) { + super(message, cause); + this.causeCode = causeCode; + } + + public int getCauseCode() { + return causeCode; + } +}