1
|
/**
|
2
|
* Copyright (C) 2017 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.vaadin.model.registration;
|
10
|
|
11
|
import java.util.UUID;
|
12
|
|
13
|
import org.apache.commons.collections.CollectionUtils;
|
14
|
|
15
|
import eu.etaxonomy.cdm.api.util.DerivedUnitConversionException;
|
16
|
import eu.etaxonomy.cdm.api.util.DerivedUnitConverter;
|
17
|
import eu.etaxonomy.cdm.common.URI;
|
18
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
19
|
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
|
20
|
import eu.etaxonomy.cdm.model.media.Media;
|
21
|
import eu.etaxonomy.cdm.model.media.MediaRepresentation;
|
22
|
import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
|
23
|
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
|
24
|
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
|
25
|
import eu.etaxonomy.cdm.model.occurrence.Collection;
|
26
|
import eu.etaxonomy.cdm.model.occurrence.DerivationEvent;
|
27
|
import eu.etaxonomy.cdm.model.occurrence.DerivationEventType;
|
28
|
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
|
29
|
import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen;
|
30
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
|
31
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
32
|
import eu.etaxonomy.cdm.model.term.DefinedTerm;
|
33
|
|
34
|
/**
|
35
|
* A DTO which is use in the context of the {@link SpecimenTypeDesignationSetDTO} which is backed up
|
36
|
* <code>SpecimenTypeDesignation.typeSpecimen.derivedFrom.type</code> object graph.
|
37
|
* <p>
|
38
|
* The contained {@link DerivedUnit} either is a {@link MediaSpecimen} with or a {@link DerivedUnit}, depending on the
|
39
|
* "kind of unit" which is defined by the associated <code>DerivationEvent.type</code>:
|
40
|
*
|
41
|
* <ul>
|
42
|
* <li>{@link DerivationEventType#GATHERING_IN_SITU()} -> {@link DerivedUnit}</li>
|
43
|
* <li>{@link DerivationEventTypes#CULTURE_METABOLIC_INACTIVE()} -> {@link DerivedUnit}</li>
|
44
|
* <li>{@link DerivationEventTypes#UNPUBLISHED_IMAGE()} -> {@link MediaSpecimen}</li>
|
45
|
* <li>{@link DerivationEventTypes#PUBLISHED_IMAGE()} -> {@link MediaSpecimen}</li>
|
46
|
* </ul>
|
47
|
*
|
48
|
* @author a.kohlbecker
|
49
|
* @since Jun 22, 2017
|
50
|
*
|
51
|
*/
|
52
|
public class SpecimenTypeDesignationDTO {
|
53
|
|
54
|
SpecimenTypeDesignation std;
|
55
|
private DerivedUnit replacedTypeSpecimen;
|
56
|
|
57
|
/**
|
58
|
* @return the std
|
59
|
*/
|
60
|
public SpecimenTypeDesignation asSpecimenTypeDesignation() {
|
61
|
return std;
|
62
|
}
|
63
|
|
64
|
public DerivedUnit replacedTypeSpecimen(){
|
65
|
return replacedTypeSpecimen;
|
66
|
}
|
67
|
|
68
|
/**
|
69
|
* Creates an new new instance of SpecimenTypeDesignationDTO which is backed up
|
70
|
* by an newly instantiated <code>SpecimenTypeDesignation.typeSpecimen.derivedFrom.type</code> object graph.
|
71
|
*/
|
72
|
public SpecimenTypeDesignationDTO(){
|
73
|
this(SpecimenTypeDesignation.NewInstance());
|
74
|
}
|
75
|
|
76
|
/**
|
77
|
* Creates a new instance of SpecimenTypeDesignationDTO.
|
78
|
*
|
79
|
* The constructor assures that <code>SpecimenTypeDesignation.typeSpecimen.derivedFrom.type</code> are never null.
|
80
|
* That is in case the supplied std parameter or if any of the above listed properties is null the missing instances
|
81
|
* are created and added to the object graph.
|
82
|
*
|
83
|
* @param std
|
84
|
*/
|
85
|
public SpecimenTypeDesignationDTO(SpecimenTypeDesignation std) {
|
86
|
this.std = std;
|
87
|
if(std.getTypeSpecimen() == null){
|
88
|
DerivedUnit derivedUnit = DerivedUnit.NewInstance(SpecimenOrObservationType.PreservedSpecimen);
|
89
|
std.setTypeSpecimen(derivedUnit);
|
90
|
}
|
91
|
if(std.getTypeSpecimen().getDerivedFrom() == null){
|
92
|
DerivationEvent derivedFrom = DerivationEvent.NewInstance(DerivationEventType.GATHERING_IN_SITU());
|
93
|
std.getTypeSpecimen().setDerivedFrom(derivedFrom);
|
94
|
}
|
95
|
}
|
96
|
|
97
|
public DefinedTerm getKindOfUnit(){
|
98
|
return std.getTypeSpecimen().getKindOfUnit();
|
99
|
}
|
100
|
|
101
|
public void setKindOfUnit(DefinedTerm kindOfUnit) throws DerivedUnitConversionException{
|
102
|
|
103
|
std.getTypeSpecimen().setKindOfUnit(kindOfUnit);
|
104
|
DerivedUnit typeSpecimen = HibernateProxyHelper.deproxy(std.getTypeSpecimen());
|
105
|
|
106
|
Class<? extends DerivedUnit> requiredSpecimenType = specimenTypeFor(kindOfUnit);
|
107
|
Class<? extends DerivedUnit> currentType = typeSpecimen.getClass();
|
108
|
|
109
|
if(!requiredSpecimenType.equals(currentType)){
|
110
|
SpecimenOrObservationType convertToType = specimenOrObservationTypeFor(kindOfUnit);
|
111
|
if(requiredSpecimenType.equals(MediaSpecimen.class)){
|
112
|
DerivedUnitConverter<MediaSpecimen> converter = new DerivedUnitConverter<MediaSpecimen>(std);
|
113
|
std = converter.convertTo((Class<MediaSpecimen>)requiredSpecimenType, convertToType);
|
114
|
} else {
|
115
|
if(currentType == MediaSpecimen.class){
|
116
|
MediaSpecimen mediaSpecimen = (MediaSpecimen)typeSpecimen;
|
117
|
// set null to allow conversion
|
118
|
mediaSpecimen.setMediaSpecimen(null);
|
119
|
}
|
120
|
DerivedUnitConverter<DerivedUnit> converter = new DerivedUnitConverter<DerivedUnit>(std);
|
121
|
std = converter.convertTo((Class<DerivedUnit>)requiredSpecimenType, convertToType);
|
122
|
}
|
123
|
if(typeSpecimen.getId() != 0){
|
124
|
replacedTypeSpecimen = typeSpecimen;
|
125
|
}
|
126
|
|
127
|
}
|
128
|
}
|
129
|
|
130
|
/**
|
131
|
*
|
132
|
* @return the total count of typeDesignations associated with the type specimen
|
133
|
*/
|
134
|
public int getAssociatedTypeDesignationCount() {
|
135
|
return std.getTypeSpecimen().getSpecimenTypeDesignations().size();
|
136
|
}
|
137
|
|
138
|
|
139
|
/**
|
140
|
* See constructor doc.
|
141
|
*
|
142
|
* @param kindOfUnit
|
143
|
* @return
|
144
|
* either <code>DerivedUnit</code> or <code>MediaSpecimen</code> never null.
|
145
|
* <code>DerivedUnit</code> is the fall back return value.
|
146
|
*/
|
147
|
private Class<? extends DerivedUnit> specimenTypeFor(DefinedTerm derivationEventType) {
|
148
|
UUID detUuid = derivationEventType.getUuid();
|
149
|
|
150
|
if(detUuid.equals(KindOfUnitTerms.SPECIMEN().getUuid())) {
|
151
|
return DerivedUnit.class;
|
152
|
}
|
153
|
if(detUuid.equals(KindOfUnitTerms.CULTURE_METABOLIC_INACTIVE().getUuid())) {
|
154
|
return DerivedUnit.class;
|
155
|
}
|
156
|
if(detUuid.equals(KindOfUnitTerms.PUBLISHED_IMAGE().getUuid())) {
|
157
|
return MediaSpecimen.class;
|
158
|
}
|
159
|
if(detUuid.equals(KindOfUnitTerms.UNPUBLISHED_IMAGE().getUuid())) {
|
160
|
return MediaSpecimen.class;
|
161
|
}
|
162
|
return DerivedUnit.class;
|
163
|
}
|
164
|
|
165
|
private SpecimenOrObservationType specimenOrObservationTypeFor(DefinedTerm derivationEventType) {
|
166
|
|
167
|
UUID detUuid = derivationEventType.getUuid();
|
168
|
|
169
|
if(detUuid.equals(KindOfUnitTerms.SPECIMEN().getUuid())) {
|
170
|
return SpecimenOrObservationType.PreservedSpecimen;
|
171
|
}
|
172
|
if(detUuid.equals(KindOfUnitTerms.CULTURE_METABOLIC_INACTIVE().getUuid())) {
|
173
|
return SpecimenOrObservationType.LivingSpecimen;
|
174
|
}
|
175
|
if(detUuid.equals(KindOfUnitTerms.PUBLISHED_IMAGE().getUuid())) {
|
176
|
return SpecimenOrObservationType.StillImage;
|
177
|
}
|
178
|
if(detUuid.equals(KindOfUnitTerms.UNPUBLISHED_IMAGE().getUuid())) {
|
179
|
return SpecimenOrObservationType.StillImage;
|
180
|
}
|
181
|
return SpecimenOrObservationType.PreservedSpecimen;
|
182
|
|
183
|
}
|
184
|
|
185
|
public SpecimenTypeDesignationStatus getTypeStatus(){
|
186
|
return HibernateProxyHelper.deproxy(std.getTypeStatus(), SpecimenTypeDesignationStatus.class);
|
187
|
}
|
188
|
|
189
|
public void setTypeStatus(SpecimenTypeDesignationStatus typeStatus){
|
190
|
std.setTypeStatus(typeStatus);
|
191
|
}
|
192
|
|
193
|
public Collection getCollection(){
|
194
|
return std.getTypeSpecimen().getCollection();
|
195
|
}
|
196
|
|
197
|
public void setCollection(Collection collection){
|
198
|
std.getTypeSpecimen().setCollection(collection);
|
199
|
}
|
200
|
|
201
|
public String getAccessionNumber(){
|
202
|
return std.getTypeSpecimen().getAccessionNumber();
|
203
|
}
|
204
|
|
205
|
public void setAccessionNumber(String accessionNumber){
|
206
|
std.getTypeSpecimen().setAccessionNumber(accessionNumber);
|
207
|
}
|
208
|
|
209
|
public URI getPreferredStableUri(){
|
210
|
return std.getTypeSpecimen().getPreferredStableUri();
|
211
|
}
|
212
|
|
213
|
public void setPreferredStableUri(URI uri){
|
214
|
std.getTypeSpecimen().setPreferredStableUri(uri);
|
215
|
}
|
216
|
|
217
|
public Reference getDesignationReference() {
|
218
|
return std.getCitation();
|
219
|
}
|
220
|
|
221
|
public void setDesignationReference(Reference citation) {
|
222
|
std.setCitation(citation);
|
223
|
}
|
224
|
|
225
|
public String getDesignationReferenceDetail() {
|
226
|
return std.getCitationMicroReference();
|
227
|
}
|
228
|
|
229
|
public void setDesignationReferenceDetail(String detail) {
|
230
|
std.setCitationMicroReference(detail);
|
231
|
}
|
232
|
|
233
|
public URI getMediaUri(){
|
234
|
if(checkMediaSpecimen()){
|
235
|
MediaRepresentationPart part = findMediaRepresentationPart();
|
236
|
if(part != null){
|
237
|
return part.getUri();
|
238
|
}
|
239
|
}
|
240
|
return null;
|
241
|
}
|
242
|
|
243
|
public void setMediaUri(URI mediaUri){
|
244
|
if(checkMediaSpecimen()){
|
245
|
MediaRepresentationPart part = findOrMakeMediaRepresentationPart();
|
246
|
part.setUri(mediaUri);
|
247
|
}
|
248
|
}
|
249
|
|
250
|
public Reference getMediaSpecimenReference(){
|
251
|
if(checkMediaSpecimen()){
|
252
|
IdentifiableSource source = findMediaSpecimenIdentifieableSource();
|
253
|
if(source != null){
|
254
|
return source.getCitation();
|
255
|
}
|
256
|
}
|
257
|
return null;
|
258
|
}
|
259
|
|
260
|
public void setMediaSpecimenReference(Reference reference){
|
261
|
if(checkMediaSpecimen()){
|
262
|
IdentifiableSource source = findOrMakeMediaSpecimenIdentifieableSource();
|
263
|
source.setCitation(reference);
|
264
|
}
|
265
|
}
|
266
|
|
267
|
public String getMediaSpecimenReferenceDetail(){
|
268
|
if(checkMediaSpecimen()){
|
269
|
IdentifiableSource source = findMediaSpecimenIdentifieableSource();
|
270
|
if(source != null){
|
271
|
return source.getCitationMicroReference();
|
272
|
}
|
273
|
}
|
274
|
return null;
|
275
|
}
|
276
|
|
277
|
public void setMediaSpecimenReferenceDetail(String referenceDetail){
|
278
|
if(checkMediaSpecimen()){
|
279
|
IdentifiableSource source = findOrMakeMediaSpecimenIdentifieableSource();
|
280
|
source.setCitationMicroReference(referenceDetail);
|
281
|
}
|
282
|
}
|
283
|
|
284
|
/**
|
285
|
* @return
|
286
|
*/
|
287
|
private IdentifiableSource findMediaSpecimenIdentifieableSource() {
|
288
|
IdentifiableSource source = null;
|
289
|
Media media = findMediaSpecimen().getMediaSpecimen();
|
290
|
if(media != null){
|
291
|
// FIXME use marker type to tag the MediaSpecimenReference and make sure that there is always only one.
|
292
|
if(!CollectionUtils.isEmpty(media.getSources())){
|
293
|
source = media.getSources().iterator().next();
|
294
|
}
|
295
|
}
|
296
|
return source;
|
297
|
}
|
298
|
|
299
|
/**
|
300
|
*
|
301
|
*/
|
302
|
private MediaRepresentationPart findMediaRepresentationPart() {
|
303
|
|
304
|
Media media = findMediaSpecimen().getMediaSpecimen();
|
305
|
if(media != null){
|
306
|
if(!CollectionUtils.isEmpty(media.getRepresentations())){
|
307
|
MediaRepresentation repr = media.getRepresentations().iterator().next();
|
308
|
if(!CollectionUtils.isEmpty(repr.getParts())) {
|
309
|
MediaRepresentationPart part = repr.getParts().iterator().next();
|
310
|
return part;
|
311
|
}
|
312
|
}
|
313
|
}
|
314
|
return null;
|
315
|
}
|
316
|
|
317
|
private MediaRepresentationPart findOrMakeMediaRepresentationPart() {
|
318
|
|
319
|
if(findMediaSpecimen().getMediaSpecimen() == null){
|
320
|
findMediaSpecimen().setMediaSpecimen(Media.NewInstance());
|
321
|
}
|
322
|
Media media = findMediaSpecimen().getMediaSpecimen();
|
323
|
if(CollectionUtils.isEmpty(media.getRepresentations())){
|
324
|
media.addRepresentation(MediaRepresentation.NewInstance());
|
325
|
}
|
326
|
MediaRepresentation repr = media.getRepresentations().iterator().next();
|
327
|
if(CollectionUtils.isEmpty(repr.getParts())) {
|
328
|
repr.addRepresentationPart(MediaRepresentationPart.NewInstance(null, null));
|
329
|
}
|
330
|
MediaRepresentationPart part = repr.getParts().iterator().next();
|
331
|
|
332
|
return part;
|
333
|
}
|
334
|
|
335
|
/**
|
336
|
* @return
|
337
|
*/
|
338
|
private IdentifiableSource findOrMakeMediaSpecimenIdentifieableSource() {
|
339
|
IdentifiableSource source ;
|
340
|
Media media = findMediaSpecimen().getMediaSpecimen();
|
341
|
if(media == null){
|
342
|
media = Media.NewInstance();
|
343
|
findMediaSpecimen().setMediaSpecimen(Media.NewInstance());
|
344
|
}
|
345
|
if(CollectionUtils.isEmpty(media.getSources())){
|
346
|
source = IdentifiableSource.NewPrimaryMediaSourceInstance(null, null);
|
347
|
media.addSource(source);
|
348
|
} else {
|
349
|
source = media.getSources().iterator().next();
|
350
|
}
|
351
|
return source;
|
352
|
}
|
353
|
|
354
|
/**
|
355
|
* @return
|
356
|
*/
|
357
|
public boolean checkMediaSpecimen() {
|
358
|
return findMediaSpecimen() != null;
|
359
|
}
|
360
|
|
361
|
/**
|
362
|
* @return
|
363
|
*/
|
364
|
private MediaSpecimen findMediaSpecimen() {
|
365
|
DerivedUnit sp = HibernateProxyHelper.deproxy(std.getTypeSpecimen());
|
366
|
if(MediaSpecimen.class.isAssignableFrom(sp.getClass())){
|
367
|
return (MediaSpecimen) sp;
|
368
|
}
|
369
|
return null;
|
370
|
}
|
371
|
|
372
|
}
|