From: Roland Haeder Date: Thu, 15 Oct 2015 13:15:24 +0000 (+0200) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=4a84313149f0fa22adebc4a61c85b609ae563a84;p=addressbook-ejb.git Continued: - implemented business method getAddressbookById() - some minor cleanups 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 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