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.util.converter;
|
10
|
|
11
|
import java.util.ArrayList;
|
12
|
import java.util.Arrays;
|
13
|
import java.util.Collection;
|
14
|
import java.util.Collections;
|
15
|
import java.util.Comparator;
|
16
|
import java.util.HashMap;
|
17
|
import java.util.LinkedHashMap;
|
18
|
import java.util.LinkedList;
|
19
|
import java.util.List;
|
20
|
import java.util.Map;
|
21
|
import java.util.Optional;
|
22
|
import java.util.Set;
|
23
|
|
24
|
import org.hibernate.search.hcore.util.impl.HibernateHelper;
|
25
|
|
26
|
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeCacheStrategy;
|
27
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
28
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
29
|
import eu.etaxonomy.cdm.model.common.TermVocabulary;
|
30
|
import eu.etaxonomy.cdm.model.name.NameTypeDesignation;
|
31
|
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
|
32
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
33
|
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
|
34
|
import eu.etaxonomy.cdm.model.name.TypeDesignationStatusBase;
|
35
|
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
|
36
|
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
|
37
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
38
|
import eu.etaxonomy.cdm.vaadin.model.EntityReference;
|
39
|
import eu.etaxonomy.cdm.vaadin.model.TypedEntityReference;
|
40
|
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationValidationException;
|
41
|
|
42
|
/**
|
43
|
* Manages a collection of {@link TypeDesignationBase TypeDesignations} for the same typified name.
|
44
|
*
|
45
|
* Type designations are ordered by the base type which is a {@link TaxonName} for {@link NameTypeDesignation NameTypeDesignations} or
|
46
|
* in case of {@link SpecimenTypeDesignation SpecimenTypeDesignations} the associate {@link FieldUnit} or the {@link DerivedUnit}
|
47
|
* if the former is missing. The type designations per base type are furthermore ordered by the {@link TypeDesignationStatusBase}.
|
48
|
*
|
49
|
* The TypeDesignationSetManager also provides string representations of the whole ordered set of all
|
50
|
* {@link TypeDesignationBase TypeDesignations} and of the TypeDesignationWorkingSets:
|
51
|
* <ul>
|
52
|
* <li>{@link #print()})
|
53
|
* <li>{@link #getOrderdTypeDesignationWorkingSets()} ... {@link TypeDesignationWorkingSet#getRepresentation()}
|
54
|
* </ul>
|
55
|
* Prior using the representations you need to trigger their generation by calling {@link #buildString()}
|
56
|
*
|
57
|
* @author a.kohlbecker
|
58
|
* @since Mar 10, 2017
|
59
|
*
|
60
|
*/
|
61
|
public class TypeDesignationSetManager {
|
62
|
|
63
|
|
64
|
private static final String TYPE_STATUS_SEPARATOR = "; ";
|
65
|
|
66
|
private static final String TYPE_SEPARATOR = "; ";
|
67
|
|
68
|
private static final String TYPE_DESIGNATION_SEPARATOR = ", ";
|
69
|
|
70
|
private Collection<TypeDesignationBase> typeDesignations;
|
71
|
|
72
|
/**
|
73
|
* Groups the EntityReferences for each of the TypeDesignations by the according TypeDesignationStatus.
|
74
|
* The TypeDesignationStatusBase keys are already ordered by the term order defined in the vocabulary.
|
75
|
*/
|
76
|
private LinkedHashMap<TypedEntityReference, TypeDesignationWorkingSet> orderedByTypesByBaseEntity;
|
77
|
|
78
|
private EntityReference typifiedNameRef;
|
79
|
|
80
|
private TaxonName typifiedName;
|
81
|
|
82
|
private String finalString = null;
|
83
|
|
84
|
final NullTypeDesignationStatus NULL_STATUS = new NullTypeDesignationStatus();
|
85
|
|
86
|
private List<String> probelms = new ArrayList<>();
|
87
|
|
88
|
private boolean printCitation = false;
|
89
|
|
90
|
/**
|
91
|
* @param containgEntity
|
92
|
* @param taxonName
|
93
|
* @throws RegistrationValidationException
|
94
|
*
|
95
|
*/
|
96
|
public TypeDesignationSetManager(Collection<TypeDesignationBase> typeDesignations) throws RegistrationValidationException {
|
97
|
this.typeDesignations = typeDesignations;
|
98
|
findTypifiedName();
|
99
|
mapAndSort();
|
100
|
}
|
101
|
|
102
|
/**
|
103
|
* @param typifiedName2
|
104
|
*/
|
105
|
public TypeDesignationSetManager(TaxonName typifiedName) {
|
106
|
this.typeDesignations = new ArrayList<>();
|
107
|
this.typifiedNameRef = new EntityReference(typifiedName.getId(), typifiedName.getTitleCache());
|
108
|
}
|
109
|
|
110
|
/**
|
111
|
* Add one or more TypeDesignations to the manager. This causes re-grouping and re-ordering
|
112
|
* of all managed TypeDesignations.
|
113
|
*
|
114
|
* @param containgEntity
|
115
|
* @param typeDesignations
|
116
|
*/
|
117
|
public void addTypeDesigations(CdmBase containgEntity, TypeDesignationBase ... typeDesignations){
|
118
|
this.typeDesignations.addAll(Arrays.asList(typeDesignations));
|
119
|
mapAndSort();
|
120
|
}
|
121
|
|
122
|
/**
|
123
|
* Groups and orders all managed TypeDesignations.
|
124
|
*
|
125
|
* @param containgEntity
|
126
|
*/
|
127
|
protected void mapAndSort() {
|
128
|
finalString = null;
|
129
|
Map<TypedEntityReference, TypeDesignationWorkingSet> byBaseEntityByTypeStatus = new HashMap<>();
|
130
|
this.typeDesignations.forEach(td -> mapTypeDesignation(byBaseEntityByTypeStatus, td));
|
131
|
orderedByTypesByBaseEntity = orderByTypeByBaseEntity(byBaseEntityByTypeStatus);
|
132
|
}
|
133
|
|
134
|
|
135
|
/**
|
136
|
*
|
137
|
* @param containgEntity
|
138
|
* @param byBaseEntityByTypeStatus
|
139
|
* @param td
|
140
|
*/
|
141
|
private void mapTypeDesignation(Map<TypedEntityReference, TypeDesignationWorkingSet> byBaseEntityByTypeStatus,
|
142
|
TypeDesignationBase<?> td){
|
143
|
|
144
|
TypeDesignationStatusBase<?> status = td.getTypeStatus();
|
145
|
|
146
|
try {
|
147
|
final IdentifiableEntity<?> baseEntity = baseEntity(td);
|
148
|
final TypedEntityReference<IdentifiableEntity<?>> baseEntityReference = makeEntityReference(baseEntity);
|
149
|
|
150
|
EntityReference typeDesignationEntityReference = new EntityReference(td.getId(), stringify(td));
|
151
|
|
152
|
TypeDesignationWorkingSet typedesignationWorkingSet;
|
153
|
if(!byBaseEntityByTypeStatus.containsKey(baseEntityReference)){
|
154
|
byBaseEntityByTypeStatus.put(baseEntityReference, new TypeDesignationWorkingSet(baseEntity, baseEntityReference));
|
155
|
}
|
156
|
|
157
|
typedesignationWorkingSet = byBaseEntityByTypeStatus.get(baseEntityReference);
|
158
|
typedesignationWorkingSet.insert(status, typeDesignationEntityReference);
|
159
|
} catch (DataIntegrityException e){
|
160
|
probelms.add(e.getMessage());
|
161
|
}
|
162
|
}
|
163
|
|
164
|
/**
|
165
|
* @param td
|
166
|
* @return
|
167
|
* @throws DataIntegrityException
|
168
|
*/
|
169
|
protected IdentifiableEntity<?> baseEntity(TypeDesignationBase<?> td) throws DataIntegrityException {
|
170
|
|
171
|
IdentifiableEntity<?> baseEntity = null;
|
172
|
if(td instanceof SpecimenTypeDesignation){
|
173
|
SpecimenTypeDesignation std = (SpecimenTypeDesignation) td;
|
174
|
FieldUnit fu = findFieldUnit(std);
|
175
|
if(fu != null){
|
176
|
baseEntity = fu;
|
177
|
} else if(((SpecimenTypeDesignation) td).getTypeSpecimen() != null){
|
178
|
baseEntity = ((SpecimenTypeDesignation) td).getTypeSpecimen();
|
179
|
}
|
180
|
} else if(td instanceof NameTypeDesignation){
|
181
|
baseEntity = ((NameTypeDesignation)td).getTypeName();
|
182
|
}
|
183
|
if(baseEntity == null) {
|
184
|
throw new DataIntegrityException("Incomplete TypeDesignation, no type missin in " + td.toString());
|
185
|
}
|
186
|
return baseEntity;
|
187
|
}
|
188
|
|
189
|
/**
|
190
|
* @param td
|
191
|
* @return
|
192
|
*/
|
193
|
protected TypedEntityReference<IdentifiableEntity<?>> makeEntityReference(IdentifiableEntity<?> baseEntity) {
|
194
|
|
195
|
baseEntity = (IdentifiableEntity<?>) HibernateHelper.unproxy(baseEntity);
|
196
|
String label = "";
|
197
|
if(baseEntity instanceof FieldUnit){
|
198
|
label = ((FieldUnit)baseEntity).getTitleCache();
|
199
|
}
|
200
|
|
201
|
TypedEntityReference<IdentifiableEntity<?>> baseEntityReference = new TypedEntityReference(baseEntity.getClass(), baseEntity.getId(), label);
|
202
|
|
203
|
return baseEntityReference;
|
204
|
}
|
205
|
|
206
|
|
207
|
private LinkedHashMap<TypedEntityReference, TypeDesignationWorkingSet> orderByTypeByBaseEntity(
|
208
|
Map<TypedEntityReference, TypeDesignationWorkingSet> stringsByTypeByBaseEntity){
|
209
|
|
210
|
// order the FieldUnit TypeName keys
|
211
|
List<TypedEntityReference> baseEntityKeyList = new LinkedList<>(stringsByTypeByBaseEntity.keySet());
|
212
|
Collections.sort(baseEntityKeyList, new Comparator<TypedEntityReference>(){
|
213
|
/**
|
214
|
* Sorts the base entities (TypedEntityReference) in the following order:
|
215
|
*
|
216
|
* 1. FieldUnits
|
217
|
* 2. DerivedUnit (in case of missing FieldUnit we expect the base type to be DerivedUnit)
|
218
|
* 3. NameType
|
219
|
*
|
220
|
* {@inheritDoc}
|
221
|
*/
|
222
|
@Override
|
223
|
public int compare(TypedEntityReference o1, TypedEntityReference o2) {
|
224
|
|
225
|
Class type1 = o1.getType();
|
226
|
Class type2 = o2.getType();
|
227
|
|
228
|
if(!type1.equals(type2)) {
|
229
|
if(type1.equals(FieldUnit.class) || type2.equals(FieldUnit.class)){
|
230
|
// FieldUnits first
|
231
|
return type1.equals(FieldUnit.class) ? -1 : 1;
|
232
|
} else {
|
233
|
// name types last (in case of missing FieldUnit we expect the base type to be DerivedUnit which comes into the middle)
|
234
|
return type2.equals(TaxonName.class) ? -1 : 1;
|
235
|
}
|
236
|
} else {
|
237
|
return o1.getLabel().compareTo(o2.getLabel());
|
238
|
}
|
239
|
}});
|
240
|
|
241
|
// new LinkedHashMap for the ordered FieldUnitOrTypeName keys
|
242
|
LinkedHashMap<TypedEntityReference, TypeDesignationWorkingSet> stringsOrderedbyBaseEntityOrderdByType = new LinkedHashMap<>(stringsByTypeByBaseEntity.size());
|
243
|
|
244
|
for(TypedEntityReference baseEntityRef : baseEntityKeyList){
|
245
|
|
246
|
TypeDesignationWorkingSet typeDesignationWorkingSet = stringsByTypeByBaseEntity.get(baseEntityRef);
|
247
|
// order the TypeDesignationStatusBase keys
|
248
|
List<TypeDesignationStatusBase<?>> keyList = new LinkedList<>(typeDesignationWorkingSet.keySet());
|
249
|
Collections.sort(keyList, new Comparator<TypeDesignationStatusBase>() {
|
250
|
@SuppressWarnings("unchecked")
|
251
|
@Override
|
252
|
public int compare(TypeDesignationStatusBase o1, TypeDesignationStatusBase o2) {
|
253
|
// fix inverted order of cdm terms by -1*
|
254
|
if(o1 == null && o2 == null || o1 instanceof NullTypeDesignationStatus && o2 instanceof NullTypeDesignationStatus){
|
255
|
return 0;
|
256
|
}
|
257
|
if(o1 == null || o1 instanceof NullTypeDesignationStatus){
|
258
|
return -1;
|
259
|
}
|
260
|
|
261
|
if(o2 == null || o2 instanceof NullTypeDesignationStatus){
|
262
|
return 1;
|
263
|
}
|
264
|
return -1 * o1.compareTo(o2);
|
265
|
}
|
266
|
});
|
267
|
// new LinkedHashMap for the ordered TypeDesignationStatusBase keys
|
268
|
TypeDesignationWorkingSet orderedStringsByOrderedTypes = new TypeDesignationWorkingSet(
|
269
|
typeDesignationWorkingSet.getBaseEntity(),
|
270
|
baseEntityRef);
|
271
|
keyList.forEach(key -> orderedStringsByOrderedTypes.put(key, typeDesignationWorkingSet.get(key)));
|
272
|
stringsOrderedbyBaseEntityOrderdByType.put(baseEntityRef, orderedStringsByOrderedTypes);
|
273
|
}
|
274
|
|
275
|
return stringsOrderedbyBaseEntityOrderdByType;
|
276
|
}
|
277
|
|
278
|
/*
|
279
|
private LinkedHashMap<TypedEntityReference, LinkedHashMap<String, Collection<EntityReference>>> buildOrderedRepresentations(){
|
280
|
|
281
|
orderedStringsByOrderedTypes.keySet().forEach(
|
282
|
key -> orderedRepresentations.put(
|
283
|
getTypeDesignationStytusLabel(key),
|
284
|
orderedStringsByOrderedTypes.get(key))
|
285
|
);
|
286
|
return orderedRepresentations;
|
287
|
}
|
288
|
*/
|
289
|
|
290
|
public TypeDesignationSetManager buildString(){
|
291
|
|
292
|
if(finalString == null){
|
293
|
|
294
|
finalString = "";
|
295
|
if(getTypifiedNameCache() != null){
|
296
|
finalString += getTypifiedNameCache() + " ";
|
297
|
}
|
298
|
|
299
|
int typeCount = 0;
|
300
|
if(orderedByTypesByBaseEntity != null){
|
301
|
for(TypedEntityReference baseEntityRef : orderedByTypesByBaseEntity.keySet()) {
|
302
|
StringBuilder sb = new StringBuilder();
|
303
|
if(typeCount++ > 0){
|
304
|
sb.append(TYPE_SEPARATOR);
|
305
|
}
|
306
|
boolean isNameTypeDesignation = false;
|
307
|
if(SpecimenOrObservationBase.class.isAssignableFrom(baseEntityRef.getType())){
|
308
|
sb.append("Type: ");
|
309
|
} else {
|
310
|
sb.append("NameType: ");
|
311
|
isNameTypeDesignation = true;
|
312
|
}
|
313
|
if(!baseEntityRef.getLabel().isEmpty()){
|
314
|
sb.append(baseEntityRef.getLabel()).append(" ");
|
315
|
}
|
316
|
TypeDesignationWorkingSet typeDesignationWorkingSet = orderedByTypesByBaseEntity.get(baseEntityRef);
|
317
|
if(!isNameTypeDesignation ){
|
318
|
sb.append("(");
|
319
|
}
|
320
|
int typeStatusCount = 0;
|
321
|
for(TypeDesignationStatusBase<?> typeStatus : typeDesignationWorkingSet.keySet()) {
|
322
|
if(typeStatusCount++ > 0){
|
323
|
sb.append(TYPE_STATUS_SEPARATOR);
|
324
|
}
|
325
|
boolean isPlural = typeDesignationWorkingSet.get(typeStatus).size() > 1;
|
326
|
if(!typeStatus.equals(NULL_STATUS)) {
|
327
|
sb.append(typeStatus.getLabel());
|
328
|
if(isPlural){
|
329
|
sb.append("s: ");
|
330
|
} else {
|
331
|
sb.append(", ");
|
332
|
}
|
333
|
}
|
334
|
int typeDesignationCount = 0;
|
335
|
for(EntityReference typeDesignationEntityReference : typeDesignationWorkingSet.get(typeStatus)) {
|
336
|
if(typeDesignationCount++ > 0){
|
337
|
sb.append(TYPE_DESIGNATION_SEPARATOR);
|
338
|
}
|
339
|
sb.append(typeDesignationEntityReference.getLabel());
|
340
|
}
|
341
|
}
|
342
|
if(!isNameTypeDesignation ){
|
343
|
sb.append(")");
|
344
|
}
|
345
|
typeDesignationWorkingSet.setRepresentation(sb.toString());
|
346
|
finalString += typeDesignationWorkingSet.getRepresentation();
|
347
|
}
|
348
|
}
|
349
|
}
|
350
|
return this;
|
351
|
}
|
352
|
|
353
|
/**
|
354
|
* FIXME use the validation framework validators and to store the validation problems!!!
|
355
|
*
|
356
|
* @return
|
357
|
* @throws RegistrationValidationException
|
358
|
*/
|
359
|
private void findTypifiedName() throws RegistrationValidationException {
|
360
|
|
361
|
List<String> problems = new ArrayList<>();
|
362
|
|
363
|
TaxonName typifiedName = null;
|
364
|
|
365
|
for(TypeDesignationBase<?> typeDesignation : typeDesignations){
|
366
|
typeDesignation.getTypifiedNames();
|
367
|
if(typeDesignation.getTypifiedNames().isEmpty()){
|
368
|
|
369
|
//TODO instead throw RegistrationValidationException()
|
370
|
problems.add("Missing typifiedName in " + typeDesignation.toString());
|
371
|
continue;
|
372
|
}
|
373
|
if(typeDesignation.getTypifiedNames().size() > 1){
|
374
|
//TODO instead throw RegistrationValidationException()
|
375
|
problems.add("Multiple typifiedName in " + typeDesignation.toString());
|
376
|
continue;
|
377
|
}
|
378
|
if(typifiedName == null){
|
379
|
// remember
|
380
|
typifiedName = typeDesignation.getTypifiedNames().iterator().next();
|
381
|
} else {
|
382
|
// compare
|
383
|
TaxonName otherTypifiedName = typeDesignation.getTypifiedNames().iterator().next();
|
384
|
if(typifiedName.getId() != otherTypifiedName.getId()){
|
385
|
//TODO instead throw RegistrationValidationException()
|
386
|
problems.add("Multiple typifiedName in " + typeDesignation.toString());
|
387
|
}
|
388
|
}
|
389
|
|
390
|
}
|
391
|
if(!problems.isEmpty()){
|
392
|
// FIXME use the validation framework
|
393
|
throw new RegistrationValidationException("Inconsistent type designations", problems);
|
394
|
}
|
395
|
|
396
|
if(typifiedName != null){
|
397
|
// ON SUCCESS -------------------
|
398
|
this.typifiedName = typifiedName;
|
399
|
this.typifiedNameRef = new EntityReference(typifiedName.getId(), typifiedName.getTitleCache());
|
400
|
|
401
|
}
|
402
|
}
|
403
|
|
404
|
|
405
|
/**
|
406
|
* @return the title cache of the typifying name or <code>null</code>
|
407
|
*/
|
408
|
public String getTypifiedNameCache() {
|
409
|
if(typifiedNameRef != null){
|
410
|
return typifiedNameRef.getLabel();
|
411
|
}
|
412
|
return null;
|
413
|
}
|
414
|
|
415
|
/**
|
416
|
* @return the title cache of the typifying name or <code>null</code>
|
417
|
*/
|
418
|
public EntityReference getTypifiedNameRef() {
|
419
|
|
420
|
return typifiedNameRef;
|
421
|
}
|
422
|
|
423
|
/**
|
424
|
* @return
|
425
|
*/
|
426
|
public Collection<TypeDesignationBase> getTypeDesignations() {
|
427
|
return typeDesignations;
|
428
|
}
|
429
|
|
430
|
/**
|
431
|
* @param ref
|
432
|
* @return
|
433
|
*/
|
434
|
public TypeDesignationBase findTypeDesignation(EntityReference typeDesignationRef) {
|
435
|
for(TypeDesignationBase td : typeDesignations){
|
436
|
if(td.getId() == typeDesignationRef.getId()){
|
437
|
return td;
|
438
|
}
|
439
|
}
|
440
|
// TODO Auto-generated method stub
|
441
|
return null;
|
442
|
}
|
443
|
|
444
|
|
445
|
public LinkedHashMap<TypedEntityReference, TypeDesignationWorkingSet> getOrderdTypeDesignationWorkingSets() {
|
446
|
return orderedByTypesByBaseEntity;
|
447
|
}
|
448
|
|
449
|
/**
|
450
|
* @param td
|
451
|
* @return
|
452
|
*/
|
453
|
private String stringify(TypeDesignationBase td) {
|
454
|
|
455
|
if(td instanceof NameTypeDesignation){
|
456
|
return stringify((NameTypeDesignation)td);
|
457
|
} else {
|
458
|
return stringify((SpecimenTypeDesignation)td, false);
|
459
|
}
|
460
|
}
|
461
|
|
462
|
|
463
|
/**
|
464
|
* @param td
|
465
|
* @return
|
466
|
*/
|
467
|
protected String stringify(NameTypeDesignation td) {
|
468
|
|
469
|
StringBuffer sb = new StringBuffer();
|
470
|
|
471
|
if(td.getTypeName() != null){
|
472
|
sb.append(td.getTypeName().getTitleCache());
|
473
|
}
|
474
|
if(td.getCitation() != null){
|
475
|
sb.append(" ").append(td.getCitation().getTitleCache());
|
476
|
if(td.getCitationMicroReference() != null){
|
477
|
sb.append(":").append(td.getCitationMicroReference());
|
478
|
}
|
479
|
}
|
480
|
if(td.isNotDesignated()){
|
481
|
sb.append(" not designated");
|
482
|
}
|
483
|
if(td.isRejectedType()){
|
484
|
sb.append(" rejected");
|
485
|
}
|
486
|
if(td.isConservedType()){
|
487
|
sb.append(" conserved");
|
488
|
}
|
489
|
return sb.toString();
|
490
|
}
|
491
|
|
492
|
/**
|
493
|
* @param td
|
494
|
* @return
|
495
|
*/
|
496
|
private String stringify(SpecimenTypeDesignation td, boolean useFullTitleCache) {
|
497
|
String result = "";
|
498
|
|
499
|
if(useFullTitleCache){
|
500
|
if(td.getTypeSpecimen() != null){
|
501
|
String nameTitleCache = td.getTypeSpecimen().getTitleCache();
|
502
|
if(getTypifiedNameCache() != null){
|
503
|
nameTitleCache = nameTitleCache.replace(getTypifiedNameCache(), "");
|
504
|
}
|
505
|
result += nameTitleCache;
|
506
|
}
|
507
|
} else {
|
508
|
if(td.getTypeSpecimen() != null){
|
509
|
DerivedUnit du = td.getTypeSpecimen();
|
510
|
if(du.isProtectedTitleCache()){
|
511
|
result += du.getTitleCache();
|
512
|
} else {
|
513
|
DerivedUnitFacadeCacheStrategy cacheStrategy = new DerivedUnitFacadeCacheStrategy();
|
514
|
result += cacheStrategy.getTitleCache(du, true);
|
515
|
}
|
516
|
}
|
517
|
}
|
518
|
|
519
|
if(isPrintCitation() && td.getCitation() != null){
|
520
|
if(td.getCitation().getAbbrevTitle() != null){
|
521
|
result += " " + td.getCitation().getAbbrevTitle();
|
522
|
} else {
|
523
|
result += " " + td.getCitation().getTitleCache();
|
524
|
}
|
525
|
if(td.getCitationMicroReference() != null){
|
526
|
result += " :" + td.getCitationMicroReference();
|
527
|
}
|
528
|
}
|
529
|
if(td.isNotDesignated()){
|
530
|
result += " not designated";
|
531
|
}
|
532
|
|
533
|
return result;
|
534
|
}
|
535
|
|
536
|
/**
|
537
|
* @param td
|
538
|
* @return
|
539
|
* @deprecated
|
540
|
*/
|
541
|
@Deprecated
|
542
|
private FieldUnit findFieldUnit(SpecimenTypeDesignation td) {
|
543
|
|
544
|
DerivedUnit du = td.getTypeSpecimen();
|
545
|
return findFieldUnit(du);
|
546
|
}
|
547
|
|
548
|
private FieldUnit findFieldUnit(DerivedUnit du) {
|
549
|
|
550
|
if(du == null || du.getOriginals() == null){
|
551
|
return null;
|
552
|
}
|
553
|
@SuppressWarnings("rawtypes")
|
554
|
Set<SpecimenOrObservationBase> originals = du.getDerivedFrom().getOriginals();
|
555
|
@SuppressWarnings("rawtypes")
|
556
|
Optional<SpecimenOrObservationBase> fieldUnit = originals.stream()
|
557
|
.filter(original -> original instanceof FieldUnit).findFirst();
|
558
|
if (fieldUnit.isPresent()) {
|
559
|
return (FieldUnit) fieldUnit.get();
|
560
|
} else {
|
561
|
for (@SuppressWarnings("rawtypes")
|
562
|
SpecimenOrObservationBase sob : originals) {
|
563
|
if (sob instanceof DerivedUnit) {
|
564
|
FieldUnit fu = findFieldUnit((DerivedUnit) sob);
|
565
|
if (fu != null) {
|
566
|
return fu;
|
567
|
}
|
568
|
}
|
569
|
}
|
570
|
}
|
571
|
|
572
|
return null;
|
573
|
}
|
574
|
|
575
|
public String print() {
|
576
|
return finalString.trim();
|
577
|
}
|
578
|
|
579
|
/**
|
580
|
* @return the printCitation
|
581
|
*/
|
582
|
public boolean isPrintCitation() {
|
583
|
return printCitation;
|
584
|
}
|
585
|
|
586
|
/**
|
587
|
* @param printCitation the printCitation to set
|
588
|
*/
|
589
|
public void setPrintCitation(boolean printCitation) {
|
590
|
this.printCitation = printCitation;
|
591
|
}
|
592
|
|
593
|
/**
|
594
|
* @return the typifiedName
|
595
|
*/
|
596
|
public TaxonName getTypifiedName() {
|
597
|
return typifiedName;
|
598
|
}
|
599
|
|
600
|
/**
|
601
|
* TypeDesignations which refer to the same FieldUnit (SpecimenTypeDesignation) or TaxonName
|
602
|
* (NameTypeDesignation) form a working set. The <code>TypeDesignationWorkingSet</code> internally
|
603
|
* works with EnityReferences to the actual TypeDesignations.
|
604
|
*
|
605
|
* The EntityReferences for TypeDesignations are grouped by the according TypeDesignationStatus.
|
606
|
* The TypeDesignationStatusBase keys can be ordered by the term order defined in the vocabulary.
|
607
|
*
|
608
|
* A workingset can be referenced by the <code>baseEntityReference</code>.
|
609
|
*/
|
610
|
public class TypeDesignationWorkingSet extends LinkedHashMap<TypeDesignationStatusBase<?>, Collection<EntityReference>> {
|
611
|
|
612
|
private static final long serialVersionUID = -1329007606500890729L;
|
613
|
|
614
|
String workingSetRepresentation = null;
|
615
|
|
616
|
TypedEntityReference<IdentifiableEntity<?>> baseEntityReference;
|
617
|
|
618
|
IdentifiableEntity<?> baseEntity;
|
619
|
|
620
|
List<DerivedUnit> derivedUnits = null;
|
621
|
|
622
|
/**
|
623
|
* @param baseEntityReference
|
624
|
*/
|
625
|
public TypeDesignationWorkingSet(IdentifiableEntity<?> baseEntity, TypedEntityReference<IdentifiableEntity<?>> baseEntityReference) {
|
626
|
this.baseEntity = baseEntity;
|
627
|
this.baseEntityReference = baseEntityReference;
|
628
|
}
|
629
|
|
630
|
/**
|
631
|
* @return
|
632
|
*/
|
633
|
public IdentifiableEntity<?> getBaseEntity() {
|
634
|
return baseEntity;
|
635
|
}
|
636
|
|
637
|
public List<EntityReference> getTypeDesignations() {
|
638
|
List<EntityReference> typeDesignations = new ArrayList<>();
|
639
|
this.values().forEach(typeDesignationReferences -> typeDesignationReferences.forEach(td -> typeDesignations.add(td)));
|
640
|
return typeDesignations;
|
641
|
}
|
642
|
|
643
|
/**
|
644
|
* @param status
|
645
|
* @param typeDesignationEntityReference
|
646
|
*/
|
647
|
public void insert(TypeDesignationStatusBase<?> status, EntityReference typeDesignationEntityReference) {
|
648
|
|
649
|
if(status == null){
|
650
|
status = NULL_STATUS;
|
651
|
}
|
652
|
if(!containsKey(status)){
|
653
|
put(status, new ArrayList<EntityReference>());
|
654
|
}
|
655
|
get(status).add(typeDesignationEntityReference);
|
656
|
}
|
657
|
|
658
|
|
659
|
public String getRepresentation() {
|
660
|
return workingSetRepresentation;
|
661
|
}
|
662
|
|
663
|
public void setRepresentation(String representation){
|
664
|
this.workingSetRepresentation = representation;
|
665
|
}
|
666
|
|
667
|
/**
|
668
|
* A reference to the entity which is the common base entity for all TypeDesignations in this workingset.
|
669
|
* For a {@link SpecimenTypeDesignation} this is usually the {@link FieldUnit} if it is present. Otherwise it can also be
|
670
|
* a {@link DerivedUnit} or something else depending on the specific use case.
|
671
|
*
|
672
|
* @return the baseEntityReference
|
673
|
*/
|
674
|
public TypedEntityReference<IdentifiableEntity<?>> getBaseEntityReference() {
|
675
|
return baseEntityReference;
|
676
|
}
|
677
|
|
678
|
@Override
|
679
|
public String toString(){
|
680
|
if(workingSetRepresentation != null){
|
681
|
return workingSetRepresentation;
|
682
|
} else {
|
683
|
return super.toString();
|
684
|
}
|
685
|
}
|
686
|
|
687
|
/**
|
688
|
* @return
|
689
|
*/
|
690
|
public boolean isSpecimenTypeDesigationWorkingSet() {
|
691
|
return SpecimenOrObservationBase.class.isAssignableFrom(baseEntityReference.getType());
|
692
|
}
|
693
|
|
694
|
public TypeDesignationWorkingSetType getWorkingsetType() {
|
695
|
return isSpecimenTypeDesigationWorkingSet() ? TypeDesignationWorkingSetType.SPECIMEN_TYPE_DESIGNATION_WORKINGSET : TypeDesignationWorkingSetType.NAME_TYPE_DESIGNATION_WORKINGSET;
|
696
|
}
|
697
|
|
698
|
}
|
699
|
|
700
|
public enum TypeDesignationWorkingSetType {
|
701
|
SPECIMEN_TYPE_DESIGNATION_WORKINGSET,
|
702
|
NAME_TYPE_DESIGNATION_WORKINGSET,
|
703
|
}
|
704
|
|
705
|
@SuppressWarnings({ "deprecation", "serial" })
|
706
|
class NullTypeDesignationStatus extends TypeDesignationStatusBase<NullTypeDesignationStatus>{
|
707
|
|
708
|
/**
|
709
|
* {@inheritDoc}
|
710
|
*/
|
711
|
@Override
|
712
|
public void resetTerms() {
|
713
|
// empty
|
714
|
|
715
|
}
|
716
|
|
717
|
/**
|
718
|
* {@inheritDoc}
|
719
|
*/
|
720
|
@Override
|
721
|
protected void setDefaultTerms(TermVocabulary<NullTypeDesignationStatus> termVocabulary) {
|
722
|
// empty
|
723
|
}
|
724
|
|
725
|
}
|
726
|
|
727
|
}
|