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