2 * Copyright (C) 2015 Roland Haeder
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.
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.
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/>.
17 package org.mxchange.jcore.model.contact;
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.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;
37 * A general contact class which should only be extended.
39 * @author Roland Haeder<roland@mxchange.org>
42 @Entity (name = "contact")
43 @Table (name = "contacts")
44 public abstract class BaseContact implements Contact, Comparable<Contact> {
49 private static final long serialVersionUID = 58_744_284_981_863L;
55 @Temporal (TemporalType.DATE)
56 private Date birthday;
61 @Column (name = "cellphone_number", length = 100)
62 private String cellphoneNumber;
67 @Column (nullable = false, length = 100)
75 private String comment;
80 @Column (name = "company_name", nullable = false)
81 private String companyName;
86 @Column (name = "country_code", length = 2, nullable = false)
87 private String countryCode;
90 * When the contact has been created
92 @Basic(optional = false)
93 @Temporal (TemporalType.TIMESTAMP)
94 @Column(nullable = false)
95 private Calendar created;
100 @Column (name = "email_address", length = 100, nullable = false)
101 private String emailAddress;
106 @Basic (optional = false)
107 @Column (name = "family_name", length = 100, nullable = false)
108 private String familyName;
113 @Column (name = "fax_number", length = 100)
114 private String faxNumber;
119 @Basic (optional = false)
120 @Column (name = "first_name", length = 100, nullable = false)
121 private String firstName;
126 @Basic (optional = false)
127 @Column (nullable = false)
128 private Gender gender;
133 @Column (name = "house_number", length = 5, nullable = false)
134 private Long houseNumber;
140 @GeneratedValue(strategy = GenerationType.IDENTITY)
144 * Flag whether this contact is user's own data
146 @Column (name = "own_contact", nullable = false)
147 private Boolean ownContact;
152 @Column (name = "phone_number", length = 100)
153 private String phoneNumber;
158 @Column (nullable = false)
159 private String street;
162 * When the contact has been updated
164 @Temporal (TemporalType.TIMESTAMP)
165 private Calendar updated;
170 @Column (name = "zip_code", nullable = false, length = 6)
171 private Long zipCode;
174 * No instances should be created of this class. Better extend this class
175 * and provide proper constructors.
177 protected BaseContact () {
181 * Compares two contacts with each other
183 * @param contact Contact comparator
184 * @return Comparison value
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
193 // Is the id the same?
194 if (Objects.equals(this.getId(), contact.getId())) {
195 // Same id, means same contact
197 } else if (this.getId() > contact.getId()) {
198 // This id is larger than compared to
202 // The other id is larger
207 public void copyAll (final Contact contact) {
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());
218 // - phone, fax, email
219 this.setPhoneNumber(contact.getPhoneNumber());
220 this.setFaxNumber(contact.getFaxNumber());
221 this.setCellphoneNumber(contact.getCellphoneNumber());
224 this.setBirthday(contact.getBirthday());
225 this.setComment(contact.getComment());
226 this.setCreated(contact.getCreated());
227 this.setUpdated(contact.getUpdated());
231 * Check if contacts are same or throw an exception
233 * @param object Other possible contact class
234 * @return Whether both contacts are same TODO Needs a lot improvements
237 public boolean equals (final Object object) {
239 if (!(object instanceof BaseContact)) {
242 } else if (!(object instanceof Contact)) {
243 // Not correct interface
248 Contact contact = (Contact) object;
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())));
257 public Date getBirthday () {
258 return this.birthday;
262 public void setBirthday (final Date birthday) {
263 this.birthday = birthday;
267 public String getCellphoneNumber () {
268 return this.cellphoneNumber;
272 public void setCellphoneNumber (final String cellphoneNumber) {
273 this.cellphoneNumber = cellphoneNumber;
277 public String getCity () {
282 public void setCity (final String city) {
287 public String getComment () {
292 public void setComment (final String comment) {
293 this.comment = comment;
297 public String getCompanyName () {
298 return this.companyName;
302 public void setCompanyName (final String companyName) {
303 this.companyName = companyName;
307 public String getCountryCode () {
308 return this.countryCode;
312 public void setCountryCode (final String countryCode) {
313 this.countryCode = countryCode;
317 public Calendar getCreated () {
322 public void setCreated (final Calendar created) {
323 this.created = created;
327 public String getEmailAddress () {
328 return this.emailAddress;
332 public void setEmailAddress (final String emailAddress) {
333 this.emailAddress = emailAddress;
337 public String getFamilyName () {
338 //* NOISY-DEBUG: */ this.getLogger().trace("CALLED!");
339 return this.familyName;
343 public void setFamilyName (final String familyName) {
344 this.familyName = familyName;
348 public String getFaxNumber () {
349 return this.faxNumber;
353 public void setFaxNumber (final String faxNumber) {
354 this.faxNumber = faxNumber;
358 public String getFirstName () {
359 return this.firstName;
363 public void setFirstName (final String firstName) {
364 this.firstName = firstName;
368 public Gender getGender () {
373 public void setGender (final Gender gender) {
374 this.gender = gender;
378 public Long getHouseNumber () {
379 return this.houseNumber;
383 public void setHouseNumber (final Long houseNumber) {
384 this.houseNumber = houseNumber;
388 public Long getId () {
393 public void setId (final Long id) {
398 public void setOwnContact (final Boolean ownContact) {
399 this.ownContact = ownContact;
403 public String getPhoneNumber () {
404 return this.phoneNumber;
408 public void setPhoneNumber (final String phoneNumber) {
409 this.phoneNumber = phoneNumber;
413 public String getStreet () {
418 public void setStreet (final String street) {
419 this.street = street;
423 public Calendar getUpdated () {
428 public void setUpdated (final Calendar updated) {
429 this.updated = updated;
433 public Long getZipCode () {
438 public void setZipCode (final Long zipCode) {
439 this.zipCode = zipCode;
443 public int hashCode () {
444 // Validate gender instance
445 assert (this.getGender() instanceof Gender) : "gender is not set."; //NOI18N
448 hash = 79 * hash + Objects.hashCode(this.getFamilyName());
449 hash = 79 * hash + this.getGender().hashCode();
450 hash = 79 * hash + Objects.hashCode(this.getFirstName());
455 public void init () {
457 this.gender = Gender.UNKNOWN;
461 public Boolean isOwnContact () {
462 return this.ownContact;
466 * Shows this contact to the user.
468 * @param client Client instance to use
469 * @deprecated Should not be called here
472 public void show (final Client client) {
473 // The client must be set
474 if (null == client) {
476 throw new NullPointerException("client is null"); //NOI18N
479 // Display name "box"
480 client.displayNameBox(this);
482 // Display address "box"
483 client.displayAddressBox(this);
485 // Display other data "box"
486 client.displayOtherDataBox(this);