]> git.mxchange.org Git - jcontacts-core.git/blob - src/org/mxchange/jcontacts/contact/UserContact.java
47554565789d91bf89924f6fbe5d043b7e9e625f
[jcontacts-core.git] / src / org / mxchange / jcontacts / contact / UserContact.java
1 /*
2  * Copyright (C) 2015 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.annotation.PostConstruct;
23 import javax.persistence.Basic;
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.Lob;
32 import javax.persistence.Table;
33 import javax.persistence.Temporal;
34 import javax.persistence.TemporalType;
35 import org.mxchange.jcontacts.contact.gender.Gender;
36
37 /**
38  * A general contact class which should only be extended.
39  * <p>
40  * @author Roland Haeder<roland@mxchange.org>
41  * @version 0.0
42  */
43 @Entity (name = "contacts")
44 @Table (name = "contacts")
45 public class UserContact implements Contact, Comparable<Contact> {
46
47         /**
48          * Serial number
49          */
50         private static final long serialVersionUID = 58_744_284_981_863L;
51
52         /**
53          * Birth day
54          */
55         @Column (name = "birthday")
56         @Temporal (TemporalType.DATE)
57         private Date birthday;
58
59         /**
60          * Cellphone number
61          */
62         @Column (name = "cellphone_number", length = 100)
63         private String cellphoneNumber;
64
65         /**
66          * City
67          */
68         @Column (name = "city", nullable = false, length = 100)
69         private String city;
70
71         /**
72          * Optional comments
73          */
74         @Lob
75         @Column (name = "comment")
76         private String comment;
77
78         /**
79          * Company name
80          */
81         @Column (name = "company_name", nullable = false)
82         private String companyName;
83
84         /**
85          * Id number
86          */
87         @Id
88         @GeneratedValue (strategy = GenerationType.IDENTITY)
89         @Column (name = "contact_id", length = 20, updatable = false)
90         private Long contactId;
91
92         /**
93          * Country code
94          */
95         @Column (name = "country_code", length = 2, nullable = false)
96         private String countryCode;
97
98         /**
99          * When the contact has been created
100          */
101         @Basic (optional = false)
102         @Temporal (TemporalType.TIMESTAMP)
103         @Column (name = "created", nullable = false)
104         private Calendar created;
105
106         /**
107          * Email address
108          */
109         @Column (name = "email_address", length = 100, nullable = false)
110         private String emailAddress;
111
112         /**
113          * Family name
114          */
115         @Basic (optional = false)
116         @Column (name = "family_name", length = 100, nullable = false)
117         private String familyName;
118
119         /**
120          * Fax number
121          */
122         @Column (name = "fax_number", length = 100)
123         private String faxNumber;
124
125         /**
126          * First name
127          */
128         @Basic (optional = false)
129         @Column (name = "first_name", length = 100, nullable = false)
130         private String firstName;
131
132         /**
133          * Gender instance
134          */
135         @Basic (optional = false)
136         @Column (name = "gender", nullable = false)
137         @Enumerated (EnumType.STRING)
138         private Gender gender;
139
140         /**
141          * House number
142          */
143         @Column (name = "house_number", length = 5, nullable = false)
144         private Short houseNumber;
145
146         /**
147          * Flag whether this contact is user's own data
148          */
149         @Column (name = "own_contact", nullable = false)
150         private Boolean ownContact;
151
152         /**
153          * Phone number
154          */
155         @Column (name = "phone_number", length = 100)
156         private String phoneNumber;
157
158         /**
159          * Street
160          */
161         @Column (name = "street", nullable = false)
162         private String street;
163
164         /**
165          * When the contact has been updated
166          */
167         @Temporal (TemporalType.TIMESTAMP)
168         @Column (name = "updated")
169         private Calendar updated;
170
171         /**
172          * ZIP code
173          */
174         @Column (name = "zip_code", nullable = false, length = 6)
175         private Long zipCode;
176
177         /**
178          * Constructor for gender and names
179          * <p>
180          * @param gender Gender instance
181          * @param firstName First name
182          * @param familyName Family name
183          * @param companyName Company name
184          */
185         public UserContact (final Gender gender, final String firstName, final String familyName, final String companyName) {
186                 // Set all
187                 this.gender = gender;
188                 this.firstName = firstName;
189                 this.familyName = familyName;
190         }
191
192         /**
193          * Default constructor
194          */
195         public UserContact () {
196         }
197
198         /**
199          * Compares two contacts with each other
200          * <p>
201          * @param contact Contact comparator
202          * @return Comparison value
203          */
204         @Override
205         public int compareTo (final Contact contact) {
206                 // contact should not be null
207                 if (null == contact) {
208                         throw new NullPointerException("contact is null"); //NOI18N
209                 }
210
211                 // Is the contactId the same?
212                 if (Objects.equals(this.getContactId(), contact.getContactId())) {
213                         // Same contactId, means same contact
214                         return 0;
215                 } else if (this.getContactId() > contact.getContactId()) {
216                         // This contactId is larger than compared to
217                         return -1;
218                 }
219
220                 // The other contactId is larger
221                 return 1;
222         }
223
224         @Override
225         public void copyAll (final Contact contact) {
226                 // Copy all:
227                 // - base data
228                 this.setFirstName(contact.getFirstName());
229                 this.setFamilyName(contact.getFamilyName());
230                 this.setCompanyName(contact.getCompanyName());
231                 this.setStreet(contact.getStreet());
232                 this.setZipCode(contact.getZipCode());
233                 this.setCity(contact.getCity());
234                 this.setCountryCode(contact.getCountryCode());
235
236                 // - phone, fax, email
237                 this.setPhoneNumber(contact.getPhoneNumber());
238                 this.setFaxNumber(contact.getFaxNumber());
239                 this.setCellphoneNumber(contact.getCellphoneNumber());
240
241                 // - other data
242                 this.setBirthday(contact.getBirthday());
243                 this.setComment(contact.getComment());
244                 this.setCreated(contact.getCreated());
245                 this.setUpdated(contact.getUpdated());
246         }
247
248         /**
249          * Check if contacts are same or throw an exception
250          * <p>
251          * @param object Other possible contact class
252          * @return Whether both contacts are same TODO Needs a lot improvements
253          */
254         @Override
255         public boolean equals (final Object object) {
256                 // Is it same type?
257                 if (!(object instanceof UserContact)) {
258                         // Not equal types
259                         return false;
260                 } else if (!(object instanceof Contact)) {
261                         // Not correct interface
262                         return false;
263                 }
264
265                 // Try to cast
266                 Contact contact = (Contact) object;
267
268                 // Now test some data TODO Definedly needs improvement
269                 return ((this.getGender().equals(contact.getGender()))
270                                 && (this.getFirstName().toLowerCase().equals(contact.getFirstName().toLowerCase()))
271                                 && (this.getFamilyName().toLowerCase().equals(contact.getFamilyName().toLowerCase())));
272         }
273
274         @Override
275         public Date getBirthday () {
276                 return this.birthday;
277         }
278
279         @Override
280         public void setBirthday (final Date birthday) {
281                 this.birthday = birthday;
282         }
283
284         @Override
285         public String getCellphoneNumber () {
286                 return this.cellphoneNumber;
287         }
288
289         @Override
290         public void setCellphoneNumber (final String cellphoneNumber) {
291                 this.cellphoneNumber = cellphoneNumber;
292         }
293
294         @Override
295         public String getCity () {
296                 return this.city;
297         }
298
299         @Override
300         public void setCity (final String city) {
301                 this.city = city;
302         }
303
304         @Override
305         public String getComment () {
306                 return this.comment;
307         }
308
309         @Override
310         public void setComment (final String comment) {
311                 this.comment = comment;
312         }
313
314         @Override
315         public String getCompanyName () {
316                 return this.companyName;
317         }
318
319         @Override
320         public void setCompanyName (final String companyName) {
321                 this.companyName = companyName;
322         }
323
324         @Override
325         public Long getContactId () {
326                 return this.contactId;
327         }
328
329         @Override
330         public void setContactId (final Long contactId) {
331                 this.contactId = contactId;
332         }
333
334         @Override
335         public String getCountryCode () {
336                 return this.countryCode;
337         }
338
339         @Override
340         public void setCountryCode (final String countryCode) {
341                 this.countryCode = countryCode;
342         }
343
344         @Override
345         public Calendar getCreated () {
346                 return this.created;
347         }
348
349         @Override
350         public void setCreated (final Calendar created) {
351                 this.created = created;
352         }
353
354         @Override
355         public String getEmailAddress () {
356                 return this.emailAddress;
357         }
358
359         @Override
360         public void setEmailAddress (final String emailAddress) {
361                 this.emailAddress = emailAddress;
362         }
363
364         @Override
365         public String getFamilyName () {
366                 //* NOISY-DEBUG: */ this.getLogger().logTrace("CALLED!");
367                 return this.familyName;
368         }
369
370         @Override
371         public void setFamilyName (final String familyName) {
372                 this.familyName = familyName;
373         }
374
375         @Override
376         public String getFaxNumber () {
377                 return this.faxNumber;
378         }
379
380         @Override
381         public void setFaxNumber (final String faxNumber) {
382                 this.faxNumber = faxNumber;
383         }
384
385         @Override
386         public String getFirstName () {
387                 return this.firstName;
388         }
389
390         @Override
391         public void setFirstName (final String firstName) {
392                 this.firstName = firstName;
393         }
394
395         @Override
396         public Gender getGender () {
397                 return this.gender;
398         }
399
400         @Override
401         public void setGender (final Gender gender) {
402                 this.gender = gender;
403         }
404
405         @Override
406         public Short getHouseNumber () {
407                 return this.houseNumber;
408         }
409
410         @Override
411         public void setHouseNumber (final Short houseNumber) {
412                 this.houseNumber = houseNumber;
413         }
414
415         @Override
416         public void setOwnContact (final Boolean ownContact) {
417                 this.ownContact = ownContact;
418         }
419
420         @Override
421         public String getPhoneNumber () {
422                 return this.phoneNumber;
423         }
424
425         @Override
426         public void setPhoneNumber (final String phoneNumber) {
427                 this.phoneNumber = phoneNumber;
428         }
429
430         @Override
431         public String getStreet () {
432                 return this.street;
433         }
434
435         @Override
436         public void setStreet (final String street) {
437                 this.street = street;
438         }
439
440         @Override
441         public Calendar getUpdated () {
442                 return this.updated;
443         }
444
445         @Override
446         public void setUpdated (final Calendar updated) {
447                 this.updated = updated;
448         }
449
450         @Override
451         public Long getZipCode () {
452                 return this.zipCode;
453         }
454
455         @Override
456         public void setZipCode (final Long zipCode) {
457                 this.zipCode = zipCode;
458         }
459
460         @Override
461         public int hashCode () {
462                 // Validate gender instance
463                 assert (this.getGender() instanceof Gender) : "gender is not set."; //NOI18N
464
465                 int hash = 7;
466                 hash = 79 * hash + Objects.hashCode(this.getFamilyName());
467                 hash = 79 * hash + this.getGender().hashCode();
468                 hash = 79 * hash + Objects.hashCode(this.getFirstName());
469                 return hash;
470         }
471
472         /**
473          * Initialization with fake gender UNKNOWN
474          */
475         @PostConstruct
476         public void init () {
477                 // Fake gender
478                 this.gender = Gender.UNKNOWN;
479         }
480
481         @Override
482         public Boolean isOwnContact () {
483                 return this.ownContact;
484         }
485 }