From 4a84313149f0fa22adebc4a61c85b609ae563a84 Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Thu, 15 Oct 2015 15:15:24 +0200 Subject: [PATCH] =?utf8?q?Continued:=20-=20implemented=20business=20method?= =?utf8?q?=20getAddressbookById()=20-=20some=20minor=20cleanups=20Signed-o?= =?utf8?q?ff-by:Roland=20H=C3=A4der=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../addressbook/AddressbookSessionBean.java | 42 +++++++++++++++---- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/src/java/org/mxchange/addressbook/model/addressbook/AddressbookSessionBean.java b/src/java/org/mxchange/addressbook/model/addressbook/AddressbookSessionBean.java index c1bd4a5..02f8f3f 100644 --- a/src/java/org/mxchange/addressbook/model/addressbook/AddressbookSessionBean.java +++ b/src/java/org/mxchange/addressbook/model/addressbook/AddressbookSessionBean.java @@ -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 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 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 -- 2.39.5