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