Major changes to the cdmlib default term loading and initialization, plus indexing...
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / common / AnnotatableEntity.java
index 31ead9d6d3ef9929ec8b613a404ded726d6b7137..881c4bfce70f7eeeff027393c6cd5f59e7eeca29 100644 (file)
@@ -9,26 +9,47 @@
 
 package eu.etaxonomy.cdm.model.common;
 
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.persistence.FetchType;
+import javax.persistence.MappedSuperclass;
+import javax.persistence.OneToMany;
+import javax.persistence.Transient;
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlElementWrapper;
+import javax.xml.bind.annotation.XmlType;
+
 import org.apache.log4j.Logger;
 import org.hibernate.annotations.Cascade;
 import org.hibernate.annotations.CascadeType;
 
-import java.util.*;
-
-import javax.persistence.*;
-
 /**
  * Abstract superclass implementing human annotations and machine markers to be assigned to CDM objects.
  * @author m.doering
  * @version 1.0
  * @created 08-Nov-2007 13:06:10
  */
+@XmlAccessorType(XmlAccessType.FIELD)
+@XmlType(name = "AnnotatableEntity", propOrder = {
+    "markers",
+    "annotations"
+})
 @MappedSuperclass
-public abstract class AnnotatableEntity<T extends AnnotatableEntity> extends VersionableEntity<T> {
-       static Logger logger = Logger.getLogger(AnnotatableEntity.class);
+public abstract class AnnotatableEntity extends VersionableEntity {
+       private static final long serialVersionUID = 9151211842542443102L;
+       @SuppressWarnings("unused")
+       private static final Logger logger = Logger.getLogger(AnnotatableEntity.class);
 
-       protected Set<Marker> markers = new HashSet<Marker>();
-       protected Set<Annotation> annotations = new HashSet<Annotation>();
+       @XmlElementWrapper(name = "Markers")
+       @XmlElement(name = "Marker")
+       protected Set<Marker> markers = getNewMarkerSet();
+       
+       @XmlElementWrapper(name = "Annotations")
+       @XmlElement(name = "Annotation")
+       protected Set<Annotation> annotations = getNewAnnotationSet();
        
        protected AnnotatableEntity() {
                super();
@@ -57,8 +78,8 @@ public abstract class AnnotatableEntity<T extends AnnotatableEntity> extends Ver
 
 //*************** ANNOTATIONS **********************************************
        
-       @OneToMany(fetch=FetchType.LAZY) //(mappedBy="AnnotatedObj")
-       @Cascade({CascadeType.SAVE_UPDATE})
+       @OneToMany(fetch=FetchType.LAZY)
+       @Cascade({CascadeType.SAVE_UPDATE, CascadeType.DELETE})
        public Set<Annotation> getAnnotations(){
                return this.annotations;
        }
@@ -68,11 +89,54 @@ public abstract class AnnotatableEntity<T extends AnnotatableEntity> extends Ver
                        annotations.add(annotation);
                }
        }
+       
        public void removeAnnotation(Annotation annotation){
+               this.annotations.remove(annotation);
                annotation.setAnnotatedObj(null);
        }
+       
        protected void setAnnotations(Set<Annotation> annotations) {
                this.annotations = annotations;
        }
+       
+//********************** CLONE *****************************************/
 
-}
\ No newline at end of file
+       /* (non-Javadoc)
+        * @see eu.etaxonomy.cdm.model.common.VersionableEntity#clone()
+        */
+       @Override
+       public Object clone() throws CloneNotSupportedException{
+               AnnotatableEntity result = (AnnotatableEntity)super.clone();
+               
+               //Annotations
+               Set<Annotation> newAnnotations = getNewAnnotationSet();
+               for (Annotation annotation : this.annotations ){
+                       Annotation newExtension = annotation.clone(this);
+                       newAnnotations.add(newExtension);
+               }
+               result.setAnnotations(newAnnotations);
+               
+               
+               //Markers
+               Set<Marker> newMarkers = getNewMarkerSet();
+               for (Marker marker : this.markers ){
+                       Marker newMarker = marker.clone(this);
+                       newMarkers.add(newMarker);
+               }
+               result.setMarkers(newMarkers);
+               
+               //no changes to: -
+               return result;
+       }
+       
+       @Transient
+       private Set<Annotation> getNewAnnotationSet(){
+               return new HashSet<Annotation>();
+       }
+       
+       @Transient
+       private Set<Marker> getNewMarkerSet(){
+               return new HashSet<Marker>();
+       }
+       
+}