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.api.service.name;
|
10
|
|
11
|
import java.util.ArrayList;
|
12
|
import java.util.Collection;
|
13
|
import java.util.Collections;
|
14
|
import java.util.Comparator;
|
15
|
import java.util.HashMap;
|
16
|
import java.util.LinkedHashMap;
|
17
|
import java.util.LinkedList;
|
18
|
import java.util.List;
|
19
|
import java.util.Map;
|
20
|
import java.util.Optional;
|
21
|
import java.util.Set;
|
22
|
import java.util.UUID;
|
23
|
|
24
|
import org.apache.commons.lang3.StringUtils;
|
25
|
import org.hibernate.search.hcore.util.impl.HibernateHelper;
|
26
|
|
27
|
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeCacheStrategy;
|
28
|
import eu.etaxonomy.cdm.api.service.exception.RegistrationValidationException;
|
29
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
30
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
31
|
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
|
32
|
import eu.etaxonomy.cdm.model.common.TermVocabulary;
|
33
|
import eu.etaxonomy.cdm.model.common.VersionableEntity;
|
34
|
import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
|
35
|
import eu.etaxonomy.cdm.model.name.NameTypeDesignation;
|
36
|
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
|
37
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
38
|
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
|
39
|
import eu.etaxonomy.cdm.model.name.TypeDesignationStatusBase;
|
40
|
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
|
41
|
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
|
42
|
import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen;
|
43
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
44
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
45
|
import eu.etaxonomy.cdm.ref.EntityReference;
|
46
|
import eu.etaxonomy.cdm.ref.TypedEntityReference;
|
47
|
import eu.etaxonomy.cdm.strategy.cache.TagEnum;
|
48
|
import eu.etaxonomy.cdm.strategy.cache.TaggedText;
|
49
|
import eu.etaxonomy.cdm.strategy.cache.TaggedTextBuilder;
|
50
|
import eu.etaxonomy.cdm.strategy.cache.reference.DefaultReferenceCacheStrategy;
|
51
|
/**
|
52
|
* Manages a collection of {@link TypeDesignationBase TypeDesignations} for the same typified name.
|
53
|
*
|
54
|
* Type designations are ordered by the base type which is a {@link TaxonName} for {@link NameTypeDesignation NameTypeDesignations} or
|
55
|
* in case of {@link SpecimenTypeDesignation SpecimenTypeDesignations} the associate {@link FieldUnit} or the {@link DerivedUnit}
|
56
|
* if the former is missing. The type designations per base type are furthermore ordered by the {@link TypeDesignationStatusBase}.
|
57
|
*
|
58
|
* The TypeDesignationSetManager also provides string representations of the whole ordered set of all
|
59
|
* {@link TypeDesignationBase TypeDesignations} and of the TypeDesignationWorkingSets:
|
60
|
* <ul>
|
61
|
* <li>{@link #print()}
|
62
|
* <li>{@link #getOrderdTypeDesignationWorkingSets()} ... {@link TypeDesignationWorkingSet#getRepresentation()}
|
63
|
* </ul>
|
64
|
* Prior using the representations you need to trigger their generation by calling {@link #buildString()}
|
65
|
*
|
66
|
* @author a.kohlbecker
|
67
|
* @since Mar 10, 2017
|
68
|
*
|
69
|
*/
|
70
|
public class TypeDesignationSetManager {
|
71
|
|
72
|
enum NameTypeBaseEntityType{
|
73
|
|
74
|
NAME_TYPE_DESIGNATION,
|
75
|
TYPE_NAME;
|
76
|
|
77
|
}
|
78
|
|
79
|
private static final String TYPE_STATUS_SEPARATOR = "; ";
|
80
|
|
81
|
private static final String TYPE_SEPARATOR = "; ";
|
82
|
|
83
|
private static final String TYPE_DESIGNATION_SEPARATOR = ", ";
|
84
|
private static final String TYPE_STATUS_SEPARATOR_WITHCITATION = ": ";
|
85
|
private static final String TYPE_STATUS_PARENTHESIS_LEFT = " (";
|
86
|
private static final String TYPE_STATUS_PARENTHESIS_RIGHT = ")";
|
87
|
private static final String REFERENCE_PARENTHESIS_RIGHT = "]";
|
88
|
private static final String REFERENCE_PARENTHESIS_LEFT = " [";
|
89
|
private static final String REFERENCE_FIDE = "fide ";
|
90
|
|
91
|
private Map<UUID,TypeDesignationBase<?>> typeDesignations;
|
92
|
|
93
|
private NameTypeBaseEntityType nameTypeBaseEntityType = NameTypeBaseEntityType.NAME_TYPE_DESIGNATION;
|
94
|
|
95
|
/**
|
96
|
* Groups the EntityReferences for each of the TypeDesignations by the according TypeDesignationStatus.
|
97
|
* The TypeDesignationStatusBase keys are already ordered by the term order defined in the vocabulary.
|
98
|
*/
|
99
|
private LinkedHashMap<TypedEntityReference, TypeDesignationWorkingSet> orderedByTypesByBaseEntity;
|
100
|
|
101
|
private EntityReference typifiedNameRef;
|
102
|
|
103
|
private TaxonName typifiedName;
|
104
|
|
105
|
private String finalString = null;
|
106
|
|
107
|
final NullTypeDesignationStatus NULL_STATUS = new NullTypeDesignationStatus();
|
108
|
|
109
|
private List<String> problems = new ArrayList<>();
|
110
|
|
111
|
private boolean printCitation = false;
|
112
|
private boolean useShortCitation = false;
|
113
|
|
114
|
private List<TaggedText> taggedText;
|
115
|
|
116
|
/**
|
117
|
* @param containgEntity
|
118
|
* @param taxonName
|
119
|
* @throws RegistrationValidationException
|
120
|
*
|
121
|
*/
|
122
|
public TypeDesignationSetManager(Collection<TypeDesignationBase> typeDesignations) throws RegistrationValidationException {
|
123
|
if (this.typeDesignations == null){
|
124
|
this.typeDesignations = new HashMap();
|
125
|
}
|
126
|
for (TypeDesignationBase typeDes:typeDesignations){
|
127
|
this.typeDesignations.put(typeDes.getUuid(), typeDes);
|
128
|
}
|
129
|
findTypifiedName();
|
130
|
mapAndSort();
|
131
|
}
|
132
|
|
133
|
/**
|
134
|
* @param containgEntity
|
135
|
* @param taxonName
|
136
|
* @throws RegistrationValidationException
|
137
|
*
|
138
|
*/
|
139
|
public TypeDesignationSetManager(HomotypicalGroup group) throws RegistrationValidationException {
|
140
|
if (this.typeDesignations == null){
|
141
|
this.typeDesignations = new HashMap<>();
|
142
|
}
|
143
|
for (TypeDesignationBase<?> typeDes:group.getTypeDesignations()){
|
144
|
this.typeDesignations.put(typeDes.getUuid(), typeDes);
|
145
|
}
|
146
|
//findTypifiedName();
|
147
|
mapAndSort();
|
148
|
}
|
149
|
|
150
|
/**
|
151
|
* @param typifiedName2
|
152
|
*/
|
153
|
public TypeDesignationSetManager(TaxonName typifiedName) {
|
154
|
this.typeDesignations = new HashMap<>();
|
155
|
this.typifiedNameRef = new EntityReference(typifiedName.getUuid(), typifiedName.getTitleCache());
|
156
|
}
|
157
|
|
158
|
/**
|
159
|
* Add one or more TypeDesignations to the manager. This causes re-grouping and re-ordering
|
160
|
* of all managed TypeDesignations.
|
161
|
*
|
162
|
* @param containgEntity
|
163
|
* @param typeDesignations
|
164
|
*/
|
165
|
public void addTypeDesigations(CdmBase containgEntity, TypeDesignationBase ... typeDesignations){
|
166
|
for (TypeDesignationBase typeDes: typeDesignations){
|
167
|
this.typeDesignations.put(typeDes.getUuid(), typeDes);
|
168
|
}
|
169
|
mapAndSort();
|
170
|
}
|
171
|
|
172
|
/**
|
173
|
* Groups and orders all managed TypeDesignations.
|
174
|
*
|
175
|
* @param containgEntity
|
176
|
*/
|
177
|
protected void mapAndSort() {
|
178
|
finalString = null;
|
179
|
Map<TypedEntityReference<?>, TypeDesignationWorkingSet> byBaseEntityByTypeStatus = new HashMap<>();
|
180
|
|
181
|
this.typeDesignations.values().forEach(td -> mapTypeDesignation(byBaseEntityByTypeStatus, td));
|
182
|
orderedByTypesByBaseEntity = orderByTypeByBaseEntity(byBaseEntityByTypeStatus);
|
183
|
}
|
184
|
|
185
|
|
186
|
/**
|
187
|
* @param byBaseEntityByTypeStatus
|
188
|
* @param td
|
189
|
*/
|
190
|
private void mapTypeDesignation(Map<TypedEntityReference<?>, TypeDesignationWorkingSet> byBaseEntityByTypeStatus,
|
191
|
TypeDesignationBase<?> td){
|
192
|
|
193
|
TypeDesignationStatusBase status = td.getTypeStatus();
|
194
|
|
195
|
try {
|
196
|
final VersionableEntity baseEntity = baseEntity(td);
|
197
|
final TypedEntityReference<VersionableEntity> baseEntityReference = makeEntityReference(baseEntity);
|
198
|
|
199
|
TypedEntityReference<?> typeDesignationEntityReference = new TypedEntityReference<>(
|
200
|
HibernateProxyHelper.deproxy(td).getClass(),
|
201
|
td.getUuid(),
|
202
|
stringify(td));
|
203
|
|
204
|
if(!byBaseEntityByTypeStatus.containsKey(baseEntityReference)){
|
205
|
byBaseEntityByTypeStatus.put(baseEntityReference, new TypeDesignationWorkingSet(baseEntity, baseEntityReference));
|
206
|
}
|
207
|
byBaseEntityByTypeStatus.get(baseEntityReference).insert(status, typeDesignationEntityReference);
|
208
|
|
209
|
} catch (DataIntegrityException e){
|
210
|
problems.add(e.getMessage());
|
211
|
}
|
212
|
}
|
213
|
|
214
|
/**
|
215
|
* @param td
|
216
|
* @return
|
217
|
* @throws DataIntegrityException
|
218
|
*/
|
219
|
protected VersionableEntity baseEntity(TypeDesignationBase<?> td) throws DataIntegrityException {
|
220
|
|
221
|
VersionableEntity baseEntity = null;
|
222
|
if(td instanceof SpecimenTypeDesignation){
|
223
|
SpecimenTypeDesignation std = (SpecimenTypeDesignation) td;
|
224
|
FieldUnit fu = findFieldUnit(std);
|
225
|
if(fu != null){
|
226
|
baseEntity = fu;
|
227
|
} else if(((SpecimenTypeDesignation) td).getTypeSpecimen() != null){
|
228
|
baseEntity = ((SpecimenTypeDesignation) td).getTypeSpecimen();
|
229
|
}
|
230
|
} else if(td instanceof NameTypeDesignation){
|
231
|
if(nameTypeBaseEntityType == NameTypeBaseEntityType.NAME_TYPE_DESIGNATION){
|
232
|
baseEntity = td;
|
233
|
} else {
|
234
|
// only other option is TaxonName
|
235
|
baseEntity = ((NameTypeDesignation)td).getTypeName();
|
236
|
}
|
237
|
}
|
238
|
if(baseEntity == null) {
|
239
|
throw new DataIntegrityException("Incomplete TypeDesignation, no type missin in " + td.toString());
|
240
|
}
|
241
|
return baseEntity;
|
242
|
}
|
243
|
|
244
|
/**
|
245
|
* @param td
|
246
|
* @return
|
247
|
*/
|
248
|
protected TypedEntityReference<VersionableEntity> makeEntityReference(VersionableEntity baseEntity) {
|
249
|
|
250
|
baseEntity = (VersionableEntity) HibernateHelper.unproxy(baseEntity);
|
251
|
String label = "";
|
252
|
if(baseEntity instanceof FieldUnit){
|
253
|
label = ((FieldUnit)baseEntity).getTitleCache();
|
254
|
}
|
255
|
|
256
|
TypedEntityReference<VersionableEntity> baseEntityReference = new TypedEntityReference(baseEntity.getClass(), baseEntity.getUuid(), label);
|
257
|
|
258
|
return baseEntityReference;
|
259
|
}
|
260
|
|
261
|
|
262
|
private LinkedHashMap<TypedEntityReference, TypeDesignationWorkingSet> orderByTypeByBaseEntity(
|
263
|
Map<TypedEntityReference<?>, TypeDesignationWorkingSet> stringsByTypeByBaseEntity){
|
264
|
|
265
|
// order the FieldUnit TypeName keys
|
266
|
List<TypedEntityReference<?>> baseEntityKeyList = new LinkedList<>(stringsByTypeByBaseEntity.keySet());
|
267
|
Collections.sort(baseEntityKeyList, new Comparator<TypedEntityReference<?>>(){
|
268
|
/**
|
269
|
* Sorts the base entities (TypedEntityReference) in the following order:
|
270
|
*
|
271
|
* 1. FieldUnits
|
272
|
* 2. DerivedUnit (in case of missing FieldUnit we expect the base type to be DerivedUnit)
|
273
|
* 3. NameType
|
274
|
*
|
275
|
* {@inheritDoc}
|
276
|
*/
|
277
|
@Override
|
278
|
public int compare(TypedEntityReference<?> o1, TypedEntityReference<?> o2) {
|
279
|
|
280
|
Class<?> type1 = o1.getType();
|
281
|
Class<?> type2 = o2.getType();
|
282
|
|
283
|
if(!type1.equals(type2)) {
|
284
|
if(type1.equals(FieldUnit.class) || type2.equals(FieldUnit.class)){
|
285
|
// FieldUnits first
|
286
|
return type1.equals(FieldUnit.class) ? -1 : 1;
|
287
|
} else {
|
288
|
// name types last (in case of missing FieldUnit we expect the base type to be DerivedUnit which comes into the middle)
|
289
|
return type2.equals(TaxonName.class) || type2.equals(NameTypeDesignation.class) ? -1 : 1;
|
290
|
}
|
291
|
} else {
|
292
|
return o1.getLabel().compareTo(o2.getLabel());
|
293
|
}
|
294
|
}});
|
295
|
|
296
|
// new LinkedHashMap for the ordered FieldUnitOrTypeName keys
|
297
|
LinkedHashMap<TypedEntityReference, TypeDesignationWorkingSet> stringsOrderedbyBaseEntityOrderdByType = new LinkedHashMap<>(stringsByTypeByBaseEntity.size());
|
298
|
|
299
|
for(TypedEntityReference baseEntityRef : baseEntityKeyList){
|
300
|
|
301
|
TypeDesignationWorkingSet typeDesignationWorkingSet = stringsByTypeByBaseEntity.get(baseEntityRef);
|
302
|
// order the TypeDesignationStatusBase keys
|
303
|
List<TypeDesignationStatusBase<?>> keyList = new LinkedList<>(typeDesignationWorkingSet.keySet());
|
304
|
Collections.sort(keyList, new TypeDesignationStatusComparator());
|
305
|
// new LinkedHashMap for the ordered TypeDesignationStatusBase keys
|
306
|
TypeDesignationWorkingSet orderedStringsByOrderedTypes = new TypeDesignationWorkingSet(
|
307
|
typeDesignationWorkingSet.getBaseEntity(),
|
308
|
baseEntityRef);
|
309
|
keyList.forEach(key -> orderedStringsByOrderedTypes.put(key, typeDesignationWorkingSet.get(key)));
|
310
|
stringsOrderedbyBaseEntityOrderdByType.put(baseEntityRef, orderedStringsByOrderedTypes);
|
311
|
}
|
312
|
|
313
|
return stringsOrderedbyBaseEntityOrderdByType;
|
314
|
}
|
315
|
|
316
|
/*
|
317
|
private LinkedHashMap<TypedEntityReference, LinkedHashMap<String, Collection<EntityReference>>> buildOrderedRepresentations(){
|
318
|
|
319
|
orderedStringsByOrderedTypes.keySet().forEach(
|
320
|
key -> orderedRepresentations.put(
|
321
|
getTypeDesignationStytusLabel(key),
|
322
|
orderedStringsByOrderedTypes.get(key))
|
323
|
);
|
324
|
return orderedRepresentations;
|
325
|
}
|
326
|
*/
|
327
|
|
328
|
public void buildString(){
|
329
|
|
330
|
if(finalString == null){
|
331
|
|
332
|
TaggedTextBuilder finalBuilder = new TaggedTextBuilder();
|
333
|
finalString = "";
|
334
|
|
335
|
if(getTypifiedNameCache() != null){
|
336
|
finalString += getTypifiedNameCache() + " ";
|
337
|
finalBuilder.add(TagEnum.name, getTypifiedNameCache(), new TypedEntityReference<>(TaxonName.class, getTypifiedNameRef().getUuid()));
|
338
|
}
|
339
|
|
340
|
int typeCount = 0;
|
341
|
if(orderedByTypesByBaseEntity != null){
|
342
|
for(TypedEntityReference baseEntityRef : orderedByTypesByBaseEntity.keySet()) {
|
343
|
|
344
|
TaggedTextBuilder workingsetBuilder = new TaggedTextBuilder();
|
345
|
if(typeCount++ > 0){
|
346
|
workingsetBuilder.add(TagEnum.separator, TYPE_SEPARATOR);
|
347
|
}
|
348
|
boolean isNameTypeDesignation = false;
|
349
|
if(SpecimenOrObservationBase.class.isAssignableFrom(baseEntityRef.getType()) ){
|
350
|
workingsetBuilder.add(TagEnum.label, "Type:");
|
351
|
} else{
|
352
|
workingsetBuilder.add(TagEnum.label, "NameType:");
|
353
|
isNameTypeDesignation = true;
|
354
|
}
|
355
|
if(!baseEntityRef.getLabel().isEmpty()){
|
356
|
workingsetBuilder.add(TagEnum.specimenOrObservation, baseEntityRef.getLabel(), baseEntityRef);
|
357
|
}
|
358
|
TypeDesignationWorkingSet typeDesignationWorkingSet = orderedByTypesByBaseEntity.get(baseEntityRef);
|
359
|
int typeStatusCount = 0;
|
360
|
for(TypeDesignationStatusBase<?> typeStatus : typeDesignationWorkingSet.keySet()) {
|
361
|
if(typeStatusCount++ > 0){
|
362
|
workingsetBuilder.add(TagEnum.separator, TYPE_STATUS_SEPARATOR);
|
363
|
|
364
|
}
|
365
|
boolean isPlural = typeDesignationWorkingSet.get(typeStatus).size() > 1;
|
366
|
if(!typeStatus.equals(NULL_STATUS)) {
|
367
|
|
368
|
workingsetBuilder.add(TagEnum.label, typeStatus.getLabel() + (isPlural ? "s:" : ","));
|
369
|
}
|
370
|
|
371
|
|
372
|
int typeDesignationCount = 0;
|
373
|
for(TypedEntityReference typeDesignationEntityReference : createSortedList(typeDesignationWorkingSet, typeStatus)) {
|
374
|
|
375
|
if(typeDesignationCount++ > 0){
|
376
|
workingsetBuilder.add(TagEnum.separator, TYPE_DESIGNATION_SEPARATOR);
|
377
|
}
|
378
|
|
379
|
workingsetBuilder.add(TagEnum.typeDesignation, typeDesignationEntityReference.getLabel(), typeDesignationEntityReference);
|
380
|
|
381
|
}
|
382
|
|
383
|
}
|
384
|
typeDesignationWorkingSet.setRepresentation(workingsetBuilder.toString());
|
385
|
finalString += typeDesignationWorkingSet.getRepresentation();
|
386
|
finalBuilder.addAll(workingsetBuilder);
|
387
|
}
|
388
|
}
|
389
|
finalString = finalString.trim();
|
390
|
taggedText = finalBuilder.getTaggedText();
|
391
|
}
|
392
|
}
|
393
|
|
394
|
public void buildStringWithCitation(){
|
395
|
|
396
|
if(finalString == null){
|
397
|
|
398
|
TaggedTextBuilder finalBuilder = new TaggedTextBuilder();
|
399
|
finalString = "";
|
400
|
|
401
|
if(getTypifiedNameCache() != null){
|
402
|
finalString += getTypifiedNameCache() + " ";
|
403
|
finalBuilder.add(TagEnum.name, getTypifiedNameCache(), new TypedEntityReference<>(TaxonName.class, getTypifiedNameRef().getUuid()));
|
404
|
}
|
405
|
|
406
|
int typeCount = 0;
|
407
|
if(orderedByTypesByBaseEntity != null){
|
408
|
for(TypedEntityReference baseEntityRef : orderedByTypesByBaseEntity.keySet()) {
|
409
|
|
410
|
TaggedTextBuilder workingsetBuilder = new TaggedTextBuilder();
|
411
|
if(typeCount++ > 0){
|
412
|
workingsetBuilder.add(TagEnum.separator, TYPE_SEPARATOR);
|
413
|
}
|
414
|
boolean isNameTypeDesignation = false;
|
415
|
|
416
|
if(!baseEntityRef.getLabel().isEmpty()){
|
417
|
workingsetBuilder.add(TagEnum.specimenOrObservation, baseEntityRef.getLabel(), baseEntityRef);
|
418
|
}
|
419
|
TypeDesignationWorkingSet typeDesignationWorkingSet = orderedByTypesByBaseEntity.get(baseEntityRef);
|
420
|
int typeStatusCount = 0;
|
421
|
for(TypeDesignationStatusBase<?> typeStatus : typeDesignationWorkingSet.keySet()) {
|
422
|
if(typeStatusCount++ > 0){
|
423
|
workingsetBuilder.add(TagEnum.separator, TYPE_STATUS_SEPARATOR);
|
424
|
|
425
|
}
|
426
|
boolean isPlural = typeDesignationWorkingSet.get(typeStatus).size() > 1;
|
427
|
if(!typeStatus.equals(NULL_STATUS)) {
|
428
|
workingsetBuilder.add(TagEnum.separator, TYPE_STATUS_PARENTHESIS_LEFT);
|
429
|
workingsetBuilder.add(TagEnum.label, typeStatus.getLabel() + (isPlural ? "s:" : ":"));
|
430
|
}
|
431
|
int typeDesignationCount = 0;
|
432
|
for(TypedEntityReference<?> typeDesignationEntityReference : createSortedList(typeDesignationWorkingSet, typeStatus)) {
|
433
|
if(typeDesignationCount++ > 0){
|
434
|
workingsetBuilder.add(TagEnum.separator, TYPE_DESIGNATION_SEPARATOR);
|
435
|
}
|
436
|
|
437
|
workingsetBuilder.add(TagEnum.typeDesignation, typeDesignationEntityReference.getLabel(), typeDesignationEntityReference);
|
438
|
|
439
|
TypeDesignationBase<?> typeDes = typeDesignations.get(typeDesignationEntityReference.getUuid());
|
440
|
if (typeDes.getCitation() != null){
|
441
|
// workingsetBuilder.add(TagEnum.separator, REFERENCE_PARENTHESIS_LEFT);
|
442
|
String shortCitation = ((DefaultReferenceCacheStrategy)typeDes.getCitation().getCacheStrategy()).createShortCitation(typeDes.getCitation());
|
443
|
workingsetBuilder.add(TagEnum.reference, shortCitation, typeDesignationEntityReference);
|
444
|
//workingsetBuilder.add(TagEnum.separator, REFERENCE_PARENTHESIS_RIGHT);
|
445
|
}
|
446
|
|
447
|
if ((!typeStatus.equals(NULL_STATUS)) &&(typeDesignationCount == typeDesignationWorkingSet.get(typeStatus).size())){
|
448
|
workingsetBuilder.add(TagEnum.separator, TYPE_STATUS_PARENTHESIS_RIGHT);
|
449
|
}
|
450
|
}
|
451
|
|
452
|
}
|
453
|
typeDesignationWorkingSet.setRepresentation(workingsetBuilder.toString());
|
454
|
finalString += typeDesignationWorkingSet.getRepresentation();
|
455
|
finalBuilder.addAll(workingsetBuilder);
|
456
|
}
|
457
|
}
|
458
|
finalString = finalString.trim();
|
459
|
taggedText = finalBuilder.getTaggedText();
|
460
|
}
|
461
|
}
|
462
|
|
463
|
/**
|
464
|
* @param typeDesignationWorkingSet
|
465
|
* @param typeStatus
|
466
|
* @return
|
467
|
*/
|
468
|
private List<TypedEntityReference<TypeDesignationBase>> createSortedList(
|
469
|
TypeDesignationWorkingSet typeDesignationWorkingSet, TypeDesignationStatusBase<?> typeStatus) {
|
470
|
List<TypedEntityReference<TypeDesignationBase>> typeDesignationEntityrReferences = new ArrayList(typeDesignationWorkingSet.get(typeStatus));
|
471
|
Collections.sort(typeDesignationEntityrReferences, new TypedEntityComparator());
|
472
|
return typeDesignationEntityrReferences;
|
473
|
}
|
474
|
|
475
|
|
476
|
/**
|
477
|
* FIXME use the validation framework validators to store the validation problems!!!
|
478
|
*
|
479
|
* @return
|
480
|
* @throws RegistrationValidationException
|
481
|
*/
|
482
|
private void findTypifiedName() throws RegistrationValidationException {
|
483
|
|
484
|
List<String> problems = new ArrayList<>();
|
485
|
|
486
|
TaxonName typifiedName = null;
|
487
|
|
488
|
for(TypeDesignationBase<?> typeDesignation : typeDesignations.values()){
|
489
|
typeDesignation.getTypifiedNames();
|
490
|
if(typeDesignation.getTypifiedNames().isEmpty()){
|
491
|
|
492
|
//TODO instead throw RegistrationValidationException()
|
493
|
problems.add("Missing typifiedName in " + typeDesignation.toString());
|
494
|
continue;
|
495
|
}
|
496
|
if(typeDesignation.getTypifiedNames().size() > 1){
|
497
|
//TODO instead throw RegistrationValidationException()
|
498
|
problems.add("Multiple typifiedName in " + typeDesignation.toString());
|
499
|
continue;
|
500
|
}
|
501
|
if(typifiedName == null){
|
502
|
// remember
|
503
|
typifiedName = typeDesignation.getTypifiedNames().iterator().next();
|
504
|
} else {
|
505
|
// compare
|
506
|
TaxonName otherTypifiedName = typeDesignation.getTypifiedNames().iterator().next();
|
507
|
if(!typifiedName.getUuid().equals(otherTypifiedName.getUuid())){
|
508
|
//TODO instead throw RegistrationValidationException()
|
509
|
problems.add("Multiple typifiedName in " + typeDesignation.toString());
|
510
|
}
|
511
|
}
|
512
|
|
513
|
}
|
514
|
if(!problems.isEmpty()){
|
515
|
// FIXME use the validation framework
|
516
|
throw new RegistrationValidationException("Inconsistent type designations", problems);
|
517
|
}
|
518
|
|
519
|
if(typifiedName != null){
|
520
|
// ON SUCCESS -------------------
|
521
|
this.typifiedName = typifiedName;
|
522
|
this.typifiedNameRef = new EntityReference(typifiedName.getUuid(), typifiedName.getTitleCache());
|
523
|
|
524
|
}
|
525
|
}
|
526
|
|
527
|
|
528
|
/**
|
529
|
* @return the title cache of the typifying name or <code>null</code>
|
530
|
*/
|
531
|
public String getTypifiedNameCache() {
|
532
|
if(typifiedNameRef != null){
|
533
|
return typifiedNameRef.getLabel();
|
534
|
}
|
535
|
return null;
|
536
|
}
|
537
|
|
538
|
/**
|
539
|
* @return the title cache of the typifying name or <code>null</code>
|
540
|
*/
|
541
|
public EntityReference getTypifiedNameRef() {
|
542
|
|
543
|
return typifiedNameRef;
|
544
|
}
|
545
|
|
546
|
/**
|
547
|
* @return
|
548
|
*/
|
549
|
public Collection<TypeDesignationBase<?>> getTypeDesignations() {
|
550
|
return typeDesignations.values();
|
551
|
}
|
552
|
|
553
|
/**
|
554
|
* @param ref
|
555
|
* @return
|
556
|
*/
|
557
|
public TypeDesignationBase<?> findTypeDesignation(EntityReference typeDesignationRef) {
|
558
|
return this.typeDesignations.get(typeDesignationRef.getUuid());
|
559
|
}
|
560
|
|
561
|
|
562
|
public LinkedHashMap<TypedEntityReference, TypeDesignationWorkingSet> getOrderdTypeDesignationWorkingSets() {
|
563
|
return orderedByTypesByBaseEntity;
|
564
|
}
|
565
|
|
566
|
/**
|
567
|
* @param td
|
568
|
* @return
|
569
|
*/
|
570
|
private String stringify(TypeDesignationBase<?> td) {
|
571
|
|
572
|
if(td instanceof NameTypeDesignation){
|
573
|
return stringify((NameTypeDesignation)td);
|
574
|
} else {
|
575
|
return stringify((SpecimenTypeDesignation)td, false);
|
576
|
}
|
577
|
}
|
578
|
|
579
|
|
580
|
/**
|
581
|
* @param td
|
582
|
* @return
|
583
|
*/
|
584
|
protected String stringify(NameTypeDesignation td) {
|
585
|
|
586
|
StringBuffer sb = new StringBuffer();
|
587
|
|
588
|
if(td.getTypeName() != null){
|
589
|
sb.append(td.getTypeName().getTitleCache());
|
590
|
}
|
591
|
if(td.getCitation() != null){
|
592
|
sb.append(" ").append(td.getCitation().getTitleCache());
|
593
|
if(td.getCitationMicroReference() != null){
|
594
|
sb.append(":").append(td.getCitationMicroReference());
|
595
|
}
|
596
|
}
|
597
|
if(td.isNotDesignated()){
|
598
|
sb.append(" not designated");
|
599
|
}
|
600
|
if(td.isRejectedType()){
|
601
|
sb.append(" rejected");
|
602
|
}
|
603
|
if(td.isConservedType()){
|
604
|
sb.append(" conserved");
|
605
|
}
|
606
|
return sb.toString();
|
607
|
}
|
608
|
|
609
|
/**
|
610
|
* @param td
|
611
|
* @return
|
612
|
*/
|
613
|
private String stringify(SpecimenTypeDesignation td, boolean useFullTitleCache) {
|
614
|
String result = "";
|
615
|
|
616
|
if(useFullTitleCache){
|
617
|
if(td.getTypeSpecimen() != null){
|
618
|
String nameTitleCache = td.getTypeSpecimen().getTitleCache();
|
619
|
if(getTypifiedNameCache() != null){
|
620
|
nameTitleCache = nameTitleCache.replace(getTypifiedNameCache(), "");
|
621
|
}
|
622
|
result += nameTitleCache;
|
623
|
}
|
624
|
} else {
|
625
|
if(td.getTypeSpecimen() != null){
|
626
|
DerivedUnit du = td.getTypeSpecimen();
|
627
|
if(du.isProtectedTitleCache()){
|
628
|
result += du.getTitleCache();
|
629
|
} else {
|
630
|
du = HibernateProxyHelper.deproxy(du);
|
631
|
boolean isMediaSpecimen = du instanceof MediaSpecimen;
|
632
|
String typeSpecimenTitle = "";
|
633
|
if(isMediaSpecimen && HibernateProxyHelper.deproxyOrNull(du.getCollection()) == null) {
|
634
|
// special case of an published image which is not covered by the DerivedUnitFacadeCacheStrategy
|
635
|
MediaSpecimen msp = (MediaSpecimen)du;
|
636
|
if(msp.getMediaSpecimen() != null){
|
637
|
for(IdentifiableSource source : msp.getMediaSpecimen().getSources()){
|
638
|
String refDetailStr = source.getCitationMicroReference();
|
639
|
String referenceStr = source.getCitation() == null? "": source.getCitation().getTitleCache();
|
640
|
if(StringUtils.isNotBlank(source.getCitationMicroReference())){
|
641
|
typeSpecimenTitle += refDetailStr;
|
642
|
}
|
643
|
if(!typeSpecimenTitle.isEmpty() && !referenceStr.isEmpty()){
|
644
|
typeSpecimenTitle += " in ";
|
645
|
}
|
646
|
typeSpecimenTitle += referenceStr + " ";
|
647
|
}
|
648
|
}
|
649
|
} else {
|
650
|
DerivedUnitFacadeCacheStrategy cacheStrategy = new DerivedUnitFacadeCacheStrategy();
|
651
|
typeSpecimenTitle += cacheStrategy.getTitleCache(du, true);
|
652
|
|
653
|
}
|
654
|
|
655
|
result += (isMediaSpecimen ? "[icon] " : "") + typeSpecimenTitle.trim();
|
656
|
}
|
657
|
}
|
658
|
}
|
659
|
|
660
|
if(isPrintCitation() && td.getCitation() != null){
|
661
|
Reference citation = HibernateProxyHelper.deproxy(td.getCitation(), Reference.class);
|
662
|
if(citation.getAbbrevTitle() != null){
|
663
|
|
664
|
result += " " + citation.getAbbrevTitle();
|
665
|
} else {
|
666
|
result += " " + citation.getTitleCache();
|
667
|
}
|
668
|
if(td.getCitationMicroReference() != null){
|
669
|
result += " :" + td.getCitationMicroReference();
|
670
|
}
|
671
|
}
|
672
|
if(td.isNotDesignated()){
|
673
|
result += " not designated";
|
674
|
}
|
675
|
|
676
|
return result;
|
677
|
}
|
678
|
|
679
|
/**
|
680
|
* @param td
|
681
|
* @return
|
682
|
* @deprecated
|
683
|
*/
|
684
|
@Deprecated
|
685
|
private FieldUnit findFieldUnit(SpecimenTypeDesignation td) {
|
686
|
|
687
|
DerivedUnit du = td.getTypeSpecimen();
|
688
|
return findFieldUnit(du);
|
689
|
}
|
690
|
|
691
|
private FieldUnit findFieldUnit(DerivedUnit du) {
|
692
|
|
693
|
if(du == null || du.getOriginals() == null){
|
694
|
return null;
|
695
|
}
|
696
|
@SuppressWarnings("rawtypes")
|
697
|
Set<SpecimenOrObservationBase> originals = du.getDerivedFrom().getOriginals();
|
698
|
@SuppressWarnings("rawtypes")
|
699
|
Optional<SpecimenOrObservationBase> fieldUnit = originals.stream()
|
700
|
.filter(original -> original instanceof FieldUnit).findFirst();
|
701
|
if (fieldUnit.isPresent()) {
|
702
|
return (FieldUnit) fieldUnit.get();
|
703
|
} else {
|
704
|
for (@SuppressWarnings("rawtypes")
|
705
|
SpecimenOrObservationBase sob : originals) {
|
706
|
if (sob instanceof DerivedUnit) {
|
707
|
FieldUnit fu = findFieldUnit((DerivedUnit) sob);
|
708
|
if (fu != null) {
|
709
|
return fu;
|
710
|
}
|
711
|
}
|
712
|
}
|
713
|
}
|
714
|
|
715
|
return null;
|
716
|
}
|
717
|
|
718
|
public String print() {
|
719
|
buildString();
|
720
|
return finalString;
|
721
|
}
|
722
|
|
723
|
public List<TaggedText> toTaggedText() {
|
724
|
buildString();
|
725
|
return taggedText;
|
726
|
}
|
727
|
|
728
|
public List<TaggedText> toTaggedTextWithCitation() {
|
729
|
buildStringWithCitation();
|
730
|
return taggedText;
|
731
|
}
|
732
|
|
733
|
|
734
|
/**
|
735
|
* @return the printCitation
|
736
|
*/
|
737
|
public boolean isPrintCitation() {
|
738
|
return printCitation;
|
739
|
}
|
740
|
|
741
|
/**
|
742
|
* @param printCitation the printCitation to set
|
743
|
*/
|
744
|
public void setPrintCitation(boolean printCitation) {
|
745
|
this.printCitation = printCitation;
|
746
|
}
|
747
|
|
748
|
/**
|
749
|
* @return the typifiedName
|
750
|
*/
|
751
|
public TaxonName getTypifiedName() {
|
752
|
return typifiedName;
|
753
|
}
|
754
|
|
755
|
public void setNameTypeBaseEntityType(NameTypeBaseEntityType nameTypeBaseEntityType){
|
756
|
this.nameTypeBaseEntityType = nameTypeBaseEntityType;
|
757
|
}
|
758
|
|
759
|
public NameTypeBaseEntityType getNameTypeBaseEntityType(){
|
760
|
return nameTypeBaseEntityType;
|
761
|
}
|
762
|
|
763
|
public boolean isUseShortCitation() {
|
764
|
return useShortCitation;
|
765
|
}
|
766
|
|
767
|
public void setUseShortCitation(boolean useShortCitation) {
|
768
|
this.useShortCitation = useShortCitation;
|
769
|
}
|
770
|
|
771
|
/**
|
772
|
* TypeDesignations which refer to the same FieldUnit (SpecimenTypeDesignation) or TaxonName
|
773
|
* (NameTypeDesignation) form a working set. The <code>TypeDesignationWorkingSet</code> internally
|
774
|
* works with EnityReferences to the actual TypeDesignations.
|
775
|
*
|
776
|
* The EntityReferences for TypeDesignations are grouped by the according TypeDesignationStatus.
|
777
|
* The TypeDesignationStatusBase keys can be ordered by the term order defined in the vocabulary.
|
778
|
*
|
779
|
* A workingset can be referenced by the <code>baseEntityReference</code>.
|
780
|
*/
|
781
|
public class TypeDesignationWorkingSet extends LinkedHashMap<TypeDesignationStatusBase<?>, Collection<TypedEntityReference>> {
|
782
|
|
783
|
private static final long serialVersionUID = -1329007606500890729L;
|
784
|
|
785
|
String workingSetRepresentation = null;
|
786
|
|
787
|
TypedEntityReference<VersionableEntity> baseEntityReference;
|
788
|
|
789
|
VersionableEntity baseEntity;
|
790
|
|
791
|
List<DerivedUnit> derivedUnits = null;
|
792
|
|
793
|
/**
|
794
|
* @param baseEntityReference
|
795
|
*/
|
796
|
public TypeDesignationWorkingSet(VersionableEntity baseEntity, TypedEntityReference<VersionableEntity> baseEntityReference) {
|
797
|
this.baseEntity = baseEntity;
|
798
|
this.baseEntityReference = baseEntityReference;
|
799
|
}
|
800
|
|
801
|
/**
|
802
|
* @return
|
803
|
*/
|
804
|
public VersionableEntity getBaseEntity() {
|
805
|
return baseEntity;
|
806
|
}
|
807
|
|
808
|
public List<TypedEntityReference> getTypeDesignations() {
|
809
|
List<TypedEntityReference> typeDesignations = new ArrayList<>();
|
810
|
this.values().forEach(typeDesignationReferences -> typeDesignationReferences.forEach(td -> typeDesignations.add(td)));
|
811
|
return typeDesignations;
|
812
|
}
|
813
|
|
814
|
|
815
|
|
816
|
/**
|
817
|
* @param status
|
818
|
* @param typeDesignationEntityReference
|
819
|
*/
|
820
|
public void insert(TypeDesignationStatusBase<?> status, TypedEntityReference typeDesignationEntityReference) {
|
821
|
|
822
|
if(status == null){
|
823
|
status = NULL_STATUS;
|
824
|
}
|
825
|
if(!containsKey(status)){
|
826
|
put(status, new ArrayList<>());
|
827
|
}
|
828
|
get(status).add(typeDesignationEntityReference);
|
829
|
}
|
830
|
|
831
|
|
832
|
public String getRepresentation() {
|
833
|
return workingSetRepresentation;
|
834
|
}
|
835
|
|
836
|
public void setRepresentation(String representation){
|
837
|
this.workingSetRepresentation = representation;
|
838
|
}
|
839
|
|
840
|
/**
|
841
|
* A reference to the entity which is the common base entity for all TypeDesignations in this workingset.
|
842
|
* For a {@link SpecimenTypeDesignation} this is usually the {@link FieldUnit} if it is present. Otherwise it can also be
|
843
|
* a {@link DerivedUnit} or something else depending on the specific use case.
|
844
|
*
|
845
|
* @return the baseEntityReference
|
846
|
*/
|
847
|
public TypedEntityReference<VersionableEntity> getBaseEntityReference() {
|
848
|
return baseEntityReference;
|
849
|
}
|
850
|
|
851
|
@Override
|
852
|
public String toString(){
|
853
|
if(workingSetRepresentation != null){
|
854
|
return workingSetRepresentation;
|
855
|
} else {
|
856
|
return super.toString();
|
857
|
}
|
858
|
}
|
859
|
|
860
|
/**
|
861
|
* @return
|
862
|
*/
|
863
|
public boolean isSpecimenTypeDesigationWorkingSet() {
|
864
|
return SpecimenOrObservationBase.class.isAssignableFrom(baseEntityReference.getType());
|
865
|
}
|
866
|
|
867
|
public TypeDesignationWorkingSetType getWorkingsetType() {
|
868
|
return isSpecimenTypeDesigationWorkingSet() ? TypeDesignationWorkingSetType.SPECIMEN_TYPE_DESIGNATION_WORKINGSET : TypeDesignationWorkingSetType.NAME_TYPE_DESIGNATION_WORKINGSET;
|
869
|
}
|
870
|
|
871
|
}
|
872
|
|
873
|
public enum TypeDesignationWorkingSetType {
|
874
|
SPECIMEN_TYPE_DESIGNATION_WORKINGSET,
|
875
|
NAME_TYPE_DESIGNATION_WORKINGSET,
|
876
|
}
|
877
|
|
878
|
@SuppressWarnings({ "deprecation", "serial" })
|
879
|
class NullTypeDesignationStatus extends TypeDesignationStatusBase<NullTypeDesignationStatus>{
|
880
|
|
881
|
/**
|
882
|
* {@inheritDoc}
|
883
|
*/
|
884
|
@Override
|
885
|
public void resetTerms() {
|
886
|
// empty
|
887
|
|
888
|
}
|
889
|
|
890
|
/**
|
891
|
* {@inheritDoc}
|
892
|
*/
|
893
|
@Override
|
894
|
protected void setDefaultTerms(TermVocabulary<NullTypeDesignationStatus> termVocabulary) {
|
895
|
// empty
|
896
|
}
|
897
|
|
898
|
}
|
899
|
|
900
|
class DataIntegrityException extends Exception {
|
901
|
|
902
|
private static final long serialVersionUID = 1464726696296824905L;
|
903
|
|
904
|
/**
|
905
|
* @param string
|
906
|
*/
|
907
|
public DataIntegrityException(String string) {
|
908
|
super(string);
|
909
|
}
|
910
|
}
|
911
|
}
|