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