Project

General

Profile

Download (6.57 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;
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
	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
		return this.annotations;
110
	}
111
	public void addAnnotation(Annotation annotation){
112
		if (annotation != null){
113
			annotation.setAnnotatedObj(this);
114
			getAnnotations().add(annotation);
115
		}
116
	}
117
	
118
	public void removeAnnotation(Annotation annotation){
119
		if(getAnnotations().contains(annotation)) {
120
			getAnnotations().remove(annotation);
121
		    annotation.setAnnotatedObj(null);
122
		}
123
	}
124
	
125
//********************** CLONE *****************************************/
126

    
127
	/* (non-Javadoc)
128
	 * @see eu.etaxonomy.cdm.model.common.VersionableEntity#clone()
129
	 */
130
	@Override
131
	public Object clone() throws CloneNotSupportedException{
132
		AnnotatableEntity result = (AnnotatableEntity)super.clone();
133
		
134
		//Annotations
135
		result.annotations = new HashSet<Annotation>();
136
		for (Annotation annotation : getAnnotations()){
137
			Annotation newAnnotation = (Annotation)annotation.clone();
138
			result.addAnnotation(newAnnotation);
139
		}
140
		
141
		//Markers
142
		result.markers = new HashSet<Marker>();
143
		for (Marker marker : getMarkers()){
144
			Marker newMarker = (Marker)marker.clone();
145
			result.addMarker(newMarker);
146
		}
147
		
148
		//no changes to: -
149
		return result;
150
	}
151
}
(1-1/69)