(no commit message)
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / common / Annotation.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.common;
11
12 import java.net.MalformedURLException;
13 import java.net.URL;
14
15 import javax.persistence.Column;
16 import javax.persistence.Entity;
17 import javax.persistence.FetchType;
18 import javax.persistence.JoinColumn;
19 import javax.persistence.ManyToOne;
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.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.Any;
29 import org.hibernate.annotations.Cascade;
30 import org.hibernate.annotations.CascadeType;
31 import org.hibernate.envers.Audited;
32 import org.hibernate.envers.NotAudited;
33
34 import eu.etaxonomy.cdm.model.agent.Person;
35
36 /**
37 * @author m.doering
38 * @version 1.0
39 * @created 08-Nov-2007 13:06:10
40 */
41 @XmlAccessorType(XmlAccessType.FIELD)
42 @XmlType(name = "Annotation", propOrder = {
43 "commentator",
44 "annotatedObj",
45 "annotationType",
46 "linkbackUrl"
47 })
48 @Entity
49 @Audited
50 public class Annotation extends LanguageStringBase implements Cloneable {
51 private static final long serialVersionUID = -4484677078599520233L;
52 private static final Logger logger = Logger.getLogger(Annotation.class);
53
54
55 /**
56 * Factory method.
57 * @param text
58 * @param lang
59 * @return
60 */
61 public static Annotation NewInstance(String text, Language lang){
62 return new Annotation(text, lang);
63 }
64
65 public static Annotation NewInstance(String text, AnnotationType annotationType, Language lang){
66 Annotation annotation = new Annotation(text, lang);
67 annotation.setAnnotationType(annotationType);
68 return annotation;
69 }
70
71 /**
72 * Factory method. Using default language.
73 * @param text
74 * @return
75 */
76 public static Annotation NewDefaultLanguageInstance(String text){
77 return new Annotation(text, Language.DEFAULT());
78 }
79
80 protected Annotation(){
81 super();
82 }
83
84 /**
85 * Constructor
86 * @param text
87 * @param lang
88 */
89 protected Annotation(String text, Language language) {
90 super(text, language);
91 }
92
93
94 //Human annotation
95 @XmlElement(name = "Commentator")
96 @XmlIDREF
97 @XmlSchemaType(name = "IDREF")
98 @ManyToOne(fetch = FetchType.LAZY)
99 @Cascade(CascadeType.SAVE_UPDATE)
100 private Person commentator;
101
102 @XmlElement(name = "AnnotatedObject")
103 @XmlIDREF
104 @XmlSchemaType(name = "IDREF")
105 @Any(metaDef = "CdmBase",
106 metaColumn=@Column(name = "annotatedObj_type"),
107 fetch = FetchType.EAGER,
108 optional = false)
109 @JoinColumn(name = "annotatedObj_id")
110 @NotAudited
111 private AnnotatableEntity annotatedObj;
112
113 @XmlElement(name = "AnnotationType")
114 @XmlIDREF
115 @XmlSchemaType(name = "IDREF")
116 @ManyToOne(fetch = FetchType.LAZY)
117 private AnnotationType annotationType;
118
119 // for external annotations/comments the URL of these can be set.
120 // should be useful to implement trackback, pingback or linkback:
121 // http://en.wikipedia.org/wiki/Linkback
122 @XmlElement(name = "LinkbackURL")
123 private URL linkbackUrl;
124
125 /**
126 * Currently envers does not support @Any
127 * @return
128 */
129 public AnnotatableEntity getAnnotatedObj() {
130 return annotatedObj;
131 }
132
133 //TODO make not public, but TaxonTaoHibernateImpl.delete has to be changed then
134 /**
135 *
136 * @param newAnnotatedObj
137 * @deprecated should not be used, is only public for internal reasons
138 */
139 @Deprecated
140 public void setAnnotatedObj(AnnotatableEntity newAnnotatedObj) {
141 this.annotatedObj = newAnnotatedObj;
142 }
143
144 public AnnotationType getAnnotationType() {
145 return annotationType;
146 }
147
148 public void setAnnotationType(AnnotationType annotationType) {
149 this.annotationType = annotationType;
150 }
151
152 public Person getCommentator(){
153 return this.commentator;
154 }
155 public void setCommentator(Person commentator){
156 this.commentator = commentator;
157 }
158
159
160 public URL getLinkbackUrl() {
161 return linkbackUrl;
162 }
163 public void setLinkbackUrl(URL linkbackUrl) {
164 this.linkbackUrl = linkbackUrl;
165 }
166
167 /**
168 * private get/set methods for Hibernate that allows us to save the URL as strings
169 * @return
170 */
171 private String getLinkbackUrlStr() {
172 if (linkbackUrl == null){
173 return null;
174 }
175 return linkbackUrl.toString();
176 }
177 private void setLinkbackUrlStr(String linkbackUrlString) {
178 if (linkbackUrlString == null){
179 this.linkbackUrl = null;
180 }else{
181 try {
182 this.linkbackUrl = new URL(linkbackUrlString);
183 } catch (MalformedURLException e) { //can't be thrown as otherwise Hibernate throws PropertyAccessExceptioin
184 logger.warn("Runtime error occurred in setLinkbackUrlStr");
185 e.printStackTrace();
186 }
187 }
188 }
189
190
191 //****************** CLONE ************************************************/
192
193 /* (non-Javadoc)
194 * @see java.lang.Object#clone()
195 */
196 @Override
197 public Object clone() throws CloneNotSupportedException{
198 Annotation result = (Annotation)super.clone();
199 result.setCommentator(this.getCommentator());
200 result.setAnnotationType(this.getAnnotationType());
201 result.setLinkbackUrl(this.linkbackUrl);
202 return result;
203 }
204
205 /**
206 * Clones this annotation and sets the clone's annotated object to 'annotatedObject'
207 * @see java.lang.Object#clone()
208 */
209 public Annotation clone(AnnotatableEntity annotatedObject) throws CloneNotSupportedException{
210 Annotation result = (Annotation)clone();
211 result.setAnnotatedObj(annotatedObject);
212 return result;
213 }
214 }