Project

General

Profile

Download (7.6 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.model.EntityCollectionSetterAdapter;
31
import eu.etaxonomy.cdm.model.EntityCollectionSetterAdapter.SetterAdapterException;
32
import eu.etaxonomy.cdm.strategy.merge.Merge;
33
import eu.etaxonomy.cdm.strategy.merge.MergeMode;
34

    
35
/**
36
 * Abstract superclass implementing human annotations and machine markers to be assigned to CDM objects.
37
 * @author m.doering
38
 * @since 08-Nov-2007 13:06:10
39
 */
40
@XmlAccessorType(XmlAccessType.FIELD)
41
@XmlType(name = "AnnotatableEntity", propOrder = {
42
    "markers",
43
    "annotations"
44
})
45
@Audited
46
@MappedSuperclass
47
public abstract class AnnotatableEntity
48
        extends VersionableEntity
49
        implements IAnnotatableEntity {
50

    
51
    private static final long serialVersionUID = 9151211842542443102L;
52
	@SuppressWarnings("unused")
53
	private static final Logger logger = Logger.getLogger(AnnotatableEntity.class);
54

    
55
	@XmlElementWrapper(name = "Markers", nillable = true)
56
	@XmlElement(name = "Marker")
57
    @OneToMany(fetch=FetchType.LAZY, orphanRemoval=true)
58
	@Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.DELETE})
59
	@Merge(MergeMode.ADD_CLONE)
60
	protected Set<Marker> markers = new HashSet<>();
61

    
62
	@XmlElementWrapper(name = "Annotations", nillable = true)
63
	@XmlElement(name = "Annotation")
64
    @OneToMany(fetch=FetchType.LAZY, orphanRemoval=true)
65
    @Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.DELETE})
66
	@Merge(MergeMode.ADD_CLONE)
67
	protected Set<Annotation> annotations = new HashSet<>();
68

    
69
	protected AnnotatableEntity() {
70
		super();
71
	}
72

    
73
//*************** MARKER **********************************************
74

    
75

    
76
	@Override
77
    public Set<Marker> getMarkers(){
78
		return this.markers;
79
	}
80

    
81
	@Override
82
    public void addMarker(Marker marker){
83
		if (marker != null){
84
			getMarkers().add(marker);
85
		}
86
	}
87
    public Marker addMarker(MarkerType type, boolean value){
88
        Marker marker = Marker.NewInstance(type, value);
89
        addMarker(marker);
90
        return marker;
91
    }
92
	@Override
93
    public void removeMarker(Marker marker){
94
		if(getMarkers().contains(marker)) {
95
			getMarkers().remove(marker);
96
		}
97
	}
98

    
99
	@Override
100
    public boolean hasMarker(MarkerType type, boolean value){
101
		return hasMarker(type.getUuid(), value);
102
	}
103

    
104
	@Override
105
    public boolean hasMarker(UUID uuidMarkerType, boolean value){
106
		for (Marker marker: getMarkers()){
107
			if (marker.getMarkerType().getUuid().equals(uuidMarkerType)){
108
				if (marker.getFlag() == value){
109
					return true;
110
				}
111
			}
112
		}
113
		return false;
114
	}
115

    
116
    @Override
117
    public Boolean markerValue(UUID uuidMarkerType){
118
        for (Marker marker: getMarkers()){
119
            if (marker.getMarkerType().getUuid().equals(uuidMarkerType)){
120
                 return marker.getFlag();
121
            }
122
        }
123
        return null;
124
    }
125

    
126
//*************** ANNOTATIONS **********************************************
127

    
128
	@Override
129
    public Set<Annotation> getAnnotations(){
130
		return this.annotations;
131
	}
132
	@Override
133
    public void addAnnotation(Annotation annotation){
134
		if (annotation != null){
135
			getAnnotations().add(annotation);
136
		}
137
	}
138
    public Set<Annotation> getAnnotations(UUID uuidAnnotationType){
139
        Set<Annotation> result = new HashSet<>();
140
        for (Annotation annotation: getAnnotations()){
141
            if (annotation.getAnnotationType() != null && annotation.getAnnotationType().getUuid().equals(uuidAnnotationType)){
142
                result.add(annotation);
143
            }
144
        }
145
        return result;
146
    }
147

    
148
	@Override
149
    public void removeAnnotation(Annotation annotation){
150
		if(getAnnotations().contains(annotation)) {
151
			getAnnotations().remove(annotation);
152
		}
153
	}
154

    
155
	public void setAnnotations(Set<Annotation> annotations) throws SetterAdapterException {
156
	     new EntityCollectionSetterAdapter<AnnotatableEntity, Annotation>(AnnotatableEntity.class, Annotation.class, "annotations").setCollection(this, annotations);
157
    }
158

    
159
//********************** CLONE *****************************************/
160

    
161

    
162
	@Override
163
	public Object clone() throws CloneNotSupportedException{
164
		AnnotatableEntity result = (AnnotatableEntity)super.clone();
165

    
166
		//Annotations
167
		result.annotations = new HashSet<>();
168
		for (Annotation annotation : getAnnotations()){
169
			Annotation newAnnotation = (Annotation)annotation.clone();
170
			result.addAnnotation(newAnnotation);
171
		}
172

    
173
		//Markers
174
		result.markers = new HashSet<>();
175
		for (Marker marker : getMarkers()){
176
			Marker newMarker = (Marker)marker.clone();
177
			result.addMarker(newMarker);
178
		}
179

    
180
		//no changes to: -
181
		return result;
182
	}
183
}
(2-2/56)