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