]> git.mxchange.org Git - addressbook-lib.git/blobdiff - src/org/mxchange/addressbook/model/addressbook/shared/AddressbookShare.java
added missing cascade type, at least MERGE + updated jar(s)
[addressbook-lib.git] / src / org / mxchange / addressbook / model / addressbook / shared / AddressbookShare.java
index 8777d9135b58fd9012122998cfab858a94385e03..be4b1fe5e83a20bd5e8b524d5dfc5b7c57226634 100644 (file)
  */
 package org.mxchange.addressbook.model.addressbook.shared;
 
+import java.text.MessageFormat;
+import java.util.Calendar;
+import java.util.GregorianCalendar;
 import java.util.Objects;
+import javax.persistence.Basic;
 import javax.persistence.CascadeType;
 import javax.persistence.Column;
 import javax.persistence.Entity;
@@ -24,8 +28,12 @@ import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
 import javax.persistence.JoinColumn;
+import javax.persistence.NamedQueries;
+import javax.persistence.NamedQuery;
 import javax.persistence.OneToOne;
 import javax.persistence.Table;
+import javax.persistence.Temporal;
+import javax.persistence.TemporalType;
 import org.mxchange.addressbook.model.addressbook.Addressbook;
 import org.mxchange.addressbook.model.addressbook.UserAddressbook;
 import org.mxchange.jusercore.model.user.LoginUser;
@@ -38,6 +46,18 @@ import org.mxchange.jusercore.model.user.User;
  */
 @Entity (name = "addressbook_shares")
 @Table (name = "addressbook_shares")
+@NamedQueries (
+               {
+                       @NamedQuery (
+                                       name = "SearchUserSharedAddressbooks",
+                                       query = "SELECT s FROM addressbook_shares AS s WHERE s.shareUserOwner = :user ORDER BY s.shareId ASC"
+                       ),
+                       @NamedQuery (
+                                       name = "SearchShareeAddressbookShare",
+                                       query = "SELECT s FROM addressbook_shares AS s WHERE s.shareAddressbook = :addressbook AND s.shareUserSharee = :sharee"
+                       )
+               }
+)
 public class AddressbookShare implements ShareableAddressbook, Comparable<ShareableAddressbook> {
 
        /**
@@ -57,23 +77,80 @@ public class AddressbookShare implements ShareableAddressbook, Comparable<Sharea
         * Address book this share is for
         */
        @JoinColumn (name = "share_addressbook_id", nullable = false, updatable = false)
-       @OneToOne (targetEntity = UserAddressbook.class, cascade = CascadeType.ALL, optional = false)
+       @OneToOne (targetEntity = UserAddressbook.class, cascade = CascadeType.MERGE, optional = false)
        private Addressbook shareAddressbook;
 
        /**
-        * User who is giving the share (for his/her address book)
+        * User who is owning the share
         */
        @JoinColumn (name = "share_owner_id", nullable = false, updatable = false)
-       @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.ALL, optional = false)
+       @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.MERGE, optional = false)
        private User shareUserOwner;
 
        /**
         * User the address book is shared with
         */
        @JoinColumn (name = "share_sharee_id", nullable = false, updatable = false)
-       @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.ALL, optional = false)
+       @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.MERGE, optional = false)
        private User shareUserSharee;
 
+       /**
+        * When this share has been created
+        */
+       @Basic (optional = false)
+       @Temporal (TemporalType.TIMESTAMP)
+       @Column (name = "share_created", nullable = false, updatable = false)
+       private Calendar shareCreated;
+
+       /**
+        * 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;
+               this.shareCreated = new GregorianCalendar();
+       }
+
        @Override
        public int compareTo (final ShareableAddressbook share) {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
@@ -110,6 +187,16 @@ public class AddressbookShare implements ShareableAddressbook, Comparable<Sharea
                this.shareAddressbook = shareAddressbook;
        }
 
+       @Override
+       public Calendar getShareCreated () {
+               return this.shareCreated;
+       }
+
+       @Override
+       public void setShareCreated (final Calendar shareCreated) {
+               this.shareCreated = shareCreated;
+       }
+
        @Override
        public Long getShareId () {
                return this.shareId;