Project

General

Profile

Download (6.35 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.URISyntaxException;
12
import java.util.HashSet;
13
import java.util.Set;
14

    
15
import javax.persistence.Entity;
16
import javax.persistence.FetchType;
17
import javax.persistence.ManyToOne;
18
import javax.persistence.OneToMany;
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.XmlElementWrapper;
23
import javax.xml.bind.annotation.XmlIDREF;
24
import javax.xml.bind.annotation.XmlSchemaType;
25
import javax.xml.bind.annotation.XmlType;
26

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

    
34
import eu.etaxonomy.cdm.common.URI;
35
import eu.etaxonomy.cdm.hibernate.search.UriBridge;
36
import eu.etaxonomy.cdm.model.agent.Person;
37

    
38
/**
39
 * @author m.doering
40
 * @since 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 IIntextReferencable {
52

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

    
57
// ***************************** ATTRIBUTES **************************/
58

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

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

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

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

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

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

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

    
114

    
115
// *********** CONSTRUCTOR **************************************/
116

    
117
	protected Annotation(){
118
		super();
119
	}
120

    
121
	protected Annotation(String text, Language language) {
122
		super(text, language);
123
	}
124

    
125
//******************** GETTER /SETTER *************************/
126

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

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

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

    
148
	//*************** INTEXT REFERENCE **********************************************
149

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

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

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

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

    
180
// ***************************** TO STRING ***********************************
181

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

    
191
//****************** CLONE ************************************************/
192

    
193
	@Override
194
	public Annotation clone() throws CloneNotSupportedException{
195

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

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

    
214
		return result;
215
	}
216
}
(2-2/56)