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