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.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;
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 Timestamp 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 Timestamp 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 * Check if contacts are same or throw an exception
209 * @param object Other possible contact class
210 * @return Whether both contacts are same TODO Needs a lot improvements
213 public boolean equals (final Object object) {
215 if (!(object instanceof BaseContact)) {
218 } else if (!(object instanceof Contact)) {
219 // Not correct interface
224 Contact contact = (Contact) object;
226 // Now test some data TODO Definedly needs improvement
227 return ((this.getGender().equals(contact.getGender()))
228 && (this.getFirstName().toLowerCase().equals(contact.getFirstName().toLowerCase()))
229 && (this.getFamilyName().toLowerCase().equals(contact.getFamilyName().toLowerCase())));
233 public Date getBirthday () {
234 return this.birthday;
238 public void setBirthday (final Date birthday) {
239 this.birthday = birthday;
243 public String getCellphoneNumber () {
244 return this.cellphoneNumber;
248 public void setCellphoneNumber (final String cellphoneNumber) {
249 this.cellphoneNumber = cellphoneNumber;
253 public String getCity () {
258 public void setCity (final String city) {
263 public String getComment () {
268 public void setComment (final String comment) {
269 this.comment = comment;
273 public String getCompanyName () {
274 return this.companyName;
278 public void setCompanyName (final String companyName) {
279 this.companyName = companyName;
283 public String getCountryCode () {
284 return this.countryCode;
288 public void setCountryCode (final String countryCode) {
289 this.countryCode = countryCode;
293 public Timestamp getCreated () {
298 public void setCreated (final Timestamp created) {
299 this.created = created;
303 public String getEmailAddress () {
304 return this.emailAddress;
308 public void setEmailAddress (final String emailAddress) {
309 this.emailAddress = emailAddress;
313 public String getFamilyName () {
314 //* NOISY-DEBUG: */ this.getLogger().trace("CALLED!");
315 return this.familyName;
319 public void setFamilyName (final String familyName) {
320 this.familyName = familyName;
324 public String getFaxNumber () {
325 return this.faxNumber;
329 public void setFaxNumber (final String faxNumber) {
330 this.faxNumber = faxNumber;
334 public String getFirstName () {
335 return this.firstName;
339 public void setFirstName (final String firstName) {
340 this.firstName = firstName;
344 public Gender getGender () {
349 public void setGender (final Gender gender) {
350 this.gender = gender;
354 public Long getHouseNumber () {
355 return this.houseNumber;
359 public void setHouseNumber (final Long houseNumber) {
360 this.houseNumber = houseNumber;
364 public Long getId () {
369 public void setId (final Long id) {
374 public void setOwnContact (final Boolean ownContact) {
375 this.ownContact = ownContact;
379 public String getPhoneNumber () {
380 return this.phoneNumber;
384 public void setPhoneNumber (final String phoneNumber) {
385 this.phoneNumber = phoneNumber;
389 public String getStreet () {
394 public void setStreet (final String street) {
395 this.street = street;
399 public Timestamp getUpdated () {
404 public void setUpdated (final Timestamp updated) {
405 this.updated = updated;
409 public Long getZipCode () {
414 public void setZipCode (final Long zipCode) {
415 this.zipCode = zipCode;
419 public int hashCode () {
420 // Validate gender instance
421 assert (this.getGender() instanceof Gender) : "gender is not set."; //NOI18N
424 hash = 79 * hash + Objects.hashCode(this.getFamilyName());
425 hash = 79 * hash + this.getGender().hashCode();
426 hash = 79 * hash + Objects.hashCode(this.getFirstName());
431 public void init () {
433 this.gender = Gender.UNKNOWN;
437 public Boolean isOwnContact () {
438 return this.ownContact;
442 * Shows this contact to the user.
444 * @param client Client instance to use
445 * @deprecated Should not be called here
448 public void show (final Client client) {
449 // The client must be set
450 if (null == client) {
452 throw new NullPointerException("client is null"); //NOI18N
455 // Display name "box"
456 client.displayNameBox(this);
458 // Display address "box"
459 client.displayAddressBox(this);
461 // Display other data "box"
462 client.displayOtherDataBox(this);