From: Roland Haeder Date: Thu, 15 Oct 2015 15:38:42 +0000 (+0200) Subject: Implemented business method countAllUserSharedAddressbooks() X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=5802fad890ee0164339f4ee3930be415470573fc;p=addressbook-ejb.git Implemented business method countAllUserSharedAddressbooks() --- diff --git a/src/java/org/mxchange/addressbook/model/addressbook/AddressbookSessionBean.java b/src/java/org/mxchange/addressbook/model/addressbook/AddressbookSessionBean.java index 02f8f3f..bdbd3d2 100644 --- a/src/java/org/mxchange/addressbook/model/addressbook/AddressbookSessionBean.java +++ b/src/java/org/mxchange/addressbook/model/addressbook/AddressbookSessionBean.java @@ -24,6 +24,7 @@ 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.addressbook.model.addressbook.shared.ShareableAddressbook; import org.mxchange.jcoreee.database.BaseDatabaseBean; import org.mxchange.jusercore.model.user.User; @@ -55,6 +56,52 @@ public class AddressbookSessionBean extends BaseDatabaseBean implements Addressb return query.getResultList(); } + @Override + @SuppressWarnings ("unchecked") + public Integer countAllUserSharedAddressbooks (final User user) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("countAllUserSharedAddressbooks: user={0} - CALLED!", user)); //NOI18N + + // user should be valid + if (null == user) { + // Throw NPE + throw new NullPointerException("user is null"); //NOI18N + } else if (user.getUserId() == null) { + // Throw NPE again + throw new NullPointerException("user.userId is null"); //NOI18N + } else if (user.getUserId() < 1) { + // Invalid id + throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N + } + + // Get named query + Query query = this.getEntityManager().createNamedQuery("SearchUserSharedAddressbooks"); //NOI18N + + // Set parameter + query.setParameter("user", user); //NOI18N + + // Default is zero + Integer count = 0; + + // Try it + try { + // Get whole list + List dummy = query.getResultList(); + + // Set size + count = dummy.size(); + } catch (final NoResultException ex) { + // Need to catch this, so log it + this.getLoggerBeanLocal().logDebug(MessageFormat.format("countAllUserSharedAddressbooks: getResultList() failed: {0}", ex)); //NOI18N + } + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("countAllUserSharedAddressbooks: count={0} - EXIT!", count)); //NOI18N + + // Return count + return count; + } + @Override public Addressbook createAddressbook (final Addressbook addressbook) throws AddressbookNameAlreadyUsedException { // Is it not null?