From 265533154f7d7909e32caf1250c8a618894159fc Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Fri, 16 Oct 2015 09:47:39 +0200 Subject: [PATCH] =?utf8?q?Continued:=20-=20added=20business=20method=20sta?= =?utf8?q?rtSharing()=20-=20added=20exception=20UserAlreadySharingAddressb?= =?utf8?q?ookException=20which=20is=20thrown=20when=20the=20address=20book?= =?utf8?q?'s=20owner=20is=20already=20sharing=20it=20with=20the=20sharee?= =?utf8?q?=20-=20added=20constructor=20with=20address=20book=20instance=20?= =?utf8?q?and=20sharee=20user=20instance=20-=20added=20default=20construct?= =?utf8?q?or=20for=20JPA=20functionality=20Signed-off-by:Roland=20H=C3=A4d?= =?utf8?q?er=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- ...serAlreadySharingAddressbookException.java | 44 +++++++++++++++++ .../addressbook/shared/AddressbookShare.java | 49 +++++++++++++++++++ .../SharedAddressbooksSessionBeanRemote.java | 16 ++++++ 3 files changed, 109 insertions(+) create mode 100644 src/org/mxchange/addressbook/exceptions/UserAlreadySharingAddressbookException.java diff --git a/src/org/mxchange/addressbook/exceptions/UserAlreadySharingAddressbookException.java b/src/org/mxchange/addressbook/exceptions/UserAlreadySharingAddressbookException.java new file mode 100644 index 00000000..d58c2a38 --- /dev/null +++ b/src/org/mxchange/addressbook/exceptions/UserAlreadySharingAddressbookException.java @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.exceptions; + +import java.text.MessageFormat; +import org.mxchange.addressbook.model.addressbook.Addressbook; +import org.mxchange.jusercore.model.user.User; + +/** + * An exception thrown whent the address book is already shared with given user + *

+ * @author Roland Haeder + */ +public class UserAlreadySharingAddressbookException extends Exception { + + /** + * Serial number + */ + private static final long serialVersionUID = 58_528_177_571_976_034L; + + /** + * Constructor with address book being already shared with sharee + *

+ * @param addressbook Address book instance + * @param sharee User instance the address book is being shared with + */ + public UserAlreadySharingAddressbookException (final Addressbook addressbook, final User sharee) { + super(MessageFormat.format("Address book id {0} owned by user {1} is already shared with user {2}", addressbook.getAddressbookId(), addressbook.getAddressbookUser().getUserId(), sharee.getUserId())); + } +} diff --git a/src/org/mxchange/addressbook/model/addressbook/shared/AddressbookShare.java b/src/org/mxchange/addressbook/model/addressbook/shared/AddressbookShare.java index 4e4f3fff..a88a032f 100644 --- a/src/org/mxchange/addressbook/model/addressbook/shared/AddressbookShare.java +++ b/src/org/mxchange/addressbook/model/addressbook/shared/AddressbookShare.java @@ -16,6 +16,7 @@ */ package org.mxchange.addressbook.model.addressbook.shared; +import java.text.MessageFormat; import java.util.Objects; import javax.persistence.CascadeType; import javax.persistence.Column; @@ -79,6 +80,54 @@ public class AddressbookShare implements ShareableAddressbook, Comparable + * @param addressbook Address book instance + * @param sharee User sharee instance + */ + public AddressbookShare (final Addressbook addressbook, final User sharee) { + // Call protected constructor + this(); + + // 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 + } + + // Set all instances + this.shareAddressbook = addressbook; + this.shareUserOwner = addressbook.getAddressbookUser(); + this.shareUserSharee = sharee; + } + @Override public int compareTo (final ShareableAddressbook share) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. diff --git a/src/org/mxchange/addressbook/model/shared/SharedAddressbooksSessionBeanRemote.java b/src/org/mxchange/addressbook/model/shared/SharedAddressbooksSessionBeanRemote.java index 6d292bcd..dc0cee33 100644 --- a/src/org/mxchange/addressbook/model/shared/SharedAddressbooksSessionBeanRemote.java +++ b/src/org/mxchange/addressbook/model/shared/SharedAddressbooksSessionBeanRemote.java @@ -18,6 +18,9 @@ package org.mxchange.addressbook.model.shared; import java.io.Serializable; import javax.ejb.Remote; +import org.mxchange.addressbook.exceptions.UserAlreadySharingAddressbookException; +import org.mxchange.addressbook.model.addressbook.Addressbook; +import org.mxchange.addressbook.model.addressbook.shared.ShareableAddressbook; import org.mxchange.jusercore.model.user.User; /** @@ -28,6 +31,19 @@ import org.mxchange.jusercore.model.user.User; @Remote public interface SharedAddressbooksSessionBeanRemote extends Serializable { + /** + * Starts an address book share between currently logged-in user and + * assigned user for current address book. + *

+ * @param sharee User sharee instance + * @param addressbook Address book instance + * @return Updated share instance + * @throws + * org.mxchange.addressbook.exceptions.UserAlreadySharingAddressbookException + * When the user is already sharing the address book + */ + ShareableAddressbook startSharing (final User sharee, final Addressbook addressbook) throws UserAlreadySharingAddressbookException; + /** * Checks if the given user is sharing address books with others *

-- 2.39.5