Project

General

Profile

Download (5.47 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.reference;
10

    
11
import eu.etaxonomy.cdm.common.CdmUtils;
12
import eu.etaxonomy.cdm.format.CdmFormatterBase;
13
import eu.etaxonomy.cdm.model.agent.Person;
14
import eu.etaxonomy.cdm.model.agent.Team;
15
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
16
import eu.etaxonomy.cdm.model.common.CdmBase;
17
import eu.etaxonomy.cdm.model.reference.OriginalSourceBase;
18
import eu.etaxonomy.cdm.model.reference.Reference;
19
import eu.etaxonomy.cdm.strategy.cache.agent.TeamDefaultCacheStrategy;
20

    
21
/**
22
 * @author a.mueller
23
 * @since 13.05.2021
24
 */
25
public class OriginalSourceFormatter extends CdmFormatterBase<OriginalSourceBase>{
26

    
27
    private final boolean withYearBrackets;
28

    
29
    public static OriginalSourceFormatter INSTANCE = new OriginalSourceFormatter(false);
30
    public static OriginalSourceFormatter INSTANCE_WITH_BRACKETS = new OriginalSourceFormatter(true);
31

    
32
    /**
33
      * @param withYearBrackets if <code>false</code> the result comes without brackets (default is <code>false</code>)
34
      */
35
    private OriginalSourceFormatter(boolean withYearBrackets) {
36
        this.withYearBrackets = withYearBrackets;
37
    }
38

    
39
    @Override
40
    public String format(OriginalSourceBase source) {
41
        if (source == null){
42
            return null;
43
        }
44
        Reference reference = source.getCitation();
45
        String microReference = source.getCitationMicroReference();
46
        if (reference == null && isBlank(microReference)){
47
            return null;
48
        }
49
        return format(reference, microReference);
50
    }
51

    
52
    /**
53
     * Creates a citation in form <i>author year: detail</i> or <i>author (year: detail)</i>.
54
     * <BR>
55
     * If reference has protected titlecache only the titlecache is returned (may change in future).
56
     * <BR>
57
     * The author team is abbreviated with <code>et al.</code> if more than 2 authors exist in the team.
58
     *
59
     * @param reference the reference to format
60
     * @param citationDetail the microreference (page, figure, etc.), if <code>null</code> also the colon separator is not used
61
     */
62
    public String format(Reference reference, String citationDetail){
63
        if (reference == null){
64
            return null;
65
        }
66

    
67
        if(reference.isProtectedTitleCache()){
68
            return handleCitationDetailInTitleCache(reference.getTitleCache(), citationDetail);
69
        }
70
        TeamOrPersonBase<?> authorship = reference.getAuthorship();
71
        String authorStr = "";
72
        if (authorship == null) {
73
            return handleCitationDetailInTitleCache(reference.getTitleCache(), citationDetail);
74
        }
75
        authorship = CdmBase.deproxy(authorship);
76
        if (authorship instanceof Person){
77
            authorStr = getPersonString((Person)authorship);
78
        }
79
        else if (authorship instanceof Team){
80

    
81
            Team team = CdmBase.deproxy(authorship, Team.class);
82
            if (team.isProtectedTitleCache()){
83
                authorStr = team.getTitleCache();
84
            }else{
85
                authorStr = TeamDefaultCacheStrategy.INSTANCE_ET_AL_2().getFamilyTitle(team);
86
            }
87
        }
88
        String result = CdmUtils.concat(" ", authorStr, getShortCitationDate(reference, withYearBrackets, citationDetail));
89

    
90
        return result;
91
    }
92

    
93
    private String getShortCitationDate(Reference reference, boolean withBrackets, String citationDetail) {
94
        String result = null;
95
        if (reference.getDatePublished() != null && !reference.getDatePublished().isEmpty()) {
96
            if (isNotBlank(reference.getDatePublished().getFreeText())){
97
                result = reference.getDatePublished().getFreeText();
98
            }else if (isNotBlank(reference.getYear()) ){
99
                result = reference.getYear();
100
            }
101
            if (isNotBlank(citationDetail)){
102
                result = Nz(result) + ": " + citationDetail;
103
            }
104
            if (isNotBlank(result) && withBrackets){
105
                result = "(" + result + ")";
106
            }
107
        }else if (reference.getInReference() != null){
108
            result = getShortCitationDate(reference.getInReference(), withBrackets, citationDetail);
109
        }
110
        return result;
111
    }
112

    
113
    private String getPersonString(Person person) {
114
        String shortCitation;
115
        shortCitation = person.getFamilyName();
116
        if (isBlank(shortCitation) ){
117
            shortCitation = person.getTitleCache();
118
        }
119
        return shortCitation;
120
    }
121

    
122
    /**
123
     * Adds the citationDetail to the protected titleCache assuming that this is not accurately parsed.
124
     * If the detail is contained in the titleCache it is not added again to the result, otherwise
125
     * it is concatenated with separator ":"
126
     * @return the concatenated string
127
     */
128
    private String handleCitationDetailInTitleCache(String titleCache, String citationDetail) {
129
        if (isBlank(citationDetail)){
130
            return titleCache;
131
        }else if (isBlank(titleCache)){
132
            return ": " + citationDetail;
133
        }else if (citationDetail.length() <= 3){
134
            if (titleCache.contains(": " + citationDetail)){
135
                return titleCache;
136
            }
137
        }else{
138
            if (titleCache.contains(citationDetail)){
139
                return titleCache;
140
            }
141
        }
142
        return titleCache + ": " + citationDetail;
143
    }
144
}
(2-2/3)