cleanup
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / agent / Contact.java
index 24012c5517c97479a7776a7deea0881dc643610a..a26fa651e7029abd519d38954a433f6d58b18069 100644 (file)
@@ -6,12 +6,12 @@
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * See LICENSE.TXT at the top of this package for the full license terms.
 */
-
 package eu.etaxonomy.cdm.model.agent;
 
 import java.io.Serializable;
 import java.net.URI;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
@@ -21,6 +21,7 @@ import javax.persistence.ElementCollection;
 import javax.persistence.Embeddable;
 import javax.persistence.FetchType;
 import javax.persistence.OneToMany;
+import javax.persistence.Transient;
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlElement;
@@ -29,7 +30,7 @@ import javax.xml.bind.annotation.XmlRootElement;
 import javax.xml.bind.annotation.XmlSchemaType;
 import javax.xml.bind.annotation.XmlType;
 
-import org.apache.commons.lang.StringUtils;
+import org.apache.commons.lang3.StringUtils;
 import org.apache.log4j.Logger;
 import org.hibernate.annotations.Cascade;
 import org.hibernate.annotations.CascadeType;
@@ -50,8 +51,7 @@ import eu.etaxonomy.cdm.strategy.merge.MergeException;
  * </ul>
  *
  * @author m.doering
- * @version 1.0
- * @created 08-Nov-2007 13:06:18
+ * @since 08-Nov-2007 13:06:18
  */
 @XmlAccessorType(XmlAccessType.FIELD)
 @XmlType(name = "Contact", propOrder = {
@@ -73,36 +73,34 @@ public class Contact implements Serializable, Cloneable {
        @XmlElement(name = "EmailAddress")
        @ElementCollection(fetch = FetchType.LAZY)
        @Column(name = "contact_emailaddresses_element")
-       private List<String> emailAddresses;
+       private List<String> emailAddresses = new ArrayList<>();
 
        @XmlElementWrapper(name = "URLs", nillable = true)
        @XmlElement(name = "URL")
     @XmlSchemaType(name = "anyURI")
        @ElementCollection(fetch = FetchType.LAZY)
-    @Column(name = "contact_urls_element", length=330)
-       @Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.DELETE})
-       private List<String> urls = new ArrayList<String>();
+    @Column(name = "contact_urls_element" /*, length=330  */)  //length >255 does not work in InnoDB AUD tables for Key length of (REV, id, url) key
+       private final List<String> urls = new ArrayList<>();
 
        @XmlElementWrapper(name = "PhoneNumbers", nillable = true)
        @XmlElement(name = "PhoneNumber")
        @ElementCollection(fetch = FetchType.LAZY)
     @Column(name = "contact_phonenumbers_element")
        @Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.DELETE})
-       private List<String> phoneNumbers;
+       private List<String> phoneNumbers = new ArrayList<>();
 
        @XmlElementWrapper(name = "FaxNumbers", nillable = true)
        @XmlElement(name = "FaxNumber")
        @ElementCollection(fetch = FetchType.LAZY)
     @Column(name = "contact_faxnumbers_element")
-       private List<String> faxNumbers;
+       private List<String> faxNumbers = new ArrayList<>();
 
     @XmlElementWrapper(name = "Addresses", nillable = true)
     @XmlElement(name = "Address")
     @OneToMany(fetch = FetchType.LAZY, orphanRemoval=true)
        @Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE})
-       protected Set<Address> addresses;
+       protected Set<Address> addresses = new HashSet<>();
 
-       
        public static Contact NewInstance() {
                return new Contact();
        }
@@ -147,14 +145,14 @@ public class Contact implements Serializable, Cloneable {
        }
 
 
