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