Make Contact URL an url #3920
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / agent / AgentBase.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9
10 package eu.etaxonomy.cdm.model.agent;
11
12 import java.net.URI;
13 import java.util.List;
14 import java.util.Set;
15
16 import javax.persistence.Embedded;
17 import javax.persistence.Entity;
18 import javax.persistence.Transient;
19 import javax.xml.bind.annotation.XmlAccessType;
20 import javax.xml.bind.annotation.XmlAccessorType;
21 import javax.xml.bind.annotation.XmlElement;
22 import javax.xml.bind.annotation.XmlType;
23
24 import org.apache.log4j.Logger;
25 import org.hibernate.annotations.Cascade;
26 import org.hibernate.annotations.CascadeType;
27 import org.hibernate.annotations.Index;
28 import org.hibernate.annotations.Table;
29 import org.hibernate.envers.Audited;
30
31 import eu.etaxonomy.cdm.model.location.Point;
32 import eu.etaxonomy.cdm.model.location.Country;
33 import eu.etaxonomy.cdm.model.media.IdentifiableMediaEntity;
34 import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
35 import eu.etaxonomy.cdm.strategy.match.IMatchable;
36 import eu.etaxonomy.cdm.strategy.match.Match;
37 import eu.etaxonomy.cdm.strategy.match.MatchMode;
38 import eu.etaxonomy.cdm.strategy.merge.IMergable;
39 import eu.etaxonomy.cdm.strategy.merge.Merge;
40 import eu.etaxonomy.cdm.strategy.merge.MergeMode;
41
42 /**
43 * The upmost (abstract) class for agents such as persons, teams or institutions.
44 * An agent is a conscious entity which can make decisions, act and create
45 * according to its own knowledge and goals and which may be approached.
46 * Agents can be authors for nomenclatural or bibliographical references as well
47 * as creators of pictures or field collectors or administrators of collections.
48 *
49 * @author m.doering
50 * @version 1.0
51 * @created 08-Nov-2007 13:06:57
52 */
53 @XmlAccessorType(XmlAccessType.FIELD)
54 @XmlType(name = "AgentBase", propOrder = {
55 "contact"
56 })
57 @Entity
58 @Audited
59 @Table(appliesTo="AgentBase", indexes = { @Index(name = "agentTitleCacheIndex", columnNames = { "titleCache" }) })
60 public abstract class AgentBase<S extends IIdentifiableEntityCacheStrategy> extends IdentifiableMediaEntity<S> implements IMergable, IMatchable, Cloneable{
61 private static final long serialVersionUID = 7732768617469448829L;
62 @SuppressWarnings("unused")
63 private static final Logger logger = Logger.getLogger(AgentBase.class);
64
65 @XmlElement(name = "Contact")
66 @Embedded
67 @Merge(MergeMode.MERGE)
68 @Match(MatchMode.IGNORE)
69 @Cascade({CascadeType.SAVE_UPDATE, CascadeType.DELETE})
70 private Contact contact;
71
72 /**
73 * Returns the {@link Contact contact} of <i>this</i> person.
74 * The contact contains several ways to approach <i>this</i> person.
75 *
76 * @see Contact
77 */
78 public Contact getContact(){
79 if(contact == null) {
80 this.contact = new Contact();
81 }
82 return this.contact;
83 }
84 /**
85 * @see #getContact()
86 */
87 public void setContact(Contact contact){
88 this.contact = contact;
89 }
90
91
92 /**
93 * Returns the existing contact, or if it does not exists a new contact.
94 * If <code>create</code> is true the new contact will be set as this agent's
95 * contact.
96 * @param create
97 * @return
98 */
99 @Transient
100 private Contact getNewOrExistingContact(boolean create){
101 if (contact != null){
102 return contact;
103 }else{
104 Contact newContact = Contact.NewInstance();
105 if (create){
106 contact = newContact;
107 this.setContact(contact);
108 }
109 return contact;
110 }
111 }
112
113
114 /**
115 * Adds a new address to this agent
116 * @param street
117 * @param postcode
118 * @param locality
119 * @param country
120 * @param pobox
121 * @param region
122 * @param location
123 * @see eu.etaxonomy.cdm.model.agent.Contact#addAddress(java.lang.String, java.lang.String, java.lang.String, eu.etaxonomy.cdm.model.location.Country, java.lang.String, java.lang.String, eu.etaxonomy.cdm.model.location.Point)
124 */
125 public Address addAddress(String street, String postcode, String locality,
126 Country country, String pobox, String region,
127 Point location) {
128 return getNewOrExistingContact(true).addAddress(street, postcode, locality, country, pobox, region,
129 location);
130 }
131 /**
132 * @param address
133 * @see eu.etaxonomy.cdm.model.agent.Contact#addAddress(eu.etaxonomy.cdm.model.agent.Address)
134 */
135 public void addAddress(Address address) {
136 getNewOrExistingContact(true).addAddress(address);
137 }
138 /**
139 * @param emailAddress
140 * @see eu.etaxonomy.cdm.model.agent.Contact#addEmailAddress(java.lang.String)
141 */
142 public void addEmailAddress(String emailAddress) {
143 getNewOrExistingContact(true).addEmailAddress(emailAddress);
144 }
145 /**
146 * @param faxNumber
147 * @see eu.etaxonomy.cdm.model.agent.Contact#addFaxNumber(java.lang.String)
148 */
149 public void addFaxNumber(String faxNumber) {
150 getNewOrExistingContact(true).addFaxNumber(faxNumber);
151 }
152 /**
153 * @param phoneNumber
154 * @see eu.etaxonomy.cdm.model.agent.Contact#addPhoneNumber(java.lang.String)
155 */
156 public void addPhoneNumber(String phoneNumber) {
157 getNewOrExistingContact(true).addPhoneNumber(phoneNumber);
158 }
159 /**
160 * @param url
161 * @see eu.etaxonomy.cdm.model.agent.Contact#addUrl(java.lang.String)
162 */
163 public void addUrl(URI url) {
164 getNewOrExistingContact(true).addUrl(url);
165 }
166 /**
167 * @return
168 * @see eu.etaxonomy.cdm.model.agent.Contact#getAddresses()
169 */
170 @Transient
171 public Set<Address> getAddresses() {
172 return getNewOrExistingContact(false).getAddresses();
173 }
174 /**
175 * @return
176 * @see eu.etaxonomy.cdm.model.agent.Contact#getEmailAddresses()
177 */
178 @Transient
179 public List<String> getEmailAddresses() {
180 return getNewOrExistingContact(false).getEmailAddresses();
181 }
182 /**
183 * @return
184 * @see eu.etaxonomy.cdm.model.agent.Contact#getFaxNumbers()
185 */
186 @Transient
187 public List<String> getFaxNumbers() {
188 return getNewOrExistingContact(false).getFaxNumbers();
189 }
190 /**
191 * @return
192 * @see eu.etaxonomy.cdm.model.agent.Contact#getPhoneNumbers()
193 */
194 @Transient
195 public List<String> getPhoneNumbers() {
196 return getNewOrExistingContact(false).getPhoneNumbers();
197 }
198 /**
199 * @return
200 * @see eu.etaxonomy.cdm.model.agent.Contact#getUrls()
201 */
202 @Transient
203 public List<URI> getUrls() {
204 return getNewOrExistingContact(false).getUrls();
205 }
206 /**
207 * @param address
208 * @see eu.etaxonomy.cdm.model.agent.Contact#removeAddress(eu.etaxonomy.cdm.model.agent.Address)
209 */
210 public void removeAddress(Address address) {
211 getNewOrExistingContact(false).removeAddress(address);
212 }
213 /**
214 * @param emailAddress
215 * @see eu.etaxonomy.cdm.model.agent.Contact#removeEmailAddress(java.lang.String)
216 */
217 public void removeEmailAddress(String emailAddress) {
218 getNewOrExistingContact(false).removeEmailAddress(emailAddress);
219 }
220 /**
221 * @param faxNumber
222 * @see eu.etaxonomy.cdm.model.agent.Contact#removeFaxNumber(java.lang.String)
223 */
224 public void removeFaxNumber(String faxNumber) {
225 getNewOrExistingContact(false).removeFaxNumber(faxNumber);
226 }
227 /**
228 * @param phoneNumber
229 * @see eu.etaxonomy.cdm.model.agent.Contact#removePhoneNumber(java.lang.String)
230 */
231 public void removePhoneNumber(String phoneNumber) {
232 getNewOrExistingContact(false).removePhoneNumber(phoneNumber);
233 }
234 /**
235 * @param url
236 * @see eu.etaxonomy.cdm.model.agent.Contact#removeUrl(java.lang.String)
237 */
238 public void removeUrl(URI url) {
239 getNewOrExistingContact(false).removeUrl(url);
240 }
241
242
243
244
245 }