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.common;
|
11
|
|
12
|
import java.net.URI;
|
13
|
import java.net.URISyntaxException;
|
14
|
import java.util.HashSet;
|
15
|
import java.util.Set;
|
16
|
|
17
|
import javax.persistence.Column;
|
18
|
import javax.persistence.Entity;
|
19
|
import javax.persistence.FetchType;
|
20
|
import javax.persistence.JoinColumn;
|
21
|
import javax.persistence.ManyToOne;
|
22
|
import javax.persistence.OneToMany;
|
23
|
import javax.xml.bind.annotation.XmlAccessType;
|
24
|
import javax.xml.bind.annotation.XmlAccessorType;
|
25
|
import javax.xml.bind.annotation.XmlElement;
|
26
|
import javax.xml.bind.annotation.XmlElementWrapper;
|
27
|
import javax.xml.bind.annotation.XmlIDREF;
|
28
|
import javax.xml.bind.annotation.XmlSchemaType;
|
29
|
import javax.xml.bind.annotation.XmlType;
|
30
|
|
31
|
import org.apache.commons.lang.StringUtils;
|
32
|
import org.apache.log4j.Logger;
|
33
|
import org.hibernate.annotations.Any;
|
34
|
import org.hibernate.annotations.Cascade;
|
35
|
import org.hibernate.annotations.CascadeType;
|
36
|
import org.hibernate.annotations.Type;
|
37
|
import org.hibernate.envers.Audited;
|
38
|
import org.hibernate.envers.NotAudited;
|
39
|
|
40
|
import eu.etaxonomy.cdm.model.agent.Person;
|
41
|
|
42
|
/**
|
43
|
* @author m.doering
|
44
|
* @version 1.0
|
45
|
* @created 08-Nov-2007 13:06:10
|
46
|
*/
|
47
|
@XmlAccessorType(XmlAccessType.FIELD)
|
48
|
@XmlType(name = "Annotation", propOrder = {
|
49
|
"commentator",
|
50
|
"annotatedObj",
|
51
|
"annotationType",
|
52
|
"linkbackUri",
|
53
|
"intextReferences"
|
54
|
})
|
55
|
@Entity
|
56
|
@Audited
|
57
|
public class Annotation extends LanguageStringBase implements Cloneable, IIntextReferencable {
|
58
|
private static final long serialVersionUID = -4484677078599520233L;
|
59
|
@SuppressWarnings("unused")
|
60
|
private static final Logger logger = Logger.getLogger(Annotation.class);
|
61
|
|
62
|
//TODO do we need to add it to JAXB? #4706
|
63
|
@XmlElementWrapper(name = "IntextReferences", nillable = true)
|
64
|
@XmlElement(name = "IntextReference")
|
65
|
@OneToMany(mappedBy="languageString", fetch=FetchType.LAZY, orphanRemoval=true)
|
66
|
@Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.DELETE})
|
67
|
// @Merge(MergeMode.ADD_CLONE)
|
68
|
private Set<IntextReference> intextReferences = new HashSet<IntextReference>();
|
69
|
|
70
|
/**
|
71
|
* Factory method.
|
72
|
* @param text
|
73
|
* @param lang
|
74
|
* @return
|
75
|
*/
|
76
|
public static Annotation NewInstance(String text, Language lang){
|
77
|
return new Annotation(text, lang);
|
78
|
}
|
79
|
|
80
|
public static Annotation NewInstance(String text, AnnotationType annotationType, Language lang){
|
81
|
Annotation annotation = new Annotation(text, lang);
|
82
|
annotation.setAnnotationType(annotationType);
|
83
|
return annotation;
|
84
|
}
|
85
|
|
86
|
/**
|
87
|
* Factory method. Using default language.
|
88
|
* @param text
|
89
|
* @return
|
90
|
*/
|
91
|
public static Annotation NewDefaultLanguageInstance(String text){
|
92
|
return new Annotation(text, Language.DEFAULT());
|
93
|
}
|
94
|
|
95
|
|
96
|
//Human annotation
|
97
|
@XmlElement(name = "Commentator")
|
98
|
@XmlIDREF
|
99
|
@XmlSchemaType(name = "IDREF")
|
100
|
@ManyToOne(fetch = FetchType.LAZY)
|
101
|
@Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE})
|
102
|
private Person commentator;
|
103
|
|
104
|
@XmlElement(name = "AnnotatedObject")
|
105
|
@XmlIDREF
|
106
|
@XmlSchemaType(name = "IDREF")
|
107
|
@Any(metaDef = "CdmBase",
|
108
|
metaColumn=@Column(name = "annotatedObj_type"),
|
109
|
fetch = FetchType.EAGER,
|
110
|
optional = false)
|
111
|
@JoinColumn(name = "annotatedObj_id")
|
112
|
@NotAudited
|
113
|
private AnnotatableEntity annotatedObj;
|
114
|
|
115
|
@XmlElement(name = "AnnotationType")
|
116
|
@XmlIDREF
|
117
|
@XmlSchemaType(name = "IDREF")
|
118
|
@ManyToOne(fetch = FetchType.LAZY)
|
119
|
private AnnotationType annotationType;
|
120
|
|
121
|
// for external annotations/comments the URI of these can be set.
|
122
|
// should be useful to implement trackback, pingback or linkback:
|
123
|
// http://en.wikipedia.org/wiki/Linkback
|
124
|
@XmlElement(name = "LinkbackUri")
|
125
|
@Type(type="uriUserType")
|
126
|
private URI linkbackUri;
|
127
|
|
128
|
|
129
|
// *********** CONSTRUCTOR **************************************/
|
130
|
|
131
|
protected Annotation(){
|
132
|
super();
|
133
|
}
|
134
|
|
135
|
/**
|
136
|
* Constructor
|
137
|
* @param text
|
138
|
* @param lang
|
139
|
*/
|
140
|
protected Annotation(String text, Language language) {
|
141
|
super(text, language);
|
142
|
}
|
143
|
|
144
|
//******************** GETTER /SETTER *************************/
|
145
|
|
146
|
|
147
|
/**
|
148
|
* Currently envers does not support @Any
|
149
|
* @return
|
150
|
*/
|
151
|
public AnnotatableEntity getAnnotatedObj() {
|
152
|
return annotatedObj;
|
153
|
}
|
154
|
|
155
|
//TODO make not public, but TaxonTaoHibernateImpl.delete has to be changed then
|
156
|
/**
|
157
|
*
|
158
|
* @param newAnnotatedObj
|
159
|
* @deprecated should not be used, is only public for internal reasons
|
160
|
*/
|
161
|
@Deprecated
|
162
|
public void setAnnotatedObj(AnnotatableEntity newAnnotatedObj) {
|
163
|
this.annotatedObj = newAnnotatedObj;
|
164
|
}
|
165
|
|
166
|
public AnnotationType getAnnotationType() {
|
167
|
return annotationType;
|
168
|
}
|
169
|
|
170
|
public void setAnnotationType(AnnotationType annotationType) {
|
171
|
this.annotationType = annotationType;
|
172
|
}
|
173
|
|
174
|
public Person getCommentator(){
|
175
|
return this.commentator;
|
176
|
}
|
177
|
public void setCommentator(Person commentator){
|
178
|
this.commentator = commentator;
|
179
|
}
|
180
|
|
181
|
|
182
|
public URI getLinkbackUri() {
|
183
|
return linkbackUri;
|
184
|
}
|
185
|
public void setLinkbackUri(URI linkbackUri) {
|
186
|
this.linkbackUri = linkbackUri;
|
187
|
}
|
188
|
|
189
|
|
190
|
//*************** INTEXT REFERENCE **********************************************
|
191
|
|
192
|
@Override
|
193
|
public Set<IntextReference> getIntextReferences(){
|
194
|
return this.intextReferences;
|
195
|
}
|
196
|
@Override
|
197
|
public void addIntextReference(IntextReference intextReference){
|
198
|
if (intextReference != null){
|
199
|
intextReference.setAnnotation(this);
|
200
|
getIntextReferences().add(intextReference);
|
201
|
}
|
202
|
}
|
203
|
|
204
|
@Override
|
205
|
public void removeIntextReference(IntextReference intextReference){
|
206
|
if(getIntextReferences().contains(intextReference)) {
|
207
|
getIntextReferences().remove(intextReference);
|
208
|
intextReference.setAnnotation((Annotation)null);
|
209
|
}
|
210
|
}
|
211
|
|
212
|
|
213
|
// ***************************** TO STRING ***********************************
|
214
|
|
215
|
|
216
|
/* (non-Javadoc)
|
217
|
* @see eu.etaxonomy.cdm.model.common.CdmBase#toString()
|
218
|
*/
|
219
|
@Override
|
220
|
public String toString() {
|
221
|
if (StringUtils.isNotBlank(this.text)){
|
222
|
return "Ann.: " + this.text;
|
223
|
}else{
|
224
|
return super.toString();
|
225
|
}
|
226
|
}
|
227
|
|
228
|
|
229
|
|
230
|
//****************** CLONE ************************************************/
|
231
|
|
232
|
@Override
|
233
|
public Object clone() throws CloneNotSupportedException{
|
234
|
Annotation result = (Annotation)super.clone();
|
235
|
//TODO do we really need this, it should not change anything
|
236
|
result.setCommentator(this.getCommentator());
|
237
|
//TODO do we really need this, it should not change anything
|
238
|
result.setAnnotationType(this.getAnnotationType());
|
239
|
|
240
|
try {
|
241
|
result.setLinkbackUri(this.linkbackUri == null ? null : new URI(this.linkbackUri.toString()));
|
242
|
} catch (URISyntaxException e) {
|
243
|
//do nothing
|
244
|
}
|
245
|
//IntextReferences
|
246
|
result.intextReferences = new HashSet<IntextReference>();
|
247
|
for (IntextReference intextReference : getIntextReferences()){
|
248
|
IntextReference newIntextReference = (IntextReference)intextReference.clone();
|
249
|
result.addIntextReference(newIntextReference);
|
250
|
}
|
251
|
|
252
|
return result;
|
253
|
}
|
254
|
|
255
|
|
256
|
/**
|
257
|
* Clones this annotation and sets the clone's annotated object to 'annotatedObject'
|
258
|
* @see java.lang.Object#clone()
|
259
|
*/
|
260
|
public Annotation clone(AnnotatableEntity annotatedObject) throws CloneNotSupportedException{
|
261
|
Annotation result = (Annotation)clone();
|
262
|
result.setAnnotatedObj(annotatedObject);
|
263
|
|
264
|
return result;
|
265
|
}
|
266
|
}
|