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