]> git.mxchange.org Git - jaddressbook-share-core.git/blob - src/org/mxchange/jaddressbookshare/model/addressbook/shared/AddressbookShare.java
Continued:
[jaddressbook-share-core.git] / src / org / mxchange / jaddressbookshare / model / addressbook / shared / AddressbookShare.java
1 /*
2  * Copyright (C) 2016 - 2018 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.addressbook.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.Comparables;
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_created", nullable = false, updatable = false)
84         private Date shareCreated;
85
86         /**
87          * Id number
88          */
89         @Id
90         @GeneratedValue (strategy = GenerationType.IDENTITY)
91         @Column (name = "share_id", nullable = false, updatable = false)
92         private Long shareId;
93
94         /**
95          * User who is owning the share
96          */
97         @JoinColumn (name = "share_owner_id", nullable = false, updatable = false)
98         @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.REFRESH, optional = false)
99         private User shareUserOwner;
100
101         /**
102          * User the address book is shared with
103          */
104         @JoinColumn (name = "share_sharee_id", nullable = false, updatable = false)
105         @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.REFRESH, optional = false)
106         private User shareUserSharee;
107
108         /**
109          * Default constructor
110          */
111         public AddressbookShare () {
112         }
113
114         /**
115          * Constructor with address book and sharee instance. Both parameters must
116          * not be null, their id numbers must be set and the address book's user
117          * instance must be set and have a valid id set.
118          * <p>
119          * @param addressbook Address book instance
120          * @param sharee      User sharee instance
121          */
122         public AddressbookShare (final Addressbook addressbook, final User sharee) {
123                 // Call default constructor
124                 this();
125
126                 // Check all conditions
127                 if (null == sharee) {
128                         // Throw NPE
129                         throw new NullPointerException("sharee is null"); //NOI18N
130                 } else if (sharee.getUserId() == null) {
131                         // Throw NPE again
132                         throw new NullPointerException("sharee.userId is null"); //NOI18N
133                 } else if (sharee.getUserId() < 1) {
134                         // Invalid id number
135                         throw new IllegalStateException(MessageFormat.format("sharee.userId={0} is invalid", sharee.getUserId())); //NOI18N
136                 } else if (null == addressbook) {
137                         // Throw NPE again
138                         throw new NullPointerException("addressbook is null"); //NOI18N
139                 } else if (addressbook.getAddressbookId() == null) {
140                         // Throw NPE again
141                         throw new NullPointerException("addressbook.addressbookId is null"); //NOI18N
142                 } else if (addressbook.getAddressbookId() < 1) {
143                         // Invalid id number
144                         throw new IllegalArgumentException(MessageFormat.format("addressbook.addressbookId={0} is invalid.", addressbook.getAddressbookId())); //NOI18N
145                 } else if (Objects.equals(addressbook.getAddressbookUser(), sharee)) {
146                         // Sharing with yourself!
147                         throw new IllegalStateException("User tries to share with himself."); //NOI18N
148                 }
149
150                 // Set all instances
151                 this.shareAddressbook = addressbook;
152                 this.shareUserOwner = addressbook.getAddressbookUser();
153                 this.shareUserSharee = sharee;
154         }
155
156         @Override
157         public int compareTo (final ShareableAddressbook shareableAddressbook) {
158                 // Checkparameter and return 0 if equal
159                 if (null == shareableAddressbook) {
160                         // Should not happen
161                         throw new NullPointerException("shareableAddressbook is null"); //NOI18N
162                 } else if (shareableAddressbook.equals(this)) {
163                         // Same object
164                         return 0;
165                 }
166
167                 // All comparators
168                 final int comparators[] = {
169                         // First address book
170                         this.getShareAddressbook().compareTo(shareableAddressbook.getShareAddressbook()),
171                         // ... next sharer
172                         this.getShareUserOwner().compareTo(shareableAddressbook.getShareUserOwner()),
173                         // ... next sharee
174                         this.getShareUserSharee().compareTo(shareableAddressbook.getShareUserSharee()),
175                 };
176
177                 // Check all values
178                 final int comparison = Comparables.checkAll(comparators);
179
180                 // Return value
181                 return comparison;
182         }
183
184         @Override
185         public boolean equals (final Object object) {
186                 if (null == object) {
187                         return false;
188                 } else if (this.getClass() != object.getClass()) {
189                         return false;
190                 }
191
192                 final ShareableAddressbook other = (ShareableAddressbook) object;
193
194                 if (!Objects.equals(this.getShareAddressbook(), other.getShareAddressbook())) {
195                         return false;
196                 } else if (!Objects.equals(this.getShareUserOwner(), other.getShareUserOwner())) {
197                         return false;
198                 } else if (!Objects.equals(this.getShareUserOwner(), other.getShareUserOwner())) {
199                         return false;
200                 }
201
202                 return Objects.equals(this.getShareUserSharee(), other.getShareUserSharee());
203         }
204
205         @Override
206         public Addressbook getShareAddressbook () {
207                 return this.shareAddressbook;
208         }
209
210         @Override
211         public void setShareAddressbook (final Addressbook shareAddressbook) {
212                 this.shareAddressbook = shareAddressbook;
213         }
214
215         @Override
216         @SuppressWarnings ("ReturnOfDateField")
217         public Date getShareCreated () {
218                 return this.shareCreated;
219         }
220
221         @Override
222         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
223         public void setShareCreated (final Date shareCreated) {
224                 this.shareCreated = shareCreated;
225         }
226
227         @Override
228         public Long getShareId () {
229                 return this.shareId;
230         }
231
232         @Override
233         public void setShareId (final Long shareId) {
234                 this.shareId = shareId;
235         }
236
237         @Override
238         public User getShareUserOwner () {
239                 return this.shareUserOwner;
240         }
241
242         @Override
243         public void setShareUserOwner (final User shareUserOwner) {
244                 this.shareUserOwner = shareUserOwner;
245         }
246
247         @Override
248         public User getShareUserSharee () {
249                 return this.shareUserSharee;
250         }
251
252         @Override
253         public void setShareUserSharee (final User shareUserSharee) {
254                 this.shareUserSharee = shareUserSharee;
255         }
256
257         @Override
258         public int hashCode () {
259                 int hash = 7;
260
261                 hash = 19 * hash + Objects.hashCode(this.getShareAddressbook());
262                 hash = 19 * hash + Objects.hashCode(this.getShareUserOwner());
263                 hash = 19 * hash + Objects.hashCode(this.getShareUserSharee());
264
265                 return hash;
266         }
267
268 }