From: Roland Haeder Date: Tue, 13 Oct 2015 10:07:31 +0000 (+0200) Subject: Implemented business method isAddressbookIdUsed() X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=c7f77bb94edc3dcb3c18a6caaa3a312c24db79a7;p=addressbook-ejb.git Implemented business method isAddressbookIdUsed() Signed-off-by:Roland Häder --- 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