Fixes an issue where update of title caches did not work for cloned instances.
[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 import eu.etaxonomy.cdm.strategy.merge.Merge;
29 import eu.etaxonomy.cdm.strategy.merge.MergeMode;
30
31 /**
32 * Abstract superclass implementing human annotations and machine markers to be assigned to CDM objects.
33 * @author m.doering
34 * @version 1.0
35 * @created 08-Nov-2007 13:06:10
36 */
37 @XmlAccessorType(XmlAccessType.FIELD)
38 @XmlType(name = "AnnotatableEntity", propOrder = {
39 "markers",
40 "annotations"
41 })
42 @MappedSuperclass
43 public abstract class AnnotatableEntity extends VersionableEntity implements IAnnotatableEntity {
44 private static final long serialVersionUID = 9151211842542443102L;
45 @SuppressWarnings("unused")
46 private static final Logger logger = Logger.getLogger(AnnotatableEntity.class);
47
48 @XmlElementWrapper(name = "Markers", nillable = true)
49 @XmlElement(name = "Marker")
50 @OneToMany(fetch=FetchType.LAZY)
51 @Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.DELETE, CascadeType.DELETE_ORPHAN})
52 @Merge(MergeMode.ADD_CLONE)
53 protected Set<Marker> markers;
54
55 @XmlElementWrapper(name = "Annotations", nillable = true)
56 @XmlElement(name = "Annotation")
57 @OneToMany(fetch=FetchType.LAZY)
58 @Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.DELETE, CascadeType.DELETE_ORPHAN})
59 @Merge(MergeMode.ADD_CLONE)
60 protected Set<Annotation> annotations;
61
62 protected AnnotatableEntity() {
63 super();
64 }
65
66 //*************** MARKER **********************************************
67
68
69 public Set<Marker> getMarkers(){
70 if(markers == null) {
71 this.markers = new HashSet<Marker>();
72 }
73 return this.markers;
74 }
75 public void addMarker(Marker marker){
76 if (marker != null){
77 marker.setMarkedObj(this);
78 getMarkers().add(marker);
79 }
80 }
81 public void removeMarker(Marker marker){
82 if(getMarkers().contains(marker)) {
83 getMarkers().remove(marker);
84 marker.setMarkedObj(null);
85 }
86 }
87
88 //*************** ANNOTATIONS **********************************************
89
90 public Set<Annotation> getAnnotations(){
91 if(annotations == null) {
92 this.annotations = new HashSet<Annotation>();
93 }
94 return this.annotations;
95 }
96 public void addAnnotation(Annotation annotation){
97 if (annotation != null){
98 annotation.setAnnotatedObj(this);
99 getAnnotations().add(annotation);
100 }
101 }
102
103 public void removeAnnotation(Annotation annotation){
104 if(getAnnotations().contains(annotation)) {
105 getAnnotations().remove(annotation);
106 annotation.setAnnotatedObj(null);
107 }
108 }
109
110 //********************** CLONE *****************************************/
111
112 /* (non-Javadoc)
113 * @see eu.etaxonomy.cdm.model.common.VersionableEntity#clone()
114 */
115 @Override
116 public Object clone() throws CloneNotSupportedException{
117 AnnotatableEntity result = (AnnotatableEntity)super.clone();
118
119 //Annotations
120 result.annotations = new HashSet<Annotation>();
121 for (Annotation annotation : getAnnotations()){
122 Annotation newAnnotation = (Annotation)annotation.clone();
123 result.addAnnotation(newAnnotation);
124 }
125
126 //Markers
127 result.markers = new HashSet<Marker>();
128 for (Marker marker : getMarkers()){
129 Marker newMarker = (Marker)marker.clone();
130 result.addMarker(newMarker);
131 }
132
133 //no changes to: -
134 return result;
135 }
136 }