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