]> git.mxchange.org Git - jaddressbook-share-core.git/blob - src/org/mxchange/jaddressbookshare/model/shared/AddressbookShare.java
Updated copyright year
[jaddressbook-share-core.git] / src / org / mxchange / jaddressbookshare / model / shared / AddressbookShare.java
1 /*
2  * Copyright (C) 2016 - 2024 Free Software Foundation
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.jaddressbookshare.model.shared;
18
19 import java.text.MessageFormat;
20 import java.util.Date;
21 import java.util.Objects;
22 import javax.persistence.Basic;
23 import javax.persistence.CascadeType;
24 import javax.persistence.Column;
25 import javax.persistence.Entity;
26 import javax.persistence.GeneratedValue;
27 import javax.persistence.GenerationType;
28 import javax.persistence.Id;
29 import javax.persistence.JoinColumn;
30 import javax.persistence.NamedQueries;
31 import javax.persistence.NamedQuery;
32 import javax.persistence.OneToOne;
33 import javax.persistence.Table;
34 import javax.persistence.Temporal;
35 import javax.persistence.TemporalType;
36 import javax.persistence.Transient;
37 import org.mxchange.jaddressbook.model.addressbook.Addressbook;
38 import org.mxchange.jaddressbook.model.addressbook.UserAddressbook;
39 import org.mxchange.jcoreutils.comparable.ComparableUtils;
40 import org.mxchange.jusercore.model.user.LoginUser;
41 import org.mxchange.jusercore.model.user.User;
42
43 /**
44  * A POJO for sharing address books with other users
45  * <p>
46  * @author Roland Häder<roland@mxchange.org>
47  */
48 @Entity (name = "addressbook_shares")
49 @Table (name = "addressbook_shares")
50 @NamedQueries (
51                 {
52                         @NamedQuery (
53                                         name = "SearchUserSharedAddressbooks",
54                                         query = "SELECT s FROM addressbook_shares AS s WHERE s.shareUserOwner = :user ORDER BY s.shareId ASC"
55                         ),
56                         @NamedQuery (
57                                         name = "SearchShareeAddressbookShare",
58                                         query = "SELECT s FROM addressbook_shares AS s WHERE s.shareAddressbook = :addressbook AND s.shareUserSharee = :sharee"
59                         )
60                 }
61 )
62 @SuppressWarnings ("PersistenceUnitPresent")
63 public class AddressbookShare implements ShareableAddressbook {
64
65         /**
66          * Serial number
67          */
68         @Transient
69         private static final long serialVersionUID = 167_889_678_177_691_690L;
70
71         /**
72          * Address book this share is for
73          */
74         @JoinColumn (name = "share_addressbook_id", nullable = false, updatable = false)
75         @OneToOne (targetEntity = UserAddressbook.class, cascade = CascadeType.REFRESH, optional = false)
76         private Addressbook shareAddressbook;
77
78         /**
79          * When this share has been created
80          */
81         @Basic (optional = false)
82         @Temporal (TemporalType.TIMESTAMP)
83         @Column (name = "share_entry_created", updatable = false, nullable = false)
84         private Date shareEntryCreated;
85
86         /**
87          * When this share has been updated
88          */
89         @Temporal (TemporalType.TIMESTAMP)
90         @Column (name = "share_entry_updated", insertable = false, nullable = false)
91         private Date shareEntryUpdated;
92
93         /**
94          * Id number
95          */
96         @Id
97         @GeneratedValue (strategy = GenerationType.IDENTITY)
98         @Column (name = "share_id", nullable = false, updatable = false)
99         private Long shareId;
100
101         /**
102          * User who is owning the share
103          */
104         @JoinColumn (name = "share_owner_id", nullable = false, updatable = false)
105         @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.REFRESH, optional = false)
106         private User shareUserOwner;
107
108         /**
109          * User the address book is shared with
110          */
111         @JoinColumn (name = "share_sharee_id", nullable = false, updatable = false)
112         @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.REFRESH, optional = false)
113         private User shareUserSharee;
114
115         /**
116          * Default constructor
117          */
118         public AddressbookShare () {
119         }
120
121         /**
122          * Constructor with address book and sharee instance. Both parameters must
123          * not be null, their id numbers must be set and the address book's user
124          * instance must be set and have a valid id set.
125          * <p>
126          * @param addressbook Address book instance
127          * @param sharee      User sharee instance
128          */
129         public AddressbookShare (final Addressbook addressbook, final User sharee) {
130                 // Invoke default constructor
131                 this();
132
133                 // Check all conditions
134                 if (null == sharee) {
135                         // Throw NPE
136                         throw new NullPointerException("sharee is null"); //NOI18N
137                 } else if (sharee.getUserId() == null) {
138                         // Throw NPE again
139                         throw new NullPointerException("sharee.userId is null"); //NOI18N
140                 } else if (sharee.getUserId() < 1) {
141                         // Invalid id number
142                         throw new IllegalStateException(MessageFormat.format("sharee.userId={0} is invalid", sharee.getUserId())); //NOI18N
143                 } else if (null == addressbook) {
144                         // Throw NPE again
145                         throw new NullPointerException("addressbook is null"); //NOI18N
146                 } else if (addressbook.getAddressbookId() == null) {
147                         // Throw NPE again
148                         throw new NullPointerException("addressbook.addressbookId is null"); //NOI18N
149                 } else if (addressbook.getAddressbookId() < 1) {
150                         // Invalid id number
151                         throw new IllegalArgumentException(MessageFormat.format("addressbook.addressbookId={0} is invalid.", addressbook.getAddressbookId())); //NOI18N
152                 } else if (addressbook.getAddressbookUser() == null) {
153                         // Throw NPE again
154                         throw new NullPointerException("addressbook.addressbookUser is null"); //NOI18N
155                 } else if (addressbook.getAddressbookUser().getUserId() == null) {
156                         // Throw NPE again
157                         throw new NullPointerException("addressbook.addressbookUser.userId is null"); //NOI18N
158                 } else if (addressbook.getAddressbookUser().getUserId() < 1) {
159                         // Invalid id number
160                         throw new IllegalArgumentException(MessageFormat.format("addressbook.addressbookUser.userId={0} is invalid.", addressbook.getAddressbookUser().getUserId())); //NOI18N
161                 } else if (Objects.equals(addressbook.getAddressbookUser(), sharee)) {
162                         // Sharing with yourself!
163                         throw new IllegalStateException("User tries to share with himself."); //NOI18N
164                 }
165
166                 // Set all instances
167                 this.shareAddressbook = addressbook;
168                 this.shareUserOwner = addressbook.getAddressbookUser();
169                 this.shareUserSharee = sharee;
170         }
171
172         @Override
173         public int compareTo (final ShareableAddressbook shareableAddressbook) {
174                 // Checkparameter and return 0 if equal
175                 if (null == shareableAddressbook) {
176                         // Should not happen
177                         throw new NullPointerException("shareableAddressbook is null"); //NOI18N
178                 } else if (shareableAddressbook.equals(this)) {
179                         // Same object
180                         return 0;
181                 }
182
183                 // All comparators
184                 final int comparators[] = {
185                         // First address book
186                         this.getShareAddressbook().compareTo(shareableAddressbook.getShareAddressbook()),
187                         // ... next sharer
188                         this.getShareUserOwner().compareTo(shareableAddressbook.getShareUserOwner()),
189                         // ... next sharee
190                         this.getShareUserSharee().compareTo(shareableAddressbook.getShareUserSharee())
191                 };
192
193                 // Check all values
194                 final int comparison = ComparableUtils.checkAll(comparators);
195
196                 // Return value
197                 return comparison;
198         }
199
200         @Override
201         public boolean equals (final Object object) {
202                 if (null == object) {
203                         return false;
204                 } else if (this.getClass() != object.getClass()) {
205                         return false;
206                 }
207
208                 final ShareableAddressbook shared = (ShareableAddressbook) object;
209
210                 if (!Objects.equals(this.getShareAddressbook(), shared.getShareAddressbook())) {
211                         return false;
212                 } else if (!Objects.equals(this.getShareId(), shared.getShareId())) {
213                         return false;
214                 } else if (!Objects.equals(this.getShareUserOwner(), shared.getShareUserOwner())) {
215                         return false;
216                 } else if (!Objects.equals(this.getShareUserSharee(), shared.getShareUserSharee())) {
217                         return false;
218                 }
219
220                 return Objects.equals(this.getShareUserSharee(), shared.getShareUserSharee());
221         }
222
223         @Override
224         public Addressbook getShareAddressbook () {
225                 return this.shareAddressbook;
226         }
227
228         @Override
229         public void setShareAddressbook (final Addressbook shareAddressbook) {
230                 this.shareAddressbook = shareAddressbook;
231         }
232
233         @Override
234         @SuppressWarnings ("ReturnOfDateField")
235         public Date getShareEntryCreated () {
236                 return this.shareEntryCreated;
237         }
238
239         @Override
240         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
241         public void setShareEntryCreated (final Date shareEntryCreated) {
242                 this.shareEntryCreated = shareEntryCreated;
243         }
244
245         @Override
246         @SuppressWarnings ("ReturnOfDateField")
247         public Date getShareEntryUpdated () {
248                 return this.shareEntryUpdated;
249         }
250
251         @Override
252         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
253         public void setShareEntryUpdated (final Date shareEntryUpdated) {
254                 this.shareEntryUpdated = shareEntryUpdated;
255         }
256
257         @Override
258         public Long getShareId () {
259                 return this.shareId;
260         }
261
262         @Override
263         public void setShareId (final Long shareId) {
264                 this.shareId = shareId;
265         }
266
267         @Override
268         public User getShareUserOwner () {
269                 return this.shareUserOwner;
270         }
271
272         @Override
273         public void setShareUserOwner (final User shareUserOwner) {
274                 this.shareUserOwner = shareUserOwner;
275         }
276
277         @Override
278         public User getShareUserSharee () {
279                 return this.shareUserSharee;
280         }
281
282         @Override
283         public void setShareUserSharee (final User shareUserSharee) {
284                 this.shareUserSharee = shareUserSharee;
285         }
286
287         @Override
288         public int hashCode () {
289                 int hash = 7;
290
291                 hash = 19 * hash + Objects.hashCode(this.getShareAddressbook());
292                 hash = 19 * hash + Objects.hashCode(this.getShareId());
293                 hash = 19 * hash + Objects.hashCode(this.getShareUserOwner());
294                 hash = 19 * hash + Objects.hashCode(this.getShareUserSharee());
295
296                 return hash;
297         }
298
299 }