From: Roland Haeder Date: Fri, 16 Oct 2015 07:47:39 +0000 (+0200) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=265533154f7d7909e32caf1250c8a618894159fc;p=jaddressbook-lib.git Continued: - added business method startSharing() - added exception UserAlreadySharingAddressbookException which is thrown when the address book's owner is already sharing it with the sharee - added constructor with address book instance and sharee user instance - added default constructor for JPA functionality Signed-off-by:Roland Häder --- 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 *