]> git.mxchange.org Git - jcontacts-core.git/blob - src/org/mxchange/jcontacts/contact/UserContact.java
rewrote to use unique proptery to avoid another @Index (lesser code)
[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 c.contactEmailAddress = :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", length = 5, nullable = false)
166         private Short contactHouseNumber;
167
168         /**
169          * Id number
170          */
171         @Id
172         @GeneratedValue (strategy = GenerationType.IDENTITY)
173         @Column (name = "contact_id", nullable = false, updatable = false)
174         private Long contactId;
175
176         /**
177          * Flag whether this contact is user's own data
178          */
179         @Basic (optional = false)
180         @Column (name = "contact_own_contact", nullable = false)
181         private Boolean contactOwnContact;
182
183         /**
184          * Phone number
185          */
186         @JoinColumn (name = "contact_phone_number_id", referencedColumnName = "phone_id", unique = true)
187         @OneToOne (targetEntity = LandLineNumber.class, cascade = CascadeType.ALL)
188         private DialableLandLineNumber contactPhoneNumber;
189
190         /**
191          * Street
192          */
193         @Basic (optional = false)
194         @Column (name = "contact_street", nullable = false)
195         private String contactStreet;
196
197         /**
198          * Title (Doctor, etc)
199          */
200         @Column (name = "contact_title")
201         private String contactTitle;
202
203         /**
204          * When the contact has been updated
205          */
206         @Temporal (TemporalType.TIMESTAMP)
207         @Column (name = "contact_updated")
208         private Calendar contactUpdated;
209
210         /**
211          * ZIP code
212          */
213         @Basic (optional = false)
214         @Column (name = "contact_zip_code", nullable = false, length = 6)
215         private Integer contactZipCode;
216
217         /**
218          * Default constructor
219          */
220         public UserContact () {
221                 // Default is not user's own contact
222                 this.contactOwnContact = Boolean.FALSE;
223
224                 // Unknown gender
225                 this.contactGender = Gender.UNKNOWN;
226         }
227
228         /**
229          * Constructor for contactGender and names
230          * <p>
231          * @param contactGender Gender instance
232          * @param contactFirstName First name
233          * @param contactFamilyName Family name
234          */
235         public UserContact (final Gender contactGender, final String contactFirstName, final String contactFamilyName) {
236                 // Call default constructor
237                 this();
238
239                 // Set all
240                 this.contactGender = contactGender;
241                 this.contactFirstName = contactFirstName;
242                 this.contactFamilyName = contactFamilyName;
243         }
244
245         @Override
246         public void copyAll (final Contact contact) {
247                 // Contact should be valid
248                 if (null == contact) {
249                         // Throw NPE
250                         throw new NullPointerException("contact is null"); //NOI18N
251                 }
252
253                 // Copy all:
254                 // - base data
255                 this.setContactFirstName(contact.getContactFirstName());
256                 this.setContactFamilyName(contact.getContactFamilyName());
257                 this.setContactStreet(contact.getContactStreet());
258                 this.setContactZipCode(contact.getContactZipCode());
259                 this.setContactCity(contact.getContactCity());
260                 this.setContactCountry(contact.getContactCountry());
261
262                 // - phone, fax, email
263                 this.setContactLandLineNumber(contact.getContactLandLineNumber());
264                 this.setContactFaxNumber(contact.getContactFaxNumber());
265                 this.setContactCellphoneNumber(contact.getContactCellphoneNumber());
266
267                 // - other data
268                 this.setContactBirthday(contact.getContactBirthday());
269                 this.setContactComment(contact.getContactComment());
270                 this.setContactCreated(contact.getContactCreated());
271                 this.setContactUpdated(contact.getContactUpdated());
272         }
273
274         @Override
275         public boolean equals (final Object object) {
276                 // Is it same type?
277                 if (null == object) {
278                         // Is null
279                         return false;
280                 } else if (!(object instanceof UserContact)) {
281                         // Not equal types
282                         return false;
283                 } else if (!(object instanceof Contact)) {
284                         // Not correct interface
285                         return false;
286                 }
287
288                 // Try to cast
289                 Contact contact = (Contact) object;
290
291                 // Now test some data TODO Definedly needs improvement
292                 return ((this.getContactGender().equals(contact.getContactGender())) &&
293                                 (this.getContactFirstName().toLowerCase().equals(contact.getContactFirstName().toLowerCase())) &&
294                                 (this.getContactFamilyName().toLowerCase().equals(contact.getContactFamilyName().toLowerCase())));
295         }
296
297         @Override
298         public int hashCode () {
299                 // Validate contactGender instance
300                 assert (this.getContactGender() instanceof Gender) : "gender is not set."; //NOI18N
301
302                 int hash = 7;
303                 hash = 79 * hash + Objects.hashCode(this.getContactFamilyName());
304                 hash = 79 * hash + this.getContactGender().hashCode();
305                 hash = 79 * hash + Objects.hashCode(this.getContactFirstName());
306                 return hash;
307         }
308
309         @Override
310         @SuppressWarnings ("ReturnOfDateField")
311         public Date getContactBirthday () {
312                 return this.contactBirthday;
313         }
314
315         @Override
316         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
317         public void setContactBirthday (final Date contactBirthday) {
318                 this.contactBirthday = contactBirthday;
319         }
320
321         @Override
322         public DialableCellphoneNumber getContactCellphoneNumber () {
323                 return this.contactCellphoneNumber;
324         }
325
326         @Override
327         public void setContactCellphoneNumber (final DialableCellphoneNumber contactCellphoneNumber) {
328                 this.contactCellphoneNumber = contactCellphoneNumber;
329         }
330
331         @Override
332         public String getContactCity () {
333                 return this.contactCity;
334         }
335
336         @Override
337         public void setContactCity (final String contactCity) {
338                 this.contactCity = contactCity;
339         }
340
341         @Override
342         public String getContactComment () {
343                 return this.contactComment;
344         }
345
346         @Override
347         public void setContactComment (final String contactComment) {
348                 this.contactComment = contactComment;
349         }
350
351         @Override
352         public Country getContactCountry () {
353                 return this.contactCountry;
354         }
355
356         @Override
357         public void setContactCountry (final Country contactCountry) {
358                 this.contactCountry = contactCountry;
359         }
360
361         @Override
362         @SuppressWarnings ("ReturnOfDateField")
363         public Calendar getContactCreated () {
364                 return this.contactCreated;
365         }
366
367         @Override
368         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
369         public void setContactCreated (final Calendar contactCreated) {
370                 this.contactCreated = contactCreated;
371         }
372
373         @Override
374         public String getContactEmailAddress () {
375                 return this.contactEmailAddress;
376         }
377
378         @Override
379         public void setContactEmailAddress (final String contactEmailAddress) {
380                 this.contactEmailAddress = contactEmailAddress;
381         }
382
383         @Override
384         public String getContactFamilyName () {
385                 //* NOISY-DEBUG: */ this.getLogger().logTrace("CALLED!");
386                 return this.contactFamilyName;
387         }
388
389         @Override
390         public void setContactFamilyName (final String contactFamilyName) {
391                 this.contactFamilyName = contactFamilyName;
392         }
393
394         @Override
395         public DialableFaxNumber getContactFaxNumber () {
396                 return this.contactFaxNumber;
397         }
398
399         @Override
400         public void setContactFaxNumber (final DialableFaxNumber contactFaxNumber) {
401                 this.contactFaxNumber = contactFaxNumber;
402         }
403
404         @Override
405         public String getContactFirstName () {
406                 return this.contactFirstName;
407         }
408
409         @Override
410         public void setContactFirstName (final String contactFirstName) {
411                 this.contactFirstName = contactFirstName;
412         }
413
414         @Override
415         public Gender getContactGender () {
416                 return this.contactGender;
417         }
418
419         @Override
420         public void setContactGender (final Gender contactGender) {
421                 this.contactGender = contactGender;
422         }
423
424         @Override
425         public Short getContactHouseNumber () {
426                 return this.contactHouseNumber;
427         }
428
429         @Override
430         public void setContactHouseNumber (final Short contactHouseNumber) {
431                 this.contactHouseNumber = contactHouseNumber;
432         }
433
434         @Override
435         public Long getContactId () {
436                 return this.contactId;
437         }
438
439         @Override
440         public void setContactId (final Long contactId) {
441                 this.contactId = contactId;
442         }
443
444         @Override
445         public void setContactOwnContact (final Boolean contactOwnContact) {
446                 this.contactOwnContact = contactOwnContact;
447         }
448
449         @Override
450         public DialableLandLineNumber getContactLandLineNumber () {
451                 return this.contactPhoneNumber;
452         }
453
454         @Override
455         public void setContactLandLineNumber (final DialableLandLineNumber contactPhoneNumber) {
456                 this.contactPhoneNumber = contactPhoneNumber;
457         }
458
459         @Override
460         public String getContactStreet () {
461                 return this.contactStreet;
462         }
463
464         @Override
465         public void setContactStreet (final String contactStreet) {
466                 this.contactStreet = contactStreet;
467         }
468
469         @Override
470         public String getContactTitle () {
471                 return this.contactTitle;
472         }
473
474         @Override
475         public void setContactTitle (final String contactTitle) {
476                 this.contactTitle = contactTitle;
477         }
478
479         @Override
480         @SuppressWarnings ("ReturnOfDateField")
481         public Calendar getContactUpdated () {
482                 return this.contactUpdated;
483         }
484
485         @Override
486         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
487         public void setContactUpdated (final Calendar contactUpdated) {
488                 this.contactUpdated = contactUpdated;
489         }
490
491         @Override
492         public Integer getContactZipCode () {
493                 return this.contactZipCode;
494         }
495
496         @Override
497         public void setContactZipCode (final Integer contactZipCode) {
498                 this.contactZipCode = contactZipCode;
499         }
500
501         @Override
502         public Boolean isOwnContact () {
503                 return this.contactOwnContact;
504         }
505
506 }