Project

General

Profile

Download (6.54 KB) Statistics
| Branch: | Tag: | Revision:
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 = new HashSet<Marker>();
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 = new HashSet<Annotation>();
64

    
65
	protected AnnotatableEntity() {
66
		super();
67
	}
68

    
69
//*************** MARKER **********************************************
70

    
71

    
72
	@Override
73
    public Set<Marker> getMarkers(){
74
		return this.markers;
75
	}
76

    
77
	@Override
78
    public void addMarker(Marker marker){
79
		if (marker != null){
80
			marker.setMarkedObj(this);
81
			getMarkers().add(marker);
82
		}
83
	}
84
	@Override
85
    public void removeMarker(Marker marker){
86
		if(getMarkers().contains(marker)) {
87
			getMarkers().remove(marker);
88
		    marker.setMarkedObj(null);
89
		}
90
	}
91

    
92
	@Override
93
    public boolean hasMarker(MarkerType type, boolean value){
94
		return hasMarker(type.getUuid(), value);
95
	}
96

    
97
	@Override
98
    public boolean hasMarker(UUID uuidMarkerType, boolean value){
99
		for (Marker marker: getMarkers()){
100
			if (marker.getMarkerType().getUuid().equals(uuidMarkerType)){
101
				if (marker.getFlag() == value){
102
					return true;
103
				}
104
			}
105
		}
106
		return false;
107
	}
108

    
109
//*************** ANNOTATIONS **********************************************
110

    
111
	@Override
112
    public Set<Annotation> getAnnotations(){
113
		return this.annotations;
114
	}
115
	@Override
116
    public void addAnnotation(Annotation annotation){
117
		if (annotation != null){
118
			annotation.setAnnotatedObj(this);
119
			getAnnotations().add(annotation);
120
		}
121
	}
122

    
123
	@Override
124
    public void removeAnnotation(Annotation annotation){
125
		if(getAnnotations().contains(annotation)) {
126
			getAnnotations().remove(annotation);
127
		    annotation.setAnnotatedObj(null);
128
		}
129
	}
130

    
131
//********************** CLONE *****************************************/
132

    
133

    
134
	@Override
135
	public Object clone() throws CloneNotSupportedException{
136
		AnnotatableEntity result = (AnnotatableEntity)super.clone();
137

    
138
		//Annotations
139
		result.annotations = new HashSet<Annotation>();
140
		for (Annotation annotation : getAnnotations()){
141
			Annotation newAnnotation = (Annotation)annotation.clone();
142
			result.addAnnotation(newAnnotation);
143
		}
144

    
145
		//Markers
146
		result.markers = new HashSet<Marker>();
147
		for (Marker marker : getMarkers()){
148
			Marker newMarker = (Marker)marker.clone();
149
			result.addMarker(newMarker);
150
		}
151

    
152
		//no changes to: -
153
		return result;
154
	}
155
}
(1-1/72)