Delete Warning, no substantial changes
[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 org.apache.log4j.Logger;
13 import org.hibernate.annotations.Cascade;
14 import org.hibernate.annotations.CascadeType;
15
16 import java.util.*;
17
18 import javax.persistence.*;
19 import javax.xml.bind.annotation.XmlAccessType;
20 import javax.xml.bind.annotation.XmlAccessorType;
21 import javax.xml.bind.annotation.XmlElement;
22 import javax.xml.bind.annotation.XmlElementWrapper;
23 import javax.xml.bind.annotation.XmlType;
24
25 /**
26 * Abstract superclass implementing human annotations and machine markers to be assigned to CDM objects.
27 * @author m.doering
28 * @version 1.0
29 * @created 08-Nov-2007 13:06:10
30 */
31 @XmlAccessorType(XmlAccessType.FIELD)
32 @XmlType(name = "AnnotatableEntity", propOrder = {
33 "markers",
34 "annotations"
35 })
36 @MappedSuperclass
37 public abstract class AnnotatableEntity<T extends AnnotatableEntity> extends VersionableEntity<T> {
38 private static final long serialVersionUID = 9151211842542443102L;
39 @SuppressWarnings("unused")
40 private static final Logger logger = Logger.getLogger(AnnotatableEntity.class);
41
42 @XmlElementWrapper(name = "Markers")
43 @XmlElement(name = "Marker")
44 protected Set<Marker> markers = getNewMarkerSet();
45
46 @XmlElementWrapper(name = "Annotations")
47 @XmlElement(name = "Annotation")
48 protected Set<Annotation> annotations = getNewAnnotationSet();
49
50 protected AnnotatableEntity() {
51 super();
52 }
53
54 //*************** MARKER **********************************************
55
56
57 @OneToMany(fetch=FetchType.LAZY)
58 @Cascade({CascadeType.SAVE_UPDATE})
59 public Set<Marker> getMarkers(){
60 return this.markers;
61 }
62 public void addMarker(Marker marker){
63 if (marker != null){
64 marker.setMarkedObj(this);
65 markers.add(marker);
66 }
67 }
68 public void removeMarker(Marker marker){
69 marker.setMarkedObj(null);
70 }
71 protected void setMarkers(Set<Marker> markers) {
72 this.markers = markers;
73 }
74
75 //*************** ANNOTATIONS **********************************************
76
77 @OneToMany(fetch=FetchType.LAZY) //(mappedBy="AnnotatedObj")
78 @Cascade({CascadeType.SAVE_UPDATE, CascadeType.DELETE})
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 protected void setAnnotations(Set<Annotation> annotations) {
95 this.annotations = annotations;
96 }
97
98 //********************** CLONE *****************************************/
99
100 /* (non-Javadoc)
101 * @see eu.etaxonomy.cdm.model.common.VersionableEntity#clone()
102 */
103 @Override
104 public Object clone() throws CloneNotSupportedException{
105 AnnotatableEntity result = (AnnotatableEntity)super.clone();
106
107 //Annotations
108 Set<Annotation> newAnnotations = getNewAnnotationSet();
109 for (Annotation annotation : this.annotations ){
110 Annotation newExtension = annotation.clone(this);
111 newAnnotations.add(newExtension);
112 }
113 result.setAnnotations(newAnnotations);
114
115
116 //Markers
117 Set<Marker> newMarkers = getNewMarkerSet();
118 for (Marker marker : this.markers ){
119 Marker newMarker = marker.clone(this);
120 newMarkers.add(newMarker);
121 }
122 result.setMarkers(newMarkers);
123
124 //no changes to: -
125 return result;
126 }
127
128 @Transient
129 private Set<Annotation> getNewAnnotationSet(){
130 return new HashSet<Annotation>();
131 }
132
133 @Transient
134 private Set<Marker> getNewMarkerSet(){
135 return new HashSet<Marker>();
136 }
137
138 }