]> git.mxchange.org Git - jfinancials-lib.git/blob - src/org/mxchange/addressbook/model/addressbook/shared/AddressbookShare.java
you need to not cascade here?
[jfinancials-lib.git] / src / org / mxchange / addressbook / model / addressbook / shared / AddressbookShare.java
1 /*
2  * Copyright (C) 2015 Roland Haeder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.addressbook.model.addressbook.shared;
18
19 import java.text.MessageFormat;
20 import java.util.Objects;
21 import javax.persistence.Column;
22 import javax.persistence.Entity;
23 import javax.persistence.GeneratedValue;
24 import javax.persistence.GenerationType;
25 import javax.persistence.Id;
26 import javax.persistence.JoinColumn;
27 import javax.persistence.NamedQueries;
28 import javax.persistence.NamedQuery;
29 import javax.persistence.OneToOne;
30 import javax.persistence.Table;
31 import org.mxchange.addressbook.model.addressbook.Addressbook;
32 import org.mxchange.addressbook.model.addressbook.UserAddressbook;
33 import org.mxchange.jusercore.model.user.LoginUser;
34 import org.mxchange.jusercore.model.user.User;
35
36 /**
37  * A POJO for sharing address books with other users
38  * <p>
39  * @author Roland Haeder
40  */
41 @Entity (name = "addressbook_shares")
42 @Table (name = "addressbook_shares")
43 @NamedQueries (
44                 {
45                         @NamedQuery (
46                                         name = "SearchUserSharedAddressbooks",
47                                         query = "SELECT s FROM addressbook_shares AS s WHERE s.shareUserOwner = :user ORDER BY s.shareId ASC"
48                         ),
49                         @NamedQuery (
50                                         name = "SearchShareeAddressbookShare",
51                                         query = "SELECT s FROM addressbook_shares AS s WHERE s.shareAddressbook = :addressbook AND s.shareUserSharee = :sharee"
52                         )
53                 }
54 )
55 public class AddressbookShare implements ShareableAddressbook, Comparable<ShareableAddressbook> {
56
57         /**
58          * Serial number
59          */
60         private static final long serialVersionUID = 167_889_678_177_691_690L;
61
62         /**
63          * Id number
64          */
65         @Id
66         @GeneratedValue (strategy = GenerationType.IDENTITY)
67         @Column (name = "share_id", length = 20, nullable = false, updatable = false)
68         private Long shareId;
69
70         /**
71          * Address book this share is for
72          */
73         @JoinColumn (name = "share_addressbook_id", nullable = false, updatable = false)
74         @OneToOne (targetEntity = UserAddressbook.class, optional = false)
75         private Addressbook shareAddressbook;
76
77         /**
78          * User who is giving the share (for his/her address book)
79          */
80         @JoinColumn (name = "share_owner_id", nullable = false, updatable = false)
81         @OneToOne (targetEntity = LoginUser.class, optional = false)
82         private User shareUserOwner;
83
84         /**
85          * User the address book is shared with
86          */
87         @JoinColumn (name = "share_sharee_id", nullable = false, updatable = false)
88         @OneToOne (targetEntity = LoginUser.class, optional = false)
89         private User shareUserSharee;
90
91         /**
92          * Default constructor for entity manager
93          */
94         protected AddressbookShare () {
95         }
96
97         /**
98          * Constructor with address book and sharee instance. Both parameters must
99          * not be null, their id numbers must be set and the adress book's user
100          * instance must be set and have a valid id set.
101          * <p>
102          * @param addressbook Address book instance
103          * @param sharee User sharee instance
104          */
105         public AddressbookShare (final Addressbook addressbook, final User sharee) {
106                 // Call protected constructor
107                 this();
108
109                 // Check all conditions
110                 if (null == sharee) {
111                         // Throw NPE
112                         throw new NullPointerException("sharee is null"); //NOI18N
113                 } else if (sharee.getUserId() == null) {
114                         // Throw NPE again
115                         throw new NullPointerException("sharee.userId is null"); //NOI18N
116                 } else if (sharee.getUserId() < 1) {
117                         // Invalid id number
118                         throw new IllegalStateException(MessageFormat.format("sharee.userId={0} is invalid", sharee.getUserId())); //NOI18N
119                 } else if (null == addressbook) {
120                         // Throw NPE again
121                         throw new NullPointerException("addressbook is null"); //NOI18N
122                 } else if (addressbook.getAddressbookId() == null) {
123                         // Throw NPE again
124                         throw new NullPointerException("addressbook.addressbookId is null"); //NOI18N
125                 } else if (addressbook.getAddressbookId() < 1) {
126                         // Invalid id number
127                         throw new IllegalArgumentException(MessageFormat.format("addressbook.addressbookId={0} is invalid.", addressbook.getAddressbookId())); //NOI18N
128                 } else if (Objects.equals(addressbook.getAddressbookUser(), sharee)) {
129                         // Sharing with yourself!
130                         throw new IllegalStateException("User tries to share with himself."); //NOI18N
131                 }
132
133                 // Set all instances
134                 this.shareAddressbook = addressbook;
135                 this.shareUserOwner = addressbook.getAddressbookUser();
136                 this.shareUserSharee = sharee;
137         }
138
139         @Override
140         public int compareTo (final ShareableAddressbook share) {
141                 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
142         }
143
144         @Override
145         public boolean equals (final Object object) {
146                 if (object == null) {
147                         return false;
148                 } else if (getClass() != object.getClass()) {
149                         return false;
150                 }
151
152                 final ShareableAddressbook other = (ShareableAddressbook) object;
153
154                 if (!Objects.equals(this.getShareAddressbook(), other.getShareAddressbook())) {
155                         return false;
156                 } else if (!Objects.equals(this.getShareUserOwner(), other.getShareUserOwner())) {
157                         return false;
158                 } else if (!Objects.equals(this.getShareUserOwner(), other.getShareUserOwner())) {
159                         return false;
160                 }
161
162                 return Objects.equals(this.getShareUserSharee(), other.getShareUserSharee());
163         }
164
165         @Override
166         public Addressbook getShareAddressbook () {
167                 return this.shareAddressbook;
168         }
169
170         @Override
171         public void setShareAddressbook (final Addressbook shareAddressbook) {
172                 this.shareAddressbook = shareAddressbook;
173         }
174
175         @Override
176         public Long getShareId () {
177                 return this.shareId;
178         }
179
180         @Override
181         public void setShareId (final Long shareId) {
182                 this.shareId = shareId;
183         }
184
185         @Override
186         public User getShareUserOwner () {
187                 return this.shareUserOwner;
188         }
189
190         @Override
191         public void setShareUserOwner (final User shareUserOwner) {
192                 this.shareUserOwner = shareUserOwner;
193         }
194
195         @Override
196         public User getShareUserSharee () {
197                 return this.shareUserSharee;
198         }
199
200         @Override
201         public void setShareUserSharee (final User shareUserSharee) {
202                 this.shareUserSharee = shareUserSharee;
203         }
204
205         @Override
206         public int hashCode () {
207                 int hash = 7;
208                 hash = 19 * hash + Objects.hashCode(this.getShareAddressbook());
209                 hash = 19 * hash + Objects.hashCode(this.getShareUserOwner());
210                 hash = 19 * hash + Objects.hashCode(this.getShareUserSharee());
211                 return hash;
212         }
213 }