]> 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.jusercore.model.user.LoginUser;
40 import org.mxchange.jusercore.model.user.User;
41
42 /**
43  * A POJO for sharing address books with other users
44  * <p>
45  * @author Roland Häder<roland@mxchange.org>
46  */
47 @Entity (name = "addressbook_shares")
48 @Table (name = "addressbook_shares")
49 @NamedQueries (
50                 {
51                         @NamedQuery (
52                                         name = "SearchUserSharedAddressbooks",
53                                         query = "SELECT s FROM addressbook_shares AS s WHERE s.shareUserOwner = :user ORDER BY s.shareId ASC"
54                         ),
55                         @NamedQuery (
56                                         name = "SearchShareeAddressbookShare",
57                                         query = "SELECT s FROM addressbook_shares AS s WHERE s.shareAddressbook = :addressbook AND s.shareUserSharee = :sharee"
58                         )
59                 }
60 )
61 @SuppressWarnings ("PersistenceUnitPresent")
62 public class AddressbookShare implements ShareableAddressbook {
63
64         /**
65          * Serial number
66          */
67         @Transient
68         private static final long serialVersionUID = 167_889_678_177_691_690L;
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, cascade = CascadeType.REFRESH, optional = false)
75         private Addressbook shareAddressbook;
76
77         /**
78          * When this share has been created
79          */
80         @Basic (optional = false)
81         @Temporal (TemporalType.TIMESTAMP)
82         @Column (name = "share_created", nullable = false, updatable = false)
83         private Date shareCreated;
84
85         /**
86          * Id number
87          */
88         @Id
89         @GeneratedValue (strategy = GenerationType.IDENTITY)
90         @Column (name = "share_id", nullable = false, updatable = false)
91         private Long shareId;
92
93         /**
94          * User who is owning the share
95          */
96         @JoinColumn (name = "share_owner_id", nullable = false, updatable = false)
97         @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.REFRESH, optional = false)
98         private User shareUserOwner;
99
100         /**
101          * User the address book is shared with
102          */
103         @JoinColumn (name = "share_sharee_id", nullable = false, updatable = false)
104         @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.REFRESH, optional = false)
105         private User shareUserSharee;
106
107         /**
108          * Default constructor
109          */
110         public AddressbookShare () {
111         }
112
113         /**
114          * Constructor with address book and sharee instance. Both parameters must
115          * not be null, their id numbers must be set and the adress book's user
116          * instance must be set and have a valid id set.
117          * <p>
118          * @param addressbook Address book instance
119          * @param sharee User sharee instance
120          */
121         public AddressbookShare (final Addressbook addressbook, final User sharee) {
122                 // Call default constructor
123                 this();
124
125                 // Check all conditions
126                 if (null == sharee) {
127                         // Throw NPE
128                         throw new NullPointerException("sharee is null"); //NOI18N
129                 } else if (sharee.getUserId() == null) {
130                         // Throw NPE again
131                         throw new NullPointerException("sharee.userId is null"); //NOI18N
132                 } else if (sharee.getUserId() < 1) {
133                         // Invalid id number
134                         throw new IllegalStateException(MessageFormat.format("sharee.userId={0} is invalid", sharee.getUserId())); //NOI18N
135                 } else if (null == addressbook) {
136                         // Throw NPE again
137                         throw new NullPointerException("addressbook is null"); //NOI18N
138                 } else if (addressbook.getAddressbookId() == null) {
139                         // Throw NPE again
140                         throw new NullPointerException("addressbook.addressbookId is null"); //NOI18N
141                 } else if (addressbook.getAddressbookId() < 1) {
142                         // Invalid id number
143                         throw new IllegalArgumentException(MessageFormat.format("addressbook.addressbookId={0} is invalid.", addressbook.getAddressbookId())); //NOI18N
144                 } else if (Objects.equals(addressbook.getAddressbookUser(), sharee)) {
145                         // Sharing with yourself!
146                         throw new IllegalStateException("User tries to share with himself."); //NOI18N
147                 }
148
149                 // Set all instances
150                 this.shareAddressbook = addressbook;
151                 this.shareUserOwner = addressbook.getAddressbookUser();
152                 this.shareUserSharee = sharee;
153         }
154
155         @Override
156         public boolean equals (final Object object) {
157                 if (null == object) {
158                         return false;
159                 } else if (this.getClass() != object.getClass()) {
160                         return false;
161                 }
162
163                 final ShareableAddressbook other = (ShareableAddressbook) object;
164
165                 if (!Objects.equals(this.getShareAddressbook(), other.getShareAddressbook())) {
166                         return false;
167                 } else if (!Objects.equals(this.getShareUserOwner(), other.getShareUserOwner())) {
168                         return false;
169                 } else if (!Objects.equals(this.getShareUserOwner(), other.getShareUserOwner())) {
170                         return false;
171                 }
172
173                 return Objects.equals(this.getShareUserSharee(), other.getShareUserSharee());
174         }
175
176         @Override
177         public int hashCode () {
178                 int hash = 7;
179                 hash = 19 * hash + Objects.hashCode(this.getShareAddressbook());
180                 hash = 19 * hash + Objects.hashCode(this.getShareUserOwner());
181                 hash = 19 * hash + Objects.hashCode(this.getShareUserSharee());
182                 return hash;
183         }
184
185         @Override
186         public Addressbook getShareAddressbook () {
187                 return this.shareAddressbook;
188         }
189
190         @Override
191         public void setShareAddressbook (final Addressbook shareAddressbook) {
192                 this.shareAddressbook = shareAddressbook;
193         }
194
195         @Override
196         @SuppressWarnings ("ReturnOfDateField")
197         public Date getShareCreated () {
198                 return this.shareCreated;
199         }
200
201         @Override
202         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
203         public void setShareCreated (final Date shareCreated) {
204                 this.shareCreated = shareCreated;
205         }
206
207         @Override
208         public Long getShareId () {
209                 return this.shareId;
210         }
211
212         @Override
213         public void setShareId (final Long shareId) {
214                 this.shareId = shareId;
215         }
216
217         @Override
218         public User getShareUserOwner () {
219                 return this.shareUserOwner;
220         }
221
222         @Override
223         public void setShareUserOwner (final User shareUserOwner) {
224                 this.shareUserOwner = shareUserOwner;
225         }
226
227         @Override
228         public User getShareUserSharee () {
229                 return this.shareUserSharee;
230         }
231
232         @Override
233         public void setShareUserSharee (final User shareUserSharee) {
234                 this.shareUserSharee = shareUserSharee;
235         }
236
237 }