Project

General

Profile

Download (5.9 KB) Statistics
| Branch: | Tag: | Revision:
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.Entity;
18
import javax.persistence.FetchType;
19
import javax.persistence.ManyToOne;
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.XmlIDREF;
26
import javax.xml.bind.annotation.XmlSchemaType;
27
import javax.xml.bind.annotation.XmlType;
28

    
29
import org.apache.commons.lang.StringUtils;
30
import org.apache.log4j.Logger;
31
import org.hibernate.annotations.Cascade;
32
import org.hibernate.annotations.CascadeType;
33
import org.hibernate.annotations.Type;
34
import org.hibernate.envers.Audited;
35

    
36
import eu.etaxonomy.cdm.model.agent.Person;
37

    
38
/**
39
 * @author m.doering
40
 * @created 08-Nov-2007 13:06:10
41
 */
42
@XmlAccessorType(XmlAccessType.FIELD)
43
@XmlType(name = "Annotation", propOrder = {
44
    "commentator",
45
    "annotationType",
46
    "linkbackUri",
47
    "intextReferences"
48
})
49
@Entity
50
@Audited
51
public class Annotation extends LanguageStringBase implements Cloneable, IIntextReferencable {
52
	private static final long serialVersionUID = -4484677078599520233L;
53
	@SuppressWarnings("unused")
54
	private static final Logger logger = Logger.getLogger(Annotation.class);
55

    
56
	/**
57
	 * Factory method.
58
	 * @param text
59
	 * @param lang
60
	 * @return
61
	 */
62
	public static Annotation NewInstance(String text, Language lang){
63
		return new Annotation(text, lang);
64
	}
65

    
66
	public static Annotation NewInstance(String text, AnnotationType annotationType, Language lang){
67
		Annotation annotation = new Annotation(text, lang);
68
		annotation.setAnnotationType(annotationType);
69
		return annotation;
70
	}
71

    
72
	/**
73
	 * Factory method. Using default language.
74
	 * @param text
75
	 * @return
76
	 */
77
	public static Annotation NewDefaultLanguageInstance(String text){
78
		return new Annotation(text, Language.DEFAULT());
79
	}
80

    
81

    
82
    //TODO do we need to add it to JAXB? #4706
83
    @XmlElementWrapper(name = "IntextReferences", nillable = true)
84
    @XmlElement(name = "IntextReference")
85
    @OneToMany(mappedBy="languageString", fetch=FetchType.LAZY, orphanRemoval=true)
86
    @Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.DELETE})
87
//  @Merge(MergeMode.ADD_CLONE)
88
    private Set<IntextReference> intextReferences = new HashSet<IntextReference>();
89

    
90
	//Human annotation
91
	@XmlElement(name = "Commentator")
92
    @XmlIDREF
93
    @XmlSchemaType(name = "IDREF")
94
    @ManyToOne(fetch = FetchType.LAZY)
95
    @Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE})
96
	private Person commentator;
97

    
98
    @XmlElement(name = "AnnotationType")
99
    @XmlIDREF
100
    @XmlSchemaType(name = "IDREF")
101
    @ManyToOne(fetch = FetchType.LAZY)
102
	private AnnotationType annotationType;
103

    
104
	// for external annotations/comments the URI of these can be set.
105
	// should be useful to implement trackback, pingback or linkback:
106
	// http://en.wikipedia.org/wiki/Linkback
107
	@XmlElement(name = "LinkbackUri")
108
	@Type(type="uriUserType")
109
	private URI linkbackUri;
110

    
111

    
112
// *********** CONSTRUCTOR **************************************/
113

    
114
	protected Annotation(){
115
		super();
116
	}
117

    
118
	/**
119
	 * Constructor
120
	 * @param text
121
	 * @param lang
122
	 */
123
	protected Annotation(String text, Language language) {
124
		super(text, language);
125
	}
126

    
127
//******************** GETTER /SETTER *************************/
128

    
129

    
130
	public AnnotationType getAnnotationType() {
131
		return annotationType;
132
	}
133

    
134
	public void setAnnotationType(AnnotationType annotationType) {
135
		this.annotationType = annotationType;
136
	}
137

    
138
	public Person getCommentator(){
139
		return this.commentator;
140
	}
141
	public void setCommentator(Person commentator){
142
		this.commentator = commentator;
143
	}
144

    
145

    
146
	public URI getLinkbackUri() {
147
		return linkbackUri;
148
	}
149
	public void setLinkbackUri(URI linkbackUri) {
150
		this.linkbackUri = linkbackUri;
151
	}
152

    
153

    
154
	//*************** INTEXT REFERENCE **********************************************
155

    
156
	@Override
157
    public Set<IntextReference> getIntextReferences(){
158
		return this.intextReferences;
159
	}
160
	@Override
161
    public void addIntextReference(IntextReference intextReference){
162
		if (intextReference != null){
163
			intextReference.setAnnotation(this);
164
			getIntextReferences().add(intextReference);
165
		}
166
	}
167

    
168
	@Override
169
    public void removeIntextReference(IntextReference intextReference){
170
		if(getIntextReferences().contains(intextReference)) {
171
			getIntextReferences().remove(intextReference);
172
			intextReference.setAnnotation((Annotation)null);
173
		}
174
	}
175

    
176

    
177
// ***************************** TO STRING ***********************************
178

    
179

    
180
	/* (non-Javadoc)
181
	 * @see eu.etaxonomy.cdm.model.common.CdmBase#toString()
182
	 */
183
	@Override
184
	public String toString() {
185
		if (StringUtils.isNotBlank(this.text)){
186
			return "Ann.: " + this.text;
187
		}else{
188
			return super.toString();
189
		}
190
	}
191

    
192

    
193

    
194
//****************** CLONE ************************************************/
195

    
196
	@Override
197
	public Object clone() throws CloneNotSupportedException{
198
		Annotation result = (Annotation)super.clone();
199
		//TODO do we really need this, it should not change anything
200
		result.setCommentator(this.getCommentator());
201
		//TODO do we really need this, it should not change anything
202
		result.setAnnotationType(this.getAnnotationType());
203

    
204
		try {
205
			result.setLinkbackUri(this.linkbackUri == null ? null : new URI(this.linkbackUri.toString()));
206
		} catch (URISyntaxException e) {
207
			//do nothing
208
		}
209
		//IntextReferences
210
		result.intextReferences = new HashSet<IntextReference>();
211
		for (IntextReference intextReference : getIntextReferences()){
212
			IntextReference newIntextReference = (IntextReference)intextReference.clone();
213
			result.addIntextReference(newIntextReference);
214
		}
215

    
216
		return result;
217
	}
218

    
219
}
(2-2/73)