X-Git-Url: https://dev.e-taxonomy.eu/gitweb/cdmlib.git/blobdiff_plain/d671902e4c827aec4ace5a1196e9a3f6a5281c10..2720ea2c66c7951dcab5bccad915afd3fbc77ced:/cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/agent/Contact.java diff --git a/cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/agent/Contact.java b/cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/agent/Contact.java index 8f756415f9..a26fa651e7 100644 --- a/cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/agent/Contact.java +++ b/cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/agent/Contact.java @@ -6,11 +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; @@ -20,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; @@ -28,14 +30,14 @@ 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; import org.hibernate.envers.Audited; +import eu.etaxonomy.cdm.model.location.Country; import eu.etaxonomy.cdm.model.location.Point; -import eu.etaxonomy.cdm.model.location.WaterbodyOrCountry; import eu.etaxonomy.cdm.strategy.merge.MergeException; /** @@ -49,8 +51,7 @@ import eu.etaxonomy.cdm.strategy.merge.MergeException; * * * @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 = { @@ -68,6 +69,38 @@ public class Contact implements Serializable, Cloneable { private static final Logger logger = Logger.getLogger(Contact.class); + @XmlElementWrapper(name = "EmailAddresses", nillable = true) + @XmlElement(name = "EmailAddress") + @ElementCollection(fetch = FetchType.LAZY) + @Column(name = "contact_emailaddresses_element") + private List 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 */) //length >255 does not work in InnoDB AUD tables for Key length of (REV, id, url) key + private final List 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 phoneNumbers = new ArrayList<>(); + + @XmlElementWrapper(name = "FaxNumbers", nillable = true) + @XmlElement(name = "FaxNumber") + @ElementCollection(fetch = FetchType.LAZY) + @Column(name = "contact_faxnumbers_element") + private List 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
addresses = new HashSet<>(); + public static Contact NewInstance() { return new Contact(); } @@ -88,8 +121,8 @@ public class Contact implements Serializable, Cloneable { * @return */ public static Contact NewInstance(String street, String postcode, String locality, - WaterbodyOrCountry country, String pobox, String region, - String email, String faxNumber, String phoneNumber, String url, Point location) { + Country country, String pobox, String region, + String email, String faxNumber, String phoneNumber, URI url, Point location) { Contact result = new Contact(); if (country != null || StringUtils.isNotBlank(locality) || StringUtils.isNotBlank(pobox) || StringUtils.isNotBlank(postcode) || StringUtils.isNotBlank(region) || StringUtils.isNotBlank(street) ){ @@ -112,14 +145,14 @@ public class Contact implements Serializable, Cloneable { } - public static Contact NewInstance(Set
addresses, List emailAdresses, - List faxNumbers, List phoneNumbers, List urls) { + public static Contact NewInstance(Set
addresses, List emailAddresses, + List faxNumbers, List phoneNumbers, List 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; @@ -128,49 +161,21 @@ public class Contact implements Serializable, Cloneable { result.phoneNumbers = phoneNumbers; } if (urls != null){ - result.urls = urls; + for (URI uri : urls){ + result.urls.add(uri.toString()); + } } return result; } - +// ************************ CONSTRUCTOR **************************/ /** * Class constructor. */ public Contact() { } - @XmlElementWrapper(name = "EmailAddresses", nillable = true) - @XmlElement(name = "EmailAddress") - @ElementCollection(fetch = FetchType.LAZY) - @Column(name = "contact_emailaddresses_element") - private List emailAddresses; - - @XmlElementWrapper(name = "URLs", nillable = true) - @XmlElement(name = "URL") - @XmlSchemaType(name = "anyURI") - @ElementCollection(fetch = FetchType.LAZY) - @Column(name = "contact_urls_element") - private List urls; - - @XmlElementWrapper(name = "PhoneNumbers", nillable = true) - @XmlElement(name = "PhoneNumber") - @ElementCollection(fetch = FetchType.LAZY) - @Column(name = "contact_phonenumbers_element") - private List phoneNumbers; - - @XmlElementWrapper(name = "FaxNumbers", nillable = true) - @XmlElement(name = "FaxNumber") - @ElementCollection(fetch = FetchType.LAZY) - @Column(name = "contact_faxnumbers_element") - private List faxNumbers; - - @XmlElementWrapper(name = "Addresses", nillable = true) - @XmlElement(name = "Address") - @OneToMany(fetch = FetchType.LAZY) - @Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.DELETE_ORPHAN}) - protected Set
addresses; - +// ************************ MERGE /MATCH ***************************/ public void merge(Contact contact2) throws MergeException{ if (contact2 != null){ @@ -180,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"); } @@ -196,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 collection) { + return collection == null || collection.isEmpty(); + } + + /** * Returns the set of postal {@link Address addresses} belonging to this contact. * A {@link Person person} or an {@link Institution institution} cannot have more than one contact, * but a contact may include several postal addresses. @@ -206,9 +230,6 @@ public class Contact implements Serializable, Cloneable { * @see Address */ public Set
getAddresses(){ - if(this.addresses == null) { - this.addresses = new HashSet
(); - } return this.addresses; } @@ -225,10 +246,11 @@ public class Contact implements Serializable, Cloneable { } } - public void addAddress(String street, String postcode, String locality, - WaterbodyOrCountry country, String pobox, String region, Point location){ + public Address addAddress(String street, String postcode, String locality, + Country country, String pobox, String region, Point location){ Address newAddress = Address.NewInstance(country, locality, pobox, postcode, region, street, location); getAddresses().add(newAddress); + return newAddress; } /** @@ -248,7 +270,7 @@ public class Contact implements Serializable, Cloneable { */ public List getEmailAddresses(){ if(this.emailAddresses == null) { - this.emailAddresses = new ArrayList(); + this.emailAddresses = new ArrayList<>(); } return this.emailAddresses; } @@ -270,22 +292,34 @@ public class Contact implements Serializable, Cloneable { getEmailAddresses().remove(emailAddress); } +// /** +// * Returns the list of {@link URI URIs} representing this contact +// * included in this contact. +// */ +// @Transient //TODO preliminary workaround as we get LazyInit Exception in JSON #4444 +// public List getUrls(){ +// List result = new ArrayList(); +// 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 this contact. */ public List getUrls(){ - if(this.urls == null) { - this.urls = new ArrayList(); - } return this.urls; } /** * @see #getUrls() */ - public void addUrl(String url){ - getUrls().add(url); + public void addUrl(URI url){ + this.urls.add(url.toString()); } /** @@ -294,17 +328,18 @@ public class Contact implements Serializable, Cloneable { * @param url the url of this contact which should be deleted * @see #getUrls() */ - public void removeUrl(String url){ - getUrls().remove(url); + public void removeUrl(URI url){ + this.urls.remove(url.toString()); } + /** * Returns the list of strings representing the phone numbers * included in this contact. */ public List getPhoneNumbers(){ if(this.phoneNumbers == null) { - this.phoneNumbers = new ArrayList(); + this.phoneNumbers = new ArrayList<>(); } return this.phoneNumbers; } @@ -332,7 +367,7 @@ public class Contact implements Serializable, Cloneable { */ public List getFaxNumbers(){ if(this.faxNumbers == null) { - this.faxNumbers = new ArrayList(); + this.faxNumbers = new ArrayList<>(); } return this.faxNumbers; } @@ -364,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
(); + 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"); @@ -379,4 +414,4 @@ public class Contact implements Serializable, Cloneable { return null; } } -} \ No newline at end of file +}