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