]> git.mxchange.org Git - addressbook-ejb.git/commitdiff
Continued:
authorRoland Haeder <roland@mxchange.org>
Fri, 16 Oct 2015 09:02:53 +0000 (11:02 +0200)
committerRoland Haeder <roland@mxchange.org>
Fri, 16 Oct 2015 09:04:22 +0000 (11:04 +0200)
- implemented business method allSharedAddressbooks()
- rewrote isUserSharingAddressbooks() to it
- removed this.shares as this is double-caching and adds only more complexibility
Signed-off-by:Roland Häder <roland@mxchange.org>

src/java/org/mxchange/addressbook/model/shared/SharedAddressbooksSessionBean.java

index b276bbbc3729bbbc02f69b6e7a8455ab8629f55c..2608b5812455214a01f32204324e18bf53d812ad 100644 (file)
@@ -42,24 +42,11 @@ public class SharedAddressbooksSessionBean extends BaseDatabaseBean implements S
         */
        private static final long serialVersionUID = 136_984_697_285_694_710L;
 
-       /**
-        * Shared address books
-        */
-       private List<ShareableAddressbook> shares;
-
-       /**
-        * Default constructor
-        */
-       public SharedAddressbooksSessionBean () {
-               // Init shares list
-               this.shares = null;
-       }
-
        @Override
        @SuppressWarnings ("unchecked")
-       public Boolean isUserSharingAddressbooks (final User user) {
+       public List<ShareableAddressbook> allSharedAddressbooks (final User user) {
                // Trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserSharingAddressbooks: user={0} - CALLED!", user)); //NOI18N
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("allSharedAddressbooks: user={0} - CALLED!", user)); //NOI18N
 
                // Is user null?
                if (null == user) {
@@ -74,29 +61,41 @@ public class SharedAddressbooksSessionBean extends BaseDatabaseBean implements S
                }
 
                // Get named query
-               Query query = this.getEntityManager().createNamedQuery("SearchUserSharedAddressbooks", AddressbookShare.class); //NOI18N
+               Query query = this.getEntityManager().createNamedQuery("SearchUserSharedAddressbooks"); //NOI18N
 
                // Set parameter
                query.setParameter("user", user); //NOI18N
 
-               // Default is not sharing
-               Boolean isSharing = Boolean.FALSE;
-
-               // Try it as an exception may be thrown
-               try {
-                       // Get results
-                       this.shares = query.getResultList();
+               // Return full list
+               return query.getResultList();
+       }
 
-                       // Debug message
-                       this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserSharingAddressbooks: shares.size()={0}", this.shares.size())); //NOI18N
+       @Override
+       public Boolean isUserSharingAddressbooks (final User user) {
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserSharingAddressbooks: user={0} - CALLED!", user)); //NOI18N
 
-                       // Is it not empty?
-                       isSharing = (!this.shares.isEmpty());
-               } catch (final NoResultException ex) {
-                       // Not sharing anything
-                       this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserSharingAddressbooks: User {0} is not sharing address books: {1}", user, ex)); //NOI18N
+               // Is user null?
+               if (null == user) {
+                       // Throw NPE
+                       throw new NullPointerException("user is null"); //NOI18N
+               } else if (user.getUserId() == null) {
+                       // Null userId is not allowed
+                       throw new NullPointerException("user.userId is null"); //NOI18N
+               } else if (user.getUserId() < 1) {
+                       // Not allowed value
+                       throw new IllegalArgumentException(MessageFormat.format("user.UserId={0} is an invalid value", user.getUserId())); //NOI18N
                }
 
+               // Get results
+               List<ShareableAddressbook> list = this.allSharedAddressbooks(user);
+
+               // Debug message
+               this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserSharingAddressbooks: list.size()={0}", list.size())); //NOI18N
+
+               // Is it not empty?
+               Boolean isSharing = (!list.isEmpty());
+
                // Trace message
                this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserSharingAddressbooks: iSharing={0} - EXIT!", isSharing)); //NOI18N
 
@@ -143,7 +142,7 @@ public class SharedAddressbooksSessionBean extends BaseDatabaseBean implements S
                ShareableAddressbook share = new AddressbookShare(addressbook, sharee);
 
                // Debug message
-               this.getLoggerBeanLocal().logDebug(MessageFormat.format("startSharing: share={0}", share));
+               this.getLoggerBeanLocal().logDebug(MessageFormat.format("startSharing: share={0}", share)); //NOI18N
 
                // Persist it
                this.getEntityManager().persist(share);