1
|
/**
|
2
|
*
|
3
|
*/
|
4
|
package eu.etaxonomy.cdm.strategy.cache.name;
|
5
|
|
6
|
import java.util.ArrayList;
|
7
|
import java.util.List;
|
8
|
import java.util.UUID;
|
9
|
|
10
|
import org.apache.log4j.Logger;
|
11
|
|
12
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
13
|
import eu.etaxonomy.cdm.model.agent.INomenclaturalAuthor;
|
14
|
import eu.etaxonomy.cdm.model.agent.Team;
|
15
|
import eu.etaxonomy.cdm.model.name.NonViralName;
|
16
|
import eu.etaxonomy.cdm.model.name.Rank;
|
17
|
import eu.etaxonomy.cdm.model.reference.INomenclaturalReference;
|
18
|
|
19
|
|
20
|
/**
|
21
|
* This class is a default implementation for the INonViralNameCacheStrategy<T extends NonViralName> interface.
|
22
|
* The method actually implements a cache strategy for botanical names so no method has to be overwritten by
|
23
|
* a subclass for botanic names.
|
24
|
* Where differing from this Default BotanicNameCacheStrategy other subclasses should overwrite the existing methods
|
25
|
* e.g. a CacheStrategy for zoological names should overwrite getAuthorAndExAuthor
|
26
|
* @author a.mueller
|
27
|
*/
|
28
|
/**
|
29
|
* @author AM
|
30
|
*
|
31
|
* @param <T>
|
32
|
*/
|
33
|
public class NonViralNameDefaultCacheStrategy<T extends NonViralName> extends NameCacheStrategyBase<T> implements INonViralNameCacheStrategy<T> {
|
34
|
private static final Logger logger = Logger.getLogger(NonViralNameDefaultCacheStrategy.class);
|
35
|
|
36
|
final static UUID uuid = UUID.fromString("1cdda0d1-d5bc-480f-bf08-40a510a2f223");
|
37
|
|
38
|
protected String NameAuthorSeperator = " ";
|
39
|
protected String BasionymStart = "(";
|
40
|
protected String BasionymEnd = ")";
|
41
|
protected String ExAuthorSeperator = " ex ";
|
42
|
protected CharSequence BasionymAuthorCombinationAuthorSeperator = " ";
|
43
|
|
44
|
@Override
|
45
|
public UUID getUuid(){
|
46
|
return uuid;
|
47
|
}
|
48
|
|
49
|
|
50
|
/**
|
51
|
* Factory method
|
52
|
* @return NonViralNameDefaultCacheStrategy A new instance of NonViralNameDefaultCacheStrategy
|
53
|
*/
|
54
|
public static NonViralNameDefaultCacheStrategy NewInstance(){
|
55
|
return new NonViralNameDefaultCacheStrategy();
|
56
|
}
|
57
|
|
58
|
/**
|
59
|
* Constructor
|
60
|
*/
|
61
|
protected NonViralNameDefaultCacheStrategy(){
|
62
|
super();
|
63
|
}
|
64
|
|
65
|
/* **************** GETTER / SETTER **************************************/
|
66
|
|
67
|
/**
|
68
|
* String that separates the NameCache part from the AuthorCache part
|
69
|
* @return
|
70
|
*/
|
71
|
public String getNameAuthorSeperator() {
|
72
|
return NameAuthorSeperator;
|
73
|
}
|
74
|
|
75
|
|
76
|
public void setNameAuthorSeperator(String nameAuthorSeperator) {
|
77
|
NameAuthorSeperator = nameAuthorSeperator;
|
78
|
}
|
79
|
|
80
|
|
81
|
/**
|
82
|
* String the basionym author part starts with e.g. '('.
|
83
|
* This should correspond with the {@link NonViralNameDefaultCacheStrategy#getBasionymEnd() basionymEnd} attribute
|
84
|
* @return
|
85
|
*/
|
86
|
public String getBasionymStart() {
|
87
|
return BasionymStart;
|
88
|
}
|
89
|
|
90
|
|
91
|
public void setBasionymStart(String basionymStart) {
|
92
|
BasionymStart = basionymStart;
|
93
|
}
|
94
|
|
95
|
|
96
|
/**
|
97
|
* String the basionym author part ends with e.g. ')'.
|
98
|
* This should correspond with the {@link NonViralNameDefaultCacheStrategy#getBasionymStart() basionymStart} attribute
|
99
|
* @return
|
100
|
*/
|
101
|
public String getBasionymEnd() {
|
102
|
return BasionymEnd;
|
103
|
}
|
104
|
|
105
|
|
106
|
public void setBasionymEnd(String basionymEnd) {
|
107
|
BasionymEnd = basionymEnd;
|
108
|
}
|
109
|
|
110
|
|
111
|
/**
|
112
|
* String to seperate ex author from author.
|
113
|
* @return
|
114
|
*/
|
115
|
public String getExAuthorSeperator() {
|
116
|
return ExAuthorSeperator;
|
117
|
}
|
118
|
|
119
|
|
120
|
public void setExAuthorSeperator(String exAuthorSeperator) {
|
121
|
ExAuthorSeperator = exAuthorSeperator;
|
122
|
}
|
123
|
|
124
|
|
125
|
/**
|
126
|
* String that seperates the basionym/original_combination author part from the combination author part
|
127
|
* @return
|
128
|
*/
|
129
|
public CharSequence getBasionymAuthorCombinationAuthorSeperator() {
|
130
|
return BasionymAuthorCombinationAuthorSeperator;
|
131
|
}
|
132
|
|
133
|
|
134
|
public void setBasionymAuthorCombinationAuthorSeperator(
|
135
|
CharSequence basionymAuthorCombinationAuthorSeperator) {
|
136
|
BasionymAuthorCombinationAuthorSeperator = basionymAuthorCombinationAuthorSeperator;
|
137
|
}
|
138
|
|
139
|
|
140
|
//** *****************************************************************************************/
|
141
|
|
142
|
|
143
|
/* (non-Javadoc)
|
144
|
* @see eu.etaxonomy.cdm.strategy.INameCacheStrategy#getNameCache()
|
145
|
*/
|
146
|
// just for testing
|
147
|
@Override
|
148
|
public String getTitleCache(T nonViralName) {
|
149
|
if (nonViralName == null){
|
150
|
return null;
|
151
|
}
|
152
|
String result = "";
|
153
|
if (isAutonym(nonViralName)){
|
154
|
String speciesPart = getSpeciesNameCache(nonViralName);
|
155
|
//TODO should this include basionym authors and ex authors
|
156
|
INomenclaturalAuthor author = nonViralName.getCombinationAuthorTeam();
|
157
|
String authorPart = "";
|
158
|
if (author != null){
|
159
|
authorPart = CdmUtils.Nz(author.getNomenclaturalTitle());
|
160
|
}
|
161
|
INomenclaturalAuthor basAuthor = nonViralName.getBasionymAuthorTeam();
|
162
|
String basAuthorPart = "";
|
163
|
if (basAuthor != null){
|
164
|
basAuthorPart = CdmUtils.Nz(basAuthor.getNomenclaturalTitle());
|
165
|
}
|
166
|
if (! "".equals(basAuthorPart)){
|
167
|
authorPart = "("+ basAuthorPart +")" + authorPart;
|
168
|
}
|
169
|
String infraSpeciesPart = (CdmUtils.Nz(nonViralName.getInfraSpecificEpithet()));
|
170
|
result = CdmUtils.concat(" ", new String[]{speciesPart, authorPart, infraSpeciesPart});
|
171
|
result = result.trim().replace("null", "");
|
172
|
}else{
|
173
|
String nameCache = CdmUtils.Nz(getNameCache(nonViralName));
|
174
|
String authorCache = CdmUtils.Nz(getAuthorshipCache(nonViralName));
|
175
|
result = CdmUtils.concat(NameAuthorSeperator, nameCache, authorCache);
|
176
|
}
|
177
|
return result;
|
178
|
}
|
179
|
|
180
|
|
181
|
/**
|
182
|
* Generates and returns the "name cache" (only scientific name without author teams and year).
|
183
|
* @see eu.etaxonomy.cdm.strategy.cache.name.INameCacheStrategy#getNameCache(eu.etaxonomy.cdm.model.name.TaxonNameBase)
|
184
|
*/
|
185
|
public String getNameCache(T nonViralName) {
|
186
|
if (nonViralName == null){
|
187
|
return null;
|
188
|
}
|
189
|
String result;
|
190
|
Rank rank = nonViralName.getRank();
|
191
|
|
192
|
if (rank == null){
|
193
|
result = getRanklessNameCache(nonViralName);
|
194
|
}else if (rank.isInfraSpecific()){
|
195
|
result = getInfraSpeciesNameCache(nonViralName);
|
196
|
}else if (rank.isSpecies()){
|
197
|
result = getSpeciesNameCache(nonViralName);
|
198
|
}else if (rank.isInfraGeneric()){
|
199
|
result = getInfraGenusNameCache(nonViralName);
|
200
|
}else if (rank.isGenus()){
|
201
|
result = getGenusOrUninomialNameCache(nonViralName);
|
202
|
}else if (rank.isSupraGeneric()){
|
203
|
result = getGenusOrUninomialNameCache(nonViralName);
|
204
|
}else{
|
205
|
logger.warn("Name Strategy for Name (UUID: " + nonViralName.getUuid() + ") not yet implemented");
|
206
|
result = "";
|
207
|
}
|
208
|
return result;
|
209
|
}
|
210
|
|
211
|
|
212
|
/* (non-Javadoc)
|
213
|
* @see eu.etaxonomy.cdm.strategy.cache.INonViralNameCacheStrategy#getAuthorCache(eu.etaxonomy.cdm.model.name.NonViralName)
|
214
|
*/
|
215
|
public String getAuthorshipCache(T nonViralName) {
|
216
|
if (nonViralName == null){
|
217
|
return null;
|
218
|
}
|
219
|
String result = "";
|
220
|
INomenclaturalAuthor combinationAuthor = nonViralName.getCombinationAuthorTeam();
|
221
|
INomenclaturalAuthor exCombinationAuthor = nonViralName.getExCombinationAuthorTeam();
|
222
|
INomenclaturalAuthor basionymAuthor = nonViralName.getBasionymAuthorTeam();
|
223
|
INomenclaturalAuthor exBasionymAuthor = nonViralName.getExBasionymAuthorTeam();
|
224
|
String basionymPart = "";
|
225
|
String authorPart = "";
|
226
|
//basionym
|
227
|
if (basionymAuthor != null || exBasionymAuthor != null){
|
228
|
basionymPart = BasionymStart + getAuthorAndExAuthor(basionymAuthor, exBasionymAuthor) + BasionymEnd;
|
229
|
}
|
230
|
if (combinationAuthor != null || exCombinationAuthor != null){
|
231
|
authorPart = getAuthorAndExAuthor(combinationAuthor, exCombinationAuthor);
|
232
|
}
|
233
|
result = CdmUtils.concat(BasionymAuthorCombinationAuthorSeperator, basionymPart, authorPart);
|
234
|
return result;
|
235
|
}
|
236
|
|
237
|
/**
|
238
|
* Returns the AuthorCache part for a combination of an author and an ex author. This applies on combination authors
|
239
|
* as well as on basionym/orginal combination authors.
|
240
|
* @param author the author
|
241
|
* @param exAuthor the ex-author
|
242
|
* @return
|
243
|
*/
|
244
|
protected String getAuthorAndExAuthor(INomenclaturalAuthor author, INomenclaturalAuthor exAuthor){
|
245
|
String result = "";
|
246
|
String authorString = "";
|
247
|
String exAuthorString = "";
|
248
|
if (author != null){
|
249
|
authorString = CdmUtils.Nz(author.getNomenclaturalTitle());
|
250
|
}
|
251
|
if (exAuthor != null){
|
252
|
exAuthorString = CdmUtils.Nz(exAuthor.getNomenclaturalTitle());
|
253
|
}
|
254
|
if (exAuthorString.length() > 0 ){
|
255
|
exAuthorString = exAuthorString + ExAuthorSeperator;
|
256
|
}
|
257
|
result = exAuthorString + authorString;
|
258
|
return result;
|
259
|
|
260
|
}
|
261
|
|
262
|
|
263
|
/* (non-Javadoc)
|
264
|
* @see eu.etaxonomy.cdm.strategy.INameCacheStrategy#getTaggedName(eu.etaxonomy.cdm.model.common.CdmBase)
|
265
|
*/
|
266
|
@Override
|
267
|
public List<Object> getTaggedName(T nonViralName) {
|
268
|
List<Object> tags = new ArrayList<Object>();
|
269
|
tags.add(nonViralName.getGenusOrUninomial());
|
270
|
if (nonViralName.isSpecies() || nonViralName.isInfraSpecific()){
|
271
|
tags.add(nonViralName.getSpecificEpithet());
|
272
|
}
|
273
|
if (nonViralName.isInfraSpecific()){
|
274
|
tags.add(nonViralName.getRank());
|
275
|
tags.add(nonViralName.getInfraSpecificEpithet());
|
276
|
}
|
277
|
if (nonViralName.isInfraGeneric()){
|
278
|
//TODO choose right strategy or generic approach?
|
279
|
// --- strategy 1 ---
|
280
|
tags.add(nonViralName.getRank());
|
281
|
tags.add(nonViralName.getInfraGenericEpithet());
|
282
|
// --- strategy 2 ---
|
283
|
// tags.add('('+nvn.getInfraGenericEpithet()+')');
|
284
|
}
|
285
|
Team authorTeam = Team.NewInstance();
|
286
|
authorTeam.setProtectedTitleCache(true);
|
287
|
authorTeam.setTitleCache(nonViralName.getAuthorshipCache());
|
288
|
tags.add(authorTeam);
|
289
|
if (nonViralName.getNomenclaturalReference() != null){
|
290
|
INomenclaturalReference nomenclaturalReference = nonViralName.getNomenclaturalReference();
|
291
|
tags.add(nomenclaturalReference);//nonViralName.getNomenclaturalReference().getNomenclaturalCitation(nonViralName.getNomenclaturalMicroReference()));
|
292
|
}else{
|
293
|
tags.add("");
|
294
|
}
|
295
|
return tags;
|
296
|
}
|
297
|
|
298
|
|
299
|
/************** PRIVATES ****************/
|
300
|
|
301
|
protected String getRanklessNameCache(NonViralName nonViralName){
|
302
|
String result = "";
|
303
|
result = (result + (nonViralName.getGenusOrUninomial())).trim().replace("null", "-");
|
304
|
result += " " + (CdmUtils.Nz(nonViralName.getSpecificEpithet())).trim();
|
305
|
result += " " + (CdmUtils.Nz(nonViralName.getInfraSpecificEpithet())).trim();
|
306
|
result = result.trim().replace("null", "-");
|
307
|
//result += " (rankless)";
|
308
|
result = addAppendedPhrase(result, nonViralName);
|
309
|
return result;
|
310
|
}
|
311
|
|
312
|
|
313
|
protected String getGenusOrUninomialNameCache(NonViralName nonViralName){
|
314
|
String result;
|
315
|
result = CdmUtils.Nz(nonViralName.getGenusOrUninomial());
|
316
|
result = addAppendedPhrase(result, nonViralName);
|
317
|
return result;
|
318
|
}
|
319
|
|
320
|
protected String getInfraGenusNameCache(NonViralName nonViralName){
|
321
|
String result;
|
322
|
result = CdmUtils.Nz(nonViralName.getGenusOrUninomial());
|
323
|
result += " (" + (CdmUtils.Nz(nonViralName.getInfraGenericEpithet()) + ")").trim().replace("null", "");
|
324
|
result = addAppendedPhrase(result, nonViralName);
|
325
|
return result;
|
326
|
}
|
327
|
|
328
|
|
329
|
protected String getSpeciesNameCache(NonViralName nonViralName){
|
330
|
String result;
|
331
|
result = CdmUtils.Nz(nonViralName.getGenusOrUninomial());
|
332
|
result += " " + CdmUtils.Nz(nonViralName.getSpecificEpithet()).trim().replace("null", "");
|
333
|
result = addAppendedPhrase(result, nonViralName);
|
334
|
return result;
|
335
|
}
|
336
|
|
337
|
|
338
|
protected String getInfraSpeciesNameCache(NonViralName nonViralName){
|
339
|
String result;
|
340
|
result = CdmUtils.Nz(nonViralName.getGenusOrUninomial());
|
341
|
result += " " + (CdmUtils.Nz(nonViralName.getSpecificEpithet()).trim()).replace("null", "");
|
342
|
if (! isAutonym(nonViralName)){
|
343
|
result += " " + (nonViralName.getRank().getAbbreviation()).trim().replace("null", "");
|
344
|
}
|
345
|
result += " " + (CdmUtils.Nz(nonViralName.getInfraSpecificEpithet())).trim().replace("null", "");
|
346
|
result = addAppendedPhrase(result, nonViralName);
|
347
|
return result;
|
348
|
}
|
349
|
|
350
|
|
351
|
/**
|
352
|
* @param name
|
353
|
* @return true, if name has Rank, Rank is below species and species epithet equals infraSpeciesEpithtet, else false
|
354
|
*/
|
355
|
protected boolean isAutonym(NonViralName nonViralName){
|
356
|
if (nonViralName != null && nonViralName.getRank() != null && nonViralName.getSpecificEpithet() != null && nonViralName.getInfraSpecificEpithet() != null &&
|
357
|
nonViralName.getRank().isInfraSpecific() && nonViralName.getSpecificEpithet().trim().equals(nonViralName.getInfraSpecificEpithet().trim())){
|
358
|
return true;
|
359
|
}else{
|
360
|
return false;
|
361
|
}
|
362
|
}
|
363
|
|
364
|
protected String addAppendedPhrase(String resultString, NonViralName nonViralName){
|
365
|
String appendedPhrase = nonViralName ==null ? null : nonViralName.getAppendedPhrase();
|
366
|
if (resultString == null){
|
367
|
return appendedPhrase;
|
368
|
}else if(appendedPhrase == null || "".equals(appendedPhrase.trim())) {
|
369
|
return resultString;
|
370
|
}else if ("".equals(resultString)){
|
371
|
return resultString + appendedPhrase;
|
372
|
}else {
|
373
|
return resultString + " " + appendedPhrase;
|
374
|
}
|
375
|
}
|
376
|
|
377
|
}
|