]> git.mxchange.org Git - jfinancials-core.git/blob - src/org/mxchange/jfinancials/model/bonus_card/FinancialBonusCard.java
Continued:
[jfinancials-core.git] / src / org / mxchange / jfinancials / model / bonus_card / FinancialBonusCard.java
1 /*
2  * Copyright (C) 2017, 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 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.jfinancials.model.bonus_card;
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.GeneratedValue;
27 import javax.persistence.GenerationType;
28 import javax.persistence.Id;
29 import javax.persistence.JoinColumn;
30 import javax.persistence.OneToOne;
31 import javax.persistence.Table;
32 import javax.persistence.Temporal;
33 import javax.persistence.TemporalType;
34 import javax.persistence.Transient;
35 import org.apache.commons.lang3.StringUtils;
36 import org.mxchange.jcontacts.model.contact.Contact;
37 import org.mxchange.jcontacts.model.contact.Contacts;
38 import org.mxchange.jcontacts.model.contact.UserContact;
39 import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
40 import org.mxchange.jcontactsbusiness.model.basicdata.BasicDataUtils;
41 import org.mxchange.jcontactsbusiness.model.basicdata.BusinessBasicData;
42 import org.mxchange.jcoreutils.Comparables;
43 import org.mxchange.jcustomercore.model.customer.ContactCustomer;
44 import org.mxchange.jcustomercore.model.customer.Customer;
45 import org.mxchange.jcustomercore.utils.Customers;
46
47 /**
48  * A POJO (entity) for bonus cards
49  * <p>
50  * @author Roland Häder<roland@mxchange.org>
51  */
52 @Entity (name = "bonus_cards")
53 @Table (
54                 name = "bonus_cards"
55 )
56 @SuppressWarnings ("PersistenceUnitPresent")
57 public class FinancialBonusCard implements BonusCard {
58
59         /**
60          * Serial number
61          */
62         @Transient
63         private static final long serialVersionUID = 12_586_367_165_980L;
64
65         /**
66          * Bar code on bonus card
67          */
68         @Column (name = "bonus_card_bar_code")
69         private String bonusCardBarCode;
70
71         /**
72          * The contact that this card is assigned to
73          */
74         @JoinColumn (name = "bonus_card_contact_id", referencedColumnName = "contact_id", nullable = false)
75         @OneToOne (targetEntity = UserContact.class, cascade = CascadeType.REFRESH, optional = false)
76         private Contact bonusCardContact;
77
78         /**
79          * When this entry has been created
80          */
81         @Basic (optional = false)
82         @Column (name = "bonus_card_created", nullable = false)
83         @Temporal (TemporalType.TIMESTAMP)
84         private Date bonusCardCreated;
85
86         /**
87          * Bonus card customer id
88          */
89         @JoinColumn (name = "bonus_card_customer_id", referencedColumnName = "customer_id")
90         @OneToOne (targetEntity = ContactCustomer.class, cascade = CascadeType.REFRESH)
91         private Customer bonusCardCustomer;
92
93         /**
94          * Id number
95          */
96         @Id
97         @GeneratedValue (strategy = GenerationType.IDENTITY)
98         @Column (name = "bonus_card_id", nullable = false, updatable = false)
99         private Long bonusCardId;
100
101         /**
102          * Issuing company of this card
103          */
104         @JoinColumn (name = "bonus_card_issuer_id", referencedColumnName = "company_data_id", nullable = false)
105         @OneToOne (targetEntity = BusinessBasicData.class, cascade = CascadeType.REFRESH, optional = false)
106         private BasicData bonusCardIssuer;
107
108         /**
109          * Bonus card number
110          */
111         @Column (name = "bonus_card_number")
112         private String bonusCardNumber;
113
114         /**
115          * Partnering/branding company of this card
116          */
117         @JoinColumn (name = "bonus_card_partner_id", referencedColumnName = "company_data_id")
118         @OneToOne (targetEntity = BusinessBasicData.class, cascade = CascadeType.REFRESH)
119         private BasicData bonusCardPartner;
120
121         /**
122          * Default constructor (required for JPA)
123          */
124         public FinancialBonusCard () {
125         }
126
127         /**
128          * Constructor with all required fields
129          * <p>
130          * @param bonusCardContact Contact instance
131          * @param bonusCardIssuer  Issuing company instance
132          */
133         public FinancialBonusCard (final Contact bonusCardContact, final BasicData bonusCardIssuer) {
134                 // Call other constructor first
135                 this();
136
137                 // Validate parameter
138                 if (null == bonusCardContact) {
139                         // Throw NPE
140                         throw new NullPointerException("bonusCardContact is null"); //NOI18N
141                 } else if (bonusCardContact.getContactId() == null) {
142                         // Throw it again
143                         throw new NullPointerException("bonusCardContact.contactId is null"); //NOI18N
144                 } else if (bonusCardContact.getContactId() < 1) {
145                         // Throw IAE
146                         throw new IllegalArgumentException(MessageFormat.format("bonusCardContact.contactId={0} is invalid", bonusCardContact.getContactId())); //NOI18N
147                 } else if (null == bonusCardIssuer) {
148                         // Throw NPE
149                         throw new NullPointerException("bonusCardOwner is null"); //NOI18N
150                 } else if (bonusCardIssuer.getBasicDataId() == null) {
151                         // Throw it again
152                         throw new NullPointerException("bonusCardOwner.baseDataId is null"); //NOI18N
153                 } else if (bonusCardIssuer.getBasicDataId() < 1) {
154                         // Throw IAE
155                         throw new IllegalArgumentException(MessageFormat.format("bonusCardOwner.baseDataId={0} is invalid", bonusCardIssuer.getBasicDataId())); //NOI18N
156                 }
157
158                 // Set all
159                 this.bonusCardContact = bonusCardContact;
160                 this.bonusCardIssuer = bonusCardIssuer;
161         }
162
163         @Override
164         public int compareTo (final BonusCard bonusCard) {
165                 // Check parameter on null-reference and equality to this
166                 if (null == bonusCard) {
167                         // Should not happen
168                         throw new NullPointerException("bonusCard is null"); //NOI18N
169                 } else if (bonusCard.equals(this)) {
170                         // Same object
171                         return 0;
172                 }
173
174                 // Init comparators
175                 final int comparators[] = {
176                         // First compare by number
177                         StringUtils.compare(this.getBonusCardNumber(), bonusCard.getBonusCardNumber()),
178                         // ... next by bar code
179                         StringUtils.compare(this.getBonusCardBarCode(), bonusCard.getBonusCardBarCode()),
180                         // ... customer data
181                         Customers.compare(this.getBonusCardCustomer(), bonusCard.getBonusCardCustomer()),
182                         // ... contact data (card holder)
183                         Contacts.compare(this.getBonusCardContact(), bonusCard.getBonusCardContact()),
184                         // ... issuing company data
185                         this.getBonusCardIssuer().compareTo(bonusCard.getBonusCardIssuer()),
186                         // ... partner company data
187                         BasicDataUtils.compare(this.getBonusCardPartner(), bonusCard.getBonusCardPartner())
188                 };
189
190                 // Check all values
191                 final int comparison = Comparables.checkAll(comparators);
192
193                 // Return value
194                 return comparison;
195         }
196
197         @Override
198         public boolean equals (final Object object) {
199                 if (this == object) {
200                         return true;
201                 } else if (null == object) {
202                         return false;
203                 }
204                 if (this.getClass() != object.getClass()) {
205                         return false;
206                 }
207
208                 final BonusCard other = (BonusCard) object;
209
210                 if (!Objects.equals(this.getBonusCardBarCode(), other.getBonusCardBarCode())) {
211                         return false;
212                 } else if (!Objects.equals(this.getBonusCardContact(), other.getBonusCardContact())) {
213                         return false;
214                 } else if (!Objects.equals(this.getBonusCardCustomer(), other.getBonusCardCustomer())) {
215                         return false;
216                 } else if (!Objects.equals(this.getBonusCardId(), other.getBonusCardId())) {
217                         return false;
218                 } else if (!Objects.equals(this.getBonusCardIssuer(), other.getBonusCardIssuer())) {
219                         return false;
220                 } else if (!Objects.equals(this.getBonusCardNumber(), other.getBonusCardNumber())) {
221                         return false;
222                 } else if (!Objects.equals(this.getBonusCardPartner(), other.getBonusCardPartner())) {
223                         return false;
224                 }
225
226                 return true;
227         }
228
229         @Override
230         public String getBonusCardBarCode () {
231                 return this.bonusCardBarCode;
232         }
233
234         @Override
235         public void setBonusCardBarCode (final String bonusCardBarCode) {
236                 this.bonusCardBarCode = bonusCardBarCode;
237         }
238
239         @Override
240         public Contact getBonusCardContact () {
241                 return this.bonusCardContact;
242         }
243
244         @Override
245         public void setBonusCardContact (final Contact bonusCardContact) {
246                 this.bonusCardContact = bonusCardContact;
247         }
248
249         @Override
250         @SuppressWarnings ("ReturnOfDateField")
251         public Date getBonusCardCreated () {
252                 return this.bonusCardCreated;
253         }
254
255         @Override
256         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
257         public void setBonusCardCreated (final Date bonusCardCreated) {
258                 this.bonusCardCreated = bonusCardCreated;
259         }
260
261         @Override
262         public Customer getBonusCardCustomer () {
263                 return this.bonusCardCustomer;
264         }
265
266         @Override
267         public void setBonusCardCustomer (final Customer bonusCardCustomer) {
268                 this.bonusCardCustomer = bonusCardCustomer;
269         }
270
271         @Override
272         public Long getBonusCardId () {
273                 return this.bonusCardId;
274         }
275
276         @Override
277         public void setBonusCardId (final Long bonusCardId) {
278                 this.bonusCardId = bonusCardId;
279         }
280
281         @Override
282         public BasicData getBonusCardIssuer () {
283                 return this.bonusCardIssuer;
284         }
285
286         @Override
287         public void setBonusCardIssuer (final BasicData bonusCardIssuer) {
288                 this.bonusCardIssuer = bonusCardIssuer;
289         }
290
291         @Override
292         public String getBonusCardNumber () {
293                 return this.bonusCardNumber;
294         }
295
296         @Override
297         public void setBonusCardNumber (final String bonusCardNumber) {
298                 this.bonusCardNumber = bonusCardNumber;
299         }
300
301         @Override
302         public BasicData getBonusCardPartner () {
303                 return this.bonusCardPartner;
304         }
305
306         @Override
307         public void setBonusCardPartner (final BasicData bonusCardPartner) {
308                 this.bonusCardPartner = bonusCardPartner;
309         }
310
311         @Override
312         public int hashCode () {
313                 int hash = 5;
314
315                 hash = 13 * hash + Objects.hashCode(this.getBonusCardBarCode());
316                 hash = 13 * hash + Objects.hashCode(this.getBonusCardContact());
317                 hash = 13 * hash + Objects.hashCode(this.getBonusCardCustomer());
318                 hash = 13 * hash + Objects.hashCode(this.getBonusCardId());
319                 hash = 13 * hash + Objects.hashCode(this.getBonusCardIssuer());
320                 hash = 13 * hash + Objects.hashCode(this.getBonusCardNumber());
321                 hash = 13 * hash + Objects.hashCode(this.getBonusCardPartner());
322
323                 return hash;
324         }
325
326 }