]> git.mxchange.org Git - jcontacts-core.git/blobdiff - src/org/mxchange/jcontacts/contact/UserContact.java
Email address should be optional
[jcontacts-core.git] / src / org / mxchange / jcontacts / contact / UserContact.java
index b25ae2779e5ce1756754b6ef60ab3055111c8026..7d65e3b0ca2b79b2a0b35ca32fbd0437abeb5597 100644 (file)
@@ -19,20 +19,20 @@ 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;
 import javax.persistence.Enumerated;
-import javax.persistence.FetchType;
 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;
@@ -48,7 +48,7 @@ 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
@@ -68,7 +68,16 @@ import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
                        )
                }
 )
-public class UserContact implements Contact, Comparable<Contact> {
+@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")
+               }
+)
+@SuppressWarnings ("PersistenceUnitPresent")
+public class UserContact implements Contact {
 
        /**
         * Serial number
@@ -85,13 +94,14 @@ public class UserContact implements Contact, Comparable<Contact> {
        /**
         * Cellphone number
         */
-       @JoinColumn (name = "contact_cellphone_number_id")
+       @JoinColumn (name = "contact_cellphone_number_id", referencedColumnName = "cellphone_id", unique = true)
        @OneToOne (targetEntity = CellphoneNumber.class, cascade = CascadeType.ALL)
        private DialableCellphoneNumber contactCellphoneNumber;
 
        /**
         * City
         */
+       @Basic (optional = false)
        @Column (name = "contact_city", nullable = false, length = 100)
        private String contactCity;
 
@@ -105,8 +115,8 @@ public class UserContact implements Contact, Comparable<Contact> {
        /**
         * Country code
         */
-       @JoinColumn (name = "contact_country_id", nullable = false)
-       @OneToOne (targetEntity = CountryData.class, cascade = CascadeType.MERGE, optional = false, fetch = FetchType.EAGER)
+       @JoinColumn (name = "contact_country_id", nullable = false, referencedColumnName = "country_id")
+       @OneToOne (targetEntity = CountryData.class, cascade = CascadeType.REFRESH, optional = false)
        private Country contactCountry;
 
        /**
@@ -120,7 +130,7 @@ public class UserContact implements Contact, Comparable<Contact> {
        /**
         * Email address
         */
-       @Column (name = "contact_email_address", length = 100, nullable = false)
+       @Column (name = "contact_email_address", length = 100)
        private String contactEmailAddress;
 
        /**
@@ -133,7 +143,7 @@ public class UserContact implements Contact, Comparable<Contact> {
        /**
         * Fax number
         */
-       @JoinColumn (name = "contact_fax_number_id")
+       @JoinColumn (name = "contact_fax_number_id", referencedColumnName = "fax_id", unique = true)
        @OneToOne (targetEntity = FaxNumber.class, cascade = CascadeType.ALL)
        private DialableFaxNumber contactFaxNumber;
 
@@ -155,6 +165,7 @@ public class UserContact implements Contact, Comparable<Contact> {
        /**
         * House number
         */
+       @Basic (optional = false)
        @Column (name = "contact_house_number", length = 5, nullable = false)
        private Short contactHouseNumber;
 
@@ -163,25 +174,27 @@ public class UserContact implements Contact, Comparable<Contact> {
         */
        @Id
        @GeneratedValue (strategy = GenerationType.IDENTITY)
-       @Column (name = "contact_id", length = 20, nullable = false, updatable = false)
+       @Column (name = "contact_id", nullable = false, updatable = false)
        private Long contactId;
 
        /**
         * Flag whether this contact is user's own data
         */
+       @Basic (optional = false)
        @Column (name = "contact_own_contact", nullable = false)
        private Boolean contactOwnContact;
 
        /**
         * Phone number
         */
-       @JoinColumn (name = "contact_phone_number_id")
+       @JoinColumn (name = "contact_phone_number_id", referencedColumnName = "phone_id", unique = true)
        @OneToOne (targetEntity = LandLineNumber.class, cascade = CascadeType.ALL)
        private DialableLandLineNumber contactPhoneNumber;
 
        /**
         * Street
         */
+       @Basic (optional = false)
        @Column (name = "contact_street", nullable = false)
        private String contactStreet;
 
@@ -201,58 +214,46 @@ public class UserContact implements Contact, Comparable<Contact> {
        /**
         * ZIP code
         */
+       @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;
+
+               // Unknown gender
+               this.contactGender = Gender.UNKNOWN;
+       }
+
        /**
         * Constructor for contactGender and names
         * <p>
-        * @param contactGender Gender instance
-        * @param contactFirstName First name
+        * @param contactGender     Gender instance
+        * @param contactFirstName  First name
         * @param contactFamilyName Family name
         */
        public UserContact (final Gender contactGender, final String contactFirstName, final String contactFamilyName) {
+               // Call default constructor
+               this();
+
                // Set all
                this.contactGender = contactGender;
                this.contactFirstName = contactFirstName;
                this.contactFamilyName = contactFamilyName;
        }
 
-       /**
-        * Default constructor
-        */
-       public UserContact () {
-       }
-
-       /**
-        * Compares two contacts with each other
-        * <p>
-        * @param contact Contact comparator
-        * <p>
-        * @return Comparison value
-        */
        @Override
-       public int compareTo (final Contact contact) {
-               // contact should not be null
+       public void copyAll (final Contact contact) {
+               // Contact should be valid
                if (null == contact) {
+                       // Throw NPE
                        throw new NullPointerException("contact is null"); //NOI18N
                }
 
-               // 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;
-       }
-
-       @Override
-       public void copyAll (final Contact contact) {
                // Copy all:
                // - base data
                this.setContactFirstName(contact.getContactFirstName());
@@ -263,7 +264,7 @@ public class UserContact implements Contact, Comparable<Contact> {
                this.setContactCountry(contact.getContactCountry());
 
                // - phone, fax, email
-               this.setContactPhoneNumber(contact.getContactPhoneNumber());
+               this.setContactLandLineNumber(contact.getContactLandLineNumber());
                this.setContactFaxNumber(contact.getContactFaxNumber());
                this.setContactCellphoneNumber(contact.getContactCellphoneNumber());
 
@@ -294,6 +295,18 @@ public class UserContact implements Contact, Comparable<Contact> {
                                (this.getContactFamilyName().toLowerCase().equals(contact.getContactFamilyName().toLowerCase())));
        }
 
+       @Override
+       public int hashCode () {
+               // Validate contactGender instance
+               assert (this.getContactGender() instanceof Gender) : "gender is not set."; //NOI18N
+
+               int hash = 7;
+               hash = 79 * hash + Objects.hashCode(this.getContactFamilyName());
+               hash = 79 * hash + this.getContactGender().hashCode();
+               hash = 79 * hash + Objects.hashCode(this.getContactFirstName());
+               return hash;
+       }
+
        @Override
        public Date getContactBirthday () {
                return this.contactBirthday;
@@ -431,12 +444,12 @@ public class UserContact implements Contact, Comparable<Contact> {
        }
 
        @Override
-       public DialableLandLineNumber getContactPhoneNumber () {
+       public DialableLandLineNumber getContactLandLineNumber () {
                return this.contactPhoneNumber;
        }
 
        @Override
-       public void setContactPhoneNumber (final DialableLandLineNumber contactPhoneNumber) {
+       public void setContactLandLineNumber (final DialableLandLineNumber contactPhoneNumber) {
                this.contactPhoneNumber = contactPhoneNumber;
        }
 
@@ -480,27 +493,6 @@ public class UserContact implements Contact, Comparable<Contact> {
                this.contactZipCode = contactZipCode;
        }
 
-       @Override
-       public int hashCode () {
-               // Validate contactGender instance
-               assert (this.getContactGender() instanceof Gender) : "gender is not set."; //NOI18N
-
-               int hash = 7;
-               hash = 79 * hash + Objects.hashCode(this.getContactFamilyName());
-               hash = 79 * hash + this.getContactGender().hashCode();
-               hash = 79 * hash + Objects.hashCode(this.getContactFirstName());
-               return hash;
-       }
-
-       /**
-        * Initialization with fake contactGender UNKNOWN
-        */
-       @PostConstruct
-       public void init () {
-               // Fake contactGender
-               this.contactGender = Gender.UNKNOWN;
-       }
-
        @Override
        public Boolean isOwnContact () {
                return this.contactOwnContact;