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
|
|
31
|
//this can be used e.g. for in-text references, like "... following the opinion of Autor (2000: 22) we posit that ..."
|
32
|
public static OriginalSourceFormatter INSTANCE_WITH_YEAR_BRACKETS = new OriginalSourceFormatter(true);
|
33
|
|
34
|
/**
|
35
|
* @param withYearBrackets if <code>false</code> the result comes without brackets (default is <code>false</code>)
|
36
|
*/
|
37
|
private OriginalSourceFormatter(boolean withYearBrackets) {
|
38
|
this.withYearBrackets = withYearBrackets;
|
39
|
}
|
40
|
|
41
|
@Override
|
42
|
public String format(OriginalSourceBase source) {
|
43
|
if (source == null){
|
44
|
return null;
|
45
|
}
|
46
|
Reference reference = source.getCitation();
|
47
|
String microReference = source.getCitationMicroReference();
|
48
|
if (reference == null && isBlank(microReference)){
|
49
|
return null;
|
50
|
}
|
51
|
return format(reference, microReference);
|
52
|
}
|
53
|
|
54
|
/**
|
55
|
* Creates a citation in form <i>author year: detail</i> or <i>author (year: detail)</i>.
|
56
|
* <BR>
|
57
|
* If reference has protected titlecache only the titlecache is returned (may change in future).
|
58
|
* <BR>
|
59
|
* The author team is abbreviated with <code>et al.</code> if more than 2 authors exist in the team.
|
60
|
*
|
61
|
* @param reference the reference to format
|
62
|
* @param citationDetail the microreference (page, figure, etc.), if <code>null</code> also the colon separator is not used
|
63
|
*/
|
64
|
public String format(Reference reference, String citationDetail){
|
65
|
if (reference == null){
|
66
|
return null;
|
67
|
}
|
68
|
|
69
|
if(reference.isProtectedTitleCache()){
|
70
|
return handleCitationDetailInTitleCache(reference.getTitleCache(), citationDetail);
|
71
|
}
|
72
|
TeamOrPersonBase<?> authorship = reference.getAuthorship();
|
73
|
String authorStr = "";
|
74
|
if (authorship == null) {
|
75
|
return handleCitationDetailInTitleCache(reference.getTitleCache(), citationDetail);
|
76
|
}
|
77
|
authorship = CdmBase.deproxy(authorship);
|
78
|
if (authorship instanceof Person){
|
79
|
authorStr = getPersonString((Person)authorship);
|
80
|
}
|
81
|
else if (authorship instanceof Team){
|
82
|
|
83
|
Team team = CdmBase.deproxy(authorship, Team.class);
|
84
|
if (team.isProtectedTitleCache()){
|
85
|
authorStr = team.getTitleCache();
|
86
|
}else{
|
87
|
authorStr = TeamDefaultCacheStrategy.INSTANCE_ET_AL_2().getFamilyTitle(team);
|
88
|
}
|
89
|
}
|
90
|
String result = CdmUtils.concat(" ", authorStr, getShortCitationDate(reference, withYearBrackets, citationDetail));
|
91
|
|
92
|
return result;
|
93
|
}
|
94
|
|
95
|
private String getShortCitationDate(Reference reference, boolean withBrackets, String citationDetail) {
|
96
|
String result = null;
|
97
|
if (reference.getDatePublished() != null && !reference.getDatePublished().isEmpty()) {
|
98
|
if (isNotBlank(reference.getDatePublished().getFreeText())){
|
99
|
result = reference.getDatePublished().getFreeText();
|
100
|
}else if (isNotBlank(reference.getYear()) ){
|
101
|
result = reference.getYear();
|
102
|
}
|
103
|
if (isNotBlank(citationDetail)){
|
104
|
result = Nz(result) + ": " + citationDetail;
|
105
|
}
|
106
|
if (isNotBlank(result) && withBrackets){
|
107
|
result = "(" + result + ")";
|
108
|
}
|
109
|
}else if (reference.getInReference() != null){
|
110
|
result = getShortCitationDate(reference.getInReference(), withBrackets, citationDetail);
|
111
|
}
|
112
|
return result;
|
113
|
}
|
114
|
|
115
|
private String getPersonString(Person person) {
|
116
|
String shortCitation;
|
117
|
shortCitation = person.getFamilyName();
|
118
|
if (isBlank(shortCitation) ){
|
119
|
shortCitation = person.getTitleCache();
|
120
|
}
|
121
|
return shortCitation;
|
122
|
}
|
123
|
|
124
|
/**
|
125
|
* Adds the citationDetail to the protected titleCache assuming that this is not accurately parsed.
|
126
|
* If the detail is contained in the titleCache it is not added again to the result, otherwise
|
127
|
* it is concatenated with separator ":"
|
128
|
* @return the concatenated string
|
129
|
*/
|
130
|
private String handleCitationDetailInTitleCache(String titleCache, String citationDetail) {
|
131
|
if (isBlank(citationDetail)){
|
132
|
return titleCache;
|
133
|
}else if (isBlank(titleCache)){
|
134
|
return ": " + citationDetail;
|
135
|
}else if (citationDetail.length() <= 3){
|
136
|
if (titleCache.contains(": " + citationDetail)){
|
137
|
return titleCache;
|
138
|
}
|
139
|
}else{
|
140
|
if (titleCache.contains(citationDetail)){
|
141
|
return titleCache;
|
142
|
}
|
143
|
}
|
144
|
return titleCache + ": " + citationDetail;
|
145
|
}
|
146
|
}
|