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