]> git.mxchange.org Git - jcustomer-core.git/blob - src/org/mxchange/jcustomercore/model/customer/ContactCustomer.java
Continued:
[jcustomer-core.git] / src / org / mxchange / jcustomercore / model / customer / ContactCustomer.java
1 /*
2  * Copyright (C) 2016 - 2022 Free Software Foundation
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (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 Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jcustomercore.model.customer;
18
19 import java.util.Date;
20 import java.util.Objects;
21 import javax.persistence.Basic;
22 import javax.persistence.CascadeType;
23 import javax.persistence.Column;
24 import javax.persistence.Entity;
25 import javax.persistence.EnumType;
26 import javax.persistence.Enumerated;
27 import javax.persistence.GeneratedValue;
28 import javax.persistence.GenerationType;
29 import javax.persistence.Id;
30 import javax.persistence.JoinColumn;
31 import javax.persistence.Lob;
32 import javax.persistence.NamedQueries;
33 import javax.persistence.NamedQuery;
34 import javax.persistence.OneToOne;
35 import javax.persistence.Table;
36 import javax.persistence.Temporal;
37 import javax.persistence.TemporalType;
38 import javax.persistence.Transient;
39 import org.apache.commons.lang3.StringUtils;
40 import org.mxchange.jcontacts.model.contact.Contact;
41 import org.mxchange.jcontacts.model.contact.UserContact;
42 import org.mxchange.jcontacts.model.utils.ContactUtils;
43 import org.mxchange.jcoreutils.Comparables;
44 import org.mxchange.jcustomercore.model.customer.status.CustomerAccountStatus;
45
46 /**
47  * A customer entity
48  * <p>
49  * @author Roland Häder<roland@mxchange.org>
50  */
51 @Entity (name = "customer")
52 @Table (
53                 name = "customer"
54 )
55 @NamedQueries (
56                 {
57                         @NamedQuery (name = "AllCustomers", query = "SELECT c FROM customer AS c ORDER BY c.customerId ASC")
58                 }
59 )
60 @SuppressWarnings ("PersistenceUnitPresent")
61 public class ContactCustomer implements Customer {
62
63         /**
64          * Serial number
65          */
66         @Transient
67         private static final long serialVersionUID = 14_857_923_178_504_617L;
68
69         /**
70          * Account status for this customer
71          */
72         @Basic (optional = false)
73         @Enumerated (EnumType.STRING)
74         @Column (name = "customer_status", nullable = false)
75         private CustomerAccountStatus customerAccountStatus;
76
77         /**
78          * Contact instance (personal data)
79          */
80         @JoinColumn (name = "customer_contact_id", nullable = false, updatable = false, unique = true)
81         @OneToOne (targetEntity = UserContact.class, cascade = CascadeType.ALL, optional = false)
82         private Contact customerContact;
83
84         /**
85          * When this customer has been created
86          */
87         @Basic (optional = false)
88         @Column (name = "customer_entry_created", updatable = false, nullable = false)
89         @Temporal (TemporalType.TIMESTAMP)
90         private Date customerEntryCreated;
91
92         /**
93          * When this customer has been updated
94          */
95         @Column (name = "customer_entry_updated", insertable = false)
96         @Temporal (TemporalType.TIMESTAMP)
97         private Date customerEntryUpdated;
98
99         /**
100          * Id number for this entry
101          */
102         @Id
103         @GeneratedValue (strategy = GenerationType.IDENTITY)
104         @Column (name = "customer_id", nullable = false, updatable = false)
105         private Long customerId;
106
107         /**
108          * When this customer has been locked (last timestamp)
109          */
110         @Column (name = "customer_last_locked")
111         @Temporal (TemporalType.TIMESTAMP)
112         private Date customerLastLocked;
113
114         /**
115          * Last locked reason
116          */
117         @Lob
118         @Column (name = "customer_last_locked_reason")
119         private String customerLastLockedReason;
120
121         /**
122          * Customer number
123          */
124         @Basic (optional = false)
125         @Column (name = "customer_number", nullable = false, updatable = false, unique = true)
126         private String customerNumber;
127
128         /**
129          * Default constructor
130          */
131         public ContactCustomer () {
132         }
133
134         /**
135          * Constructor with account status, contact instance and customer number
136          * <p>
137          * @param customerAccountStatus Account status (Call-agents may only call
138          *                              unlocked accounts)
139          * @param customerContact       Contact instance
140          * @param customerNumber        Customer number
141          */
142         public ContactCustomer (final CustomerAccountStatus customerAccountStatus, final Contact customerContact, final String customerNumber) {
143                 // Call other constructor
144                 this();
145
146                 // Validate parameter
147                 if (null == customerAccountStatus) {
148                         // Throw NPE
149                         throw new NullPointerException("customerAccountStatus is null"); //NOI18N
150                 } else if (null == customerContact) {
151                         // Throw NPE again
152                         throw new NullPointerException("customerContact is null"); //NOI18N
153                 } else if (null == customerNumber) {
154                         // Throw NPE again
155                         throw new NullPointerException("customerNumber is null"); //NOI18N
156                 } else if (customerNumber.isEmpty()) {
157                         // Throw IAE
158                         throw new IllegalArgumentException("customerNumber is empty"); //NOI18N
159                 }
160
161                 // Set all parameter
162                 this.customerAccountStatus = customerAccountStatus;
163                 this.customerContact = customerContact;
164                 this.customerNumber = customerNumber;
165         }
166
167         @Override
168         public int compareTo (final Customer customer) {
169                 // Check parameter on null-reference and equality to this
170                 if (null == customer) {
171                         // Should not happen
172                         throw new NullPointerException("Parameter 'customer' is null"); //NOI18N
173                 } else if (Objects.equals(this, customer)) {
174                         // Same object
175                         return 0;
176                 }
177
178                 // Init comparators
179                 final int comparators[] = {
180                         // First check contact instance
181                         ContactUtils.compare(this.getCustomerContact(), customer.getCustomerContact()),
182                         // ... then customer number
183                         StringUtils.compare(this.getCustomerNumber(), customer.getCustomerNumber()),
184                         // ... last is confirmation key
185                         StringUtils.compare(this.getCustomerConfirmKey(), customer.getCustomerConfirmKey())
186                 };
187
188                 // Check all values
189                 final int comparison = Comparables.checkAll(comparators);
190
191                 // Return value
192                 return comparison;
193         }
194
195         @Override
196         public boolean equals (final Object object) {
197                 if (this == object) {
198                         return true;
199                 } else if (null == object) {
200                         return false;
201                 } else if (this.getClass() != object.getClass()) {
202                         return false;
203                 }
204
205                 final Customer customer = (Customer) object;
206
207                 if (!Objects.equals(this.getCustomerAccountStatus(), customer.getCustomerAccountStatus())) {
208                         return false;
209                 } else if (!Objects.equals(this.getCustomerConfirmKey(), customer.getCustomerConfirmKey())) {
210                         return false;
211                 } else if (!Objects.equals(this.getCustomerContact(), customer.getCustomerContact())) {
212                         return false;
213                 } else if (!Objects.equals(this.getCustomerId(), customer.getCustomerId())) {
214                         return false;
215                 } else if (!Objects.equals(this.getCustomerLastLocked(), customer.getCustomerLastLocked())) {
216                         return false;
217                 } else if (!Objects.equals(this.getCustomerLastLockedReason(), customer.getCustomerLastLockedReason())) {
218                         return false;
219                 } else if (!Objects.equals(this.getCustomerNumber(), customer.getCustomerNumber())) {
220                         return false;
221                 } else if (!Objects.equals(this.getCustomerPasswordHash(), customer.getCustomerPasswordHash())) {
222                         return false;
223                 }
224
225                 return true;
226         }
227
228         @Override
229         public CustomerAccountStatus getCustomerAccountStatus () {
230                 return this.customerAccountStatus;
231         }
232
233         @Override
234         public void setCustomerAccountStatus (final CustomerAccountStatus customerStatus) {
235                 this.customerAccountStatus = customerStatus;
236         }
237
238         @Override
239         public String getCustomerConfirmKey () {
240                 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
241         }
242
243         @Override
244         public void setCustomerConfirmKey (final String customerConfirmKey) {
245                 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
246         }
247
248         @Override
249         public Contact getCustomerContact () {
250                 return this.customerContact;
251         }
252
253         @Override
254         public void setCustomerContact (final Contact customerContact) {
255                 this.customerContact = customerContact;
256         }
257
258         @Override
259         @SuppressWarnings ("ReturnOfDateField")
260         public Date getCustomerEntryCreated () {
261                 return this.customerEntryCreated;
262         }
263
264         @Override
265         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
266         public void setCustomerEntryCreated (final Date customerEntryCreated) {
267                 this.customerEntryCreated = customerEntryCreated;
268         }
269
270         @Override
271         @SuppressWarnings ("ReturnOfDateField")
272         public Date getCustomerEntryUpdated () {
273                 return this.customerEntryUpdated;
274         }
275
276         @Override
277         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
278         public void setCustomerEntryUpdated (final Date customerEntryUpdated) {
279                 this.customerEntryUpdated = customerEntryUpdated;
280         }
281
282         @Override
283         public Long getCustomerId () {
284                 return this.customerId;
285         }
286
287         @Override
288         public void setCustomerId (final Long customerId) {
289                 this.customerId = customerId;
290         }
291
292         @Override
293         @SuppressWarnings ("ReturnOfDateField")
294         public Date getCustomerLastLocked () {
295                 return this.customerLastLocked;
296         }
297
298         @Override
299         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
300         public void setCustomerLastLocked (final Date customerLastLocked) {
301                 this.customerLastLocked = customerLastLocked;
302         }
303
304         @Override
305         public String getCustomerLastLockedReason () {
306                 return this.customerLastLockedReason;
307         }
308
309         @Override
310         public void setCustomerLastLockedReason (final String customerLastLockedReason) {
311                 this.customerLastLockedReason = customerLastLockedReason;
312         }
313
314         @Override
315         public String getCustomerNumber () {
316                 return this.customerNumber;
317         }
318
319         @Override
320         public void setCustomerNumber (final String customerNumber) {
321                 this.customerNumber = customerNumber;
322         }
323
324         @Override
325         public String getCustomerPasswordHash () {
326                 throw new UnsupportedOperationException("Unfinished"); //NOI18N
327         }
328
329         @Override
330         public void setCustomerPasswordHash (final String customerPasswordHash) {
331                 throw new UnsupportedOperationException("Unfinished"); //NOI18N
332         }
333
334         @Override
335         public int hashCode () {
336                 int hash = 7;
337
338                 hash = 53 * hash + Objects.hashCode(this.getCustomerAccountStatus());
339                 hash = 53 * hash + Objects.hashCode(this.getCustomerConfirmKey());
340                 hash = 53 * hash + Objects.hashCode(this.getCustomerContact());
341                 hash = 53 * hash + Objects.hashCode(this.getCustomerId());
342                 hash = 53 * hash + Objects.hashCode(this.getCustomerLastLocked());
343                 hash = 53 * hash + Objects.hashCode(this.getCustomerLastLockedReason());
344                 hash = 53 * hash + Objects.hashCode(this.getCustomerNumber());
345                 hash = 53 * hash + Objects.hashCode(this.getCustomerPasswordHash());
346
347                 return hash;
348         }
349
350 }