From 9a60900169da6da6f2eed0c1d6f0b73aa827c50d Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Fri, 16 Oct 2015 09:48:41 +0200 Subject: [PATCH] =?utf8?q?Continued:=20-=20implemented=20business=20method?= =?utf8?q?=20startSharing()=20-=20added=20private=20method=20isUserAlready?= =?utf8?q?SharingAddressbook()=20to=20check=20if=20the=20address=20book's?= =?utf8?q?=20owner=20is=20already=20sharing=20it=20with=20the=20sharee=20S?= =?utf8?q?igned-off-by:Roland=20H=C3=A4der=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../shared/SharedAddressbooksSessionBean.java | 100 +++++++++++++++++- 1 file changed, 99 insertions(+), 1 deletion(-) diff --git a/src/java/org/mxchange/addressbook/model/shared/SharedAddressbooksSessionBean.java b/src/java/org/mxchange/addressbook/model/shared/SharedAddressbooksSessionBean.java index 8407f00..b276bbb 100644 --- a/src/java/org/mxchange/addressbook/model/shared/SharedAddressbooksSessionBean.java +++ b/src/java/org/mxchange/addressbook/model/shared/SharedAddressbooksSessionBean.java @@ -18,9 +18,12 @@ package org.mxchange.addressbook.model.shared; import java.text.MessageFormat; import java.util.List; +import java.util.Objects; import javax.ejb.Stateless; import javax.persistence.NoResultException; import javax.persistence.Query; +import org.mxchange.addressbook.exceptions.UserAlreadySharingAddressbookException; +import org.mxchange.addressbook.model.addressbook.Addressbook; import org.mxchange.addressbook.model.addressbook.shared.AddressbookShare; import org.mxchange.addressbook.model.addressbook.shared.ShareableAddressbook; import org.mxchange.jcoreee.database.BaseDatabaseBean; @@ -85,7 +88,7 @@ public class SharedAddressbooksSessionBean extends BaseDatabaseBean implements S this.shares = query.getResultList(); // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserSharingAddressbooks: shares.size()={0}", this.shares.size())); + this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserSharingAddressbooks: shares.size()={0}", this.shares.size())); //NOI18N // Is it not empty? isSharing = (!this.shares.isEmpty()); @@ -100,4 +103,99 @@ public class SharedAddressbooksSessionBean extends BaseDatabaseBean implements S // Return it return isSharing; } + + @Override + public ShareableAddressbook startSharing (final User sharee, final Addressbook addressbook) throws UserAlreadySharingAddressbookException { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("startSharing: sharee={0},addressbook={1} - CALLED!", sharee, addressbook)); //NOI18N + + // Check all conditions + if (null == sharee) { + // Throw NPE + throw new NullPointerException("sharee is null"); //NOI18N + } else if (sharee.getUserId() == null) { + // Throw NPE again + throw new NullPointerException("sharee.userId is null"); //NOI18N + } else if (sharee.getUserId() < 1) { + // Invalid id number + throw new IllegalStateException(MessageFormat.format("sharee.userId={0} is invalid", sharee.getUserId())); //NOI18N + } else if (null == addressbook) { + // Throw NPE again + throw new NullPointerException("addressbook is null"); //NOI18N + } else if (addressbook.getAddressbookId() == null) { + // Throw NPE again + throw new NullPointerException("addressbook.addressbookId is null"); //NOI18N + } else if (addressbook.getAddressbookId() < 1) { + // Invalid id number + throw new IllegalArgumentException(MessageFormat.format("addressbook.addressbookId={0} is invalid.", addressbook.getAddressbookId())); //NOI18N + } else if (Objects.equals(addressbook.getAddressbookUser(), sharee)) { + // Sharing with yourself! + throw new IllegalStateException("User tries to share with himself."); //NOI18N + } + + // Is the entry already there? + if (this.isUserAlreadySharingAddressbook(addressbook, sharee)) { + // Abort here + throw new UserAlreadySharingAddressbookException(addressbook, sharee); + } + + // All fine so far, then create the instance + ShareableAddressbook share = new AddressbookShare(addressbook, sharee); + + // Debug message + this.getLoggerBeanLocal().logDebug(MessageFormat.format("startSharing: share={0}", share)); + + // Persist it + this.getEntityManager().persist(share); + + // Flush to get id number + this.getEntityManager().flush(); + + // Return updated instance + return share; + } + + /** + * Checks whether the owner of the given address book is already sharing it + * with the sharee. + *

+ * @param addressbook Address book to be shared with + * @param sharee User sharee instance + * @return Wether the address book is already shared with the sharee + */ + private boolean isUserAlreadySharingAddressbook (final Addressbook addressbook, final User sharee) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserAlreadySharingAddressbook: addressbook={0},sharee={1} - CALLED!", addressbook, sharee)); //NOI18N + + // Get named query + Query query = this.getEntityManager().createNamedQuery("SearchShareeAddressbookShare", AddressbookShare.class); //NOI18N + + // Set parameter + query.setParameter("addressbook", addressbook); //NOI18N + query.setParameter("sharee", sharee); //NOI18N + + // Default is not found + boolean isFound = false; + + // Try it + try { + // Get single instance + ShareableAddressbook share = (ShareableAddressbook) query.getSingleResult(); + + // Debug message + this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserAlreadySharingAddressbook: share={0} - FOUND!", share)); //NOI18N + + // Set found + isFound = true; + } catch (final NoResultException ex) { + // Not found, log exception + this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserAlreadySharingAddressbook: Notfound. Exception: {0}", ex)); //NOI18N + } + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserAlreadySharingAddressbook: isFound={0} - EXIT!", isFound)); //NOI18N + + // Return it + return isFound; + } } -- 2.39.5