From: Roland Haeder Date: Fri, 16 Oct 2015 09:02:53 +0000 (+0200) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=a0826a84171aafbb572ce60a2194fe794837f14a;p=pizzaservice-mailer-ejb.git Continued: - 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 --- 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);