]> git.mxchange.org Git - jcontacts-core.git/blobdiff - src/org/mxchange/jcontacts/contact/UserContact.java
Thumb of a rule:
[jcontacts-core.git] / src / org / mxchange / jcontacts / contact / UserContact.java
index 47554565789d91bf89924f6fbe5d043b7e9e625f..c4f6ba74bb906fd3a7b4ed4756292aeed076e428 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015 Roland Haeder
+ * Copyright (C) 2016 Roland Haeder
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -19,8 +19,8 @@ package org.mxchange.jcontacts.contact;
 import java.util.Calendar;
 import java.util.Date;
 import java.util.Objects;
-import javax.annotation.PostConstruct;
 import javax.persistence.Basic;
+import javax.persistence.CascadeType;
 import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.EnumType;
@@ -28,21 +28,52 @@ import javax.persistence.Enumerated;
 import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
+import javax.persistence.Index;
+import javax.persistence.JoinColumn;
 import javax.persistence.Lob;
+import javax.persistence.NamedQueries;
+import javax.persistence.NamedQuery;
+import javax.persistence.OneToOne;
 import javax.persistence.Table;
 import javax.persistence.Temporal;
 import javax.persistence.TemporalType;
 import org.mxchange.jcontacts.contact.gender.Gender;
+import org.mxchange.jcountry.data.Country;
+import org.mxchange.jcountry.data.CountryData;
+import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
+import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
+import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
+import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
+import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
+import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
 
 /**
- * A general contact class which should only be extended.
+ * A general contact class which serves as an entity.
  * <p>
  * @author Roland Haeder<roland@mxchange.org>
  * @version 0.0
  */
 @Entity (name = "contacts")
