2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.strategy
.cache
.name
;
11 import java
.util
.ArrayList
;
12 import java
.util
.Iterator
;
13 import java
.util
.List
;
15 import java
.util
.UUID
;
17 import org
.apache
.log4j
.Logger
;
19 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
20 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
21 import eu
.etaxonomy
.cdm
.model
.agent
.INomenclaturalAuthor
;
22 import eu
.etaxonomy
.cdm
.model
.agent
.Team
;
23 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
24 import eu
.etaxonomy
.cdm
.model
.common
.Representation
;
25 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalStatus
;
26 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalStatusType
;
27 import eu
.etaxonomy
.cdm
.model
.name
.NonViralName
;
28 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
29 import eu
.etaxonomy
.cdm
.model
.reference
.INomenclaturalReference
;
30 import eu
.etaxonomy
.cdm
.strategy
.exceptions
.UnknownCdmTypeException
;
34 * This class is a default implementation for the INonViralNameCacheStrategy<T extends NonViralName> interface.
35 * The method actually implements a cache strategy for botanical names so no method has to be overwritten by
36 * a subclass for botanic names.
37 * Where differing from this Default BotanicNameCacheStrategy other subclasses should overwrite the existing methods
38 * e.g. a CacheStrategy for zoological names should overwrite getAuthorAndExAuthor
46 public class NonViralNameDefaultCacheStrategy
<T
extends NonViralName
> extends NameCacheStrategyBase
<T
> implements INonViralNameCacheStrategy
<T
> {
47 private static final Logger logger
= Logger
.getLogger(NonViralNameDefaultCacheStrategy
.class);
49 final static UUID uuid
= UUID
.fromString("1cdda0d1-d5bc-480f-bf08-40a510a2f223");
51 protected String NameAuthorSeperator
= " ";
52 protected String BasionymStart
= "(";
53 protected String BasionymEnd
= ")";
54 protected String ExAuthorSeperator
= " ex ";
55 protected CharSequence BasionymAuthorCombinationAuthorSeperator
= " ";
58 public UUID
getUuid(){
65 * @return NonViralNameDefaultCacheStrategy A new instance of NonViralNameDefaultCacheStrategy
67 public static NonViralNameDefaultCacheStrategy
NewInstance(){
68 return new NonViralNameDefaultCacheStrategy();
74 protected NonViralNameDefaultCacheStrategy(){
78 /* **************** GETTER / SETTER **************************************/
81 * String that separates the NameCache part from the AuthorCache part
84 public String
getNameAuthorSeperator() {
85 return NameAuthorSeperator
;
89 public void setNameAuthorSeperator(String nameAuthorSeperator
) {
90 NameAuthorSeperator
= nameAuthorSeperator
;
95 * String the basionym author part starts with e.g. '('.
96 * This should correspond with the {@link NonViralNameDefaultCacheStrategy#getBasionymEnd() basionymEnd} attribute
99 public String
getBasionymStart() {
100 return BasionymStart
;
104 public void setBasionymStart(String basionymStart
) {
105 BasionymStart
= basionymStart
;
110 * String the basionym author part ends with e.g. ')'.
111 * This should correspond with the {@link NonViralNameDefaultCacheStrategy#getBasionymStart() basionymStart} attribute
114 public String
getBasionymEnd() {
119 public void setBasionymEnd(String basionymEnd
) {
120 BasionymEnd
= basionymEnd
;
125 * String to seperate ex author from author.
128 public String
getExAuthorSeperator() {
129 return ExAuthorSeperator
;
133 public void setExAuthorSeperator(String exAuthorSeperator
) {
134 ExAuthorSeperator
= exAuthorSeperator
;
139 * String that seperates the basionym/original_combination author part from the combination author part
142 public CharSequence
getBasionymAuthorCombinationAuthorSeperator() {
143 return BasionymAuthorCombinationAuthorSeperator
;
147 public void setBasionymAuthorCombinationAuthorSeperator(
148 CharSequence basionymAuthorCombinationAuthorSeperator
) {
149 BasionymAuthorCombinationAuthorSeperator
= basionymAuthorCombinationAuthorSeperator
;
153 //** *****************************************************************************************/
157 * @see eu.etaxonomy.cdm.strategy.INameCacheStrategy#getNameCache()
160 public String
getTitleCache(T nonViralName
) {
161 if (nonViralName
== null){
165 if (nonViralName
.isProtectedTitleCache()){
166 return nonViralName
.getTitleCache();
170 if (isAutonym(nonViralName
)){
171 String speciesPart
= getSpeciesNameCache(nonViralName
);
172 //TODO should this include basionym authors and ex authors
173 INomenclaturalAuthor author
= nonViralName
.getCombinationAuthorTeam();
174 String authorPart
= "";
176 authorPart
= CdmUtils
.Nz(author
.getNomenclaturalTitle());
178 INomenclaturalAuthor basAuthor
= nonViralName
.getBasionymAuthorTeam();
179 String basAuthorPart
= "";
180 if (basAuthor
!= null){
181 basAuthorPart
= CdmUtils
.Nz(basAuthor
.getNomenclaturalTitle());
183 if (! "".equals(basAuthorPart
)){
184 authorPart
= "("+ basAuthorPart
+")" + authorPart
;
186 String infraSpeciesPart
= (CdmUtils
.Nz(nonViralName
.getInfraSpecificEpithet()));
188 String infraSpeciesSeparator
= "";
189 if (nonViralName
.getRank() == null || !nonViralName
.getRank().isInfraSpecific()){
190 //TODO handle exception
191 logger
.warn("Rank for autonym does not exist or is not lower than species !!");
193 infraSpeciesSeparator
= nonViralName
.getRank().getAbbreviation();
196 result
= CdmUtils
.concat(" ", new String
[]{speciesPart
, authorPart
, infraSpeciesSeparator
, infraSpeciesPart
});
197 result
= result
.trim().replace("null", "");
199 String nameCache
= nonViralName
.getNameCache(); //OLD: CdmUtils.Nz(getNameCache(nonViralName));
200 if (nameIncludesAuthorship(nonViralName
)){
201 String authorCache
= CdmUtils
.Nz(getAuthorshipCache(nonViralName
));
202 result
= CdmUtils
.concat(NameAuthorSeperator
, nameCache
, authorCache
);
210 protected boolean nameIncludesAuthorship(NonViralName nonViralName
){
211 Rank rank
= nonViralName
.getRank();
212 if (rank
!= null && rank
.isSpeciesAggregate()){
221 public String
getFullTitleCache(T nonViralName
) {
223 if (nonViralName
== null){
227 if (nonViralName
.isProtectedFullTitleCache() == true) {
228 return nonViralName
.getFullTitleCache();
233 String titleCache
= nonViralName
.getTitleCache(); // OLD: getTitleCache(nonViralName);
235 String microReference
= nonViralName
.getNomenclaturalMicroReference();
236 INomenclaturalReference ref
= nonViralName
.getNomenclaturalReference();
237 String referenceBaseCache
= null;
239 INomenclaturalReference nomenclaturalReference
= HibernateProxyHelper
.deproxy(ref
, INomenclaturalReference
.class);
240 nomenclaturalReference
.setCacheStrategy(nomenclaturalReference
.getType().getCacheStrategy());
241 referenceBaseCache
= nomenclaturalReference
.getNomenclaturalCitation(microReference
);
244 //make nomenclatural status
245 String ncStatusCache
= "";
246 Set
<NomenclaturalStatus
> ncStati
= nonViralName
.getStatus();
247 Iterator
<NomenclaturalStatus
> iterator
= ncStati
.iterator();
248 while (iterator
.hasNext()) {
249 NomenclaturalStatus ncStatus
= (NomenclaturalStatus
)iterator
.next();
250 // since the NewInstance method of nomencatural status allows null as parameter
251 // we have to check for null values here
252 String suffix
= "not defined";
253 if(ncStatus
.getType() != null){
254 NomenclaturalStatusType statusType
= ncStatus
.getType();
255 Language lang
= Language
.LATIN();
256 Representation repr
= statusType
.getRepresentation(lang
);
257 suffix
= repr
.getAbbreviatedLabel();
258 }else if(ncStatus
.getRuleConsidered() != null && ! ncStatus
.getRuleConsidered().equals("")){
259 suffix
= ncStatus
.getRuleConsidered();
261 ncStatusCache
= ", " + suffix
;
263 String refConcat
= " ";
264 if (referenceBaseCache
!= null && ! referenceBaseCache
.trim().startsWith("in ")){
267 result
= CdmUtils
.concat(refConcat
, titleCache
, referenceBaseCache
);
268 result
= CdmUtils
.concat("", result
, ncStatusCache
);
274 * Generates and returns the "name cache" (only scientific name without author teams and year).
275 * @see eu.etaxonomy.cdm.strategy.cache.name.INameCacheStrategy#getNameCache(eu.etaxonomy.cdm.model.name.TaxonNameBase)
277 public String
getNameCache(T nonViralName
) {
278 if (nonViralName
== null){
282 Rank rank
= nonViralName
.getRank();
284 if (nonViralName
.isProtectedNameCache()){
285 result
= nonViralName
.getNameCache();
286 }else if (rank
== null){
287 result
= getRanklessNameCache(nonViralName
);
288 }else if (rank
.isInfraSpecific()){
289 result
= getInfraSpeciesNameCache(nonViralName
);
290 }else if (rank
.isSpecies()){
291 result
= getSpeciesNameCache(nonViralName
);
292 }else if (rank
.isInfraGeneric()){
293 result
= getInfraGenusNameCache(nonViralName
);
294 }else if (rank
.isGenus()){
295 result
= getGenusOrUninomialNameCache(nonViralName
);
296 }else if (rank
.isSupraGeneric()){
297 result
= getGenusOrUninomialNameCache(nonViralName
);
299 logger
.warn("Name Strategy for Name (UUID: " + nonViralName
.getUuid() + ") not yet implemented");
307 * @see eu.etaxonomy.cdm.strategy.cache.INonViralNameCacheStrategy#getAuthorCache(eu.etaxonomy.cdm.model.name.NonViralName)
309 public String
getAuthorshipCache(T nonViralName
) {
310 if (nonViralName
== null){
314 if (nonViralName
.isProtectedAuthorshipCache() == true) {
315 return nonViralName
.getAuthorshipCache();
317 return getNonCacheAuthorshipCache(nonViralName
);
322 * Returns the authorshipcache string for the atomized authorship fields. Does not use the authorshipfield.
323 * @throws NullPointerException if nonViralName is null.
324 * @param nonViralName
327 protected String
getNonCacheAuthorshipCache(T nonViralName
){
329 INomenclaturalAuthor combinationAuthor
= nonViralName
.getCombinationAuthorTeam();
330 INomenclaturalAuthor exCombinationAuthor
= nonViralName
.getExCombinationAuthorTeam();
331 INomenclaturalAuthor basionymAuthor
= nonViralName
.getBasionymAuthorTeam();
332 INomenclaturalAuthor exBasionymAuthor
= nonViralName
.getExBasionymAuthorTeam();
333 String basionymPart
= "";
334 String authorPart
= "";
336 if (basionymAuthor
!= null || exBasionymAuthor
!= null){
337 basionymPart
= BasionymStart
+ getAuthorAndExAuthor(basionymAuthor
, exBasionymAuthor
) + BasionymEnd
;
339 if (combinationAuthor
!= null || exCombinationAuthor
!= null){
340 authorPart
= getAuthorAndExAuthor(combinationAuthor
, exCombinationAuthor
);
342 result
= CdmUtils
.concat(BasionymAuthorCombinationAuthorSeperator
, basionymPart
, authorPart
);
347 * Returns the AuthorCache part for a combination of an author and an ex author. This applies on combination authors
348 * as well as on basionym/orginal combination authors.
349 * @param author the author
350 * @param exAuthor the ex-author
353 protected String
getAuthorAndExAuthor(INomenclaturalAuthor author
, INomenclaturalAuthor exAuthor
){
355 String authorString
= "";
356 String exAuthorString
= "";
358 authorString
= CdmUtils
.Nz(author
.getNomenclaturalTitle());
360 if (exAuthor
!= null){
361 exAuthorString
= CdmUtils
.Nz(exAuthor
.getNomenclaturalTitle());
363 if (exAuthorString
.length() > 0 ){
364 exAuthorString
= exAuthorString
+ ExAuthorSeperator
;
366 result
= exAuthorString
+ authorString
;
373 * @see eu.etaxonomy.cdm.strategy.INameCacheStrategy#getTaggedName(eu.etaxonomy.cdm.model.common.CdmBase)
376 public List
<Object
> getTaggedName(T nonViralName
) {
377 List
<Object
> tags
= new ArrayList
<Object
>();
378 tags
.add(nonViralName
.getGenusOrUninomial());
379 if (nonViralName
.isSpecies() || nonViralName
.isInfraSpecific()){
380 tags
.add(nonViralName
.getSpecificEpithet());
384 if (nonViralName
.isInfraSpecific() && ! nonViralName
.getSpecificEpithet().equals(nonViralName
.getInfraSpecificEpithet())){
385 tags
.add(nonViralName
.getRank());
386 tags
.add(nonViralName
.getInfraSpecificEpithet());
389 if (nonViralName
.isInfraGeneric()){
390 //TODO choose right strategy or generic approach?
391 // --- strategy 1 ---
392 tags
.add(nonViralName
.getRank());
393 tags
.add(nonViralName
.getInfraGenericEpithet());
394 // --- strategy 2 ---
395 // tags.add('('+nvn.getInfraGenericEpithet()+')');
397 Team authorTeam
= Team
.NewInstance();
398 authorTeam
.setProtectedTitleCache(true);
399 authorTeam
.setTitleCache(nonViralName
.getAuthorshipCache());
400 tags
.add(authorTeam
);
402 // Name is an autonym. Rank and infraspecific eitheton follow the author
403 if (nonViralName
.isInfraSpecific() && nonViralName
.getSpecificEpithet().equals(nonViralName
.getInfraSpecificEpithet())){
404 tags
.add(nonViralName
.getRank());
405 tags
.add(nonViralName
.getInfraSpecificEpithet());
408 if(! "".equals(nonViralName
.getAppendedPhrase())){
409 tags
.add(nonViralName
.getAppendedPhrase());
416 /************** PRIVATES ****************/
418 protected String
getRanklessNameCache(NonViralName nonViralName
){
420 result
= (result
+ (nonViralName
.getGenusOrUninomial())).trim().replace("null", "");
421 result
+= " " + (CdmUtils
.Nz(nonViralName
.getSpecificEpithet())).trim();
422 result
+= " " + (CdmUtils
.Nz(nonViralName
.getInfraSpecificEpithet())).trim();
423 result
= result
.trim().replace("null", "");
424 //result += " (rankless)";
425 result
= addAppendedPhrase(result
, nonViralName
);
430 protected String
getGenusOrUninomialNameCache(NonViralName nonViralName
){
432 result
= CdmUtils
.Nz(nonViralName
.getGenusOrUninomial());
433 result
= addAppendedPhrase(result
, nonViralName
).trim();
437 protected String
getInfraGenusNameCache(NonViralName nonViralName
){
439 Rank rank
= nonViralName
.getRank();
440 if (rank
.isSpeciesAggregate()){
441 return getSpeciesAggregateCache(nonViralName
);
443 String infraGenericMarker
= "xxx.";
446 infraGenericMarker
= rank
.getInfraGenericMarker();
447 } catch (UnknownCdmTypeException e
) {
448 infraGenericMarker
= "'unhandled infrageneric rank'";
451 result
= CdmUtils
.Nz(nonViralName
.getGenusOrUninomial());
452 result
+= " " + infraGenericMarker
+ " " + (CdmUtils
.Nz(nonViralName
.getInfraGenericEpithet())).trim().replace("null", "");
453 result
= addAppendedPhrase(result
, nonViralName
).trim();
458 protected String
getSpeciesAggregateCache(NonViralName nonViralName
){
460 result
= CdmUtils
.Nz(nonViralName
.getGenusOrUninomial());
461 result
+= " " + CdmUtils
.Nz(nonViralName
.getSpecificEpithet()).trim().replace("null", "");
464 marker
= nonViralName
.getRank().getInfraGenericMarker();
465 } catch (UnknownCdmTypeException e
) {
466 marker
= "'unknown aggregat type'";
468 result
+= " " + marker
;
469 result
= addAppendedPhrase(result
, nonViralName
).trim();
473 protected String
getSpeciesNameCache(NonViralName nonViralName
){
475 result
= CdmUtils
.Nz(nonViralName
.getGenusOrUninomial());
476 result
+= " " + CdmUtils
.Nz(nonViralName
.getSpecificEpithet()).trim().replace("null", "");
477 result
= addAppendedPhrase(result
, nonViralName
).trim();
478 result
= result
.replace("\\s\\", " ");
483 protected String
getInfraSpeciesNameCache(NonViralName nonViralName
){
484 return getInfraSpeciesNameCache(nonViralName
, true);
487 protected String
getInfraSpeciesNameCache(NonViralName nonViralName
, boolean includeMarker
){
489 result
= CdmUtils
.Nz(nonViralName
.getGenusOrUninomial());
490 result
+= " " + (CdmUtils
.Nz(nonViralName
.getSpecificEpithet()).trim()).replace("null", "");
492 result
+= " " + (nonViralName
.getRank().getAbbreviation()).trim().replace("null", "");
494 result
+= " " + (CdmUtils
.Nz(nonViralName
.getInfraSpecificEpithet())).trim().replace("null", "");
495 result
= addAppendedPhrase(result
, nonViralName
).trim();
503 * @return true, if name has Rank, Rank is below species and species epithet equals infraSpeciesEpithtet, else false
505 protected boolean isAutonym(NonViralName nonViralName
){
506 if (nonViralName
!= null && nonViralName
.getRank() != null && nonViralName
.getSpecificEpithet() != null && nonViralName
.getInfraSpecificEpithet() != null &&
507 nonViralName
.getRank().isInfraSpecific() && nonViralName
.getSpecificEpithet().trim().equals(nonViralName
.getInfraSpecificEpithet().trim())){
514 protected String
addAppendedPhrase(String resultString
, NonViralName nonViralName
){
515 String appendedPhrase
= nonViralName
==null ?
null : nonViralName
.getAppendedPhrase();
516 if (resultString
== null){
517 return appendedPhrase
;
518 }else if(appendedPhrase
== null || "".equals(appendedPhrase
.trim())) {
520 }else if ("".equals(resultString
)){
521 return resultString
+ appendedPhrase
;
523 return resultString
+ " " + appendedPhrase
;
528 public String
getLastEpithet(T taxonNameBase
) {
529 Rank rank
= taxonNameBase
.getRank();
530 if(rank
.isGenus() || rank
.isSupraGeneric()) {
531 return taxonNameBase
.getGenusOrUninomial();
532 } else if(rank
.isInfraGeneric()) {
533 return taxonNameBase
.getInfraGenericEpithet();
534 } else if(rank
.isSpecies()) {
535 return taxonNameBase
.getSpecificEpithet();
537 return taxonNameBase
.getInfraSpecificEpithet();