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