minor
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / common / AnnotatableEntity.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.util.HashSet;
13 import java.util.Set;
14
15 import javax.persistence.FetchType;
16 import javax.persistence.MappedSuperclass;
17 import javax.persistence.OneToMany;
18 import javax.xml.bind.annotation.XmlAccessType;
19 import javax.xml.bind.annotation.XmlAccessorType;
20 import javax.xml.bind.annotation.XmlElement;
21 import javax.xml.bind.annotation.XmlElementWrapper;
22 import javax.xml.bind.annotation.XmlType;
23
24 import org.apache.log4j.Logger;
25 import org.hibernate.annotations.Cascade;
26 import org.hibernate.annotations.CascadeType;
27
28 /**
29 * Abstract superclass implementing human annotations and machine markers to be assigned to CDM objects.
30 * @author m.doering
31 * @version 1.0
32 * @created 08-Nov-2007 13:06:10
33 */
34 @XmlAccessorType(XmlAccessType.FIELD)
35 @XmlType(name = "AnnotatableEntity", propOrder = {
36 "markers",
37 "annotations"
38 })
39 @MappedSuperclass
40 public abstract class AnnotatableEntity extends VersionableEntity {
41 private static final long serialVersionUID = 9151211842542443102L;
42 @SuppressWarnings("unused")
43 private static final Logger logger = Logger.getLogger(AnnotatableEntity.class);
44
45 @XmlElementWrapper(name = "Markers")
46 @XmlElement(name = "Marker")
47 @OneToMany(fetch=FetchType.LAZY)
48 @Cascade({CascadeType.SAVE_UPDATE})
49 protected Set<Marker> markers = new HashSet<Marker>();
50
51 @XmlElementWrapper(name = "Annotations")
52 @XmlElement(name = "Annotation")
53 @OneToMany(fetch=FetchType.LAZY)
54 @Cascade({CascadeType.SAVE_UPDATE})
55 protected Set<Annotation> annotations = new HashSet<Annotation>();
56
57 protected AnnotatableEntity() {
58 super();
59 }
60
61 //*************** MARKER **********************************************
62
63
64 public Set<Marker> getMarkers(){
65 return this.markers;
66 }
67 public void addMarker(Marker marker){
68 if (marker != null){
69 marker.setMarkedObj(this);
70 markers.add(marker);
71 }
72 }
73 public void removeMarker(Marker marker){
74 marker.setMarkedObj(null);
75 }
76
77 //*************** ANNOTATIONS **********************************************
78
79 public Set<Annotation> getAnnotations(){
80 return this.annotations;
81 }
82 public void addAnnotation(Annotation annotation){
83 if (annotation != null){
84 annotation.setAnnotatedObj(this);
85 annotations.add(annotation);
86 }
87 }
88
89 public void removeAnnotation(Annotation annotation){
90 this.annotations.remove(annotation);
91 annotation.setAnnotatedObj(null);
92 }
93
94 //********************** CLONE *****************************************/
95
96 /* (non-Javadoc)
97 * @see eu.etaxonomy.cdm.model.common.VersionableEntity#clone()
98 */
99 @Override
100 public Object clone() throws CloneNotSupportedException{
101 AnnotatableEntity result = (AnnotatableEntity)super.clone();
102
103 //Annotations
104 result.annotations = new HashSet<Annotation>();
105 for (Annotation annotation : this.annotations ){
106 Annotation newAnnotation = (Annotation)annotation.clone();
107 result.addAnnotation(newAnnotation);
108 }
109
110 //Markers
111 result.markers = new HashSet<Marker>();
112 for (Marker marker : this.markers ){
113 Marker newMarker = (Marker)marker.clone();
114 result.addMarker(newMarker);
115 }
116
117 //no changes to: -
118 return result;
119 }
120 }