merge hibernate4 migration branch into trunk
[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 import java.util.UUID;
15
16 import javax.persistence.FetchType;
17 import javax.persistence.MappedSuperclass;
18 import javax.persistence.OneToMany;
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 import org.apache.log4j.Logger;
26 import org.hibernate.annotations.Cascade;
27 import org.hibernate.annotations.CascadeType;
28 import org.hibernate.envers.Audited;
29
30 import eu.etaxonomy.cdm.strategy.merge.Merge;
31 import eu.etaxonomy.cdm.strategy.merge.MergeMode;
32
33 /**
34 * Abstract superclass implementing human annotations and machine markers to be assigned to CDM objects.
35 * @author m.doering
36 * @version 1.0
37 * @created 08-Nov-2007 13:06:10
38 */
39 @XmlAccessorType(XmlAccessType.FIELD)
40 @XmlType(name = "AnnotatableEntity", propOrder = {
41 "markers",
42 "annotations"
43 })
44 @Audited
45 @MappedSuperclass
46 public abstract class AnnotatableEntity extends VersionableEntity implements IAnnotatableEntity {
47 private static final long serialVersionUID = 9151211842542443102L;
48 @SuppressWarnings("unused")
49 private static final Logger logger = Logger.getLogger(AnnotatableEntity.class);
50
51 @XmlElementWrapper(name = "Markers", nillable = true)
52 @XmlElement(name = "Marker")
53 @OneToMany(fetch=FetchType.LAZY, orphanRemoval=true)
54 @Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.DELETE})
55 @Merge(MergeMode.ADD_CLONE)
56 protected Set<Marker> markers;
57
58 @XmlElementWrapper(name = "Annotations", nillable = true)
59 @XmlElement(name = "Annotation")
60 @OneToMany(fetch=FetchType.LAZY, orphanRemoval=true)
61 @Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.DELETE})
62 @Merge(MergeMode.ADD_CLONE)
63 protected Set<Annotation> annotations;
64
65 protected AnnotatableEntity() {
66 super();
67 }
68
69 //*************** MARKER **********************************************
70
71
72 public Set<Marker> getMarkers(){
73 if(markers == null) {
74 this.markers = new HashSet<Marker>();
75 }
76 return this.markers;
77 }
78 public void addMarker(Marker marker){
79 if (marker != null){
80 marker.setMarkedObj(this);
81 getMarkers().add(marker);
82 }
83 }
84 public void removeMarker(Marker marker){
85 if(getMarkers().contains(marker)) {
86 getMarkers().remove(marker);
87 marker.setMarkedObj(null);
88 }
89 }
90
91 public boolean hasMarker(MarkerType type, boolean value){
92 return hasMarker(type.getUuid(), value);
93 }
94
95 public boolean hasMarker(UUID uuidMarkerType, boolean value){
96 for (Marker marker: getMarkers()){
97 if (marker.getMarkerType().getUuid().equals(uuidMarkerType)){
98 if (marker.getFlag() == value){
99 return true;
100 }
101 }
102 }
103 return false;
104 }
105
106 //*************** ANNOTATIONS **********************************************
107
108 public Set<Annotation> getAnnotations(){
109 if(annotations == null) {
110 this.annotations = new HashSet<Annotation>();
111 }
112 return this.annotations;
113 }
114 public void addAnnotation(Annotation annotation){
115 if (annotation != null){
116 annotation.setAnnotatedObj(this);
117 getAnnotations().add(annotation);
118 }
119 }
120
121 public void removeAnnotation(Annotation annotation){
122 if(getAnnotations().contains(annotation)) {
123 getAnnotations().remove(annotation);
124 annotation.setAnnotatedObj(null);
125 }
126 }
127
128 //********************** CLONE *****************************************/
129
130 /* (non-Javadoc)
131 * @see eu.etaxonomy.cdm.model.common.VersionableEntity#clone()
132 */
133 @Override
134 public Object clone() throws CloneNotSupportedException{
135 AnnotatableEntity result = (AnnotatableEntity)super.clone();
136
137 //Annotations
138 result.annotations = new HashSet<Annotation>();
139 for (Annotation annotation : getAnnotations()){
140 Annotation newAnnotation = (Annotation)annotation.clone();
141 result.addAnnotation(newAnnotation);
142 }
143
144 //Markers
145 result.markers = new HashSet<Marker>();
146 for (Marker marker : getMarkers()){
147 Marker newMarker = (Marker)marker.clone();
148 result.addMarker(newMarker);
149 }
150
151 //no changes to: -
152 return result;
153 }
154 }