]> git.mxchange.org Git - jaddressbook-share-lib.git/blob - src/org/mxchange/addressbook/model/addressbook/shared/AddressbookShare.java
450c8a64a08ca2dfb665db068e666de1c92749eb
[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.GregorianCalendar;
22 import java.util.Objects;
23 import javax.persistence.Basic;
24 import javax.persistence.CascadeType;
25 import javax.persistence.Column;
26 import javax.persistence.Entity;
27 import javax.persistence.GeneratedValue;
28 import javax.persistence.GenerationType;
29 import javax.persistence.Id;
30 import javax.persistence.JoinColumn;
31 import javax.persistence.NamedQueries;
32 import javax.persistence.NamedQuery;
33 import javax.persistence.OneToOne;
34 import javax.persistence.Table;
35 import javax.persistence.Temporal;
36 import javax.persistence.TemporalType;
37 import org.mxchange.addressbook.model.addressbook.Addressbook;
38 import org.mxchange.addressbook.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 Haeder
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 public class AddressbookShare implements ShareableAddressbook, Comparable<ShareableAddressbook> {
62
63         /**
64          * Serial number
65          */
66         private static final long serialVersionUID = 167_889_678_177_691_690L;
67
68         /**
69          * Id number
70          */
71         @Id
72         @GeneratedValue (strategy = GenerationType.IDENTITY)
73         @Column (name = "share_id", length = 20, nullable = false, updatable = false)
74         private Long shareId;
75
76         /**
77          * Address book this share is for
78          */
79         @JoinColumn (name = "share_addressbook_id", nullable = false, updatable = false)
80         @OneToOne (targetEntity = UserAddressbook.class, cascade = CascadeType.MERGE, optional = false)
81         private Addressbook shareAddressbook;
82
83         /**
84          * User who is owning the share
85          */
86         @JoinColumn (name = "share_owner_id", nullable = false, updatable = false)
87         @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.MERGE, optional = false)
88         private User shareUserOwner;
89
90         /**
91          * User the address book is shared with
92          */
93         @JoinColumn (name = "share_sharee_id", nullable = false, updatable = false)
94         @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.MERGE, optional = false)
95         private User shareUserSharee;
96
97         /**
98          * When this share has been created
99          */
100         @Basic (optional = false)
101         @Temporal (TemporalType.TIMESTAMP)
102         @Column (name = "share_created", nullable = false, updatable = false)
103         private Calendar shareCreated;
104
105         /**
106          * Default constructor for entity manager
107          */
108         protected 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 protected 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                 this.shareCreated = new GregorianCalendar();
152         }
153
154         @Override
155         public int compareTo (final ShareableAddressbook share) {
156                 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
157         }
158
159         @Override
160         public boolean equals (final Object object) {
161                 if (object == null) {
162                         return false;
163                 } else if (getClass() != object.getClass()) {
164                         return false;
165                 }
166
167                 final ShareableAddressbook other = (ShareableAddressbook) object;
168
169                 if (!Objects.equals(this.getShareAddressbook(), other.getShareAddressbook())) {
170                         return false;
171                 } else if (!Objects.equals(this.getShareUserOwner(), other.getShareUserOwner())) {
172                         return false;
173                 } else if (!Objects.equals(this.getShareUserOwner(), other.getShareUserOwner())) {
174                         return false;
175                 }
176
177                 return Objects.equals(this.getShareUserSharee(), other.getShareUserSharee());
178         }
179
180         @Override
181         public Addressbook getShareAddressbook () {
182                 return this.shareAddressbook;
183         }
184
185         @Override
186         public void setShareAddressbook (final Addressbook shareAddressbook) {
187                 this.shareAddressbook = shareAddressbook;
188         }
189
190         @Override
191         public Calendar getShareCreated () {
192                 return this.shareCreated;
193         }
194
195         @Override
196         public void setShareCreated (final Calendar shareCreated) {
197                 this.shareCreated = shareCreated;
198         }
199
200         @Override
201         public Long getShareId () {
202                 return this.shareId;
203         }
204
205         @Override
206         public void setShareId (final Long shareId) {
207                 this.shareId = shareId;
208         }
209
210         @Override
211         public User getShareUserOwner () {
212                 return this.shareUserOwner;
213         }
214
215         @Override
216         public void setShareUserOwner (final User shareUserOwner) {
217                 this.shareUserOwner = shareUserOwner;
218         }
219
220         @Override
221         public User getShareUserSharee () {
222                 return this.shareUserSharee;
223         }
224
225         @Override
226         public void setShareUserSharee (final User shareUserSharee) {
227                 this.shareUserSharee = shareUserSharee;
228         }
229
230         @Override
231         public int hashCode () {
232                 int hash = 7;
233                 hash = 19 * hash + Objects.hashCode(this.getShareAddressbook());
234                 hash = 19 * hash + Objects.hashCode(this.getShareUserOwner());
235                 hash = 19 * hash + Objects.hashCode(this.getShareUserSharee());
236                 return hash;
237         }
238 }