]> git.mxchange.org Git - jcontacts-core.git/blob - src/org/mxchange/jcontacts/contact/UserContact.java
check on user instance
[jcontacts-core.git] / src / org / mxchange / jcontacts / contact / UserContact.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
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.jcontacts.contact;
18
19 import java.util.Calendar;
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.EnumType;
27 import javax.persistence.Enumerated;
28 import javax.persistence.GeneratedValue;
29 import javax.persistence.GenerationType;
30 import javax.persistence.Id;
31 import javax.persistence.Index;
32 import javax.persistence.JoinColumn;
33 import javax.persistence.Lob;
34 import javax.persistence.NamedQueries;
35 import javax.persistence.NamedQuery;
36 import javax.persistence.OneToOne;
37 import javax.persistence.Table;
38 import javax.persistence.Temporal;
39 import javax.persistence.TemporalType;
40 import org.mxchange.jcontacts.contact.gender.Gender;
41 import org.mxchange.jcountry.data.Country;
42 import org.mxchange.jcountry.data.CountryData;
43 import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
44 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
45 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
46 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
47 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
48 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
49
50 /**
51  * A general contact class which serves as an entity.
52  * <p>
53  * @author Roland Haeder<roland@mxchange.org>
54  * @version 0.0
55  */
56 @Entity (name = "contacts")
57 @Table (
58                 name = "contacts",
59                 indexes = {
60                         @Index (
61                                         name = "contact_gender",
62                                         columnList = "contact_gender"
63                         ),
64                         @Index (
65                                         name = "contact_email_address",
66                                         unique = true,
67                                         columnList = "contact_email_address"
68                         )
69                 }
70 )
71 @NamedQueries (
72                 {
73                         @NamedQuery( name = "AllContacts", query = "SELECT c FROM contacts AS c ORDER BY c.contactId ASC"),
74                         @NamedQuery( name = "AllContactEmailAddresses", query = "SELECT c.contactEmailAddress FROM contacts AS c ORDER BY c.contactId ASC"),
75                         @NamedQuery (name = "AllContactsByCellphone", query = "SELECT c FROM contacts AS c WHERE c.contactCellphoneNumber = :cellPhone ORDER BY c.contactId ASC"),
76                         @NamedQuery (name = "SearchContactById", query = "SELECT c FROM contacts AS c WHERE c.contactId = :contactId")
77                 }
78 )
79 public class UserContact implements Contact {
80
81         /**
82          * Serial number
83          */
84         private static final long serialVersionUID = 58_744_284_981_863L;
85
86         /**
87          * Birth day
88          */
89         @Column (name = "contact_birthday")
90         @Temporal (TemporalType.DATE)
91         private Date contactBirthday;
92
93         /**
94          * Cellphone number
95          */
96         @JoinColumn (name = "contact_cellphone_number_id", referencedColumnName = "cellphone_id", unique = true)
97         @OneToOne (targetEntity = CellphoneNumber.class, cascade = CascadeType.ALL)
98         private DialableCellphoneNumber contactCellphoneNumber;
99
100         /**
101          * City
102          */
103         @Basic (optional = false)
104         @Column (name = "contact_city", nullable = false, length = 100)
105         private String contactCity;
106
107         /**
108          * Optional comments
109          */
110         @Lob
111         @Column (name = "contact_comment")
112         private String contactComment;
113
114         /**
115          * Country code
116          */
117         @JoinColumn (name = "contact_country_id", nullable = false, referencedColumnName = "country_id")
118         @OneToOne (targetEntity = CountryData.class, cascade = CascadeType.REFRESH, optional = false)
119         private Country contactCountry;
120
121         /**
122          * When the contact has been created
123          */
124         @Basic (optional = false)
125         @Temporal (TemporalType.TIMESTAMP)
126         @Column (name = "contact_created", nullable = false)
127         private Calendar contactCreated;
128
129         /**
130          * Email address
131          */
132         @Basic (optional = false)
133         @Column (name = "contact_email_address", length = 100, nullable = false)
134         private String contactEmailAddress;
135
136         /**
137          * Family name
138          */
139         @Basic (optional = false)
140         @Column (name = "contact_family_name", length = 100, nullable = false)
141         private String contactFamilyName;
142
143         /**
144          * Fax number
145          */
146         @JoinColumn (name = "contact_fax_number_id", referencedColumnName = "fax_id", unique = true)
147         @OneToOne (targetEntity = FaxNumber.class, cascade = CascadeType.ALL)
148         private DialableFaxNumber contactFaxNumber;
149
150         /**
151          * First name
152          */
153         @Basic (optional = false)
154         @Column (name = "contact_first_name", length = 100, nullable = false)
155         private String contactFirstName;
156
157         /**
158          * Gender instance
159          */
160         @Basic (optional = false)
161         @Column (name = "contact_gender", nullable = false)
162         @Enumerated (EnumType.STRING)
163         private Gender contactGender;
164
165         /**
166          * House number
167          */
168         @Basic (optional = false)
169         @Column (name = "contact_house_number", length = 5, nullable = false)
170         private Short contactHouseNumber;
171
172         /**
173          * Id number
174          */
175         @Id
176         @GeneratedValue (strategy = GenerationType.IDENTITY)
177         @Column (name = "contact_id", nullable = false, updatable = false)
178         private Long contactId;
179
180         /**
181          * Flag whether this contact is user's own data
182          */
183         @Basic (optional = false)
184         @Column (name = "contact_own_contact", nullable = false)
185         private Boolean contactOwnContact;
186
187         /**
188          * Phone number
189          */
190         @JoinColumn (name = "contact_phone_number_id", referencedColumnName = "phone_id", unique = true)
191         @OneToOne (targetEntity = LandLineNumber.class, cascade = CascadeType.ALL)
192         private DialableLandLineNumber contactPhoneNumber;
193
194         /**
195          * Street
196          */
197         @Basic (optional = false)
198         @Column (name = "contact_street", nullable = false)
199         private String contactStreet;
200
201         /**
202          * Title (Doctor, etc)
203          */
204         @Column (name = "contact_title")
205         private String contactTitle;
206
207         /**
208          * When the contact has been updated
209          */
210         @Temporal (TemporalType.TIMESTAMP)
211         @Column (name = "contact_updated")
212         private Calendar contactUpdated;
213
214         /**
215          * ZIP code
216          */
217         @Basic (optional = false)
218         @Column (name = "contact_zip_code", nullable = false, length = 6)
219         private Integer contactZipCode;
220
221         /**
222          * Default constructor
223          */
224         public UserContact () {
225                 // Default is not user's own contact
226                 this.contactOwnContact = Boolean.FALSE;
227
228                 // Unknown gender
229                 this.contactGender = Gender.UNKNOWN;
230         }
231
232         /**
233          * Constructor for contactGender and names
234          * <p>
235          * @param contactGender     Gender instance
236          * @param contactFirstName  First name
237          * @param contactFamilyName Family name
238          */
239         public UserContact (final Gender contactGender, final String contactFirstName, final String contactFamilyName) {
240                 // Call default constructor
241                 this();
242
243                 // Set all
244                 this.contactGender = contactGender;
245                 this.contactFirstName = contactFirstName;
246                 this.contactFamilyName = contactFamilyName;
247         }
248
249         @Override
250         public void copyAll (final Contact contact) {
251                 // Contact should be valid
252                 if (null == contact) {
253                         // Throw NPE
254                         throw new NullPointerException("contact is null"); //NOI18N
255                 }
256
257                 // Copy all:
258                 // - base data
259                 this.setContactFirstName(contact.getContactFirstName());
260                 this.setContactFamilyName(contact.getContactFamilyName());
261                 this.setContactStreet(contact.getContactStreet());
262                 this.setContactZipCode(contact.getContactZipCode());
263                 this.setContactCity(contact.getContactCity());
264                 this.setContactCountry(contact.getContactCountry());
265
266                 // - phone, fax, email
267                 this.setContactLandLineNumber(contact.getContactLandLineNumber());
268                 this.setContactFaxNumber(contact.getContactFaxNumber());
269                 this.setContactCellphoneNumber(contact.getContactCellphoneNumber());
270
271                 // - other data
272                 this.setContactBirthday(contact.getContactBirthday());
273                 this.setContactComment(contact.getContactComment());
274                 this.setContactCreated(contact.getContactCreated());
275                 this.setContactUpdated(contact.getContactUpdated());
276         }
277
278         @Override
279         public boolean equals (final Object object) {
280                 // Is it same type?
281                 if (!(object instanceof UserContact)) {
282                         // Not equal types
283                         return false;
284                 } else if (!(object instanceof Contact)) {
285                         // Not correct interface
286                         return false;
287                 }
288
289                 // Try to cast
290                 Contact contact = (Contact) object;
291
292                 // Now test some data TODO Definedly needs improvement
293                 return ((this.getContactGender().equals(contact.getContactGender())) &&
294                                 (this.getContactFirstName().toLowerCase().equals(contact.getContactFirstName().toLowerCase())) &&
295                                 (this.getContactFamilyName().toLowerCase().equals(contact.getContactFamilyName().toLowerCase())));
296         }
297
298         @Override
299         public int hashCode () {
300                 // Validate contactGender instance
301                 assert (this.getContactGender() instanceof Gender) : "gender is not set."; //NOI18N
302
303                 int hash = 7;
304                 hash = 79 * hash + Objects.hashCode(this.getContactFamilyName());
305                 hash = 79 * hash + this.getContactGender().hashCode();
306                 hash = 79 * hash + Objects.hashCode(this.getContactFirstName());
307                 return hash;
308         }
309
310         @Override
311         public Date getContactBirthday () {
312                 return this.contactBirthday;
313         }
314
315         @Override
316         public void setContactBirthday (final Date contactBirthday) {
317                 this.contactBirthday = contactBirthday;
318         }
319
320         @Override
321         public DialableCellphoneNumber getContactCellphoneNumber () {
322                 return this.contactCellphoneNumber;
323         }
324
325         @Override
326         public void setContactCellphoneNumber (final DialableCellphoneNumber contactCellphoneNumber) {
327                 this.contactCellphoneNumber = contactCellphoneNumber;
328         }
329
330         @Override
331         public String getContactCity () {
332                 return this.contactCity;
333         }
334
335         @Override
336         public void setContactCity (final String contactCity) {
337                 this.contactCity = contactCity;
338         }
339
340         @Override
341         public String getContactComment () {
342                 return this.contactComment;
343         }
344
345         @Override
346         public void setContactComment (final String contactComment) {
347                 this.contactComment = contactComment;
348         }
349
350         @Override
351         public Country getContactCountry () {
352                 return this.contactCountry;
353         }
354
355         @Override
356         public void setContactCountry (final Country contactCountry) {
357                 this.contactCountry = contactCountry;
358         }
359
360         @Override
361         public Calendar getContactCreated () {
362                 return this.contactCreated;
363         }
364
365         @Override
366         public void setContactCreated (final Calendar contactCreated) {
367                 this.contactCreated = contactCreated;
368         }
369
370         @Override
371         public String getContactEmailAddress () {
372                 return this.contactEmailAddress;
373         }
374
375         @Override
376         public void setContactEmailAddress (final String contactEmailAddress) {
377                 this.contactEmailAddress = contactEmailAddress;
378         }
379
380         @Override
381         public String getContactFamilyName () {
382                 //* NOISY-DEBUG: */ this.getLogger().logTrace("CALLED!");
383                 return this.contactFamilyName;
384         }
385
386         @Override
387         public void setContactFamilyName (final String contactFamilyName) {
388                 this.contactFamilyName = contactFamilyName;
389         }
390
391         @Override
392         public DialableFaxNumber getContactFaxNumber () {
393                 return this.contactFaxNumber;
394         }
395
396         @Override
397         public void setContactFaxNumber (final DialableFaxNumber contactFaxNumber) {
398                 this.contactFaxNumber = contactFaxNumber;
399         }
400
401         @Override
402         public String getContactFirstName () {
403                 return this.contactFirstName;
404         }
405
406         @Override
407         public void setContactFirstName (final String contactFirstName) {
408                 this.contactFirstName = contactFirstName;
409         }
410
411         @Override
412         public Gender getContactGender () {
413                 return this.contactGender;
414         }
415
416         @Override
417         public void setContactGender (final Gender contactGender) {
418                 this.contactGender = contactGender;
419         }
420
421         @Override
422         public Short getContactHouseNumber () {
423                 return this.contactHouseNumber;
424         }
425
426         @Override
427         public void setContactHouseNumber (final Short contactHouseNumber) {
428                 this.contactHouseNumber = contactHouseNumber;
429         }
430
431         @Override
432         public Long getContactId () {
433                 return this.contactId;
434         }
435
436         @Override
437         public void setContactId (final Long contactId) {
438                 this.contactId = contactId;
439         }
440
441         @Override
442         public void setContactOwnContact (final Boolean contactOwnContact) {
443                 this.contactOwnContact = contactOwnContact;
444         }
445
446         @Override
447         public DialableLandLineNumber getContactLandLineNumber () {
448                 return this.contactPhoneNumber;
449         }
450
451         @Override
452         public void setContactLandLineNumber (final DialableLandLineNumber contactPhoneNumber) {
453                 this.contactPhoneNumber = contactPhoneNumber;
454         }
455
456         @Override
457         public String getContactStreet () {
458                 return this.contactStreet;
459         }
460
461         @Override
462         public void setContactStreet (final String contactStreet) {
463                 this.contactStreet = contactStreet;
464         }
465
466         @Override
467         public String getContactTitle () {
468                 return this.contactTitle;
469         }
470
471         @Override
472         public void setContactTitle (final String contactTitle) {
473                 this.contactTitle = contactTitle;
474         }
475
476         @Override
477         public Calendar getContactUpdated () {
478                 return this.contactUpdated;
479         }
480
481         @Override
482         public void setContactUpdated (final Calendar contactUpdated) {
483                 this.contactUpdated = contactUpdated;
484         }
485
486         @Override
487         public Integer getContactZipCode () {
488                 return this.contactZipCode;
489         }
490
491         @Override
492         public void setContactZipCode (final Integer contactZipCode) {
493                 this.contactZipCode = contactZipCode;
494         }
495
496         @Override
497         public Boolean isOwnContact () {
498                 return this.contactOwnContact;
499         }
500 }