From 5802fad890ee0164339f4ee3930be415470573fc Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Thu, 15 Oct 2015 17:38:42 +0200 Subject: [PATCH] Implemented business method countAllUserSharedAddressbooks() --- .../addressbook/AddressbookSessionBean.java | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) 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? -- 2.39.5