]> git.mxchange.org Git - jaddressbook-core.git/blob - src/org/mxchange/jaddressbook/model/addressbook/entry/UserAddressbookEntry.java
updated references
[jaddressbook-core.git] / src / org / mxchange / jaddressbook / model / addressbook / entry / UserAddressbookEntry.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 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.jaddressbook.model.addressbook.entry;
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.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.comparable.Comparables;
43
44 /**
45  * A POJO for address book entries
46  * <p>
47  * @author Roland Häder<roland@mxchange.org>
48  */
49 @Entity (name = "addressbook_entries")
50 @Table (name = "addressbook_entries")
51 @NamedQueries (
52                 {
53                         @NamedQuery (name = "AllAddressBookEntries", query = "SELECT e FROM addressbook_entries AS e ORDER BY e.addressbookEntryId ASC")
54                 }
55 )
56 @SuppressWarnings ("PersistenceUnitPresent")
57 public class UserAddressbookEntry implements AddressbookEntry {
58
59         /**
60          * Serial number
61          */
62         @Transient
63         private static final long serialVersionUID = 178_581_768_581_960L;
64
65         /**
66          * Connection to table "business_contacts" (commercial contacts)
67          */
68         @JoinColumn (name = "entry_business_contact_id", updatable = false)
69         @OneToOne (targetEntity = BusinessBasicData.class, cascade = CascadeType.REFRESH)
70         private BasicData addressbookEntryBusinessBasicData;
71
72         /**
73          * When this address book entry has been created
74          */
75         @Basic (optional = false)
76         @Temporal (TemporalType.TIMESTAMP)
77         @Column (name = "entry_created", updatable = false, nullable = false)
78         private Date addressbookEntryCreated;
79
80         /**
81          * Id number
82          */
83         @Id
84         @GeneratedValue (strategy = GenerationType.IDENTITY)
85         @Column (name = "entry_id", nullable = false, updatable = false)
86         private Long addressbookEntryId;
87
88         /**
89          * Connection to table "contacts" (private contacts)
90          */
91         @JoinColumn (name = "entry_private_contact_id", updatable = false)
92         @OneToOne (targetEntity = UserContact.class, cascade = CascadeType.REFRESH)
93         private Contact addressbookEntryPrivateContact;
94
95         /**
96          * When this address book entry has been updated
97          */
98         @Temporal (TemporalType.TIMESTAMP)
99         @Column (name = "entry_updated", insertable = false, nullable = false)
100         private Date addressbookEntryUpdated;
101
102         /**
103          * Connection to table "address books"
104          */
105         @JoinColumn (name = "entry_addressbook_id", nullable = false, updatable = false)
106         @OneToOne (targetEntity = UserAddressbook.class, cascade = CascadeType.REFRESH, optional = false)
107         private Addressbook entryAddressbook;
108
109         @Override
110         public int compareTo (final AddressbookEntry addressbookEntry) {
111                 // Parameter should not be NULL
112                 if (null == addressbookEntry) {
113                         // Should not happen
114                         throw new NullPointerException("addressbookEntry is null"); //NOI18N
115                 } else if (addressbookEntry.equals(this)) {
116                         // Same object
117                         return 0;
118                 }
119
120                 // Init comparitors
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())
128                 };
129
130                 // Check all vaules
131                 final int comparison = Comparables.checkAll(comparitors);
132
133                 // Return value
134                 return comparison;
135         }
136
137         @Override
138         public boolean equals (final Object object) {
139                 if (null == object) {
140                         return false;
141                 } else if (this.getClass() != object.getClass()) {
142                         return false;
143                 }
144
145                 final AddressbookEntry addressbookEntry = (AddressbookEntry) object;
146
147                 if (!Objects.equals(this.getAddressbookEntryBusinessBasicData(), addressbookEntry.getAddressbookEntryBusinessBasicData())) {
148                         return false;
149                 } else if (!Objects.equals(this.getAddressbookEntryId(), addressbookEntry.getAddressbookEntryId())) {
150                         return false;
151                 } else if (!Objects.equals(this.getAddressbookEntryPrivateContact(), addressbookEntry.getAddressbookEntryPrivateContact())) {
152                         return false;
153                 } else if (!Objects.equals(this.getEntryAddressbook(), addressbookEntry.getEntryAddressbook())) {
154                         return false;
155                 }
156
157                 return true;
158         }
159
160         @Override
161         public BasicData getAddressbookEntryBusinessBasicData () {
162                 return this.addressbookEntryBusinessBasicData;
163         }
164
165         @Override
166         public void setAddressbookEntryBusinessBasicData (final BasicData addressbookEntryBusinessBasicData) {
167                 this.addressbookEntryBusinessBasicData = addressbookEntryBusinessBasicData;
168         }
169
170         @Override
171         @SuppressWarnings ("ReturnOfDateField")
172         public Date getAddressbookEntryCreated () {
173                 return this.addressbookEntryCreated;
174         }
175
176         @Override
177         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
178         public void setAddressbookEntryCreated (final Date addressbookEntryCreated) {
179                 this.addressbookEntryCreated = addressbookEntryCreated;
180         }
181
182         @Override
183         public Long getAddressbookEntryId () {
184                 return this.addressbookEntryId;
185         }
186
187         @Override
188         public void setAddressbookEntryId (final Long addressbookEntryId) {
189                 this.addressbookEntryId = addressbookEntryId;
190         }
191
192         @Override
193         public Contact getAddressbookEntryPrivateContact () {
194                 return this.addressbookEntryPrivateContact;
195         }
196
197         @Override
198         public void setAddressbookEntryPrivateContact (final Contact addressbookEntryPrivateContact) {
199                 this.addressbookEntryPrivateContact = addressbookEntryPrivateContact;
200         }
201
202         @Override
203         @SuppressWarnings ("ReturnOfDateField")
204         public Date getAddressbookEntryUpdated () {
205                 return this.addressbookEntryUpdated;
206         }
207
208         @Override
209         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
210         public void setAddressbookEntryUpdated (final Date addressbookEntryUpdated) {
211                 this.addressbookEntryUpdated = addressbookEntryUpdated;
212         }
213
214         @Override
215         public Addressbook getEntryAddressbook () {
216                 return this.entryAddressbook;
217         }
218
219         @Override
220         public void setEntryAddressbook (final Addressbook entryAddressbook) {
221                 this.entryAddressbook = entryAddressbook;
222         }
223
224         @Override
225         public int hashCode () {
226                 int hash = 3;
227
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());
232
233                 return hash;
234         }
235
236 }