From a0826a84171aafbb572ce60a2194fe794837f14a Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Fri, 16 Oct 2015 11:02:53 +0200 Subject: [PATCH] =?utf8?q?Continued:=20-=20implemented=20business=20method?= =?utf8?q?=20allSharedAddressbooks()=20-=20rewrote=20isUserSharingAddressb?= =?utf8?q?ooks()=20to=20it=20-=20removed=20this.shares=20as=20this=20is=20?= =?utf8?q?double-caching=20and=20adds=20only=20more=20complexibility=20Sig?= =?utf8?q?ned-off-by:Roland=20H=C3=A4der=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../shared/SharedAddressbooksSessionBean.java | 61 +++++++++---------- 1 file changed, 30 insertions(+), 31 deletions(-) diff --git a/src/java/org/mxchange/addressbook/model/shared/SharedAddressbooksSessionBean.java b/src/java/org/mxchange/addressbook/model/shared/SharedAddressbooksSessionBean.java index b276bbb..2608b58 100644 --- a/src/java/org/mxchange/addressbook/model/shared/SharedAddressbooksSessionBean.java +++ b/src/java/org/mxchange/addressbook/model/shared/SharedAddressbooksSessionBean.java @@ -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 shares; - - /** - * Default constructor - */ - public SharedAddressbooksSessionBean () { - // Init shares list - this.shares = null; - } - @Override @SuppressWarnings ("unchecked") - public Boolean isUserSharingAddressbooks (final User user) { + public List 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 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); -- 2.39.5