Project

General

Profile

Download (25.3 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2021 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.format;
10

    
11
import java.lang.reflect.InvocationTargetException;
12
import java.lang.reflect.Method;
13
import java.util.Set;
14

    
15
import org.apache.commons.lang3.StringUtils;
16

    
17
import eu.etaxonomy.cdm.common.CdmUtils;
18
import eu.etaxonomy.cdm.format.ICdmFormatter.FormatKey;
19
import eu.etaxonomy.cdm.format.description.CategoricalDataFormatter;
20
import eu.etaxonomy.cdm.format.description.QuantitativeDataFormatter;
21
import eu.etaxonomy.cdm.format.occurrences.DistanceStringFormatter;
22
import eu.etaxonomy.cdm.model.agent.AgentBase;
23
import eu.etaxonomy.cdm.model.common.CdmBase;
24
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
25
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
26
import eu.etaxonomy.cdm.model.common.Language;
27
import eu.etaxonomy.cdm.model.common.LanguageString;
28
import eu.etaxonomy.cdm.model.common.LanguageStringBase;
29
import eu.etaxonomy.cdm.model.common.Marker;
30
import eu.etaxonomy.cdm.model.common.MarkerType;
31
import eu.etaxonomy.cdm.model.common.RelationshipBase;
32
import eu.etaxonomy.cdm.model.common.RelationshipTermBase;
33
import eu.etaxonomy.cdm.model.common.TimePeriod;
34
import eu.etaxonomy.cdm.model.description.CategoricalData;
35
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
36
import eu.etaxonomy.cdm.model.description.DescriptionBase;
37
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
38
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
39
import eu.etaxonomy.cdm.model.description.Distribution;
40
import eu.etaxonomy.cdm.model.description.IDescribable;
41
import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
42
import eu.etaxonomy.cdm.model.description.KeyStatement;
43
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
44
import eu.etaxonomy.cdm.model.description.QuantitativeData;
45
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
46
import eu.etaxonomy.cdm.model.description.StateData;
47
import eu.etaxonomy.cdm.model.description.StatisticalMeasurementValue;
48
import eu.etaxonomy.cdm.model.description.TaxonDescription;
49
import eu.etaxonomy.cdm.model.description.TaxonInteraction;
50
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
51
import eu.etaxonomy.cdm.model.description.TemporalData;
52
import eu.etaxonomy.cdm.model.description.TextData;
53
import eu.etaxonomy.cdm.model.location.NamedArea;
54
import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
55
import eu.etaxonomy.cdm.model.name.HybridRelationship;
56
import eu.etaxonomy.cdm.model.name.NameRelationship;
57
import eu.etaxonomy.cdm.model.name.NameTypeDesignation;
58
import eu.etaxonomy.cdm.model.name.NomenclaturalSource;
59
import eu.etaxonomy.cdm.model.name.NomenclaturalStatus;
60
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
61
import eu.etaxonomy.cdm.model.name.TaxonName;
62
import eu.etaxonomy.cdm.model.name.TextualTypeDesignation;
63
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
64
import eu.etaxonomy.cdm.model.name.TypeDesignationStatusBase;
65
import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
66
import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
67
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
68
import eu.etaxonomy.cdm.model.permission.Group;
69
import eu.etaxonomy.cdm.model.permission.User;
70
import eu.etaxonomy.cdm.model.reference.NamedSource;
71
import eu.etaxonomy.cdm.model.reference.OriginalSourceBase;
72
import eu.etaxonomy.cdm.model.taxon.Classification;
73
import eu.etaxonomy.cdm.model.taxon.SecundumSource;
74
import eu.etaxonomy.cdm.model.taxon.Taxon;
75
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
76
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
77
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
78
import eu.etaxonomy.cdm.model.term.Representation;
79

    
80
/**
81
 * @author a.mueller
82
 * @date 19.03.2021
83
 */
84
public class ReferencingObjectFormatter {
85

    
86
    public static String format(CdmBase element, Language defaultLanguage) {
87
        return format(element, null, defaultLanguage);
88
    }
89

    
90
    public static String format(CdmBase element, String target, Language defaultLanguage) {
91

    
92
        String resultString = null;
93
        if (element == null){
94
            return null;
95
        }else if (element instanceof IdentifiableEntity) {
96
            resultString = ((IdentifiableEntity<?>) element).getTitleCache();
97
        }else if (element instanceof OriginalSourceBase) {
98
            OriginalSourceBase originalSource = (OriginalSourceBase) element;
99
//              ISourceable sourcedObject = originalSource.getSourcedObj();
100
            //due to #5743 the bidirectionality for sourced object had to be removed
101

    
102
            String sourceObjectTitle = "sourced object data not available (#5743)";
103

    
104
            //it is now possible for NomenclaturalSource as they link to the sourced name
105
            if (originalSource instanceof NomenclaturalSource){
106
                TaxonName sourcedName = ((NomenclaturalSource)originalSource).getSourcedName();
107
                sourceObjectTitle = sourcedName == null ? "Source orphaned, not attached to a name" :
108
                    "for " + sourcedName.getTitleCache();
109
            }else if (originalSource instanceof SecundumSource){
110
                TaxonBase<?> sourcedTaxon = ((SecundumSource)originalSource).getSourcedTaxon();
111
                sourceObjectTitle = sourcedTaxon == null ? "Source orphaned, not attached to a taxon" :
112
                    "for " + sourcedTaxon.getTitleCache();
113
            }else if (originalSource instanceof DescriptionElementSource){
114
                sourceObjectTitle = getCache((DescriptionElementSource)originalSource, defaultLanguage);
115
            }else if (originalSource instanceof IdentifiableSource && isNotBlank(target) ){
116
                sourceObjectTitle = "for " + target;
117
            }else if (originalSource instanceof NamedSource && isNotBlank(target) ){
118
                sourceObjectTitle = "for " + target;
119
            }
120
            resultString = CdmUtils.concat("; ", new String[]{originalSource.getIdNamespace(), originalSource.getIdInSource(), sourceObjectTitle});
121
        }else if (element instanceof LanguageStringBase) {
122
            resultString = ((LanguageStringBase) element).getText();
123
        }else if (element instanceof DescriptionElementBase) {
124
            resultString = getCache((DescriptionElementBase) element, defaultLanguage);
125
        }else if (element instanceof StateData) {
126
            resultString = getCache((StateData) element, defaultLanguage);
127
        }else if (element instanceof StatisticalMeasurementValue) {
128
            resultString = getCache((StatisticalMeasurementValue) element, defaultLanguage);
129
        }else if (element instanceof RelationshipBase<?, ?, ?>) {
130
            resultString = getCache((RelationshipBase<?, ?, ?>) element, defaultLanguage);
131
        }else if (element instanceof TypeDesignationBase<?>) {
132
            resultString = getCache((TypeDesignationBase<?>) element, defaultLanguage);
133
        }else if (element instanceof HomotypicalGroup) {
134
            resultString = getCache((HomotypicalGroup) element);
135
        }else if (element instanceof TaxonNode) {
136
            resultString = getCache((TaxonNode) element);
137
        }else if (element instanceof DeterminationEvent) {
138
            resultString = getCache((DeterminationEvent) element);
139
        }else if (element instanceof NomenclaturalStatus) {
140
            resultString = getCache((NomenclaturalStatus) element);
141
        }else if (element instanceof GatheringEvent){
142
            resultString = getCache((GatheringEvent) element);
143
        }else if (element instanceof Marker) {
144
            Marker marker = (Marker) element;
145
            MarkerType type = marker.getMarkerType();
146
            resultString = (type == null ? "- no marker type -" : marker.getMarkerType().getLabel()) + " (" + marker.getFlag() + ")";
147
        }else if (element instanceof User) {
148
            User user = (User) element;
149
            resultString = user.getUsername();
150
        }else if (element instanceof Group) {
151
            Group group = (Group) element;
152
            resultString = group.getName();
153
        }else if (element instanceof KeyStatement) {
154
            KeyStatement keyStatement = (KeyStatement) element;
155
            resultString = getCache(keyStatement);
156
        }else{
157
            // TODO write return texts for HomotypicalGroup, etc.
158
            resultString = element.toString();
159
        }
160

    
161
        if (resultString == null){
162
            resultString = element.toString();
163
        }
164
        return resultString;
165
    }
166

    
167
    private static String getCache(DescriptionElementSource source, Language defaultLanguage) {
168
        DescriptionElementBase sourcedElement = source.getSourcedElement();
169

    
170
        if (sourcedElement == null){
171
            return "Source orphaned, not attached to a fact";
172
        }
173
        String superLabel = getDescribedObjectLabel(sourcedElement.getInDescription());
174
        String result = CdmUtils.concat(": ", DescriptionElementFormatter.format(sourcedElement, defaultLanguage), superLabel);
175
        return result;
176
    }
177

    
178
    private static String getDescribedObjectLabel(DescriptionBase<?> inDescription) {
179
        IDescribable<?> entity = inDescription.describedEntity();
180
        if (entity != null){
181
            return entity.getTitleCache();
182
        }else{
183
            return inDescription.getTitleCache();
184
        }
185
    }
186

    
187
    private static String getCache(RelationshipBase<?, ?, ?> rel, Language defaultLanguage) {
188
        rel = CdmBase.deproxy(rel);
189
        RelationshipTermBase<?> type = rel.getType();
190
        IdentifiableEntity<?> from;
191
        IdentifiableEntity<?> to;
192
        if (rel.isInstanceOf(NameRelationship.class)){
193
            from = ((NameRelationship)rel).getFromName();
194
            to = ((NameRelationship)rel).getToName();
195
        }else if (rel.isInstanceOf(HybridRelationship.class)){
196
            from = ((HybridRelationship)rel).getParentName();
197
            to = ((HybridRelationship)rel).getHybridName();
198
        }else if (rel.isInstanceOf(TaxonRelationship.class)){
199
            from = ((TaxonRelationship)rel).getFromTaxon();
200
            to = ((TaxonRelationship)rel).getToTaxon();
201
        }else{
202
            try {
203
                Method fromMethod = rel.getClass().getMethod("getRelatedFrom");
204
                Method toMethod = rel.getClass().getMethod("getRelatedFrom");
205
                fromMethod.setAccessible(true);
206
                toMethod.setAccessible(true);
207
                from = (IdentifiableEntity<?>)fromMethod.invoke(rel);
208
                to = (IdentifiableEntity<?>)toMethod.invoke(rel);
209
            } catch (NoSuchMethodException | SecurityException | IllegalAccessException
210
                | IllegalArgumentException | InvocationTargetException e) {
211
                throw new RuntimeException(e);
212
            }
213
        }
214
        String typeLabel = null;
215
        if (type != null){
216
            Representation typeRepr = type.getPreferredRepresentation(defaultLanguage);
217
            if (typeRepr != null){
218
                typeLabel = typeRepr.getAbbreviatedLabel();
219
            }
220
            if (isBlank(typeLabel) && typeRepr != null){
221
                typeLabel = typeRepr.getLabel();
222
            }
223
            if (isBlank(typeLabel) ){
224
                typeLabel = type.getSymbol();
225
            }
226
            if (isBlank(typeLabel)){
227
                typeLabel = type.getTitleCache();
228
            }
229
        }
230
        if (isBlank(typeLabel)){
231
            typeLabel = "->";
232
        }
233
        String result = CdmUtils.concat(" ", new String[]{from == null ? null : from.getTitleCache(),
234
                typeLabel, to == null? null : to.getTitleCache()});
235
        return result;
236
    }
237

    
238
    private static String getCache(GatheringEvent gatheringEvent){
239
        String ALTITUDE_PREFIX = "alt. ";
240
        final String METER = "m";
241

    
242
        String result = "";
243

    
244
        //collector
245
        AgentBase<?> collector = CdmBase.deproxy(gatheringEvent.getCollector());
246
        String collectorStr = collector == null? null : collector.getTitleCache();
247
        result = CdmUtils.concat(", ", result, collectorStr);
248

    
249
        //gathering period
250
        TimePeriod gatheringPeriod = gatheringEvent.getTimeperiod();
251
        result = CdmUtils.concat(", ", result, (gatheringPeriod == null? null : gatheringPeriod.toString()));
252

    
253
        //country
254
        String strCountry = null;
255
        NamedArea country = gatheringEvent.getCountry();
256
        Representation repCountry = country == null ? null : country.getRepresentation(Language.DEFAULT());
257
        strCountry = repCountry == null ? null: repCountry.getLabel();
258
        result = CdmUtils.concat(", ", result, strCountry);
259

    
260
        //locality
261
        LanguageString locality = gatheringEvent.getLocality();
262
        if (locality != null) {
263
            result = CdmUtils.concat(", ", result, locality.getText());
264
        }
265

    
266
        //elevation
267
        String elevationStr;
268
        if (isNotBlank(gatheringEvent.getAbsoluteElevationText())){
269
            elevationStr = gatheringEvent.getAbsoluteElevationText();
270
        }else{
271
            String text = gatheringEvent.getAbsoluteElevationText();
272
            Integer min = gatheringEvent.getAbsoluteElevation();
273
            Integer max = gatheringEvent.getAbsoluteElevationMax();
274
            elevationStr = DistanceStringFormatter.distanceString(min, max, text, METER);
275
        }
276
        if (isNotBlank(elevationStr)){
277
            result = CdmUtils.concat(", " , result, ALTITUDE_PREFIX);
278
            result += elevationStr;
279
        }
280

    
281
        //exact locality
282
        if (gatheringEvent.getExactLocation() != null){
283
            String exactLocation = gatheringEvent.getExactLocation().toSexagesimalString(false, false);
284
            result = CdmUtils.concat(", ", result, exactLocation);
285
        }
286

    
287
        return result;
288
    }
289

    
290
    private static String getCache(DeterminationEvent detEvent) {
291
        //taxon
292
        String taxonStr = null;
293
        TaxonName taxonName = detEvent.getTaxonName();
294
        TaxonBase<?> taxon = detEvent.getTaxon();
295
        if (taxonName != null){
296
            taxonStr = taxonName.getTitleCache();
297
        }
298
        if (isBlank(taxonStr) && taxon != null){
299
            taxonStr = taxon.getTitleCache();
300
        }
301
        if (isBlank(taxonStr)){
302
            taxonStr = "no or unlabled taxon";
303
        }
304

    
305
        //unit
306
        SpecimenOrObservationBase<?> unit = detEvent.getIdentifiedUnit();
307
        String unitStr;
308
        if (unit != null){
309
            unitStr = unit.getTitleCache();
310
            if (isBlank(unitStr)){
311
                unitStr = "Unlabled unit";
312
            }
313
        }else{
314
            unitStr = "no unit";
315
        }
316

    
317
        String result = CdmUtils.concat(" determined as ", unitStr, taxonStr);
318

    
319
        return result;
320
    }
321

    
322
    private static String getCache(TaxonNode taxonNode) {
323
        String result = "";
324
        Taxon taxon = taxonNode.getTaxon();
325
        if (taxon != null){
326
            result = taxon.getName() != null ? taxon.getName().getTitleCache() : taxon.getTitleCache();
327
        }
328

    
329
        final String invisible = "Invisible root of ";
330
        String parentStr;
331
        TaxonNode parentNode = taxonNode.getParent();
332
        if (parentNode == null){
333
            parentStr = invisible;
334
        }else{
335
            Taxon parentTaxon = parentNode.getTaxon();
336
            if (parentTaxon == null){
337
                parentStr = " (root of ";
338
            }else{
339
                TaxonName parentName = parentTaxon.getName();
340
                if (parentName == null){
341
                    parentStr = " (child of " + parentTaxon.getTitleCache();
342
                }else{
343
                    parentStr = " (child of " + parentName.getTitleCache();
344
                }
345
                parentStr += " in ";
346
            }
347
        }
348

    
349
        //classification
350
        Classification classification = taxonNode.getClassification();
351
        String classificationStr ;
352
        if (classification != null){
353
            classificationStr = classification.getName() == null ? "classification:"+classification.getId() : classification.getName().getText();
354
            if (isBlank(classificationStr)){
355
                classificationStr = classification.toString();
356
            }
357
        }else{
358
            classificationStr = "-no classification-"; //should not happen
359
        }
360

    
361
        result = CdmUtils.concat("", parentStr, classificationStr, parentStr == invisible? "" : ")");
362

    
363
        return result;
364
    }
365

    
366
    private static String getCache(TypeDesignationBase<?> designation, Language defaultLanguage) {
367
        designation = CdmBase.deproxy(designation);
368
        //from
369
        String fromString = null;
370
        Set<TaxonName> from = designation.getTypifiedNames();
371
        if(from != null){
372
            for (TaxonName name : from){
373
                fromString = CdmUtils.concat(",", fromString, name.getTitleCache());
374
            }
375
        }
376
        //to
377
        IdentifiableEntity<?> to = null;
378
        String toStr = "";
379
        if (designation.isInstanceOf(SpecimenTypeDesignation.class)){
380
            to = ((SpecimenTypeDesignation)designation).getTypeSpecimen();
381
        }else if (designation.isInstanceOf(NameTypeDesignation.class)){
382
            to = ((NameTypeDesignation)designation).getTypeName();
383
        }else if (designation.isInstanceOf(TextualTypeDesignation.class)){
384
            toStr = ((TextualTypeDesignation)designation).getPreferredText(defaultLanguage);
385
        }else{
386
            throw new RuntimeException("Type Designation class not supported: " +  designation.getClass().getName());
387
        }
388
        toStr = to == null? toStr : to.getTitleCache();
389
        //status
390
        String typeLabel = null;
391
        TypeDesignationStatusBase<?> status = designation.getTypeStatus();
392
        if (status != null){
393
            Representation typeRepr = status.getPreferredRepresentation(defaultLanguage);
394
            if (typeRepr != null){
395
                typeLabel = typeRepr.getAbbreviatedLabel();
396
            }
397
            if (isBlank(typeLabel) && typeRepr != null){
398
                typeLabel = typeRepr.getLabel();
399
            }
400
            if (isBlank(typeLabel) ){
401
                typeLabel = status.getSymbol();
402
            }
403
            if (isBlank(typeLabel)){
404
                typeLabel = status.getTitleCache();
405
            }
406
        }
407
        if (isBlank(typeLabel)){
408
            typeLabel = "->";
409
        }
410
        //concat
411
        String result = CdmUtils.concat(" ", new String[]{fromString, typeLabel, toStr});
412
        return result;
413
    }
414

    
415
    private static String getCache(HomotypicalGroup hg) {
416
        String result = "";
417
        for (TaxonName tnb : hg.getTypifiedNames()){
418
            result = CdmUtils.concat(", ", result, tnb.getTitleCache());
419
        }
420
        if (isBlank(result)){
421
            result = "No typified names";
422
        }
423
        return result;
424
    }
425

    
426
    private static String getCache(KeyStatement ks) {
427
        String result = "";
428
        LanguageString ls = ks.getPreferredLanguageString(Language.DEFAULT());
429
        if (ls != null && CdmUtils.isNotBlank(ls.getText())){
430
            result = ls.getText();
431
        }else{
432
            result = ks.toString();
433
        }
434
        return result;
435
    }
436

    
437
    private static String getCache(NomenclaturalStatus nomStatus) {
438
        String result = nomStatus.getName().getTitleCache();
439
        if (nomStatus.getType()!= null){
440
            Representation rep = nomStatus.getType().getPreferredRepresentation(Language.DEFAULT());
441
            if (rep != null){
442
                result = CdmUtils.concat(": ", rep.getAbbreviatedLabel(), result);
443
            }
444
        }
445
        return result;
446
    }
447

    
448

    
449
    private static boolean isNotBlank(String str){
450
        return StringUtils.isNotBlank(str);
451
    }
452

    
453
    private static boolean isBlank(String str){
454
        return StringUtils.isBlank(str);
455
    }
456

    
457
    //from taxeditor DescriptionHelper
458
    private static String getCache(DescriptionElementBase element,
459
            Language defaultLanguage) {
460

    
461
        DescriptionBase<?> descr = element.getInDescription();
462
        descr = CdmBase.deproxy(descr);
463

    
464
        String mainElementLabel = mainElementLabel(descr);
465

    
466
        String cache = null;
467
        //TextData
468
        if (element instanceof TextData) {
469
            LanguageString text = ((TextData) element).getPreferredLanguageString(defaultLanguage);
470
            if (text != null) {
471
                cache = text.getText();
472
            }
473
            cache = cache == null || isBlank(cache)? "empty" : StringUtils.truncate(cache, 20);
474
        //CommonTaxonName
475
        }else  if (element instanceof CommonTaxonName) {
476
            cache = ((CommonTaxonName) element).getName();
477
        //TaxonInteraction
478
        }else if (element instanceof TaxonInteraction) {
479
            Taxon taxon2 = ((TaxonInteraction) element).getTaxon2();
480
            if(taxon2 != null && taxon2.getName() != null){
481
                cache = taxon2.getName().getTitleCache();
482
            }else{
483
                cache = "No taxon chosen";
484
            }
485
         //IndividualsAssociation
486
        }else if (element instanceof IndividualsAssociation) {
487
            SpecimenOrObservationBase<?> unit = ((IndividualsAssociation) element).getAssociatedSpecimenOrObservation();
488
            if(unit != null){
489
                cache = unit.getIdentityCache();
490
                if (isBlank(cache)) {
491
                    cache = unit.getTitleCache();
492
                }
493
            }else{
494
                cache = "No unit chosen";
495
            }
496
        //Distribution
497
        }else if (element instanceof Distribution) {
498
            Distribution distribution = (Distribution) element;
499

    
500
            NamedArea area = distribution.getArea();
501
            if(area != null){
502
                cache =  area.getLabel();
503

    
504
                PresenceAbsenceTerm status = distribution.getStatus();
505
                if (status == null){
506
                    cache += ", no status";
507
                }else {
508
                    cache += ", " + status.getLabel();
509
                }
510
            }
511
        //CategoricalData
512
        }else if (element instanceof CategoricalData) {
513
            CategoricalData categoricalData = (CategoricalData) element;
514

    
515
            cache = CategoricalDataFormatter.NewInstance(new FormatKey[] {}).format(categoricalData);
516
        //QuantitativeData
517
        }else if (element instanceof QuantitativeData) {
518
            QuantitativeData quantitativeData = (QuantitativeData) element;
519

    
520
            cache = QuantitativeDataFormatter.NewInstance(new FormatKey[] {}).format(quantitativeData);
521
        //CategoricalData
522
        }else if (element instanceof TemporalData) {
523
            TemporalData temporalData = (TemporalData) element;
524
            cache = temporalData.toString();
525
        }
526

    
527
        String result = cache == null ? "" : cache;
528
        result = concatWithMainElement(mainElementLabel, result);
529
        return result;
530
    }
531

    
532
    private static String getCache(StateData stateData,
533
            Language defaultLanguage) {
534
        String cache = null;
535
        if (stateData.getState() != null) {
536
            Representation rep = stateData.getState().getPreferredRepresentation(defaultLanguage);
537
            if (rep != null) {
538
                cache = rep.getLabel();
539
            }
540
        }
541
        cache = isBlank(cache)? stateData.getUuid().toString() : cache;
542
        DescriptionBase<?> desc = stateData.getCategoricalData() == null? null : stateData.getCategoricalData().getInDescription();
543
        String mainElementLabel = mainElementLabel(desc);
544
        return concatWithMainElement(mainElementLabel, cache);
545
    }
546

    
547
    private static String getCache(StatisticalMeasurementValue smv, Language defaultLanguage) {
548
        String cache = null;
549
        if (smv.getType() != null) {
550
            Representation rep = smv.getType().getPreferredRepresentation(defaultLanguage);
551
            if (rep != null) {
552
                cache = rep.getLabel();
553
            }
554
        }
555
        if (smv.getValue() != null) {
556
            cache = CdmUtils.concat("=", cache, smv.getValue().toString());
557
        }
558
        cache = isBlank(cache)? smv.getUuid().toString() : cache;
559
        DescriptionBase<?> desc = smv.getQuantitativeData() == null? null : smv.getQuantitativeData().getInDescription();
560
        String mainElementLabel = mainElementLabel(desc);
561
        return concatWithMainElement(mainElementLabel, cache);
562
    }
563

    
564
    /**
565
     * Returns the label of the main element (taxon, specimen or name) for
566
     * the given description.
567
     */
568
    private static String mainElementLabel(DescriptionBase<?> descr) {
569
        String mainElementLabel = null;
570
        if (descr != null){
571
            if (descr.isInstanceOf(TaxonDescription.class)){
572
                Taxon taxon = CdmBase.deproxy(descr, TaxonDescription.class).getTaxon();
573
                if (taxon != null){
574
                    mainElementLabel = taxon.getTitleCache();
575
                }
576
            }else if (descr.isInstanceOf(SpecimenDescription.class)){
577
                SpecimenOrObservationBase<?> specimen = CdmBase.deproxy(descr, SpecimenDescription.class).getDescribedSpecimenOrObservation();
578
                if (specimen != null){
579
                    mainElementLabel = specimen.getTitleCache();
580
                }
581
            }else if (descr.isInstanceOf(TaxonNameDescription.class)){
582
                TaxonName name = CdmBase.deproxy(descr, TaxonNameDescription.class).getTaxonName();
583
                if (name != null){
584
                    mainElementLabel = name.getTitleCache();
585
                }
586
            }
587
        }
588
        return mainElementLabel;
589
    }
590

    
591
    private static String concatWithMainElement(String mainElementLabel, String result) {
592
        if (isNotBlank(mainElementLabel)){
593
            result = CdmUtils.concat(" ", result, "(" + mainElementLabel + ")");
594
        }
595
        return result;
596
    }
597
}
(10-10/10)