Project

General

Profile

« Previous | Next » 

Revision ab1f58a9

Added by Andreas Müller over 3 years ago

ref #9279 improve TypeDesignationSetManager

View differences:

cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/name/TypeDesignationSetManager.java
26 26

  
27 27
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeCacheStrategy;
28 28
import eu.etaxonomy.cdm.api.service.exception.RegistrationValidationException;
29
import eu.etaxonomy.cdm.common.UTF8;
29 30
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
30 31
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
31 32
import eu.etaxonomy.cdm.model.common.Language;
......
300 301
                );
301 302
        return orderedRepresentations;
302 303
    }
303
*/
304
     */
304 305

  
305
    public void buildString(boolean withCitation){
306
    private void buildString(boolean withCitation, boolean withStartingTypeLabel, boolean withNameIfAvailable){
307
        boolean withBrackets = true;  //still unclear if this should become a parameter or should be always true
306 308

  
307
        if(finalString == null){
309
        TaggedTextBuilder finalBuilder = new TaggedTextBuilder();
310
        finalString = "";
308 311

  
309
            TaggedTextBuilder finalBuilder = new TaggedTextBuilder();
310
            finalString = "";
312
        if(withNameIfAvailable && getTypifiedNameCache() != null){
313
            finalString += getTypifiedNameCache();
314
            finalBuilder.add(TagEnum.name, getTypifiedNameCache(), new TypedEntityReference<>(TaxonName.class, getTypifiedNameRef().getUuid()));
315
        }
311 316

  
312
            if(getTypifiedNameCache() != null){
313
                finalString += getTypifiedNameCache() + " ";
314
                finalBuilder.add(TagEnum.name, getTypifiedNameCache(), new TypedEntityReference<>(TaxonName.class, getTypifiedNameRef().getUuid()));
317
        int typeSetCount = 0;
318
        if(orderedByTypesByBaseEntity != null){
319
            String separator = UTF8.EN_DASH_SPATIUM.toString();
320
            finalString += separator;
321
            finalBuilder.addSeparator(separator);
322
            for(TypedEntityReference<?> baseEntityRef : orderedByTypesByBaseEntity.keySet()) {
323
                buildStringForSingleTypeSet(withCitation, withStartingTypeLabel, withBrackets, finalBuilder,
324
                        typeSetCount, baseEntityRef);
325
                typeSetCount++;
315 326
            }
327
        }
328
        finalString = finalString.trim();
329
        taggedText = finalBuilder.getTaggedText();
330
    }
331

  
332
    private int buildStringForSingleTypeSet(boolean withCitation, boolean withStartingTypeLabel, boolean withBrackets,
333
            TaggedTextBuilder finalBuilder, int typeSetCount, TypedEntityReference<?> baseEntityRef) {
334

  
335
        TaggedTextBuilder workingsetBuilder = new TaggedTextBuilder();
336
        boolean isSpecimenTypeDesignation = SpecimenOrObservationBase.class.isAssignableFrom(baseEntityRef.getType());
337
        if(typeSetCount > 0){
338
            workingsetBuilder.add(TagEnum.separator, TYPE_SEPARATOR);
339
        }else if (withStartingTypeLabel){
340
            //TODO this is not really exact as we may want to handle specimen types and
341
            //name types separately, but this is such a rare case (if at all) and
342
            //increases complexity so it is not yet implemented
343
            boolean isPlural = hasMultipleTypes(orderedByTypesByBaseEntity);
344
            if(isSpecimenTypeDesignation){
345
                workingsetBuilder.add(TagEnum.label, (isPlural? "Types:": "Type:"));
346
            } else if (NameTypeDesignation.class.isAssignableFrom(baseEntityRef.getType())){
347
                workingsetBuilder.add(TagEnum.label, (isPlural? "Nametypes:": "Nametype:"));
348
            } else {
349
                //do nothing for now
350
            }
351
        }
316 352

  
317
            int typeCount = 0;
318
            if(orderedByTypesByBaseEntity != null){
319
                for(TypedEntityReference<?> baseEntityRef : orderedByTypesByBaseEntity.keySet()) {
353
        if(!baseEntityRef.getLabel().isEmpty()){
354
            workingsetBuilder.add(TagEnum.specimenOrObservation, baseEntityRef.getLabel(), baseEntityRef);
355
        }
356
        TypeDesignationWorkingSet typeDesignationWorkingSet = orderedByTypesByBaseEntity.get(baseEntityRef);
357
        int typeStatusCount = 0;
358
        if (withBrackets && isSpecimenTypeDesignation){
359
            workingsetBuilder.add(TagEnum.separator, TYPE_STATUS_PARENTHESIS_LEFT);
360
        }
361
        for(TypeDesignationStatusBase<?> typeStatus : typeDesignationWorkingSet.keySet()) {
362
            typeStatusCount = buildStringForSingleTypeStatus(withCitation, workingsetBuilder,
363
                    typeDesignationWorkingSet, typeStatusCount, typeStatus, typeSetCount);
364
        }
365
        if (withBrackets && isSpecimenTypeDesignation){
366
            workingsetBuilder.add(TagEnum.separator, TYPE_STATUS_PARENTHESIS_RIGHT);
367
        }
368
        typeDesignationWorkingSet.setRepresentation(workingsetBuilder.toString());
369
        finalString += typeDesignationWorkingSet.getRepresentation();
370
        finalBuilder.addAll(workingsetBuilder);
371
        return typeSetCount;
372
    }
320 373

  
321
                    TaggedTextBuilder workingsetBuilder = new TaggedTextBuilder();
322
                    if(typeCount++ > 0){
323
                        workingsetBuilder.add(TagEnum.separator, TYPE_SEPARATOR);
324
                    }
325
                    boolean isNameTypeDesignation = false;
326
                    if (!withCitation){
327
                        //TODO unclear why only without citation, it probably should be another parameter like
328
                        //"with generic type" or so and comes from different requirements in cdmlight an phycobank
329
                        if(SpecimenOrObservationBase.class.isAssignableFrom(baseEntityRef.getType()) ){
330
                            workingsetBuilder.add(TagEnum.label, "Type:");
331
                        } else{
332
                            workingsetBuilder.add(TagEnum.label, "Nametype:");
333
                            isNameTypeDesignation = true;
334
                        }
335
                    }
374
    private boolean hasMultipleTypes(
375
            LinkedHashMap<TypedEntityReference, TypeDesignationWorkingSet> typeWorkingSets) {
376
        if (typeWorkingSets == null || typeWorkingSets.isEmpty()){
377
            return false;
378
        }else if (typeWorkingSets.keySet().size() > 1) {
379
            return true;
380
        }
381
        TypeDesignationWorkingSet singleSet = typeWorkingSets.values().iterator().next();
382
        return singleSet.getTypeDesignations().size() > 1;
383
    }
336 384

  
337
                    if(!baseEntityRef.getLabel().isEmpty()){
338
                        workingsetBuilder.add(TagEnum.specimenOrObservation, baseEntityRef.getLabel(), baseEntityRef);
339
                    }
340
                    TypeDesignationWorkingSet typeDesignationWorkingSet = orderedByTypesByBaseEntity.get(baseEntityRef);
341
                    int typeStatusCount = 0;
342
                    for(TypeDesignationStatusBase<?> typeStatus : typeDesignationWorkingSet.keySet()) {
343
                        if(typeStatusCount++  > 0){
344
                            workingsetBuilder.add(TagEnum.separator, TYPE_STATUS_SEPARATOR);
345
                        }
346
                        boolean isPlural = typeDesignationWorkingSet.get(typeStatus).size() > 1;
347
                        if(typeStatus != NULL_STATUS){
348
                            if (withCitation){
349
                                //TODO maybe
350
                                workingsetBuilder.add(TagEnum.separator, TYPE_STATUS_PARENTHESIS_LEFT);
351
                            }
352
                            //END
353
                            workingsetBuilder.add(TagEnum.label, typeStatus.getLabel() + (isPlural ? "s" : ""));
354
                            workingsetBuilder.add(TagEnum.postSeparator, POST_STATUS_SEPARATOR);
355
                        }
356
                        int typeDesignationCount = 0;
357
                        for(TypedEntityReference<?> typeDesignationEntityReference : createSortedList(typeDesignationWorkingSet, typeStatus)) {
358
                            if(typeDesignationCount++  > 0){
359
                                workingsetBuilder.add(TagEnum.separator, TYPE_DESIGNATION_SEPARATOR);
360
                            }
385
    private int buildStringForSingleTypeStatus(boolean withCitation, TaggedTextBuilder workingsetBuilder,
386
            TypeDesignationWorkingSet typeDesignationWorkingSet, int typeStatusCount,
387
            TypeDesignationStatusBase<?> typeStatus, int typeSetCount) {
388
        //starting separator
389
        if(typeStatusCount++ > 0){
390
            workingsetBuilder.add(TagEnum.separator, TYPE_STATUS_SEPARATOR);
391
        }
392

  
393
        boolean isPlural = typeDesignationWorkingSet.get(typeStatus).size() > 1;
394
        if(typeStatus != NULL_STATUS){
395
            String label = typeStatus.getLabel() + (isPlural ? "s" : "");
396
            if (workingsetBuilder.size() == 0){
397
                label = StringUtils.capitalize(label);
398
            }
399
            workingsetBuilder.add(TagEnum.label, label);
400
            workingsetBuilder.add(TagEnum.postSeparator, POST_STATUS_SEPARATOR);
401
        }else if (workingsetBuilder.size() > 0 && typeSetCount > 0){
402
            workingsetBuilder.add(TagEnum.label, (isPlural? "Nametypes:": "Nametype:"));
403
        }
361 404

  
362
                            workingsetBuilder.add(TagEnum.typeDesignation, typeDesignationEntityReference.getLabel(), typeDesignationEntityReference);
405
        //designation + sources
406
        int typeDesignationCount = 0;
407
        for(TypedEntityReference<?> typeDesignationEntityReference : createSortedList(typeDesignationWorkingSet, typeStatus)) {
408
            typeDesignationCount = buildStringForSingleType(withCitation, workingsetBuilder, typeDesignationCount,
409
                    typeDesignationEntityReference);
410
        }
411
        return typeStatusCount;
412
    }
363 413

  
364
                            if (withCitation){
365
                                TypeDesignationBase<?> typeDes = typeDesignations.get(typeDesignationEntityReference.getUuid());
414
    private int buildStringForSingleType(boolean withCitation, TaggedTextBuilder workingsetBuilder, int typeDesignationCount,
415
            TypedEntityReference<?> typeDesignationEntityReference) {
416
        if(typeDesignationCount++ > 0){
417
            workingsetBuilder.add(TagEnum.separator, TYPE_DESIGNATION_SEPARATOR);
418
        }
366 419

  
367
                                //lectotype source
368
                                OriginalSourceBase<?> lectoSource = typeDes.getSource();
369
                                if (hasLectoSource(typeDes)){
370
                                    workingsetBuilder.add(TagEnum.separator, REFERENCE_DESIGNATED_BY);
371
                                    addSource(workingsetBuilder, typeDesignationEntityReference, lectoSource);
372
                                }
373
                                //general sources
374
                                if (!typeDes.getSources().isEmpty()) {
375
                                    workingsetBuilder.add(TagEnum.separator, REFERENCE_PARENTHESIS_LEFT + REFERENCE_FIDE);
376
                                    int count = 0;
377
                                    for (IdentifiableSource source: typeDes.getSources()){
378
                                        if (count++ > 0){
379
                                            workingsetBuilder.add(TagEnum.separator, SOURCE_SEPARATOR);
380
                                        }
381
                                        addSource(workingsetBuilder, typeDesignationEntityReference, source);
382
                                    }
383
                                    workingsetBuilder.add(TagEnum.separator, REFERENCE_PARENTHESIS_RIGHT);
384
                                }
420
        workingsetBuilder.add(TagEnum.typeDesignation, typeDesignationEntityReference.getLabel(), typeDesignationEntityReference);
385 421

  
386
                                if ((typeStatus != NULL_STATUS) &&(typeDesignationCount == typeDesignationWorkingSet.get(typeStatus).size())){
387
                                    workingsetBuilder.add(TagEnum.separator, TYPE_STATUS_PARENTHESIS_RIGHT);
388
                                }
422
        if (withCitation){
423
            TypeDesignationBase<?> typeDes = typeDesignations.get(typeDesignationEntityReference.getUuid());
389 424

  
390
                            }
391
                        }
425
            //lectotype source
426
            OriginalSourceBase<?> lectoSource = typeDes.getSource();
427
            if (hasLectoSource(typeDes)){
428
                workingsetBuilder.add(TagEnum.separator, REFERENCE_DESIGNATED_BY);
429
                addSource(workingsetBuilder, typeDesignationEntityReference, lectoSource);
430
            }
431
            //general sources
432
            if (!typeDes.getSources().isEmpty()) {
433
                workingsetBuilder.add(TagEnum.separator, REFERENCE_PARENTHESIS_LEFT + REFERENCE_FIDE);
434
                int count = 0;
435
                for (IdentifiableSource source: typeDes.getSources()){
436
                    if (count++ > 0){
437
                        workingsetBuilder.add(TagEnum.separator, SOURCE_SEPARATOR);
392 438
                    }
393
                    typeDesignationWorkingSet.setRepresentation(workingsetBuilder.toString());
394
                    finalString += typeDesignationWorkingSet.getRepresentation();
395
                    finalBuilder.addAll(workingsetBuilder);
439
                    addSource(workingsetBuilder, typeDesignationEntityReference, source);
396 440
                }
441
                workingsetBuilder.add(TagEnum.separator, REFERENCE_PARENTHESIS_RIGHT);
397 442
            }
398
            finalString = finalString.trim();
399
            taggedText = finalBuilder.getTaggedText();
400 443
        }
444
        return typeDesignationCount;
401 445
    }
402 446

  
403 447
    /**
......
589 633
                        }
590 634
                    } else {
591 635
                        DerivedUnitFacadeCacheStrategy cacheStrategy = new DerivedUnitFacadeCacheStrategy();
592
                        String titleCache = cacheStrategy.getTitleCache(du, true);
636
                        String titleCache = cacheStrategy.getTitleCache(du, true, false);
593 637
                        // removing parentheses from code + accession number, see https://dev.e-taxonomy.eu/redmine/issues/8365
594 638
                        titleCache = titleCache.replaceAll("[\\(\\)]", "");
595 639
                        typeSpecimenTitle += titleCache;
......
634 678
        return null;
635 679
    }
636 680

  
637
    public String print() {
638
        buildString(false);
681
    public String print(boolean withCitation, boolean withStartingTypeLabel, boolean withNameIfAvailable) {
682
        buildString(withCitation, withStartingTypeLabel, withNameIfAvailable);
639 683
        return finalString;
640 684
    }
641 685

  
642
    public List<TaggedText> toTaggedText(boolean withCitation) {
643
        buildString(withCitation);
686
    public List<TaggedText> toTaggedText(boolean withCitation, boolean withStartingTypeLabel, boolean withNameIfAvailable) {
687
        buildString(withCitation, withStartingTypeLabel, withNameIfAvailable);
644 688
        return taggedText;
645 689
    }
646 690

  

Also available in: Unified diff