]> git.mxchange.org Git - addressbook-ejb.git/commitdiff
Implemented business method isAddressbookIdUsed()
authorRoland Haeder <roland@mxchange.org>
Tue, 13 Oct 2015 10:07:31 +0000 (12:07 +0200)
committerRoland Haeder <roland@mxchange.org>
Tue, 13 Oct 2015 10:07:31 +0000 (12:07 +0200)
Signed-off-by:Roland Häder <roland@mxchange.org>

src/java/org/mxchange/addressbook/model/addressbook/AddressbookSessionBean.java

index ef6472b2a9cabd6457daae22cf5a3e43a43f04af..84cca40a6910a5a278cdbf4aeddd6cab0620b86c 100644 (file)
@@ -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