]> git.mxchange.org Git - addressbook-lib.git/commitdiff
Continued:
authorRoland Haeder <roland@mxchange.org>
Fri, 16 Oct 2015 07:47:39 +0000 (09:47 +0200)
committerRoland Haeder <roland@mxchange.org>
Fri, 16 Oct 2015 07:55:45 +0000 (09:55 +0200)
- 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 <roland@mxchange.org>

src/org/mxchange/addressbook/exceptions/UserAlreadySharingAddressbookException.java [new file with mode: 0644]
src/org/mxchange/addressbook/model/addressbook/shared/AddressbookShare.java
src/org/mxchange/addressbook/model/shared/SharedAddressbooksSessionBeanRemote.java

diff --git a/src/org/mxchange/addressbook/exceptions/UserAlreadySharingAddressbookException.java b/src/org/mxchange/addressbook/exceptions/UserAlreadySharingAddressbookException.java
new file mode 100644 (file)
index 0000000..d58c2a3
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+ */
+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
+ * <p>
+ * @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
+        * <p>
+        * @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()));
+       }
+}
index 4e4f3fffade0dba318d01349dd4f4d8008bff441..a88a032faabdfd4ad5d06b96b1a52bccdaa769c3 100644 (file)
@@ -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<Sharea
        @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.ALL, optional = false)
        private User shareUserSharee;
 
+       /**
+        * Default constructor for entity manager
+        */
+       protected AddressbookShare () {
+       }
+
+       /**
+        * Constructor with address book and sharee instance. Both parameters must
+        * not be null, their id numbers must be set and the adress book's user
+        * instance must be set and have a valid id set.
+        * <p>
+        * @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.
index 6d292bcdbed90e988fb2e2ed7ce7af54087a6e68..dc0cee33ee55563a2cd3b0134a6de68ea104049c 100644 (file)
@@ -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.
+        * <p>
+        * @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
         * <p>