(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 eu.etaxonomy.cdm.model.agent.Person;
13 import org.apache.log4j.Logger;
14 import org.hibernate.annotations.Cascade;
15 import org.hibernate.annotations.CascadeType;
16
17 import java.net.MalformedURLException;
18 import java.net.URL;
19 import javax.persistence.*;
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.XmlType;
24
25 /**
26 * @author m.doering
27 * @version 1.0
28 * @created 08-Nov-2007 13:06:10
29 */
30 @XmlAccessorType(XmlAccessType.FIELD)
31 @XmlType(name = "Annotation", propOrder = {
32 "",
33 ""
34 })
35 @Entity
36 public class Annotation extends LanguageStringBase implements Cloneable {
37 private static final Logger logger = Logger.getLogger(Annotation.class);
38
39
40 /**
41 * Factory method.
42 * @param text
43 * @param lang
44 * @return
45 */
46 public static Annotation NewInstance(String text, Language lang){
47 return new Annotation(text, lang);
48 }
49
50 /**
51 * Factory method. Using default language.
52 * @param text
53 * @return
54 */
55 public static Annotation NewDefaultLanguageInstance(String text){
56 return new Annotation(text, Language.DEFAULT());
57 }
58
59 private Annotation(){
60 super();
61 }
62
63 /**
64 * Constructor
65 * @param text
66 * @param lang
67 */
68 protected Annotation(String text, Language language) {
69 super(text, language);
70 }
71
72
73 //Human annotation
74 @XmlElement(name = "Commentator")
75 //@XmlIDREF
76 //@XmlSchemaType(name = "IDREF")
77 private Person commentator;
78
79 @XmlElement(name = "AnnotatedObject")
80 //@XmlIDREF
81 //@XmlSchemaType(name = "IDREF")
82 private AnnotatableEntity annotatedObj;
83
84 // for external annotations/comments the URL of these can be set.
85 // should be useful to implement trackback, pingback or linkback:
86 // http://en.wikipedia.org/wiki/Linkback
87 @XmlElement(name = "LinkbackURL")
88 private URL linkbackUrl;
89
90 @Transient
91 public AnnotatableEntity getAnnotatedObj() {
92 return annotatedObj;
93 }
94 protected void setAnnotatedObj(AnnotatableEntity newAnnotatedObj) {
95 this.annotatedObj = newAnnotatedObj;
96 }
97
98 @ManyToOne
99 @Cascade({CascadeType.SAVE_UPDATE})
100 public Person getCommentator(){
101 return this.commentator;
102 }
103 public void setCommentator(Person commentator){
104 this.commentator = commentator;
105 }
106
107 @Transient
108 public URL getLinkbackUrl() {
109 return linkbackUrl;
110 }
111 public void setLinkbackUrl(URL linkbackUrl) {
112 this.linkbackUrl = linkbackUrl;
113 }
114
115 /**
116 * private get/set methods for Hibernate that allows us to save the URL as strings
117 * @return
118 */
119 private String getLinkbackUrlStr() {
120 if (linkbackUrl == null){
121 return null;
122 }
123 return linkbackUrl.toString();
124 }
125 private void setLinkbackUrlStr(String linkbackUrlString) {
126 if (linkbackUrlString == null){
127 this.linkbackUrl = null;
128 }else{
129 try {
130 this.linkbackUrl = new URL(linkbackUrlString);
131 } catch (MalformedURLException e) { //can't be thrown as otherwise Hibernate throws PropertyAccessExceptioin
132 logger.warn("Runtime error occurred in setLinkbackUrlStr");
133 e.printStackTrace();
134 }
135 }
136 }
137
138
139 //****************** CLONE ************************************************/
140
141 /* (non-Javadoc)
142 * @see java.lang.Object#clone()
143 */
144 public Object clone() throws CloneNotSupportedException{
145 Annotation result = (Annotation)super.clone();
146 //no changes to: type, flag
147 return result;
148 }
149
150 /**
151 * Clones this annotation and sets the clone's annotated object to 'annotatedObject'
152 * @see java.lang.Object#clone()
153 */
154 public Annotation clone(AnnotatableEntity annotatedObject) throws CloneNotSupportedException{
155 Annotation result = (Annotation)clone();
156 result.setAnnotatedObj(annotatedObject);
157 return result;
158 }
159 }