]> 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 - 2018 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_created", nullable = false, updatable = false)
88         @Temporal (TemporalType.TIMESTAMP)
89         private Date customerCreated;
90
91         /**
92          * Id number for this entry
93          */
94         @Id
95         @GeneratedValue (strategy = GenerationType.IDENTITY)
96         @Column (name = "customer_id", nullable = false, updatable = false)
97         private Long customerId;
98
99         /**
100          * When this customer has been locked (last timestamp)
101          */
102         @Column (name = "customer_last_locked")
103         @Temporal (TemporalType.TIMESTAMP)
104         private Date customerLastLocked;
105
106         /**
107          * Last locked reason
108          */
109         @Lob
110         @Column (name = "customer_last_locked_reason")
111         private String customerLastLockedReason;
112
113         /**
114          * Customer number
115          */
116         @Basic (optional = false)
117         @Column (name = "customer_number", nullable = false, updatable = false, unique = true)
118         private String customerNumber;
119
120         /**
121          * When this customer has been updated
122          */
123         @Column (name = "customer_updated")
124         @Temporal (TemporalType.TIMESTAMP)
125         private Date customerUpdated;
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                 // Set all parameter
146                 this.customerAccountStatus = customerAccountStatus;
147                 this.customerContact = customerContact;
148                 this.customerNumber = customerNumber;
149         }
150
151         @Override
152         public int compareTo (final Customer customer) {
153                 // For performance reasons
154                 if (null == customer) {
155                         // Should not happen
156                         throw new NullPointerException("customer is null"); //NOI18N
157                 } else if (customer.equals(this)) {
158                         // Same object
159                         return 0;
160                 }
161
162                 // Init comparators
163                 final int comparators[] = {
164                         // First check contact instance
165                         this.getCustomerContact().compareTo(customer.getCustomerContact()),
166                         // ... then customer number
167                         this.getCustomerNumber().compareTo(customer.getCustomerNumber()),
168                         // ... last is confirmation key
169                         StringUtils.compare(this.getCustomerConfirmKey(), customer.getCustomerConfirmKey())
170                 };
171
172                 // Check all values
173                 final int comparison = Comparables.checkAll(comparators);
174
175                 // Return value
176                 return comparison;
177         }
178
179         @Override
180         public boolean equals (final Object object) {
181                 if (this == object) {
182                         return true;
183                 } else if (null == object) {
184                         return false;
185                 } else if (this.getClass() != object.getClass()) {
186                         return false;
187                 }
188
189                 final Customer other = (Customer) object;
190
191                 if (!Objects.equals(this.getCustomerNumber(), other.getCustomerNumber())) {
192                         return false;
193                 } else if (!Objects.equals(this.getCustomerContact(), other.getCustomerContact())) {
194                         return false;
195                 } else if (!Objects.equals(this.getCustomerId(), other.getCustomerId())) {
196                         return false;
197                 }
198
199                 return true;
200         }
201
202         @Override
203         public CustomerAccountStatus getCustomerAccountStatus () {
204                 return this.customerAccountStatus;
205         }
206
207         @Override
208         public void setCustomerAccountStatus (final CustomerAccountStatus customerStatus) {
209                 this.customerAccountStatus = customerStatus;
210         }
211
212         @Override
213         public String getCustomerConfirmKey () {
214                 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
215         }
216
217         @Override
218         public void setCustomerConfirmKey (final String customerConfirmKey) {
219                 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
220         }
221
222         @Override
223         public Contact getCustomerContact () {
224                 return this.customerContact;
225         }
226
227         @Override
228         public void setCustomerContact (final Contact customerContact) {
229                 this.customerContact = customerContact;
230         }
231
232         @Override
233         @SuppressWarnings ("ReturnOfDateField")
234         public Date getCustomerCreated () {
235                 return this.customerCreated;
236         }
237
238         @Override
239         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
240         public void setCustomerCreated (final Date customerCreated) {
241                 this.customerCreated = customerCreated;
242         }
243
244         @Override
245         public Long getCustomerId () {
246                 return this.customerId;
247         }
248
249         @Override
250         public void setCustomerId (final Long customerId) {
251                 this.customerId = customerId;
252         }
253
254         @Override
255         @SuppressWarnings ("ReturnOfDateField")
256         public Date getCustomerLastLocked () {
257                 return this.customerLastLocked;
258         }
259
260         @Override
261         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
262         public void setCustomerLastLocked (final Date customerLastLocked) {
263                 this.customerLastLocked = customerLastLocked;
264         }
265
266         @Override
267         public String getCustomerLastLockedReason () {
268                 return this.customerLastLockedReason;
269         }
270
271         @Override
272         public void setCustomerLastLockedReason (final String customerLastLockedReason) {
273                 this.customerLastLockedReason = customerLastLockedReason;
274         }
275
276         @Override
277         public String getCustomerNumber () {
278                 return this.customerNumber;
279         }
280
281         @Override
282         public void setCustomerNumber (final String customerNumber) {
283                 this.customerNumber = customerNumber;
284         }
285
286         @Override
287         public String getCustomerPasswordHash () {
288                 throw new UnsupportedOperationException("Unfinished"); //NOI18N
289         }
290
291         @Override
292         public void setCustomerPasswordHash (final String customerPasswordHash) {
293                 throw new UnsupportedOperationException("Unfinished"); //NOI18N
294         }
295
296         @Override
297         @SuppressWarnings ("ReturnOfDateField")
298         public Date getCustomerUpdated () {
299                 return this.customerUpdated;
300         }
301
302         @Override
303         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
304         public void setCustomerUpdated (final Date customerUpdated) {
305                 this.customerUpdated = customerUpdated;
306         }
307
308         @Override
309         public int hashCode () {
310                 int hash = 7;
311
312                 hash = 53 * hash + Objects.hashCode(this.getCustomerContact());
313                 hash = 53 * hash + Objects.hashCode(this.getCustomerId());
314                 hash = 53 * hash + Objects.hashCode(this.getCustomerNumber());
315
316                 return hash;
317         }
318
319 }