]> git.mxchange.org Git - jaddressbook-share-lib.git/blob - src/org/mxchange/addressbook/model/addressbook/shared/AddressbookShare.java
resorted members + added this.
[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          * Address book this share is for
70          */
71         @JoinColumn (name = "share_addressbook_id", nullable = false, updatable = false)
72         @OneToOne (targetEntity = UserAddressbook.class, cascade = CascadeType.MERGE, 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", length = 20, 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.MERGE, 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.MERGE, optional = false)
103         private User shareUserSharee;
104
105         /**
106          * Constructor with address book and sharee instance. Both parameters must
107          * not be null, their id numbers must be set and the adress book's user
108          * instance must be set and have a valid id set.
109          * <p>
110          * @param addressbook Address book instance
111          * @param sharee User sharee instance
112          */
113         public AddressbookShare (final Addressbook addressbook, final User sharee) {
114                 // Call protected constructor
115                 this();
116
117                 // Check all conditions
118                 if (null == sharee) {
119                         // Throw NPE
120                         throw new NullPointerException("sharee is null"); //NOI18N
121                 } else if (sharee.getUserId() == null) {
122                         // Throw NPE again
123                         throw new NullPointerException("sharee.userId is null"); //NOI18N
124                 } else if (sharee.getUserId() < 1) {
125                         // Invalid id number
126                         throw new IllegalStateException(MessageFormat.format("sharee.userId={0} is invalid", sharee.getUserId())); //NOI18N
127                 } else if (null == addressbook) {
128                         // Throw NPE again
129                         throw new NullPointerException("addressbook is null"); //NOI18N
130                 } else if (addressbook.getAddressbookId() == null) {
131                         // Throw NPE again
132                         throw new NullPointerException("addressbook.addressbookId is null"); //NOI18N
133                 } else if (addressbook.getAddressbookId() < 1) {
134                         // Invalid id number
135                         throw new IllegalArgumentException(MessageFormat.format("addressbook.addressbookId={0} is invalid.", addressbook.getAddressbookId())); //NOI18N
136                 } else if (Objects.equals(addressbook.getAddressbookUser(), sharee)) {
137                         // Sharing with yourself!
138                         throw new IllegalStateException("User tries to share with himself."); //NOI18N
139                 }
140
141                 // Set all instances
142                 this.shareAddressbook = addressbook;
143                 this.shareUserOwner = addressbook.getAddressbookUser();
144                 this.shareUserSharee = sharee;
145                 this.shareCreated = new GregorianCalendar();
146         }
147
148         /**
149          * Default constructor for entity manager
150          */
151         protected AddressbookShare () {
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 (this.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 int hashCode () {
182                 int hash = 7;
183                 hash = 19 * hash + Objects.hashCode(this.getShareAddressbook());
184                 hash = 19 * hash + Objects.hashCode(this.getShareUserOwner());
185                 hash = 19 * hash + Objects.hashCode(this.getShareUserSharee());
186                 return hash;
187         }
188
189         @Override
190         public Addressbook getShareAddressbook () {
191                 return this.shareAddressbook;
192         }
193
194         @Override
195         public void setShareAddressbook (final Addressbook shareAddressbook) {
196                 this.shareAddressbook = shareAddressbook;
197         }
198
199         @Override
200         public Calendar getShareCreated () {
201                 return this.shareCreated;
202         }
203
204         @Override
205         public void setShareCreated (final Calendar shareCreated) {
206                 this.shareCreated = shareCreated;
207         }
208
209         @Override
210         public Long getShareId () {
211                 return this.shareId;
212         }
213
214         @Override
215         public void setShareId (final Long shareId) {
216                 this.shareId = shareId;
217         }
218
219         @Override
220         public User getShareUserOwner () {
221                 return this.shareUserOwner;
222         }
223
224         @Override
225         public void setShareUserOwner (final User shareUserOwner) {
226                 this.shareUserOwner = shareUserOwner;
227         }
228
229         @Override
230         public User getShareUserSharee () {
231                 return this.shareUserSharee;
232         }
233
234         @Override
235         public void setShareUserSharee (final User shareUserSharee) {
236                 this.shareUserSharee = shareUserSharee;
237         }
238
239 }