From c7f77bb94edc3dcb3c18a6caaa3a312c24db79a7 Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Tue, 13 Oct 2015 12:07:31 +0200 Subject: [PATCH] =?utf8?q?Implemented=20business=20method=20isAddressbookI?= =?utf8?q?dUsed()=20Signed-off-by:Roland=20H=C3=A4der=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../addressbook/AddressbookSessionBean.java | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/src/java/org/mxchange/addressbook/model/addressbook/AddressbookSessionBean.java b/src/java/org/mxchange/addressbook/model/addressbook/AddressbookSessionBean.java index ef6472b..84cca40 100644 --- a/src/java/org/mxchange/addressbook/model/addressbook/AddressbookSessionBean.java +++ b/src/java/org/mxchange/addressbook/model/addressbook/AddressbookSessionBean.java @@ -109,6 +109,42 @@ public class AddressbookSessionBean extends BaseDatabaseBean implements Addressb return addressbook; } + @Override + public boolean isAddressbookIdUsed (final Long addressbookId) { + // Is it null or zero? + if (null == addressbookId) { + // Throw NPE + throw new NullPointerException("addressbookId is null"); //NOI18N + } else if (addressbookId < 1) { + // Not valid id number + throw new IllegalArgumentException(MessageFormat.format("addressbookId is not valid: {0}", addressbookId)); //NOI18N + } + + // Get query instance + Query query = this.getEntityManager().createNamedQuery("FindAddressbookById"); + + // Set parameter + query.setParameter("id", addressbookId); + + // Default is not valid + boolean isValid = false; + + // Try it again, yes no other way + try { + // Get single result + Addressbook addressbook = (Addressbook) query.getSingleResult(); + + // Found one! + isValid = true; + } catch (final NoResultException ex) { + // Debug log only, maybe out-dated link followed + this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookIdUsed: addressbookId={0} is not valid: {1}", addressbookId, ex)); + } + + // Return result + return isValid; + } + @Override public boolean isAddressbookNameUsed (final Addressbook addressbook) { // Is it not null? @@ -148,7 +184,7 @@ public class AddressbookSessionBean extends BaseDatabaseBean implements Addressb isUsed = true; } catch (final NoResultException ex) { // No result found, so log it away - this.getLoggerBeanLocal().logDebug("isAddressbookNameUsed: getSingleResult() did not return a result: " + ex); //NOI18N + this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookNameUsed: getSingleResult() did not return a result: {0}", ex)); //NOI18N //NOI18N } // Return result -- 2.39.5