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
|
|
13
|
import java.util.HashSet;
|
14
|
import java.util.Set;
|
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.XmlRootElement;
|
24
|
import javax.xml.bind.annotation.XmlType;
|
25
|
|
26
|
import org.apache.log4j.Logger;
|
27
|
import org.hibernate.annotations.Cascade;
|
28
|
import org.hibernate.annotations.CascadeType;
|
29
|
import org.hibernate.envers.Audited;
|
30
|
|
31
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
32
|
import eu.etaxonomy.cdm.strategy.merge.Merge;
|
33
|
import eu.etaxonomy.cdm.strategy.merge.MergeMode;
|
34
|
|
35
|
/**
|
36
|
* Abstract class for all objects that may have (multiple) sources
|
37
|
* @author a.mueller
|
38
|
* @since 14-Jan-2019 13:06:47
|
39
|
*/
|
40
|
@XmlAccessorType(XmlAccessType.FIELD)
|
41
|
@XmlType(name = "SourcedEntityBase", propOrder = {
|
42
|
"sources"
|
43
|
})
|
44
|
@XmlRootElement(name = "SourcedEntityBase")
|
45
|
@MappedSuperclass
|
46
|
@Audited
|
47
|
public abstract class SourcedEntityBase<SOURCE extends OriginalSourceBase<? extends SourcedEntityBase<SOURCE>>>
|
48
|
extends AnnotatableEntity
|
49
|
implements ISourceable<SOURCE>{
|
50
|
|
51
|
private static final long serialVersionUID = -5614669050360359126L;
|
52
|
@SuppressWarnings("unused")
|
53
|
private static final Logger logger = Logger.getLogger(SourcedEntityBase.class);
|
54
|
|
55
|
@XmlElementWrapper(name = "Sources")
|
56
|
@XmlElement(name = "Source")
|
57
|
@OneToMany(fetch = FetchType.LAZY, orphanRemoval=true)
|
58
|
@Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.DELETE})
|
59
|
@Merge(MergeMode.ADD_CLONE)
|
60
|
//TODO should be Set<SOURCE> but this currently throws exception in DefaultMergeStrategyTest
|
61
|
private Set<OriginalSourceBase> sources = new HashSet<>();
|
62
|
|
63
|
// ************ CONSTRUCTOR ********************************************/
|
64
|
|
65
|
//for hibernate use only
|
66
|
protected SourcedEntityBase() {
|
67
|
super();
|
68
|
}
|
69
|
|
70
|
//
|
71
|
// public SourcedEntityBase(Reference citation, String citationMicroReference,
|
72
|
// String originalNameString) {
|
73
|
// this.citationMicroReference = citationMicroReference;
|
74
|
// this.originalNameString = originalNameString;
|
75
|
// this.citation = citation;
|
76
|
// }
|
77
|
|
78
|
//********************* GETTER / SETTER *******************************/
|
79
|
|
80
|
@Override
|
81
|
public Set<SOURCE> getSources() {
|
82
|
return (Set<SOURCE>)this.sources;
|
83
|
}
|
84
|
|
85
|
@Override
|
86
|
public void addSource(SOURCE source) {
|
87
|
if (source != null){
|
88
|
this.sources.add(source);
|
89
|
}
|
90
|
}
|
91
|
|
92
|
@Override
|
93
|
public SOURCE addSource(OriginalSourceType type, String id, String idNamespace, Reference citation, String microCitation) {
|
94
|
if (id == null && idNamespace == null && citation == null && microCitation == null){
|
95
|
return null;
|
96
|
}
|
97
|
SOURCE source = createNewSource(type, id, idNamespace, citation, microCitation, null);
|
98
|
addSource(source);
|
99
|
return source;
|
100
|
}
|
101
|
|
102
|
@Override
|
103
|
public void addSources(Set<SOURCE> sources){
|
104
|
if (sources != null){
|
105
|
for (SOURCE source: sources){
|
106
|
addSource(source);
|
107
|
}
|
108
|
}
|
109
|
}
|
110
|
|
111
|
@Override
|
112
|
public SOURCE addImportSource(String id, String idNamespace, Reference citation, String microCitation) {
|
113
|
if (id == null && idNamespace == null && citation == null && microCitation == null){
|
114
|
return null;
|
115
|
}
|
116
|
SOURCE source = createNewSource(OriginalSourceType.Import, id, idNamespace, citation, microCitation, null);
|
117
|
addSource(source);
|
118
|
return source;
|
119
|
}
|
120
|
|
121
|
@Override
|
122
|
public SOURCE addPrimaryTaxonomicSource(Reference citation, String microCitation) {
|
123
|
if (citation == null && microCitation == null){
|
124
|
return null;
|
125
|
}
|
126
|
SOURCE source = createNewSource(OriginalSourceType.PrimaryTaxonomicSource, null, null, citation, microCitation, null);
|
127
|
addSource(source);
|
128
|
return source;
|
129
|
}
|
130
|
|
131
|
@Override
|
132
|
public SOURCE addPrimaryTaxonomicSource(Reference citation) {
|
133
|
return addPrimaryTaxonomicSource(citation, null);
|
134
|
}
|
135
|
//
|
136
|
// /**
|
137
|
// * Adds a {@link IOriginalSource source} to this description element.
|
138
|
// * @param type the type of the source
|
139
|
// * @param idInSource the id used in the source
|
140
|
// * @param idNamespace the namespace for the id in the source
|
141
|
// * @param citation the source as a {@link Reference reference}
|
142
|
// * @param microReference the details (e.g. page number) in the reference
|
143
|
// * @param nameUsedInSource the taxon name used in the source
|
144
|
// * @param originalNameString the name as text used in the source
|
145
|
// */
|
146
|
// public void addSource(OriginalSourceType type, String idInSource, String idNamespace, Reference citation, String microReference, TaxonName nameUsedInSource, String originalNameString){
|
147
|
// DescriptionElementSource newSource = DescriptionElementSource.NewInstance(type, idInSource, idNamespace, citation, microReference, nameUsedInSource, originalNameString);
|
148
|
// addSource(newSource);
|
149
|
// }
|
150
|
|
151
|
@Override
|
152
|
public void removeSource(SOURCE source) {
|
153
|
this.sources.remove(source);
|
154
|
}
|
155
|
|
156
|
public void removeSources(){
|
157
|
this.sources.clear();
|
158
|
}
|
159
|
|
160
|
protected abstract SOURCE createNewSource(OriginalSourceType type, String idInSource, String idNamespace, Reference citation, String microReference, String originalNameString);
|
161
|
|
162
|
//****************** CLONE ************************************************/
|
163
|
|
164
|
@Override
|
165
|
public Object clone() throws CloneNotSupportedException{
|
166
|
@SuppressWarnings("unchecked")
|
167
|
SourcedEntityBase<SOURCE> result = (SourcedEntityBase<SOURCE>)super.clone();
|
168
|
|
169
|
//Sources
|
170
|
result.sources = new HashSet<>();
|
171
|
for (SOURCE source : getSources()){
|
172
|
@SuppressWarnings("unchecked")
|
173
|
SOURCE newSource = (SOURCE)source.clone();
|
174
|
result.addSource(newSource);
|
175
|
}
|
176
|
|
177
|
//no changes to: -
|
178
|
return result;
|
179
|
}
|
180
|
|
181
|
}
|