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