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
{
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
.addresses
.add(newAddress
);
100 result
.emailAddresses
.add(email
);
102 if (faxNumber
!= null){
103 result
.faxNumbers
.add(faxNumber
);
105 if (phoneNumber
!= null){
106 result
.phoneNumbers
.add(phoneNumber
);
109 result
.urls
.add(url
);
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")
144 @XmlElement(name
= "EmailAddress")
145 @CollectionOfElements(fetch
= FetchType
.LAZY
)
146 private List
<String
> emailAddresses
= new ArrayList
<String
>();
148 @XmlElementWrapper(name
= "URLs")
149 @XmlElement(name
= "URL")
150 @XmlSchemaType(name
= "anyURI")
151 @CollectionOfElements(fetch
= FetchType
.LAZY
)
152 private List
<String
> urls
= new ArrayList
<String
>();
154 @XmlElementWrapper(name
= "PhoneNumbers")
155 @XmlElement(name
= "PhoneNumber")
156 @CollectionOfElements(fetch
= FetchType
.LAZY
)
157 private List
<String
> phoneNumbers
= new ArrayList
<String
>();
159 @XmlElementWrapper(name
= "FaxNumbers")
160 @XmlElement(name
= "FaxNumber")
161 @CollectionOfElements(fetch
= FetchType
.LAZY
)
162 private List
<String
> faxNumbers
= new ArrayList
<String
>();
164 @XmlElementWrapper(name
= "Addresses")
165 @XmlElement(name
= "Address")
166 @OneToMany(fetch
= FetchType
.LAZY
)
167 @Cascade({CascadeType
.SAVE_UPDATE
, CascadeType
.MERGE
, CascadeType
.DELETE_ORPHAN
})
168 protected Set
<Address
> addresses
= new HashSet
<Address
>();
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 return this.addresses
;
209 * Adds a new postal {@link Address address} to the set of postal addresses of <i>this</i> contact.
211 * @param address the address to be added
212 * @see #getAddresses()
215 public void addAddress(Address address
){
216 if (address
!= null){
217 addresses
.add(address
);
221 public void addAddress(String street
, String postcode
, String locality
,
222 WaterbodyOrCountry country
, String pobox
, String region
, Point location
){
223 Address newAddress
= Address
.NewInstance(country
, locality
, pobox
, postcode
, region
, street
, location
);
224 addresses
.add(newAddress
);
228 * Removes one element from the set of postal addresses of <i>this</i> contact.
230 * @param address the postal address of <i>this</i> contact which should be deleted
231 * @see #getAddresses()
233 public void removeAddress(Address address
){
234 addresses
.remove(address
);
239 * Returns the List of strings representing the electronic mail addresses
240 * included in <i>this</i> contact.
242 public List
<String
> getEmailAddresses(){
243 return this.emailAddresses
;
247 * @see #getEmailAddress()
249 public void addEmailAddress(String emailAddress
){
250 this.emailAddresses
.add(emailAddress
);
254 * Removes one element from the list of email addresses of <i>this</i> contact.
256 * @param emailAddress the email address of <i>this</i> contact which should be deleted
257 * @see #getEmailAddresses()
259 public void removeEmailAddress(String emailAddress
){
260 emailAddresses
.remove(emailAddress
);
264 * Returns the list of strings representing the "Uniform Resource Locators" (urls)
265 * included in <i>this</i> contact.
267 public List
<String
> getUrls(){
274 public void addUrl(String url
){
279 * Removes one element from the list of urls of <i>this</i> contact.
281 * @param url the url of <i>this</i> contact which should be deleted
284 public void removeUrl(String url
){
289 * Returns the list of strings representing the phone numbers
290 * included in <i>this</i> contact.
292 public List
<String
> getPhoneNumbers(){
293 return this.phoneNumbers
;
299 public void addPhoneNumber(String phoneNumber
){
300 this.phoneNumbers
.add(phoneNumber
);
304 * Removes one element from the list of phone numbers of <i>this</i> contact.
306 * @param phoneNumber the phone number of <i>this</i> contact which should be deleted
307 * @see #getPhoneNumber()
309 public void removePhoneNumber(String phoneNumber
){
310 phoneNumbers
.remove(phoneNumber
);
314 * Returns the list of strings representing the telefax numbers
315 * included in <i>this</i> contact.
317 public List
<String
> getFaxNumbers(){
318 return this.faxNumbers
;
322 * @see #getFaxNumbers()
324 public void addFaxNumber(String faxNumber
){
325 this.faxNumbers
.add(faxNumber
);
329 * Removes one element from the list of telefax numbers of <i>this</i> contact.
331 * @param faxNumber the telefax number of <i>this</i> contact which should be deleted
332 * @see #getFaxNumber()
334 public void removeFaxNumber(String faxNumber
){
335 faxNumbers
.remove(faxNumber
);