-@Table (name = "contacts")
-public class UserContact implements Contact, Comparable<Contact> {
+@Table (
+               name = "contacts",
+               indexes = {
+                       @Index (
+                                       name = "contact_gender",
+                                       columnList = "contact_gender"
+                       )
+               }
+)
+@NamedQueries (
+               {
+                       @NamedQuery (name = "AllContacts", query = "SELECT c FROM contacts AS c ORDER BY c.contactId ASC"),
+                       @NamedQuery (name = "AllContactEmailAddresses", query = "SELECT c.contactEmailAddress FROM contacts AS c ORDER BY c.contactId ASC"),
+                       @NamedQuery (name = "AllContactsByCellphone", query = "SELECT c FROM contacts AS c WHERE c.contactCellphoneNumber = :cellPhone ORDER BY c.contactId ASC"),
+                       @NamedQuery (name = "SearchContactById", query = "SELECT c FROM contacts AS c WHERE c.contactId = :contactId"),
+                       @NamedQuery (name = "SearchContactByEmailAddress", query = "SELECT c FROM contacts AS c WHERE LOWER(c.contactEmailAddress) LIKE LOWER(:emailAddress)")
+               }
+)
+@SuppressWarnings ("PersistenceUnitPresent")
+public class UserContact implements Contact {
 
        /**
         * Serial number
@@ -52,434 +83,470 @@ public class UserContact implements Contact, Comparable<Contact> {
        /**
         * Birth day
         */
-       @Column (name = "birthday")
+       @Column (name = "contact_birthday")
        @Temporal (TemporalType.DATE)
-       private Date birthday;
+       private Date contactBirthday;
 
        /**
         * Cellphone number
         */
-       @Column (name = "cellphone_number", length = 100)
-       private String cellphoneNumber;
+       @JoinColumn (name = "contact_cellphone_number_id", referencedColumnName = "cellphone_id", unique = true)
+       @OneToOne (targetEntity = CellphoneNumber.class, cascade = CascadeType.ALL)
+       private DialableCellphoneNumber contactCellphoneNumber;
 
        /**
         * City
         */
-       @Column (name = "city", nullable = false, length = 100)
-       private String city;
+       @Basic (optional = false)
+       @Column (name = "contact_city", nullable = false, length = 100)
+       private String contactCity;
 
        /**
         * Optional comments
         */
        @Lob
-       @Column (name = "comment")
-       private String comment;
-
-       /**
-        * Company name
-        */
-       @Column (name = "company_name", nullable = false)
-       private String companyName;
-
-       /**
-        * Id number
-        */
-       @Id
-       @GeneratedValue (strategy = GenerationType.IDENTITY)
-       @Column (name = "contact_id", length = 20, updatable = false)
-       private Long contactId;
+       @Column (name = "contact_comment")
+       private String contactComment;
 
        /**
         * Country code
         */
-       @Column (name = "country_code", length = 2, nullable = false)
-       private String countryCode;
+       @JoinColumn (name = "contact_country_id", nullable = false, referencedColumnName = "country_id")
+       @OneToOne (targetEntity = CountryData.class, cascade = CascadeType.REFRESH, optional = false)
+       private Country contactCountry;
 
        /**
         * When the contact has been created
         */
        @Basic (optional = false)
        @Temporal (TemporalType.TIMESTAMP)
-       @Column (name = "created", nullable = false)
-       private Calendar created;
+       @Column (name = "contact_created", nullable = false)
+       private Calendar contactCreated;
 
        /**
         * Email address
         */
-       @Column (name = "email_address", length = 100, nullable = false)
-       private String emailAddress;
+       @Column (name = "contact_email_address", length = 100, unique = true)
+       private String contactEmailAddress;
 
        /**
         * Family name
         */
        @Basic (optional = false)
-       @Column (name = "family_name", length = 100, nullable = false)
-       private String familyName;
+       @Column (name = "contact_family_name", length = 100, nullable = false)
+       private String contactFamilyName;
 
        /**
         * Fax number
         */
-       @Column (name = "fax_number", length = 100)
-       private String faxNumber;
+       @JoinColumn (name = "contact_fax_number_id", referencedColumnName = "fax_id", unique = true)
+       @OneToOne (targetEntity = FaxNumber.class, cascade = CascadeType.ALL)
+       private DialableFaxNumber contactFaxNumber;
 
        /**
         * First name
         */
        @Basic (optional = false)
-       @Column (name = "first_name", length = 100, nullable = false)
-       private String firstName;
+       @Column (name = "contact_first_name", length = 100, nullable = false)
+       private String contactFirstName;
 
        /**
         * Gender instance
         */
        @Basic (optional = false)
-       @Column (name = "gender", nullable = false)
+       @Column (name = "contact_gender", nullable = false)
        @Enumerated (EnumType.STRING)
-       private Gender gender;
+       private Gender contactGender;
 
        /**
         * House number
         */
-       @Column (name = "house_number", length = 5, nullable = false)
-       private Short houseNumber;
+       @Basic (optional = false)
+       @Column (name = "contact_house_number", nullable = false)
+       private Short contactHouseNumber;
+
+       /**
+        * House number extension
+        */
+       @Column (name = "contact_house_number_extension", length = 5)
+       private String contactHouseNumberExtension;
+
+       /**
+        * Id number
+        */
+       @Id
+       @GeneratedValue (strategy = GenerationType.IDENTITY)
+       @Column (name = "contact_id", nullable = false, updatable = false)
+       private Long contactId;
 
        /**
         * Flag whether this contact is user's own data
         */
-       @Column (name = "own_contact", nullable = false)
-       private Boolean ownContact;
+       @Basic (optional = false)
+       @Column (name = "contact_own_contact", nullable = false)
+       private Boolean contactOwnContact;
 
        /**
         * Phone number
         */
-       @Column (name = "phone_number", length = 100)
-       private String phoneNumber;
+       @JoinColumn (name = "contact_phone_number_id", referencedColumnName = "phone_id", unique = true)
+       @OneToOne (targetEntity = LandLineNumber.class, cascade = CascadeType.ALL)
+       private DialableLandLineNumber contactPhoneNumber;
 
        /**
         * Street
         */
-       @Column (name = "street", nullable = false)
-       private String street;
+       @Basic (optional = false)
+       @Column (name = "contact_street", nullable = false)
+       private String contactStreet;
 
        /**
-        * When the contact has been updated
+        * Title (Doctor, etc)
         */
-       @Temporal (TemporalType.TIMESTAMP)
-       @Column (name = "updated")
-       private Calendar updated;
+       @Column (name = "contact_title")
+       private String contactTitle;
 
        /**
-        * ZIP code
+        * When the contact has been updated
         */
-       @Column (name = "zip_code", nullable = false, length = 6)
-       private Long zipCode;
+       @Temporal (TemporalType.TIMESTAMP)
+       @Column (name = "contact_updated")
+       private Calendar contactUpdated;
 
        /**
-        * Constructor for gender and names
-        * <p>
-        * @param gender Gender instance
-        * @param firstName First name
-        * @param familyName Family name
-        * @param companyName Company name
+        * ZIP code
         */
-       public UserContact (final Gender gender, final String firstName, final String familyName, final String companyName) {
-               // Set all
-               this.gender = gender;
-               this.firstName = firstName;
-               this.familyName = familyName;
-       }
+       @Basic (optional = false)
+       @Column (name = "contact_zip_code", nullable = false, length = 6)
+       private Integer contactZipCode;
 
        /**
         * Default constructor
         */
        public UserContact () {
+               // Default is not user's own contact
+               this.contactOwnContact = Boolean.FALSE;
        }
 
        /**
-        * Compares two contacts with each other
+        * Constructor for contactGender and names
         * <p>
-        * @param contact Contact comparator
-        * @return Comparison value
+        * @param contactGender Gender instance
+        * @param contactFirstName First name
+        * @param contactFamilyName Family name
         */
-       @Override
-       public int compareTo (final Contact contact) {
-               // contact should not be null
-               if (null == contact) {
-                       throw new NullPointerException("contact is null"); //NOI18N
-               }
+       public UserContact (final Gender contactGender, final String contactFirstName, final String contactFamilyName) {
+               // Call default constructor
+               this();
 
-               // Is the contactId the same?
-               if (Objects.equals(this.getContactId(), contact.getContactId())) {
-                       // Same contactId, means same contact
-                       return 0;
-               } else if (this.getContactId() > contact.getContactId()) {
-                       // This contactId is larger than compared to
-                       return -1;
-               }
-
-               // The other contactId is larger
-               return 1;
+               // Set all
+               this.contactGender = contactGender;
+               this.contactFirstName = contactFirstName;
+               this.contactFamilyName = contactFamilyName;
        }
 
        @Override
        public void copyAll (final Contact contact) {
+               // Contact should be valid
+               if (null == contact) {
+                       // Throw NPE
+                       throw new NullPointerException("contact is null"); //NOI18N
+               }
+
                // Copy all:
                // - base data
-               this.setFirstName(contact.getFirstName());
-               this.setFamilyName(contact.getFamilyName());
-               this.setCompanyName(contact.getCompanyName());
-               this.setStreet(contact.getStreet());
-               this.setZipCode(contact.getZipCode());
-               this.setCity(contact.getCity());
-               this.setCountryCode(contact.getCountryCode());
+               this.setContactGender(contact.getContactGender());
+               this.setContactTitle(contact.getContactTitle());
+               this.setContactFirstName(contact.getContactFirstName());
+               this.setContactFamilyName(contact.getContactFamilyName());
+               this.setContactStreet(contact.getContactStreet());
+               this.setContactHouseNumber(contact.getContactHouseNumber());
+               this.setContactHouseNumberExtension(contact.getContactHouseNumberExtension());
+               this.setContactZipCode(contact.getContactZipCode());
+               this.setContactCity(contact.getContactCity());
+               this.setContactCountry(contact.getContactCountry());
 
                // - phone, fax, email
-               this.setPhoneNumber(contact.getPhoneNumber());
-               this.setFaxNumber(contact.getFaxNumber());
-               this.setCellphoneNumber(contact.getCellphoneNumber());
+               this.setContactLandLineNumber(contact.getContactLandLineNumber());
+               this.setContactFaxNumber(contact.getContactFaxNumber());
+               this.setContactCellphoneNumber(contact.getContactCellphoneNumber());
 
                // - other data
-               this.setBirthday(contact.getBirthday());
-               this.setComment(contact.getComment());
-               this.setCreated(contact.getCreated());
-               this.setUpdated(contact.getUpdated());
+               this.setContactBirthday(contact.getContactBirthday());
+               this.setContactComment(contact.getContactComment());
+               this.setContactCreated(contact.getContactCreated());
+               this.setContactUpdated(contact.getContactUpdated());
        }
 
-       /**
-        * Check if contacts are same or throw an exception
-        * <p>
-        * @param object Other possible contact class
-        * @return Whether both contacts are same TODO Needs a lot improvements
-        */
        @Override
        public boolean equals (final Object object) {
-               // Is it same type?
-               if (!(object instanceof UserContact)) {
-                       // Not equal types
+               if (this == object) {
+                       return true;
+               } else if (null == object) {
+                       return false;
+               } else if (this.getClass() != object.getClass()) {
                        return false;
                } else if (!(object instanceof Contact)) {
                        // Not correct interface
                        return false;
                }
 
-               // Try to cast
-               Contact contact = (Contact) object;
+               final Contact other = (Contact) object;
+
+               if (!Objects.equals(this.getContactCity(), other.getContactCity())) {
+                       return false;
+               } else if (!Objects.equals(this.getContactEmailAddress(), other.getContactEmailAddress())) {
+                       return false;
+               } else if (!Objects.equals(this.getContactFamilyName(), other.getContactFamilyName())) {
+                       return false;
+               } else if (!Objects.equals(this.getContactFirstName(), other.getContactFirstName())) {
+                       return false;
+               } else if (!Objects.equals(this.getContactStreet(), other.getContactStreet())) {
+                       return false;
+               } else if (!Objects.equals(this.getContactTitle(), other.getContactTitle())) {
+                       return false;
+               } else if (!Objects.equals(this.getContactBirthday(), other.getContactBirthday())) {
+                       return false;
+               } else if (!Objects.equals(this.getContactCountry(), other.getContactCountry())) {
+                       return false;
+               } else if (this.getContactGender() != other.getContactGender()) {
+                       return false;
+               } else if (!Objects.equals(this.getContactHouseNumber(), other.getContactHouseNumber())) {
+                       return false;
+               } else if (!Objects.equals(this.getContactHouseNumberExtension(), other.getContactHouseNumberExtension())) {
+                       return false;
+               } else if (!Objects.equals(this.getContactId(), other.getContactId())) {
+                       return false;
+               }
 
-               // Now test some data TODO Definedly needs improvement
-               return ((this.getGender().equals(contact.getGender()))
-                               && (this.getFirstName().toLowerCase().equals(contact.getFirstName().toLowerCase()))
-                               && (this.getFamilyName().toLowerCase().equals(contact.getFamilyName().toLowerCase())));
+               return true;
        }
 
        @Override
-       public Date getBirthday () {
-               return this.birthday;
+       @SuppressWarnings ("ReturnOfDateField")
+       public Date getContactBirthday () {
+               return this.contactBirthday;
        }
 
        @Override
-       public void setBirthday (final Date birthday) {
-               this.birthday = birthday;
+       @SuppressWarnings ("AssignmentToDateFieldFromParameter")
+       public void setContactBirthday (final Date contactBirthday) {
+               this.contactBirthday = contactBirthday;
        }
 
        @Override
-       public String getCellphoneNumber () {
-               return this.cellphoneNumber;
+       public DialableCellphoneNumber getContactCellphoneNumber () {
+               return this.contactCellphoneNumber;
        }
 
        @Override
-       public void setCellphoneNumber (final String cellphoneNumber) {
-               this.cellphoneNumber = cellphoneNumber;
+       public void setContactCellphoneNumber (final DialableCellphoneNumber contactCellphoneNumber) {
+               this.contactCellphoneNumber = contactCellphoneNumber;
        }
 
        @Override
-       public String getCity () {
-               return this.city;
+       public String getContactCity () {
+               return this.contactCity;
        }
 
        @Override
-       public void setCity (final String city) {
-               this.city = city;
+       public void setContactCity (final String contactCity) {
+               this.contactCity = contactCity;
        }
 
        @Override
-       public String getComment () {
-               return this.comment;
+       public String getContactComment () {
+               return this.contactComment;
        }
 
        @Override
-       public void setComment (final String comment) {
-               this.comment = comment;
+       public void setContactComment (final String contactComment) {
+               this.contactComment = contactComment;
        }
 
        @Override
-       public String getCompanyName () {
-               return this.companyName;
+       public Country getContactCountry () {
+               return this.contactCountry;
        }
 
        @Override
-       public void setCompanyName (final String companyName) {
-               this.companyName = companyName;
+       public void setContactCountry (final Country contactCountry) {
+               this.contactCountry = contactCountry;
        }
 
        @Override
-       public Long getContactId () {
-               return this.contactId;
+       @SuppressWarnings ("ReturnOfDateField")
+       public Calendar getContactCreated () {
+               return this.contactCreated;
        }
 
        @Override
-       public void setContactId (final Long contactId) {
-               this.contactId = contactId;
+       @SuppressWarnings ("AssignmentToDateFieldFromParameter")
+       public void setContactCreated (final Calendar contactCreated) {
+               this.contactCreated = contactCreated;
        }
 
        @Override
-       public String getCountryCode () {
-               return this.countryCode;
+       public String getContactEmailAddress () {
+               return this.contactEmailAddress;
        }
 
        @Override
-       public void setCountryCode (final String countryCode) {
-               this.countryCode = countryCode;
+       public void setContactEmailAddress (final String contactEmailAddress) {
+               this.contactEmailAddress = contactEmailAddress;
        }
 
        @Override
-       public Calendar getCreated () {
-               return this.created;
+       public String getContactFamilyName () {
+               //* NOISY-DEBUG: */ this.getLogger().logTrace("CALLED!");
+               return this.contactFamilyName;
        }
 
        @Override
-       public void setCreated (final Calendar created) {
-               this.created = created;
+       public void setContactFamilyName (final String contactFamilyName) {
+               this.contactFamilyName = contactFamilyName;
        }
 
        @Override
-       public String getEmailAddress () {
-               return this.emailAddress;
+       public DialableFaxNumber getContactFaxNumber () {
+               return this.contactFaxNumber;
        }
 
        @Override
-       public void setEmailAddress (final String emailAddress) {
-               this.emailAddress = emailAddress;
+       public void setContactFaxNumber (final DialableFaxNumber contactFaxNumber) {
+               this.contactFaxNumber = contactFaxNumber;
        }
 
        @Override
-       public String getFamilyName () {
-               //* NOISY-DEBUG: */ this.getLogger().logTrace("CALLED!");
-               return this.familyName;
+       public String getContactFirstName () {
+               return this.contactFirstName;
        }
 
        @Override
-       public void setFamilyName (final String familyName) {
-               this.familyName = familyName;
+       public void setContactFirstName (final String contactFirstName) {
+               this.contactFirstName = contactFirstName;
        }
 
        @Override
-       public String getFaxNumber () {
-               return this.faxNumber;
+       public Gender getContactGender () {
+               return this.contactGender;
        }
 
        @Override
-       public void setFaxNumber (final String faxNumber) {
-               this.faxNumber = faxNumber;
+       public void setContactGender (final Gender contactGender) {
+               this.contactGender = contactGender;
        }
 
        @Override
-       public String getFirstName () {
-               return this.firstName;
+       public Short getContactHouseNumber () {
+               return this.contactHouseNumber;
        }
 
        @Override
-       public void setFirstName (final String firstName) {
-               this.firstName = firstName;
+       public void setContactHouseNumber (final Short contactHouseNumber) {
+               this.contactHouseNumber = contactHouseNumber;
        }
 
        @Override
-       public Gender getGender () {
-               return this.gender;
+       public String getContactHouseNumberExtension () {
+               return this.contactHouseNumberExtension;
        }
 
        @Override
-       public void setGender (final Gender gender) {
-               this.gender = gender;
+       public void setContactHouseNumberExtension (final String contactHouseNumberExtension) {
+               this.contactHouseNumberExtension = contactHouseNumberExtension;
        }
 
        @Override
-       public Short getHouseNumber () {
-               return this.houseNumber;
+       public Long getContactId () {
+               return this.contactId;
        }
 
        @Override
-       public void setHouseNumber (final Short houseNumber) {
-               this.houseNumber = houseNumber;
+       public void setContactId (final Long contactId) {
+               this.contactId = contactId;
        }
 
        @Override
-       public void setOwnContact (final Boolean ownContact) {
-               this.ownContact = ownContact;
+       public DialableLandLineNumber getContactLandLineNumber () {
+               return this.contactPhoneNumber;
        }
 
        @Override
-       public String getPhoneNumber () {
-               return this.phoneNumber;
+       public void setContactLandLineNumber (final DialableLandLineNumber contactPhoneNumber) {
+               this.contactPhoneNumber = contactPhoneNumber;
        }
 
        @Override
-       public void setPhoneNumber (final String phoneNumber) {
-               this.phoneNumber = phoneNumber;
+       public void setContactOwnContact (final Boolean contactOwnContact) {
+               this.contactOwnContact = contactOwnContact;
        }
 
        @Override
-       public String getStreet () {
-               return this.street;
+       public String getContactStreet () {
+               return this.contactStreet;
        }
 
        @Override
-       public void setStreet (final String street) {
-               this.street = street;
+       public void setContactStreet (final String contactStreet) {
+               this.contactStreet = contactStreet;
        }
 
        @Override
-       public Calendar getUpdated () {
-               return this.updated;
+       public String getContactTitle () {
+               return this.contactTitle;
        }
 
        @Override
-       public void setUpdated (final Calendar updated) {
-               this.updated = updated;
+       public void setContactTitle (final String contactTitle) {
+               this.contactTitle = contactTitle;
        }
 
        @Override
-       public Long getZipCode () {
-               return this.zipCode;
+       @SuppressWarnings ("ReturnOfDateField")
+       public Calendar getContactUpdated () {
+               return this.contactUpdated;
        }
 
        @Override
-       public void setZipCode (final Long zipCode) {
-               this.zipCode = zipCode;
+       @SuppressWarnings ("AssignmentToDateFieldFromParameter")
+       public void setContactUpdated (final Calendar contactUpdated) {
+               this.contactUpdated = contactUpdated;
        }
 
        @Override
-       public int hashCode () {
-               // Validate gender instance
-               assert (this.getGender() instanceof Gender) : "gender is not set."; //NOI18N
+       public Integer getContactZipCode () {
+               return this.contactZipCode;
+       }
 
-               int hash = 7;
-               hash = 79 * hash + Objects.hashCode(this.getFamilyName());
-               hash = 79 * hash + this.getGender().hashCode();
-               hash = 79 * hash + Objects.hashCode(this.getFirstName());
-               return hash;
+       @Override
+       public void setContactZipCode (final Integer contactZipCode) {
+               this.contactZipCode = contactZipCode;
        }
 
-       /**
-        * Initialization with fake gender UNKNOWN
-        */
-       @PostConstruct
-       public void init () {
-               // Fake gender
-               this.gender = Gender.UNKNOWN;
+       @Override
+       public int hashCode () {
+               int hash = 5;
+
+               hash = 29 * hash + Objects.hashCode(this.getContactBirthday());
+               hash = 29 * hash + Objects.hashCode(this.getContactCity());
+               hash = 29 * hash + Objects.hashCode(this.getContactCountry());
+               hash = 29 * hash + Objects.hashCode(this.getContactEmailAddress());
+               hash = 29 * hash + Objects.hashCode(this.getContactFamilyName());
+               hash = 29 * hash + Objects.hashCode(this.getContactFirstName());
+               hash = 29 * hash + Objects.hashCode(this.getContactGender());
+               hash = 29 * hash + Objects.hashCode(this.getContactHouseNumber());
+               hash = 29 * hash + Objects.hashCode(this.getContactHouseNumberExtension());
+               hash = 29 * hash + Objects.hashCode(this.getContactId());
+               hash = 29 * hash + Objects.hashCode(this.getContactStreet());
+               hash = 29 * hash + Objects.hashCode(this.getContactTitle());
+
+               return hash;
        }
 
        @Override
        public Boolean isOwnContact () {
-               return this.ownContact;
+               return this.contactOwnContact;
        }
+
 }