]> git.mxchange.org Git - pizzaservice-mailer-ejb.git/commitdiff
Implemented business method countAllUserSharedAddressbooks()
authorRoland Haeder <roland@mxchange.org>
Thu, 15 Oct 2015 15:38:42 +0000 (17:38 +0200)
committerRoland Haeder <roland@mxchange.org>
Thu, 15 Oct 2015 15:38:42 +0000 (17:38 +0200)
src/java/org/mxchange/addressbook/model/addressbook/AddressbookSessionBean.java

index 02f8f3f11c2a6409f3d1d959c533f64ba9b2de1b..bdbd3d279c9cfb0b0679d8b53969875c67c4998d 100644 (file)
@@ -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<ShareableAddressbook> 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?