2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.model
.agent
;
12 import java
.io
.Serializable
;
14 import java
.util
.ArrayList
;
15 import java
.util
.HashSet
;
16 import java
.util
.List
;
19 import javax
.persistence
.Column
;
20 import javax
.persistence
.ElementCollection
;
21 import javax
.persistence
.Embeddable
;
22 import javax
.persistence
.FetchType
;
23 import javax
.persistence
.OneToMany
;
24 import javax
.persistence
.Transient
;
25 import javax
.xml
.bind
.annotation
.XmlAccessType
;
26 import javax
.xml
.bind
.annotation
.XmlAccessorType
;
27 import javax
.xml
.bind
.annotation
.XmlElement
;
28 import javax
.xml
.bind
.annotation
.XmlElementWrapper
;
29 import javax
.xml
.bind
.annotation
.XmlRootElement
;
30 import javax
.xml
.bind
.annotation
.XmlSchemaType
;
31 import javax
.xml
.bind
.annotation
.XmlType
;
33 import org
.apache
.commons
.lang
.StringUtils
;
34 import org
.apache
.log4j
.Logger
;
35 import org
.hibernate
.annotations
.Cascade
;
36 import org
.hibernate
.annotations
.CascadeType
;
37 import org
.hibernate
.envers
.Audited
;
39 import eu
.etaxonomy
.cdm
.model
.location
.Country
;
40 import eu
.etaxonomy
.cdm
.model
.location
.Point
;
41 import eu
.etaxonomy
.cdm
.strategy
.merge
.MergeException
;
44 * The class for information on how to approach a {@link Person person} or an {@link Institution institution}.
45 * It includes telecommunication data and an electronic as well as
46 * multiple postal addresses.
48 * This class corresponds to: <ul>
49 * <li> ContactDetails according to the TDWG ontology
50 * <li> Contact (partially) according to the ABCD schema
55 * @created 08-Nov-2007 13:06:18
57 @XmlAccessorType(XmlAccessType
.FIELD
)
58 @XmlType(name
= "Contact", propOrder
= {
65 @XmlRootElement(name
= "Contact")
68 public class Contact
implements Serializable
, Cloneable
{
69 private static final long serialVersionUID
= -1851305307069277625L;
70 private static final Logger logger
= Logger
.getLogger(Contact
.class);
73 @XmlElementWrapper(name
= "EmailAddresses", nillable
= true)
74 @XmlElement(name
= "EmailAddress")
75 @ElementCollection(fetch
= FetchType
.LAZY
)
76 @Column(name
= "contact_emailaddresses_element")
77 private List
<String
> emailAddresses
;
79 @XmlElementWrapper(name
= "URLs", nillable
= true)
80 @XmlElement(name
= "URL")
81 @XmlSchemaType(name
= "anyURI")
82 @ElementCollection(fetch
= FetchType
.LAZY
)
83 @Column(name
= "contact_urls_element" /*, length=330 */) //length >255 does not work in InnoDB AUD tables for Key length of (REV, id, url) key
84 private List
<String
> urls
= new ArrayList
<String
>();
86 @XmlElementWrapper(name
= "PhoneNumbers", nillable
= true)
87 @XmlElement(name
= "PhoneNumber")
88 @ElementCollection(fetch
= FetchType
.LAZY
)
89 @Column(name
= "contact_phonenumbers_element")
90 @Cascade({CascadeType
.SAVE_UPDATE
, CascadeType
.MERGE
, CascadeType
.DELETE
})
91 private List
<String
> phoneNumbers
;
93 @XmlElementWrapper(name
= "FaxNumbers", nillable
= true)
94 @XmlElement(name
= "FaxNumber")
95 @ElementCollection(fetch
= FetchType
.LAZY
)
96 @Column(name
= "contact_faxnumbers_element")
97 private List
<String
> faxNumbers
;
99 @XmlElementWrapper(name
= "Addresses", nillable
= true)
100 @XmlElement(name
= "Address")
101 @OneToMany(fetch
= FetchType
.LAZY
, orphanRemoval
=true)
102 @Cascade({CascadeType
.SAVE_UPDATE
, CascadeType
.MERGE
})
103 protected Set
<Address
> addresses
;
106 public static Contact
NewInstance() {
107 return new Contact();
111 * Creates a new contact
125 public static Contact
NewInstance(String street
, String postcode
, String locality
,
126 Country country
, String pobox
, String region
,
127 String email
, String faxNumber
, String phoneNumber
, URI url
, Point location
) {
128 Contact result
= new Contact();
129 if (country
!= null || StringUtils
.isNotBlank(locality
) || StringUtils
.isNotBlank(pobox
) || StringUtils
.isNotBlank(postcode
) ||
130 StringUtils
.isNotBlank(region
) || StringUtils
.isNotBlank(street
) ){
131 Address newAddress
= Address
.NewInstance(country
, locality
, pobox
, postcode
, region
, street
, location
);
132 result
.addAddress(newAddress
);
135 result
.addEmailAddress(email
);
137 if (faxNumber
!= null){
138 result
.addFaxNumber(faxNumber
);
140 if (phoneNumber
!= null){
141 result
.addPhoneNumber(phoneNumber
);
150 public static Contact
NewInstance(Set
<Address
> addresses
, List
<String
> emailAdresses
,
151 List
<String
> faxNumbers
, List
<String
> phoneNumbers
, List
<URI
> urls
) {
152 Contact result
= new Contact();
153 if (addresses
!= null){
154 result
.addresses
= addresses
;
156 if (emailAdresses
!= null){
157 result
.emailAddresses
= emailAdresses
;
159 if (faxNumbers
!= null){
160 result
.faxNumbers
= faxNumbers
;
162 if (phoneNumbers
!= null){
163 result
.phoneNumbers
= phoneNumbers
;
166 for (URI uri
: urls
){
167 result
.urls
.add(uri
.toString());
181 public void merge(Contact contact2
) throws MergeException
{
182 if (contact2
!= null){
183 mergeList(this.getEmailAddresses(), contact2
.getEmailAddresses());
184 mergeList(this.getFaxNumbers(), contact2
.getFaxNumbers());
185 mergeList(this.getPhoneNumbers(), contact2
.getPhoneNumbers());
186 mergeList(this.getUrls(), contact2
.getUrls());
187 for (Address address
: contact2
.getAddresses()){
189 this.addresses
.add((Address
)address
.clone());
190 } catch (CloneNotSupportedException e
) {
191 throw new MergeException("Address must implement Cloneable");
197 private void mergeList(List list1
, List list2
){
198 for (Object obj2
: list2
){
199 if (! list1
.contains(obj2
)){
207 * Returns the set of postal {@link Address addresses} belonging to <i>this</i> contact.
208 * A {@link Person person} or an {@link Institution institution} cannot have more than one contact,
209 * but a contact may include several postal addresses.
211 * @return the set of postal addresses
214 public Set
<Address
> getAddresses(){
215 if(this.addresses
== null) {
216 this.addresses
= new HashSet
<Address
>();
218 return this.addresses
;
222 * Adds a new postal {@link Address address} to the set of postal addresses of <i>this</i> contact.
224 * @param address the address to be added
225 * @see #getAddresses()
228 public void addAddress(Address address
){
229 if (address
!= null){
230 getAddresses().add(address
);
234 public Address
addAddress(String street
, String postcode
, String locality
,
235 Country country
, String pobox
, String region
, Point location
){
236 Address newAddress
= Address
.NewInstance(country
, locality
, pobox
, postcode
, region
, street
, location
);
237 getAddresses().add(newAddress
);
242 * Removes one element from the set of postal addresses of <i>this</i> contact.
244 * @param address the postal address of <i>this</i> contact which should be deleted
245 * @see #getAddresses()
247 public void removeAddress(Address address
){
248 getAddresses().remove(address
);
253 * Returns the List of strings representing the electronic mail addresses
254 * included in <i>this</i> contact.
256 public List
<String
> getEmailAddresses(){
257 if(this.emailAddresses
== null) {
258 this.emailAddresses
= new ArrayList
<String
>();
260 return this.emailAddresses
;
264 * @see #getEmailAddress()
266 public void addEmailAddress(String emailAddress
){
267 getEmailAddresses().add(emailAddress
);
271 * Removes one element from the list of email addresses of <i>this</i> contact.
273 * @param emailAddress the email address of <i>this</i> contact which should be deleted
274 * @see #getEmailAddresses()
276 public void removeEmailAddress(String emailAddress
){
277 getEmailAddresses().remove(emailAddress
);
281 // * Returns the list of {@link URI URIs} representing this contact
282 // * included in <i>this</i> contact.
284 // @Transient //TODO preliminary workaround as we get LazyInit Exception in JSON #4444
285 // public List<URI> getUrls(){
286 // List<URI> result = new ArrayList<URI>();
287 // if(this.urls != null) {
288 // for (String uri : this.urls){
289 // result.add(URI.create(uri));
296 * Returns the list of {@link URI URIs} representing this contact
297 * included in <i>this</i> contact.
299 public List
<String
> getUrls(){
306 public void addUrl(URI url
){
307 this.urls
.add(url
.toString());
311 * Removes one element from the list of urls of <i>this</i> contact.
313 * @param url the url of <i>this</i> contact which should be deleted
316 public void removeUrl(URI url
){
317 this.urls
.remove(url
.toString());
322 * Returns the list of strings representing the phone numbers
323 * included in <i>this</i> contact.
325 public List
<String
> getPhoneNumbers(){
326 if(this.phoneNumbers
== null) {
327 this.phoneNumbers
= new ArrayList
<String
>();
329 return this.phoneNumbers
;
335 public void addPhoneNumber(String phoneNumber
){
336 getPhoneNumbers().add(phoneNumber
);
340 * Removes one element from the list of phone numbers of <i>this</i> contact.
342 * @param phoneNumber the phone number of <i>this</i> contact which should be deleted
343 * @see #getPhoneNumber()
345 public void removePhoneNumber(String phoneNumber
){
346 getPhoneNumbers().remove(phoneNumber
);
350 * Returns the list of strings representing the telefax numbers
351 * included in <i>this</i> contact.
353 public List
<String
> getFaxNumbers(){
354 if(this.faxNumbers
== null) {
355 this.faxNumbers
= new ArrayList
<String
>();
357 return this.faxNumbers
;
361 * @see #getFaxNumbers()
363 public void addFaxNumber(String faxNumber
){
364 getFaxNumbers().add(faxNumber
);
368 * Removes one element from the list of telefax numbers of <i>this</i> contact.
370 * @param faxNumber the telefax number of <i>this</i> contact which should be deleted
371 * @see #getFaxNumber()
373 public void removeFaxNumber(String faxNumber
){
374 getFaxNumbers().remove(faxNumber
);
377 //*********************** CLONE ********************************************************/
380 * Clones <i>this</i> Contact. This is a shortcut that enables to create
381 * a new instance that differs only slightly from <i>this</i> Contact.
384 * @see java.lang.Object#clone()
387 public Object
clone() {
389 Contact result
= (Contact
) super.clone();
390 result
.addresses
= new HashSet
<Address
>();
391 for (Address adr
: this.addresses
){
392 result
.addAddress((Address
)adr
.clone());
394 //no changes to emailAdresses, faxNumbers, phoneNumbers, urls
396 }catch (CloneNotSupportedException e
){
397 logger
.warn("Object does not implement cloneable");