2 * Copyright (C) 2016 - 2022 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.jaddressbook.model.addressbook.entry;
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.GeneratedValue;
26 import javax.persistence.GenerationType;
27 import javax.persistence.Id;
28 import javax.persistence.JoinColumn;
29 import javax.persistence.NamedQueries;
30 import javax.persistence.NamedQuery;
31 import javax.persistence.OneToOne;
32 import javax.persistence.Table;
33 import javax.persistence.Temporal;
34 import javax.persistence.TemporalType;
35 import javax.persistence.Transient;
36 import org.mxchange.jaddressbook.model.addressbook.Addressbook;
37 import org.mxchange.jaddressbook.model.addressbook.UserAddressbook;
38 import org.mxchange.jcontacts.model.contact.Contact;
39 import org.mxchange.jcontacts.model.contact.UserContact;
40 import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
41 import org.mxchange.jcontactsbusiness.model.basicdata.BusinessBasicData;
42 import org.mxchange.jcoreutils.Comparables;
45 * A POJO for address book entries
47 * @author Roland Häder<roland@mxchange.org>
49 @Entity (name = "addressbook_entries")
50 @Table (name = "addressbook_entries")
53 @NamedQuery (name = "AllAddressBookEntries", query = "SELECT e FROM addressbook_entries AS e ORDER BY e.addressbookEntryId ASC")
56 @SuppressWarnings ("PersistenceUnitPresent")
57 public class UserAddressbookEntry implements AddressbookEntry {
63 private static final long serialVersionUID = 178_581_768_581_960L;
66 * Connection to table "business_contacts" (commercial contacts)
68 @JoinColumn (name = "entry_business_contact_id", updatable = false)
69 @OneToOne (targetEntity = BusinessBasicData.class, cascade = CascadeType.REFRESH)
70 private BasicData addressbookEntryBusinessBasicData;
73 * When this address book entry has been created
75 @Basic (optional = false)
76 @Temporal (TemporalType.TIMESTAMP)
77 @Column (name = "entry_created", updatable = false, nullable = false)
78 private Date addressbookEntryCreated;
84 @GeneratedValue (strategy = GenerationType.IDENTITY)
85 @Column (name = "entry_id", nullable = false, updatable = false)
86 private Long addressbookEntryId;
89 * Connection to table "contacts" (private contacts)
91 @JoinColumn (name = "entry_private_contact_id", updatable = false)
92 @OneToOne (targetEntity = UserContact.class, cascade = CascadeType.REFRESH)
93 private Contact addressbookEntryPrivateContact;
96 * When this address book entry has been updated
98 @Temporal (TemporalType.TIMESTAMP)
99 @Column (name = "entry_updated", insertable = false, nullable = false)
100 private Date addressbookEntryUpdated;
103 * Connection to table "address books"
105 @JoinColumn (name = "entry_addressbook_id", nullable = false, updatable = false)
106 @OneToOne (targetEntity = UserAddressbook.class, cascade = CascadeType.REFRESH, optional = false)
107 private Addressbook entryAddressbook;
110 public int compareTo (final AddressbookEntry addressbookEntry) {
111 // Parameter should not be NULL
112 if (null == addressbookEntry) {
114 throw new NullPointerException("addressbookEntry is null"); //NOI18N
115 } else if (addressbookEntry.equals(this)) {
121 final int comparitors[] = {
122 // First compare address books
123 this.getEntryAddressbook().compareTo(addressbookEntry.getEntryAddressbook()),
124 // ... then business address
125 this.getAddressbookEntryBusinessBasicData().compareTo(addressbookEntry.getAddressbookEntryBusinessBasicData()),
126 // ... then private contact
127 this.getAddressbookEntryPrivateContact().compareTo(addressbookEntry.getAddressbookEntryPrivateContact())
131 final int comparison = Comparables.checkAll(comparitors);
138 public boolean equals (final Object object) {
139 if (null == object) {
141 } else if (this.getClass() != object.getClass()) {
145 final AddressbookEntry addressbookEntry = (AddressbookEntry) object;
147 if (!Objects.equals(this.getAddressbookEntryBusinessBasicData(), addressbookEntry.getAddressbookEntryBusinessBasicData())) {
149 } else if (!Objects.equals(this.getAddressbookEntryId(), addressbookEntry.getAddressbookEntryId())) {
151 } else if (!Objects.equals(this.getAddressbookEntryPrivateContact(), addressbookEntry.getAddressbookEntryPrivateContact())) {
153 } else if (!Objects.equals(this.getEntryAddressbook(), addressbookEntry.getEntryAddressbook())) {
161 public BasicData getAddressbookEntryBusinessBasicData () {
162 return this.addressbookEntryBusinessBasicData;
166 public void setAddressbookEntryBusinessBasicData (final BasicData addressbookEntryBusinessBasicData) {
167 this.addressbookEntryBusinessBasicData = addressbookEntryBusinessBasicData;
171 @SuppressWarnings ("ReturnOfDateField")
172 public Date getAddressbookEntryCreated () {
173 return this.addressbookEntryCreated;
177 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
178 public void setAddressbookEntryCreated (final Date addressbookEntryCreated) {
179 this.addressbookEntryCreated = addressbookEntryCreated;
183 public Long getAddressbookEntryId () {
184 return this.addressbookEntryId;
188 public void setAddressbookEntryId (final Long addressbookEntryId) {
189 this.addressbookEntryId = addressbookEntryId;
193 public Contact getAddressbookEntryPrivateContact () {
194 return this.addressbookEntryPrivateContact;
198 public void setAddressbookEntryPrivateContact (final Contact addressbookEntryPrivateContact) {
199 this.addressbookEntryPrivateContact = addressbookEntryPrivateContact;
203 @SuppressWarnings ("ReturnOfDateField")
204 public Date getAddressbookEntryUpdated () {
205 return this.addressbookEntryUpdated;
209 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
210 public void setAddressbookEntryUpdated (final Date addressbookEntryUpdated) {
211 this.addressbookEntryUpdated = addressbookEntryUpdated;
215 public Addressbook getEntryAddressbook () {
216 return this.entryAddressbook;
220 public void setEntryAddressbook (final Addressbook entryAddressbook) {
221 this.entryAddressbook = entryAddressbook;
225 public int hashCode () {
228 hash = 19 * hash + Objects.hashCode(this.getAddressbookEntryBusinessBasicData());
229 hash = 19 * hash + Objects.hashCode(this.getAddressbookEntryId());
230 hash = 19 * hash + Objects.hashCode(this.getAddressbookEntryPrivateContact());
231 hash = 19 * hash + Objects.hashCode(this.getEntryAddressbook());