]> git.mxchange.org Git - addressbook-lib.git/blobdiff - src/org/mxchange/addressbook/model/addressbook/shared/AddressbookShare.java
Continued:
[addressbook-lib.git] / src / org / mxchange / addressbook / model / addressbook / shared / AddressbookShare.java
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.