1
|
/**
|
2
|
* Copyright (C) 2007 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
|
|
10
|
package eu.etaxonomy.cdm.strategy.cache.reference;
|
11
|
|
12
|
import org.apache.commons.lang.StringUtils;
|
13
|
import org.apache.log4j.Logger;
|
14
|
|
15
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
16
|
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
|
17
|
import eu.etaxonomy.cdm.model.reference.INomenclaturalReference;
|
18
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
19
|
import eu.etaxonomy.cdm.strategy.StrategyBase;
|
20
|
|
21
|
/**
|
22
|
* @author a.mueller
|
23
|
* @created 29.06.2008
|
24
|
*/
|
25
|
public abstract class NomRefDefaultCacheStrategyBase extends StrategyBase implements INomenclaturalReferenceCacheStrategy{
|
26
|
private static final long serialVersionUID = -725290113353165022L;
|
27
|
|
28
|
private static final Logger logger = Logger.getLogger(NomRefDefaultCacheStrategyBase.class);
|
29
|
|
30
|
protected String beforeYear = ". ";
|
31
|
protected String beforeMicroReference = ": ";
|
32
|
protected String afterYear = "";
|
33
|
protected String afterAuthor = ", ";
|
34
|
|
35
|
|
36
|
/**
|
37
|
* Returns the nomenclatural title with micro reference represented as token
|
38
|
* which can later be replaced by the real data.
|
39
|
*
|
40
|
* @see INomenclaturalReference#MICRO_REFERENCE_TOKEN
|
41
|
*
|
42
|
* @param ref The reference
|
43
|
* @return
|
44
|
*/
|
45
|
protected String getTokenizedNomenclaturalTitel(Reference ref) {
|
46
|
String result = getTitleWithoutYearAndAuthor(ref, true);
|
47
|
result += INomenclaturalReference.MICRO_REFERENCE_TOKEN;
|
48
|
result = addYear(result, ref, true);
|
49
|
return result;
|
50
|
}
|
51
|
|
52
|
@Override
|
53
|
public String getTitleCache(Reference nomenclaturalReference) {
|
54
|
return getTitleCache(nomenclaturalReference, false);
|
55
|
}
|
56
|
|
57
|
@Override
|
58
|
public String getAbbrevTitleCache(Reference nomenclaturalReference) {
|
59
|
return getTitleCache(nomenclaturalReference, true);
|
60
|
}
|
61
|
|
62
|
private String getTitleCache(Reference ref, boolean isAbbrev) {
|
63
|
//TODO needed?
|
64
|
if (isAbbrev && ref.isProtectedAbbrevTitleCache()){
|
65
|
return ref.getAbbrevTitleCache();
|
66
|
}else if (!isAbbrev && ref.isProtectedTitleCache()){
|
67
|
return ref.getTitleCache();
|
68
|
}
|
69
|
String result = getTitleWithoutYearAndAuthor(ref, isAbbrev);
|
70
|
result = addYear(result, ref, false);
|
71
|
TeamOrPersonBase<?> team = ref.getAuthorship();
|
72
|
|
73
|
if (team != null){
|
74
|
String teamTitle = CdmUtils.getPreferredNonEmptyString(team.getTitleCache(), team.getNomenclaturalTitle(), isAbbrev, true);
|
75
|
if (teamTitle.length() > 0 ){
|
76
|
String concat = isNotBlank(result) ? afterAuthor : "";
|
77
|
result = teamTitle + concat + result;
|
78
|
}
|
79
|
|
80
|
}
|
81
|
return result;
|
82
|
}
|
83
|
|
84
|
protected abstract String getTitleWithoutYearAndAuthor(Reference reference, boolean isAbbrev);
|
85
|
|
86
|
|
87
|
@Override
|
88
|
public String getCitation(Reference referenceBase) {
|
89
|
StringBuilder stringBuilder = new StringBuilder();
|
90
|
|
91
|
String nextConcat = "";
|
92
|
|
93
|
TeamOrPersonBase<?> team = referenceBase.getAuthorship();
|
94
|
if (team != null && isNotBlank(team.getTitleCache())){
|
95
|
stringBuilder.append(team.getTitleCache() );
|
96
|
nextConcat = afterAuthor;
|
97
|
}
|
98
|
|
99
|
String year = referenceBase.getYear();
|
100
|
if (StringUtils.isNotBlank(year)){
|
101
|
stringBuilder.append(nextConcat + year);
|
102
|
}
|
103
|
|
104
|
return stringBuilder.toString();
|
105
|
}
|
106
|
|
107
|
|
108
|
@Override
|
109
|
public String getBeforeMicroReference(){
|
110
|
return beforeMicroReference;
|
111
|
}
|
112
|
|
113
|
protected String addYear(String string, Reference nomRef, boolean useFullDatePublished){
|
114
|
String result;
|
115
|
if (string == null){
|
116
|
return null;
|
117
|
}
|
118
|
String year = useFullDatePublished ? nomRef.getDatePublishedString() : nomRef.getYear();
|
119
|
if (isBlank(year)){
|
120
|
result = string + afterYear;
|
121
|
}else{
|
122
|
String concat = isBlank(string) ? "" : string.endsWith(".") ? " " : beforeYear;
|
123
|
result = string + concat + year + afterYear;
|
124
|
}
|
125
|
return result;
|
126
|
}
|
127
|
|
128
|
@Override
|
129
|
public String getNomenclaturalCitation(Reference nomenclaturalReference, String microReference) {
|
130
|
if (nomenclaturalReference.isProtectedAbbrevTitleCache()){
|
131
|
String cache = nomenclaturalReference.getAbbrevTitleCache();
|
132
|
return handleDetailAndYearForPreliminary(nomenclaturalReference, cache, microReference);
|
133
|
|
134
|
}
|
135
|
String result = getTokenizedNomenclaturalTitel(nomenclaturalReference);
|
136
|
//if no data is available and only titleCache is protected take the protected title
|
137
|
//this is to avoid empty cache if someone forgets to set also the abbrevTitleCache
|
138
|
//we need to think about handling protected not separate for abbrevTitleCache and titleCache
|
139
|
if (result.equals(INomenclaturalReference.MICRO_REFERENCE_TOKEN) && nomenclaturalReference.isProtectedTitleCache() ){
|
140
|
String cache = nomenclaturalReference.getTitleCache();
|
141
|
return handleDetailAndYearForPreliminary(nomenclaturalReference, cache, microReference);
|
142
|
}
|
143
|
|
144
|
microReference = Nz(microReference);
|
145
|
if (StringUtils.isNotBlank(microReference)){
|
146
|
microReference = getBeforeMicroReference() + microReference;
|
147
|
if (microReference.endsWith(".") && result.contains(INomenclaturalReference.MICRO_REFERENCE_TOKEN + ".") ){
|
148
|
microReference = microReference.substring(0, microReference.length() - 1);
|
149
|
}
|
150
|
}
|
151
|
result = replaceMicroRefToken(microReference, result);
|
152
|
if (result.startsWith(". ")){ //only year available, remove '. '
|
153
|
result = result.substring(2);
|
154
|
}
|
155
|
return result;
|
156
|
}
|
157
|
|
158
|
/**
|
159
|
* @param microReference
|
160
|
* @param result
|
161
|
* @return
|
162
|
*/
|
163
|
private String replaceMicroRefToken(String microReference, String string) {
|
164
|
int index = string.indexOf(INomenclaturalReference.MICRO_REFERENCE_TOKEN);
|
165
|
|
166
|
if (index > -1){
|
167
|
String before = string.substring(0, index);
|
168
|
String after = string.substring(index + INomenclaturalReference.MICRO_REFERENCE_TOKEN.length() );
|
169
|
String localMicroReference = microReference.trim(); //needed ?
|
170
|
if (after.length() > 0){
|
171
|
if ( ("".equals(localMicroReference) && before.endsWith(after.substring(0,1)) || localMicroReference.endsWith(after.substring(0,1)))){
|
172
|
after = after.substring(1);
|
173
|
}
|
174
|
}
|
175
|
String result = before + localMicroReference + after;
|
176
|
return result;
|
177
|
}else{
|
178
|
return string;
|
179
|
}
|
180
|
}
|
181
|
|
182
|
/**
|
183
|
* @param nomenclaturalReference
|
184
|
* @param microRef
|
185
|
* @return
|
186
|
*/
|
187
|
private String handleDetailAndYearForPreliminary(Reference nomenclaturalReference, String cache, String microReference) {
|
188
|
String microRef = isNotBlank(microReference) ? getBeforeMicroReference() + microReference : "";
|
189
|
if (cache == null){
|
190
|
logger.warn("Cache is null. This should never be the case.");
|
191
|
cache = "";
|
192
|
}
|
193
|
String result = cache + (cache.contains(microRef) ? "" : microRef);
|
194
|
|
195
|
String date = nomenclaturalReference.getDatePublishedString();
|
196
|
if (isNotBlank(date) && ! result.contains(date)){
|
197
|
result = result + beforeYear + date;
|
198
|
}
|
199
|
return result;
|
200
|
}
|
201
|
|
202
|
}
|