-       public static Contact NewInstance(Set<Address> addresses, List<String> emailAdresses,
+       public static Contact NewInstance(Set<Address> addresses, List<String> emailAddresses,
                        List<String> faxNumbers, List<String> phoneNumbers, List<URI> urls) {
                Contact result = new Contact();
                if (addresses != null){
                        result.addresses = addresses;
                }
-               if (emailAdresses != null){
-                       result.emailAddresses = emailAdresses;
+               if (emailAddresses != null){
+                       result.emailAddresses = emailAddresses;
                }
                if (faxNumbers != null){
                        result.faxNumbers = faxNumbers;
@@ -170,13 +168,14 @@ public class Contact implements Serializable, Cloneable {
                return result;
        }
 
-
+// ************************ CONSTRUCTOR **************************/
        /**
         * Class constructor.
         */
        public Contact() {
        }
 
+// ************************ MERGE /MATCH ***************************/
 
        public void merge(Contact contact2) throws MergeException{
                if (contact2 != null){
@@ -186,7 +185,10 @@ public class Contact implements Serializable, Cloneable {
                        mergeList(this.getUrls(), contact2.getUrls());
                        for (Address address : contact2.getAddresses()){
                                try {
-                                       this.addresses.add((Address)address.clone());
+                                       if (this.addresses == null){
+                                               this.addresses = new HashSet<>();
+                                       }
+                                       this.addresses.add(address.clone());
                                } catch (CloneNotSupportedException e) {
                                        throw new MergeException("Address must implement Cloneable");
                                }
@@ -202,8 +204,24 @@ public class Contact implements Serializable, Cloneable {
                }
        }
 
-
-       /**
+    /**
+     * True, if no contact data exists in any of the lists (email, phone, ...).
+     */
+    @Transient
+    public boolean isEmpty(){
+        if (isEmpty(emailAddresses) && isEmpty(faxNumbers) && isEmpty(phoneNumbers)
+                && isEmpty(urls) && isEmpty(addresses)){
+            return true;
+        }else{
+            return false;
+        }
+    }
+
+    private boolean isEmpty(Collection<? extends Object> collection) {
+        return collection == null || collection.isEmpty();
+    }
+
+    /**
         * Returns the set of postal {@link Address addresses} belonging to <i>this</i> contact.
         * A {@link Person person} or an {@link Institution institution} cannot have more than one contact,
         * but a contact may include several postal addresses.
@@ -212,9 +230,6 @@ public class Contact implements Serializable, Cloneable {
         * @see     Address
         */
        public Set<Address> getAddresses(){
-               if(this.addresses == null) {
-                       this.addresses = new HashSet<Address>();
-               }
                return this.addresses;
        }
 
@@ -255,7 +270,7 @@ public class Contact implements Serializable, Cloneable {
         */
        public List<String> getEmailAddresses(){
                if(this.emailAddresses == null) {
-                       this.emailAddresses = new ArrayList<String>();
+                       this.emailAddresses = new ArrayList<>();
                }
                return this.emailAddresses;
        }
@@ -277,18 +292,27 @@ public class Contact implements Serializable, Cloneable {
                getEmailAddresses().remove(emailAddress);
        }
 
+//     /**
+//      * Returns the list of {@link URI URIs} representing this contact
+//      * included in <i>this</i> contact.
+//      */
+//     @Transient   //TODO preliminary workaround as we get LazyInit Exception in JSON #4444
+//     public List<URI> getUrls(){
+//             List<URI> result = new ArrayList<URI>();
+//             if(this.urls != null) {
+//                     for (String uri : this.urls){
+//                             result.add(URI.create(uri));
+//                     }
+//             }
+//             return result;
+//     }
+
        /**
-        * Returns the list of strings representing the "Uniform Resource Locators" (urls)
+        * Returns the list of {@link URI URIs} representing this contact
         * included in <i>this</i> contact.
         */
-       public List<URI> getUrls(){
-               List<URI> result = new ArrayList<URI>();
-               if(this.urls != null) {
-                       for (String uri : this.urls){
-                               result.add(URI.create(uri));
-                       }
-               }
-               return result;
+       public List<String> getUrls(){
+               return this.urls;
        }
 
        /**
@@ -308,13 +332,14 @@ public class Contact implements Serializable, Cloneable {
                this.urls.remove(url.toString());
        }
 
+
        /**
         * Returns the list of strings representing the phone numbers
         * included in <i>this</i> contact.
         */
        public List<String> getPhoneNumbers(){
                if(this.phoneNumbers == null) {
-                       this.phoneNumbers = new ArrayList<String>();
+                       this.phoneNumbers = new ArrayList<>();
                }
                return this.phoneNumbers;
        }
@@ -342,7 +367,7 @@ public class Contact implements Serializable, Cloneable {
         */
        public List<String> getFaxNumbers(){
                if(this.faxNumbers == null) {
-                       this.faxNumbers = new ArrayList<String>();
+                       this.faxNumbers = new ArrayList<>();
                }
                return this.faxNumbers;
        }
@@ -374,14 +399,14 @@ public class Contact implements Serializable, Cloneable {
         * @see java.lang.Object#clone()
         */
        @Override
-       public Object clone() {
+       public Contact clone() {
                try{
                        Contact result = (Contact) super.clone();
-                       result.addresses = new HashSet<Address>();
+                       result.addresses = new HashSet<>();
                        for (Address adr : this.addresses){
-                               result.addAddress((Address)adr.clone());
+                               result.addAddress(adr.clone());
                        }
-                       //no changes to emailAdresses, faxNumbers, phoneNumbers, urls
+                       //no changes to emailAdresses, faxnumbers, phonenumbers, urls
                        return result;
                }catch (CloneNotSupportedException e){
                        logger.warn("Object does not implement cloneable");
@@ -389,4 +414,4 @@ public class Contact implements Serializable, Cloneable {
                        return null;
                }
        }
-}
\ No newline at end of file
+}