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