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