]> git.mxchange.org Git - addressbook-ejb.git/commitdiff
Continued:
authorRoland Haeder <roland@mxchange.org>
Thu, 15 Oct 2015 13:15:24 +0000 (15:15 +0200)
committerRoland Haeder <roland@mxchange.org>
Thu, 15 Oct 2015 13:15:24 +0000 (15:15 +0200)
- implemented business method getAddressbookById()
- some minor cleanups
Signed-off-by:Roland Häder <roland@mxchange.org>

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

index c1bd4a5c3785fa49f1457c02ca629a92cc496a6f..02f8f3f11c2a6409f3d1d959c533f64ba9b2de1b 100644 (file)
@@ -22,6 +22,7 @@ import javax.ejb.Stateless;
 import javax.persistence.NoResultException;
 import javax.persistence.Query;
 import org.mxchange.addressbook.exceptions.AddressbookNameAlreadyUsedException;
+import org.mxchange.addressbook.exceptions.AddressbookNotFoundException;
 import org.mxchange.addressbook.model.addressbook.entry.AddressbookEntry;
 import org.mxchange.jcoreee.database.BaseDatabaseBean;
 import org.mxchange.jusercore.model.user.User;
@@ -83,28 +84,55 @@ public class AddressbookSessionBean extends BaseDatabaseBean implements Addressb
                // Return it updated
                return addressbook;
        }
-       
+
+       @Override
+       public Addressbook getAddressbookById (final Long addressbookId) throws AddressbookNotFoundException {
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("getAddressbookById: addressbookId={0} - CALLED!", addressbookId)); //NOI18N
+
+               // addressbookId should not be null or below 1
+               if (null == addressbookId) {
+                       // Throw NPE
+                       throw new NullPointerException("addressbookId is null"); //NOI18N
+               } else if (addressbookId < 1) {
+                       // Not valid
+                       throw new IllegalArgumentException(MessageFormat.format("addressbookId is not valid: {0}", addressbookId)); //NOI18N
+               } else if (!this.isAddressbookIdUsed(addressbookId)) {
+                       // No address book found
+                       throw new AddressbookNotFoundException(addressbookId);
+               }
+
+               // Get named query instance
+               Query query = this.getEntityManager().createNamedQuery("SearchAddressbookById"); //NOI18N
+
+               // Set parameter
+               query.setParameter("id", addressbookId); //NOI18N
+
+               // Return it
+               return (Addressbook) query.getSingleResult();
+       }
+
        @Override
        @SuppressWarnings ("unchecked")
        public List<Addressbook> getUsersList (final User loggedInUser) {
                // Trace message
                this.getLoggerBeanLocal().logTrace(MessageFormat.format("getUsersList: loggedInUser={0} - CALLED!", loggedInUser)); //NOI18N
-               
+
                // Is the user instance null?
                if (null == loggedInUser) {
                        // Abort here
                        throw new NullPointerException("loggedInUser is null"); //NOI18N
                }
-               
+
                // Get query instance
                Query query = this.getEntityManager().createNamedQuery("AllUsersAddressbooks", List.class); //NOI18N
-               
+
                // Set parameter
                query.setParameter("param", loggedInUser); //NOI18N
-               
+
                // Get full list from JPA
                List<Addressbook> addressbooks = query.getResultList();
-               
+
                // Return it
                return addressbooks;
        }
@@ -124,7 +152,7 @@ public class AddressbookSessionBean extends BaseDatabaseBean implements Addressb
                }
 
                // Get query instance
-               Query query = this.getEntityManager().createNamedQuery("FindAddressbookById"); //NOI18N
+               Query query = this.getEntityManager().createNamedQuery("SearchAddressbookById"); //NOI18N
 
                // Set parameter
                query.setParameter("id", addressbookId); //NOI18N