2 * Copyright (C) 2017, 2018 Free Software Foundation
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.
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.
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/>.
17 package org.mxchange.jfinancials.model.bonus_card;
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;
48 * A POJO (entity) for bonus cards
50 * @author Roland Häder<roland@mxchange.org>
52 @Entity (name = "bonus_cards")
56 @SuppressWarnings ("PersistenceUnitPresent")
57 public class FinancialBonusCard implements BonusCard {
63 private static final long serialVersionUID = 12_586_367_165_980L;
66 * Bar code on bonus card
68 @Column (name = "bonus_card_bar_code")
69 private String bonusCardBarCode;
72 * The contact that this card is assigned to
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;
79 * When this entry has been created
81 @Basic (optional = false)
82 @Column (name = "bonus_card_created", nullable = false)
83 @Temporal (TemporalType.TIMESTAMP)
84 private Date bonusCardCreated;
87 * Bonus card customer id
89 @JoinColumn (name = "bonus_card_customer_id", referencedColumnName = "customer_id")
90 @OneToOne (targetEntity = ContactCustomer.class, cascade = CascadeType.REFRESH)
91 private Customer bonusCardCustomer;
97 @GeneratedValue (strategy = GenerationType.IDENTITY)
98 @Column (name = "bonus_card_id", nullable = false, updatable = false)
99 private Long bonusCardId;
102 * Issuing company of this card
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;
111 @Column (name = "bonus_card_number")
112 private String bonusCardNumber;
115 * Partnering/branding company of this card
117 @JoinColumn (name = "bonus_card_partner_id", referencedColumnName = "company_data_id")
118 @OneToOne (targetEntity = BusinessBasicData.class, cascade = CascadeType.REFRESH)
119 private BasicData bonusCardPartner;
122 * Default constructor (required for JPA)
124 public FinancialBonusCard () {
128 * Constructor with all required fields
130 * @param bonusCardContact Contact instance
131 * @param bonusCardIssuer Issuing company instance
133 public FinancialBonusCard (final Contact bonusCardContact, final BasicData bonusCardIssuer) {
134 // Call other constructor first
137 // Validate parameter
138 if (null == bonusCardContact) {
140 throw new NullPointerException("bonusCardContact is null"); //NOI18N
141 } else if (bonusCardContact.getContactId() == null) {
143 throw new NullPointerException("bonusCardContact.contactId is null"); //NOI18N
144 } else if (bonusCardContact.getContactId() < 1) {
146 throw new IllegalArgumentException(MessageFormat.format("bonusCardContact.contactId={0} is invalid", bonusCardContact.getContactId())); //NOI18N
147 } else if (null == bonusCardIssuer) {
149 throw new NullPointerException("bonusCardOwner is null"); //NOI18N
150 } else if (bonusCardIssuer.getBasicDataId() == null) {
152 throw new NullPointerException("bonusCardOwner.baseDataId is null"); //NOI18N
153 } else if (bonusCardIssuer.getBasicDataId() < 1) {
155 throw new IllegalArgumentException(MessageFormat.format("bonusCardOwner.baseDataId={0} is invalid", bonusCardIssuer.getBasicDataId())); //NOI18N
159 this.bonusCardContact = bonusCardContact;
160 this.bonusCardIssuer = bonusCardIssuer;
164 public int compareTo (final BonusCard bonusCard) {
165 // Check parameter on null-reference and equality to this
166 if (null == bonusCard) {
168 throw new NullPointerException("bonusCard is null"); //NOI18N
169 } else if (bonusCard.equals(this)) {
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()),
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())
191 final int comparison = Comparables.checkAll(comparators);
198 public boolean equals (final Object object) {
199 if (this == object) {
201 } else if (null == object) {
204 if (this.getClass() != object.getClass()) {
208 final BonusCard other = (BonusCard) object;
210 if (!Objects.equals(this.getBonusCardBarCode(), other.getBonusCardBarCode())) {
212 } else if (!Objects.equals(this.getBonusCardContact(), other.getBonusCardContact())) {
214 } else if (!Objects.equals(this.getBonusCardCustomer(), other.getBonusCardCustomer())) {
216 } else if (!Objects.equals(this.getBonusCardId(), other.getBonusCardId())) {
218 } else if (!Objects.equals(this.getBonusCardIssuer(), other.getBonusCardIssuer())) {
220 } else if (!Objects.equals(this.getBonusCardNumber(), other.getBonusCardNumber())) {
222 } else if (!Objects.equals(this.getBonusCardPartner(), other.getBonusCardPartner())) {
230 public String getBonusCardBarCode () {
231 return this.bonusCardBarCode;
235 public void setBonusCardBarCode (final String bonusCardBarCode) {
236 this.bonusCardBarCode = bonusCardBarCode;
240 public Contact getBonusCardContact () {
241 return this.bonusCardContact;
245 public void setBonusCardContact (final Contact bonusCardContact) {
246 this.bonusCardContact = bonusCardContact;
250 @SuppressWarnings ("ReturnOfDateField")
251 public Date getBonusCardCreated () {
252 return this.bonusCardCreated;
256 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
257 public void setBonusCardCreated (final Date bonusCardCreated) {
258 this.bonusCardCreated = bonusCardCreated;
262 public Customer getBonusCardCustomer () {
263 return this.bonusCardCustomer;
267 public void setBonusCardCustomer (final Customer bonusCardCustomer) {
268 this.bonusCardCustomer = bonusCardCustomer;
272 public Long getBonusCardId () {
273 return this.bonusCardId;
277 public void setBonusCardId (final Long bonusCardId) {
278 this.bonusCardId = bonusCardId;
282 public BasicData getBonusCardIssuer () {
283 return this.bonusCardIssuer;
287 public void setBonusCardIssuer (final BasicData bonusCardIssuer) {
288 this.bonusCardIssuer = bonusCardIssuer;
292 public String getBonusCardNumber () {
293 return this.bonusCardNumber;
297 public void setBonusCardNumber (final String bonusCardNumber) {
298 this.bonusCardNumber = bonusCardNumber;
302 public BasicData getBonusCardPartner () {
303 return this.bonusCardPartner;
307 public void setBonusCardPartner (final BasicData bonusCardPartner) {
308 this.bonusCardPartner = bonusCardPartner;
312 public int hashCode () {
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());