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
;
13 import java
.util
.ArrayList
;
14 import java
.util
.HashSet
;
15 import java
.util
.List
;
18 import javax
.persistence
.Embeddable
;
19 import javax
.persistence
.FetchType
;
20 import javax
.persistence
.OneToMany
;
21 import javax
.xml
.bind
.annotation
.XmlAccessType
;
22 import javax
.xml
.bind
.annotation
.XmlAccessorType
;
23 import javax
.xml
.bind
.annotation
.XmlElement
;
24 import javax
.xml
.bind
.annotation
.XmlElementWrapper
;
25 import javax
.xml
.bind
.annotation
.XmlRootElement
;
26 import javax
.xml
.bind
.annotation
.XmlSchemaType
;
27 import javax
.xml
.bind
.annotation
.XmlType
;
29 import org
.apache
.log4j
.Logger
;
30 import org
.hibernate
.annotations
.Cascade
;
31 import org
.hibernate
.annotations
.CascadeType
;
32 import org
.hibernate
.annotations
.CollectionOfElements
;
33 import org
.hibernate
.envers
.Audited
;
35 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
36 import eu
.etaxonomy
.cdm
.model
.location
.Point
;
37 import eu
.etaxonomy
.cdm
.model
.location
.WaterbodyOrCountry
;
38 import eu
.etaxonomy
.cdm
.strategy
.merge
.MergeException
;
41 * The class for information on how to approach a {@link Person person} or an {@link Institution institution}.
42 * It includes telecommunication data and an electronic as well as
43 * multiple postal addresses.
45 * This class corresponds to: <ul>
46 * <li> ContactDetails according to the TDWG ontology
47 * <li> Contact (partially) according to the ABCD schema
52 * @created 08-Nov-2007 13:06:18
54 @XmlAccessorType(XmlAccessType
.FIELD
)
55 @XmlType(name
= "Contact", propOrder
= {
62 @XmlRootElement(name
= "Contact")
65 public class Contact
implements Serializable
, Cloneable
{
66 private static final long serialVersionUID
= -1851305307069277625L;
67 @SuppressWarnings("unused")
68 private static final Logger logger
= Logger
.getLogger(Contact
.class);
71 public static Contact
NewInstance() {
76 * Creates a new contact
90 public static Contact
NewInstance(String street
, String postcode
, String locality
,
91 WaterbodyOrCountry country
, String pobox
, String region
,
92 String email
, String faxNumber
, String phoneNumber
, String url
, Point location
) {
93 Contact result
= new Contact();
94 if (country
!= null || CdmUtils
.isNotEmpty(locality
) || CdmUtils
.isNotEmpty(pobox
) || CdmUtils
.isNotEmpty(postcode
) ||
95 CdmUtils
.isNotEmpty(region
) || CdmUtils
.isNotEmpty(street
) ){
96 Address newAddress
= Address
.NewInstance(country
, locality
, pobox
, postcode
, region
, street
, location
);
97 result
.addAddress(newAddress
);
100 result
.addEmailAddress(email
);
102 if (faxNumber
!= null){
103 result
.addFaxNumber(faxNumber
);
105 if (phoneNumber
!= null){
106 result
.addPhoneNumber(phoneNumber
);
115 public static Contact
NewInstance(Set
<Address
> addresses
, List
<String
> emailAdresses
,
116 List
<String
> faxNumbers
, List
<String
> phoneNumbers
, List
<String
> urls
) {
117 Contact result
= new Contact();
118 if (addresses
!= null){
119 result
.addresses
= addresses
;
121 if (emailAdresses
!= null){
122 result
.emailAddresses
= emailAdresses
;
124 if (faxNumbers
!= null){
125 result
.faxNumbers
= faxNumbers
;
127 if (phoneNumbers
!= null){
128 result
.phoneNumbers
= phoneNumbers
;
143 @XmlElementWrapper(name
= "EmailAddresses", nillable
= true)
144 @XmlElement(name
= "EmailAddress")
145 @CollectionOfElements(fetch
= FetchType
.LAZY
)
146 private List
<String
> emailAddresses
;
148 @XmlElementWrapper(name
= "URLs", nillable
= true)
149 @XmlElement(name
= "URL")
150 @XmlSchemaType(name
= "anyURI")
151 @CollectionOfElements(fetch
= FetchType
.LAZY
)
152 private List
<String
> urls
;
154 @XmlElementWrapper(name
= "PhoneNumbers", nillable
= true)
155 @XmlElement(name
= "PhoneNumber")
156 @CollectionOfElements(fetch
= FetchType
.LAZY
)
157 private List
<String
> phoneNumbers
;
159 @XmlElementWrapper(name
= "FaxNumbers", nillable
= true)
160 @XmlElement(name
= "FaxNumber")
161 @CollectionOfElements(fetch
= FetchType
.LAZY
)
162 private List
<String
> faxNumbers
;
164 @XmlElementWrapper(name
= "Addresses", nillable
= true)
165 @XmlElement(name
= "Address")
166 @OneToMany(fetch
= FetchType
.LAZY
)
167 @Cascade({CascadeType
.SAVE_UPDATE
, CascadeType
.MERGE
, CascadeType
.DELETE_ORPHAN
})
168 protected Set
<Address
> addresses
;
171 public void merge(Contact contact2
) throws MergeException
{
172 if (contact2
!= null){
173 mergeList(this.getEmailAddresses(), contact2
.getEmailAddresses());
174 mergeList(this.getFaxNumbers(), contact2
.getFaxNumbers());
175 mergeList(this.getPhoneNumbers(), contact2
.getPhoneNumbers());
176 mergeList(this.getUrls(), contact2
.getUrls());
177 for (Address address
: contact2
.getAddresses()){
179 this.addresses
.add((Address
)address
.clone());
180 } catch (CloneNotSupportedException e
) {
181 throw new MergeException("Address must implement Cloneable");
187 private void mergeList(List list1
, List list2
){
188 for (Object obj2
: list2
){
189 if (! list1
.contains(obj2
)){
197 * Returns the set of postal {@link Address addresses} belonging to <i>this</i> contact.
198 * A {@link Person person} or an {@link Institution institution} cannot have more than one contact,
199 * but a contact may include several postal addresses.
201 * @return the set of postal addresses
204 public Set
<Address
> getAddresses(){
205 if(this.addresses
== null) {
206 this.addresses
= new HashSet
<Address
>();
208 return this.addresses
;
212 * Adds a new postal {@link Address address} to the set of postal addresses of <i>this</i> contact.
214 * @param address the address to be added
215 * @see #getAddresses()
218 public void addAddress(Address address
){
219 if (address
!= null){
220 getAddresses().add(address
);
224 public void addAddress(String street
, String postcode
, String locality
,
225 WaterbodyOrCountry country
, String pobox
, String region
, Point location
){
226 Address newAddress
= Address
.NewInstance(country
, locality
, pobox
, postcode
, region
, street
, location
);
227 getAddresses().add(newAddress
);
231 * Removes one element from the set of postal addresses of <i>this</i> contact.
233 * @param address the postal address of <i>this</i> contact which should be deleted
234 * @see #getAddresses()
236 public void removeAddress(Address address
){
237 getAddresses().remove(address
);
242 * Returns the List of strings representing the electronic mail addresses
243 * included in <i>this</i> contact.
245 public List
<String
> getEmailAddresses(){
246 if(this.emailAddresses
== null) {
247 this.emailAddresses
= new ArrayList
<String
>();
249 return this.emailAddresses
;
253 * @see #getEmailAddress()
255 public void addEmailAddress(String emailAddress
){
256 getEmailAddresses().add(emailAddress
);
260 * Removes one element from the list of email addresses of <i>this</i> contact.
262 * @param emailAddress the email address of <i>this</i> contact which should be deleted
263 * @see #getEmailAddresses()
265 public void removeEmailAddress(String emailAddress
){
266 getEmailAddresses().remove(emailAddress
);
270 * Returns the list of strings representing the "Uniform Resource Locators" (urls)
271 * included in <i>this</i> contact.
273 public List
<String
> getUrls(){
274 if(this.urls
== null) {
275 this.urls
= new ArrayList
<String
>();
283 public void addUrl(String url
){
288 * Removes one element from the list of urls of <i>this</i> contact.
290 * @param url the url of <i>this</i> contact which should be deleted
293 public void removeUrl(String url
){
294 getUrls().remove(url
);
298 * Returns the list of strings representing the phone numbers
299 * included in <i>this</i> contact.
301 public List
<String
> getPhoneNumbers(){
302 if(this.phoneNumbers
== null) {
303 this.phoneNumbers
= new ArrayList
<String
>();
305 return this.phoneNumbers
;
311 public void addPhoneNumber(String phoneNumber
){
312 getPhoneNumbers().add(phoneNumber
);
316 * Removes one element from the list of phone numbers of <i>this</i> contact.
318 * @param phoneNumber the phone number of <i>this</i> contact which should be deleted
319 * @see #getPhoneNumber()
321 public void removePhoneNumber(String phoneNumber
){
322 getPhoneNumbers().remove(phoneNumber
);
326 * Returns the list of strings representing the telefax numbers
327 * included in <i>this</i> contact.
329 public List
<String
> getFaxNumbers(){
330 if(this.faxNumbers
== null) {
331 this.faxNumbers
= new ArrayList
<String
>();
333 return this.faxNumbers
;
337 * @see #getFaxNumbers()
339 public void addFaxNumber(String faxNumber
){
340 getFaxNumbers().add(faxNumber
);
344 * Removes one element from the list of telefax numbers of <i>this</i> contact.
346 * @param faxNumber the telefax number of <i>this</i> contact which should be deleted
347 * @see #getFaxNumber()
349 public void removeFaxNumber(String faxNumber
){
350 getFaxNumbers().remove(faxNumber
);
353 //*********************** CLONE ********************************************************/
356 * Clones <i>this</i> Contact. This is a shortcut that enables to create
357 * a new instance that differs only slightly from <i>this</i> Contact.
360 * @see java.lang.Object#clone()
363 public Object
clone() {
365 Contact result
= (Contact
) super.clone();
366 result
.addresses
= new HashSet
<Address
>();
367 for (Address adr
: this.addresses
){
368 result
.addAddress((Address
)adr
.clone());
370 //no changes to emailAdresses, faxNumbers, phoneNumbers, urls
372 }catch (CloneNotSupportedException e
){
373 logger
.warn("Object does not implement cloneable");