Project

General

Profile

Download (8.26 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 void removeMarker(UUID markerTypeUuid){
101
        for(Marker marker : getMarkers(markerTypeUuid)) {
102
            removeMarker(marker);
103
        }
104
    }
105

    
106
	@Override
107
    public boolean hasMarker(MarkerType type, boolean value){
108
		return hasMarker(type.getUuid(), value);
109
	}
110

    
111
	@Override
112
    public boolean hasMarker(UUID uuidMarkerType, boolean value){
113
		for (Marker marker: getMarkers(uuidMarkerType)){
114
			if (marker.getFlag() == value){
115
			    return true;
116
			}
117
		}
118
		return false;
119
	}
120

    
121
    @Override
122
    public Set<Marker> getMarkers(UUID uuidMarkerType){
123
        Set<Marker> result = new HashSet<>();
124
        for (Marker marker: getMarkers()){
125
            if (marker.getMarkerType().getUuid().equals(uuidMarkerType)){
126
                result.add(marker);
127
            }
128
        }
129
        return result;
130
    }
131

    
132
    @Override
133
    public Boolean markerValue(UUID uuidMarkerType){
134
        for (Marker marker: getMarkers()){
135
            if (marker.getMarkerType().getUuid().equals(uuidMarkerType)){
136
                 return marker.getFlag();
137
            }
138
        }
139
        return null;
140
    }
141

    
142
//*************** ANNOTATIONS **********************************************
143

    
144
	@Override
145
    public Set<Annotation> getAnnotations(){
146
		return this.annotations;
147
	}
148
	@Override
149
    public void addAnnotation(Annotation annotation){
150
		if (annotation != null){
151
			getAnnotations().add(annotation);
152
		}
153
	}
154
    public Set<Annotation> getAnnotations(UUID uuidAnnotationType){
155
        Set<Annotation> result = new HashSet<>();
156
        for (Annotation annotation: getAnnotations()){
157
            if (annotation.getAnnotationType() != null && annotation.getAnnotationType().getUuid().equals(uuidAnnotationType)){
158
                result.add(annotation);
159
            }
160
        }
161
        return result;
162
    }
163

    
164
	@Override
165
    public void removeAnnotation(Annotation annotation){
166
		if(getAnnotations().contains(annotation)) {
167
			getAnnotations().remove(annotation);
168
		}
169
	}
170

    
171
	public void setAnnotations(Set<Annotation> annotations) throws SetterAdapterException {
172
	     new EntityCollectionSetterAdapter<AnnotatableEntity, Annotation>(AnnotatableEntity.class, Annotation.class, "annotations").setCollection(this, annotations);
173
    }
174

    
175
// **************** EMPTY ************************/
176

    
177
    @Override
178
    protected boolean checkEmpty(){
179
       return super.checkEmpty()
180
            && this.annotations.isEmpty()
181
            && this.markers.isEmpty()
182
           ;
183
    }
184

    
185
//********************** CLONE *****************************************/
186

    
187
    @Override
188
	public AnnotatableEntity clone() throws CloneNotSupportedException{
189

    
190
	    AnnotatableEntity result = (AnnotatableEntity)super.clone();
191

    
192
		//Annotations
193
		result.annotations = new HashSet<>();
194
		for (Annotation annotation : getAnnotations()){
195
			Annotation newAnnotation = annotation.clone();
196
			result.addAnnotation(newAnnotation);
197
		}
198

    
199
		//Markers
200
		result.markers = new HashSet<>();
201
		for (Marker marker : getMarkers()){
202
			Marker newMarker = marker.clone();
203
			result.addMarker(newMarker);
204
		}
205

    
206
		//no changes to: -
207
		return result;
208
	}
209
}
(1-1/58)