Project

General

Profile

Download (6.24 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
package eu.etaxonomy.cdm.model.common;
10

    
11
import java.net.URI;
12
import java.net.URISyntaxException;
13
import java.util.HashSet;
14
import java.util.Set;
15

    
16
import javax.persistence.Entity;
17
import javax.persistence.FetchType;
18
import javax.persistence.ManyToOne;
19
import javax.persistence.OneToMany;
20
import javax.xml.bind.annotation.XmlAccessType;
21
import javax.xml.bind.annotation.XmlAccessorType;
22
import javax.xml.bind.annotation.XmlElement;
23
import javax.xml.bind.annotation.XmlElementWrapper;
24
import javax.xml.bind.annotation.XmlIDREF;
25
import javax.xml.bind.annotation.XmlSchemaType;
26
import javax.xml.bind.annotation.XmlType;
27

    
28
import org.apache.log4j.Logger;
29
import org.hibernate.annotations.Cascade;
30
import org.hibernate.annotations.CascadeType;
31
import org.hibernate.annotations.Type;
32
import org.hibernate.envers.Audited;
33

    
34
import eu.etaxonomy.cdm.model.agent.Person;
35

    
36
/**
37
 * @author m.doering
38
 * @since 08-Nov-2007 13:06:10
39
 */
40
@XmlAccessorType(XmlAccessType.FIELD)
41
@XmlType(name = "Annotation", propOrder = {
42
    "commentator",
43
    "annotationType",
44
    "linkbackUri",
45
    "intextReferences"
46
})
47
@Entity
48
@Audited
49
public class Annotation extends LanguageStringBase implements IIntextReferencable {
50

    
51
	private static final long serialVersionUID = -4484677078599520233L;
52
	@SuppressWarnings("unused")
53
	private static final Logger logger = Logger.getLogger(Annotation.class);
54

    
55
// ***************************** ATTRIBUTES **************************/
56

    
57
    //TODO do we need to add it to JAXB? #4706
58
    @XmlElementWrapper(name = "IntextReferences", nillable = true)
59
    @XmlElement(name = "IntextReference")
60
    @OneToMany(mappedBy="languageString", fetch=FetchType.LAZY, orphanRemoval=true)
61
    @Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.DELETE})
62
//  @Merge(MergeMode.ADD_CLONE)
63
    private Set<IntextReference> intextReferences = new HashSet<>();
64

    
65
    //Human annotation
66
    @XmlElement(name = "Commentator")
67
    @XmlIDREF
68
    @XmlSchemaType(name = "IDREF")
69
    @ManyToOne(fetch = FetchType.LAZY)
70
    @Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE})
71
    private Person commentator;
72

    
73
    @XmlElement(name = "AnnotationType")
74
    @XmlIDREF
75
    @XmlSchemaType(name = "IDREF")
76
    @ManyToOne(fetch = FetchType.LAZY)
77
    private AnnotationType annotationType;
78

    
79
    // for external annotations/comments the URI of these can be set.
80
    // should be useful to implement trackback, pingback or linkback:
81
    // http://en.wikipedia.org/wiki/Linkback
82
    @XmlElement(name = "LinkbackUri")
83
    @FieldBridge(impl = UriBridge.class)
84
    @Type(type="uriUserType")
85
    private URI linkbackUri;
86

    
87
	/**
88
	 * Factory method.
89
	 * @param text
90
	 * @param lang
91
	 * @return
92
	 */
93
	public static Annotation NewInstance(String text, Language lang){
94
		return new Annotation(text, lang);
95
	}
96

    
97
	public static Annotation NewInstance(String text, AnnotationType annotationType, Language lang){
98
		Annotation annotation = new Annotation(text, lang);
99
		annotation.setAnnotationType(annotationType);
100
		return annotation;
101
	}
102

    
103
	/**
104
	 * Factory method. Using default language.
105
	 * @param text
106
	 * @return
107
	 */
108
	public static Annotation NewDefaultLanguageInstance(String text){
109
		return new Annotation(text, Language.DEFAULT());
110
	}
111

    
112

    
113
// *********** CONSTRUCTOR **************************************/
114

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

    
119
	protected Annotation(String text, Language language) {
120
		super(text, language);
121
	}
122

    
123
//******************** GETTER /SETTER *************************/
124

    
125
	public AnnotationType getAnnotationType() {
126
		return annotationType;
127
	}
128
	public void setAnnotationType(AnnotationType annotationType) {
129
		this.annotationType = annotationType;
130
	}
131

    
132
	public Person getCommentator(){
133
		return this.commentator;
134
	}
135
	public void setCommentator(Person commentator){
136
		this.commentator = commentator;
137
	}
138

    
139
	public URI getLinkbackUri() {
140
		return linkbackUri;
141
	}
142
	public void setLinkbackUri(URI linkbackUri) {
143
		this.linkbackUri = linkbackUri;
144
	}
145

    
146
	//*************** INTEXT REFERENCE **********************************************
147

    
148
	@Override
149
    public Set<IntextReference> getIntextReferences(){
150
		return this.intextReferences;
151
	}
152

    
153
	@Override
154
    public IntextReference addIntextReference(IIntextReferenceTarget target, String start, String inner, String end){
155
        return IntextReferenceHelper.addIntextReference(target, this, start, inner, end);
156
    }
157
    @Override
158
    public IntextReference addIntextReference(IIntextReferenceTarget target, int start, int end){
159
        return IntextReferenceHelper.addIntextReference(target, this, start, end);
160
    }
161

    
162
	@Override
163
    public void addIntextReference(IntextReference intextReference){
164
		if (intextReference != null){
165
			intextReference.setReferencedEntity(this);
166
			getIntextReferences().add(intextReference);
167
		}
168
	}
169

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

    
178
// ***************************** TO STRING ***********************************
179

    
180
	@Override
181
	public String toString() {
182
		if (isNotBlank(this.text)){
183
			return "Ann.: " + this.text;
184
		}else{
185
			return super.toString();
186
		}
187
	}
188

    
189
//****************** CLONE ************************************************/
190

    
191
	@Override
192
	public Annotation clone() throws CloneNotSupportedException{
193

    
194
	    Annotation result = (Annotation)super.clone();
195
		//TODO do we really need this, it should not change anything
196
		result.setCommentator(this.getCommentator());
197
		//TODO do we really need this, it should not change anything
198
		result.setAnnotationType(this.getAnnotationType());
199

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

    
212
		return result;
213
	}
214
}
(2-2/56)