(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
21 /**
22 * @author m.doering
23 * @version 1.0
24 * @created 08-Nov-2007 13:06:10
25 */
26 @Entity
27 public class Annotation extends LanguageString {
28 private static final Logger logger = Logger.getLogger(Annotation.class);
29
30
31 /**
32 * Factory method.
33 * @param text
34 * @param lang
35 * @return
36 */
37 public static Annotation NewInstance(String text, Language lang){
38 return new Annotation(text, lang);
39 }
40
41 /**
42 * Constructor
43 * @param text
44 * @param lang
45 */
46 protected Annotation(String text, Language lang) {
47 super(text, lang);
48 }
49
50 //Human annotation
51 private Person commentator;
52 private AnnotatableEntity annotatedObj;
53 // for external annotations/comments the URL of these can be set.
54 // should be useful to implement trackback, pingback or linkback:
55 // http://en.wikipedia.org/wiki/Linkback
56 private URL linkbackUrl;
57
58 @Transient
59 public AnnotatableEntity getAnnotatedObj() {
60 return annotatedObj;
61 }
62 protected void setAnnotatedObj(AnnotatableEntity newAnnotatedObj) {
63 // Hibernate bidirectional cascade hack:
64 // http://opensource.atlassian.com/projects/hibernate/browse/HHH-1054
65 if(this.annotatedObj == newAnnotatedObj) return;
66 if (annotatedObj != null) {
67 annotatedObj.annotations.remove(this);
68 }
69 if (newAnnotatedObj!= null) {
70 newAnnotatedObj.annotations.add(this);
71 }
72 this.annotatedObj = newAnnotatedObj;
73 }
74
75 @ManyToOne
76 @Cascade({CascadeType.SAVE_UPDATE})
77 public Person getCommentator(){
78 return this.commentator;
79 }
80 public void setCommentator(Person commentator){
81 this.commentator = commentator;
82 }
83
84 @Transient
85 public URL getLinkbackUrl() {
86 return linkbackUrl;
87 }
88 public void setLinkbackUrl(URL linkbackUrl) {
89 this.linkbackUrl = linkbackUrl;
90 }
91
92 /**
93 * private get/set methods for Hibernate that allows us to save the URL as strings
94 * @return
95 */
96 private String getLinkbackUrlStr() {
97 if (linkbackUrl == null){
98 return null;
99 }
100 return linkbackUrl.toString();
101 }
102 private void setLinkbackUrlStr(String linkbackUrlString) {
103 if (linkbackUrlString == null){
104 this.linkbackUrl = null;
105 }else{
106 try {
107 this.linkbackUrl = new URL(linkbackUrlString);
108 } catch (MalformedURLException e) { //can't be thrown as otherwise Hibernate throws PropertyAccessExceptioin
109 logger.warn("Runtime error occurred in setLinkbackUrlStr");
110 e.printStackTrace();
111 }
112 }
113 }
114 }