Project

General

Profile

Download (3.2 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 org.apache.commons.lang3.StringUtils;
12

    
13
import eu.etaxonomy.cdm.model.common.Language;
14
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
15
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
16
import eu.etaxonomy.cdm.model.description.Distribution;
17
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
18
import eu.etaxonomy.cdm.model.description.TaxonInteraction;
19
import eu.etaxonomy.cdm.model.description.TextData;
20
import eu.etaxonomy.cdm.model.location.NamedArea;
21
import eu.etaxonomy.cdm.model.taxon.Taxon;
22

    
23
/**
24
 * This formatting has been copied from TaxEditor to allow serverside formatting
25
 * for referencing objects.<BR>
26
 * Maybe in future it will become a common formatting class for {@link DescriptionElementBase}
27
 * instances or it will be moved back to TaxEditor and replaced by a general formatter
28
 * for {@link DescriptionElementBase}.
29
 *
30
 * @author a.mueller
31
 * @since 31.03.2021
32
 */
33
public class DescriptionElementFormatter {
34

    
35
    public static String format(DescriptionElementBase element, Language defaultLanguage){
36

    
37
//        String mainElementLabel= null;
38
//        DescriptionBase<?> descr = element.getInDescription();
39
//        descr = CdmBase.deproxy(descr);
40
//
41
//        if (descr != null){
42
//            IDescribable<?> target = CdmBase.deproxy(descr.describedEntity());
43
//            if (target != null){
44
//                mainElementLabel = target.getTitleCache();
45
//            }else{
46
//                return descr.getTitleCache();
47
//            }
48
//        }
49

    
50
        String cache = null;
51
        if (element instanceof TextData) {
52
            //cache = ((TextData) element).getText(language);
53
            cache = "Text Data";
54
        }
55
        if (element instanceof CommonTaxonName) {
56
            cache = ((CommonTaxonName) element).getName();
57
        }
58
        if (element instanceof TaxonInteraction) {
59
            Taxon taxon2 = ((TaxonInteraction) element).getTaxon2();
60
            if(taxon2 != null && taxon2.getName() != null){
61
                cache = taxon2.getName().getTitleCache();
62
            }else{
63
                cache = "No taxon chosen";
64
            }
65
        }
66
        if (element instanceof Distribution) {
67
            Distribution distribution = (Distribution) element;
68
            NamedArea area = distribution.getArea();
69
            if(area != null){
70
                cache =  area.getLabel();
71

    
72
                PresenceAbsenceTerm status = distribution.getStatus();
73
                if (status == null){
74
                    cache += ", no status";
75
                }else {
76
                    cache += ", " + status.getLabel();
77
                }
78
            }
79
        }
80
        String result = cache == null ? "" : cache;
81
//        if (isNotBlank(mainElementLabel)){
82
//            result = CdmUtils.concat(" ", result, "(" + mainElementLabel + ")");
83
//        }
84
        return result;
85

    
86
    }
87

    
88
    private static boolean isNotBlank(String str) {
89
        return StringUtils.isNotBlank(str);
90
    }
91
}
(6-6/10)