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
.commons
.lang
.StringUtils
;
18 import org
.apache
.log4j
.Logger
;
20 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
21 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
22 import eu
.etaxonomy
.cdm
.model
.agent
.INomenclaturalAuthor
;
23 import eu
.etaxonomy
.cdm
.model
.agent
.Team
;
24 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
25 import eu
.etaxonomy
.cdm
.model
.common
.Representation
;
26 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalStatus
;
27 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalStatusType
;
28 import eu
.etaxonomy
.cdm
.model
.name
.NonViralName
;
29 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
30 import eu
.etaxonomy
.cdm
.model
.reference
.INomenclaturalReference
;
31 import eu
.etaxonomy
.cdm
.strategy
.exceptions
.UnknownCdmTypeException
;
35 * This class is a default implementation for the INonViralNameCacheStrategy<T extends NonViralName> interface.
36 * The method actually implements a cache strategy for botanical names so no method has to be overwritten by
37 * a subclass for botanic names.
38 * Where differing from this Default BotanicNameCacheStrategy other subclasses should overwrite the existing methods
39 * e.g. a CacheStrategy for zoological names should overwrite getAuthorAndExAuthor
47 public class NonViralNameDefaultCacheStrategy
<T
extends NonViralName
> extends NameCacheStrategyBase
<T
> implements INonViralNameCacheStrategy
<T
> {
48 private static final Logger logger
= Logger
.getLogger(NonViralNameDefaultCacheStrategy
.class);
50 final static UUID uuid
= UUID
.fromString("1cdda0d1-d5bc-480f-bf08-40a510a2f223");
52 protected String NameAuthorSeperator
= " ";
53 protected String BasionymStart
= "(";
54 protected String BasionymEnd
= ")";
55 protected String ExAuthorSeperator
= " ex ";
56 protected CharSequence BasionymAuthorCombinationAuthorSeperator
= " ";
59 public UUID
getUuid(){
66 * @return NonViralNameDefaultCacheStrategy A new instance of NonViralNameDefaultCacheStrategy
68 public static NonViralNameDefaultCacheStrategy
NewInstance(){
69 return new NonViralNameDefaultCacheStrategy();
75 protected NonViralNameDefaultCacheStrategy(){
79 /* **************** GETTER / SETTER **************************************/
82 * String that separates the NameCache part from the AuthorCache part
85 public String
getNameAuthorSeperator() {
86 return NameAuthorSeperator
;
90 public void setNameAuthorSeperator(String nameAuthorSeperator
) {
91 NameAuthorSeperator
= nameAuthorSeperator
;
96 * String the basionym author part starts with e.g. '('.
97 * This should correspond with the {@link NonViralNameDefaultCacheStrategy#getBasionymEnd() basionymEnd} attribute
100 public String
getBasionymStart() {
101 return BasionymStart
;
105 public void setBasionymStart(String basionymStart
) {
106 BasionymStart
= basionymStart
;
111 * String the basionym author part ends with e.g. ')'.
112 * This should correspond with the {@link NonViralNameDefaultCacheStrategy#getBasionymStart() basionymStart} attribute
115 public String
getBasionymEnd() {
120 public void setBasionymEnd(String basionymEnd
) {
121 BasionymEnd
= basionymEnd
;
126 * String to seperate ex author from author.
129 public String
getExAuthorSeperator() {
130 return ExAuthorSeperator
;
134 public void setExAuthorSeperator(String exAuthorSeperator
) {
135 ExAuthorSeperator
= exAuthorSeperator
;
140 * String that seperates the basionym/original_combination author part from the combination author part
143 public CharSequence
getBasionymAuthorCombinationAuthorSeperator() {
144 return BasionymAuthorCombinationAuthorSeperator
;
148 public void setBasionymAuthorCombinationAuthorSeperator(
149 CharSequence basionymAuthorCombinationAuthorSeperator
) {
150 BasionymAuthorCombinationAuthorSeperator
= basionymAuthorCombinationAuthorSeperator
;
154 //** *****************************************************************************************/
158 * @see eu.etaxonomy.cdm.strategy.INameCacheStrategy#getNameCache()
161 public String
getTitleCache(T nonViralName
) {
162 if (nonViralName
== null){
166 if (nonViralName
.isProtectedTitleCache()){
167 return nonViralName
.getTitleCache();
171 if (isAutonym(nonViralName
)){
172 result
= handleAutonym(nonViralName
);
174 String nameCache
= nonViralName
.getNameCache(); //OLD: CdmUtils.Nz(getNameCache(nonViralName));
175 if (nameIncludesAuthorship(nonViralName
)){
176 String authorCache
= CdmUtils
.Nz(getAuthorshipCache(nonViralName
));
177 result
= CdmUtils
.concat(NameAuthorSeperator
, nameCache
, authorCache
);
187 * @param nonViralName
191 private String
handleAutonym(T nonViralName
) {
193 String speciesPart
= getSpeciesNameCache(nonViralName
);
194 //TODO should this include basionym authors and ex authors
195 INomenclaturalAuthor author
= nonViralName
.getCombinationAuthorTeam();
196 String authorPart
= "";
198 authorPart
= CdmUtils
.Nz(author
.getNomenclaturalTitle());
200 INomenclaturalAuthor basAuthor
= nonViralName
.getBasionymAuthorTeam();
201 String basAuthorPart
= "";
202 if (basAuthor
!= null){
203 basAuthorPart
= CdmUtils
.Nz(basAuthor
.getNomenclaturalTitle());
205 if (! "".equals(basAuthorPart
)){
206 authorPart
= "("+ basAuthorPart
+")" + authorPart
;
208 String infraSpeciesPart
= (CdmUtils
.Nz(nonViralName
.getInfraSpecificEpithet()));
210 String infraSpeciesSeparator
= "";
211 if (nonViralName
.getRank() == null || !nonViralName
.getRank().isInfraSpecific()){
212 //TODO handle exception
213 logger
.warn("Rank for autonym does not exist or is not lower than species !!");
215 infraSpeciesSeparator
= nonViralName
.getRank().getAbbreviation();
218 result
= CdmUtils
.concat(" ", new String
[]{speciesPart
, authorPart
, infraSpeciesSeparator
, infraSpeciesPart
});
219 result
= result
.trim().replace("null", "");
223 protected boolean nameIncludesAuthorship(NonViralName nonViralName
){
224 Rank rank
= nonViralName
.getRank();
225 if (rank
!= null && rank
.isSpeciesAggregate()){
237 public String
getFullTitleCache(T nonViralName
) {
239 if (nonViralName
== null){
243 if (nonViralName
.isProtectedFullTitleCache() == true) {
244 return nonViralName
.getFullTitleCache();
249 String titleCache
= nonViralName
.getTitleCache(); // OLD: getTitleCache(nonViralName);
251 String microReference
= nonViralName
.getNomenclaturalMicroReference();
252 INomenclaturalReference ref
= nonViralName
.getNomenclaturalReference();
253 String referenceBaseCache
= null;
255 INomenclaturalReference nomenclaturalReference
= HibernateProxyHelper
.deproxy(ref
, INomenclaturalReference
.class);
256 nomenclaturalReference
.setCacheStrategy(nomenclaturalReference
.getType().getCacheStrategy());
257 referenceBaseCache
= nomenclaturalReference
.getNomenclaturalCitation(microReference
);
260 //make nomenclatural status
261 String ncStatusCache
= "";
262 Set
<NomenclaturalStatus
> ncStati
= nonViralName
.getStatus();
263 Iterator
<NomenclaturalStatus
> iterator
= ncStati
.iterator();
264 while (iterator
.hasNext()) {
265 NomenclaturalStatus ncStatus
= (NomenclaturalStatus
)iterator
.next();
266 // since the NewInstance method of nomencatural status allows null as parameter
267 // we have to check for null values here
268 String suffix
= "not defined";
269 if(ncStatus
.getType() != null){
270 NomenclaturalStatusType statusType
= ncStatus
.getType();
271 Language lang
= Language
.LATIN();
272 Representation repr
= statusType
.getRepresentation(lang
);
274 suffix
= repr
.getAbbreviatedLabel();
276 String message
= "No latin representation available for nom. status. " + statusType
.getTitleCache();
277 logger
.warn(message
);
278 throw new IllegalStateException(message
);
280 }else if(ncStatus
.getRuleConsidered() != null && ! ncStatus
.getRuleConsidered().equals("")){
281 suffix
= ncStatus
.getRuleConsidered();
283 ncStatusCache
= ", " + suffix
;
285 String refConcat
= " ";
286 if (referenceBaseCache
!= null && ! referenceBaseCache
.trim().startsWith("in ")){
289 result
= CdmUtils
.concat(refConcat
, titleCache
, referenceBaseCache
);
290 result
= CdmUtils
.concat("", result
, ncStatusCache
);
296 * Generates and returns the "name cache" (only scientific name without author teams and year).
297 * @see eu.etaxonomy.cdm.strategy.cache.name.INameCacheStrategy#getNameCache(eu.etaxonomy.cdm.model.name.TaxonNameBase)
299 public String
getNameCache(T nonViralName
) {
300 if (nonViralName
== null){
304 Rank rank
= nonViralName
.getRank();
306 if (nonViralName
.isProtectedNameCache()){
307 result
= nonViralName
.getNameCache();
308 }else if (rank
== null){
309 result
= getRanklessNameCache(nonViralName
);
310 }else if (rank
.isInfraSpecific()){
311 result
= getInfraSpeciesNameCache(nonViralName
);
312 }else if (rank
.isSpecies()){
313 result
= getSpeciesNameCache(nonViralName
);
314 }else if (rank
.isInfraGeneric()){
315 result
= getInfraGenusNameCache(nonViralName
);
316 }else if (rank
.isGenus()){
317 result
= getGenusOrUninomialNameCache(nonViralName
);
318 }else if (rank
.isSupraGeneric()){
319 result
= getGenusOrUninomialNameCache(nonViralName
);
321 logger
.warn("Name Strategy for Name (UUID: " + nonViralName
.getUuid() + ") not yet implemented");
329 * @see eu.etaxonomy.cdm.strategy.cache.INonViralNameCacheStrategy#getAuthorCache(eu.etaxonomy.cdm.model.name.NonViralName)
331 public String
getAuthorshipCache(T nonViralName
) {
332 if (nonViralName
== null){
336 if (nonViralName
.isProtectedAuthorshipCache() == true) {
337 return nonViralName
.getAuthorshipCache();
339 return getNonCacheAuthorshipCache(nonViralName
);
344 * Returns the authorshipcache string for the atomized authorship fields. Does not use the authorshipfield.
345 * @throws NullPointerException if nonViralName is null.
346 * @param nonViralName
349 protected String
getNonCacheAuthorshipCache(T nonViralName
){
351 INomenclaturalAuthor combinationAuthor
= nonViralName
.getCombinationAuthorTeam();
352 INomenclaturalAuthor exCombinationAuthor
= nonViralName
.getExCombinationAuthorTeam();
353 INomenclaturalAuthor basionymAuthor
= nonViralName
.getBasionymAuthorTeam();
354 INomenclaturalAuthor exBasionymAuthor
= nonViralName
.getExBasionymAuthorTeam();
355 String basionymPart
= "";
356 String authorPart
= "";
358 if (basionymAuthor
!= null || exBasionymAuthor
!= null){
359 basionymPart
= BasionymStart
+ getAuthorAndExAuthor(basionymAuthor
, exBasionymAuthor
) + BasionymEnd
;
361 if (combinationAuthor
!= null || exCombinationAuthor
!= null){
362 authorPart
= getAuthorAndExAuthor(combinationAuthor
, exCombinationAuthor
);
364 result
= CdmUtils
.concat(BasionymAuthorCombinationAuthorSeperator
, basionymPart
, authorPart
);
369 * Returns the AuthorCache part for a combination of an author and an ex author. This applies on combination authors
370 * as well as on basionym/orginal combination authors.
371 * @param author the author
372 * @param exAuthor the ex-author
375 protected String
getAuthorAndExAuthor(INomenclaturalAuthor author
, INomenclaturalAuthor exAuthor
){
377 String authorString
= "";
378 String exAuthorString
= "";
380 authorString
= CdmUtils
.Nz(author
.getNomenclaturalTitle());
382 if (exAuthor
!= null){
383 exAuthorString
= CdmUtils
.Nz(exAuthor
.getNomenclaturalTitle());
385 if (exAuthorString
.length() > 0 ){
386 exAuthorString
= exAuthorString
+ ExAuthorSeperator
;
388 result
= exAuthorString
+ authorString
;
395 * @see eu.etaxonomy.cdm.strategy.INameCacheStrategy#getTaggedName(eu.etaxonomy.cdm.model.common.CdmBase)
398 public List
<Object
> getTaggedName(T nonViralName
) {
399 List
<Object
> tags
= new ArrayList
<Object
>();
401 // Why does it make sense to add the nameCache in case of non existing genusOrUninomial?
402 // if (nonViralName.getGenusOrUninomial() == null){
403 // tags.add(nonViralName.getNameCache());
406 if (nonViralName
.getGenusOrUninomial() != null) {
407 tags
.add(nonViralName
.getGenusOrUninomial());
409 if (nonViralName
.isSpecies() || nonViralName
.isInfraSpecific()){
410 tags
.add(nonViralName
.getSpecificEpithet());
414 if (nonViralName
.isInfraSpecific() && ! nonViralName
.getSpecificEpithet().equals(nonViralName
.getInfraSpecificEpithet())){
415 tags
.add(nonViralName
.getRank());
416 tags
.add(nonViralName
.getInfraSpecificEpithet());
419 if (nonViralName
.isInfraGeneric()){
420 //TODO choose right strategy or generic approach?
421 // --- strategy 1 ---
423 if (nonViralName
.getRank().isSpeciesAggregate()){
424 tags
.add(getSpeciesAggregateEpithet(nonViralName
));
426 tags
.add(nonViralName
.getRank());
427 tags
.add(nonViralName
.getInfraGenericEpithet());
429 // --- strategy 2 ---
430 // tags.add('('+nvn.getInfraGenericEpithet()+')');
432 Team authorTeam
= Team
.NewInstance();
433 authorTeam
.setProtectedTitleCache(true);
434 authorTeam
.setTitleCache(nonViralName
.getAuthorshipCache(), true);
435 tags
.add(authorTeam
);
437 // Name is an autonym. Rank and infraspecific eitheton follow the author
438 if (nonViralName
.isInfraSpecific() && nonViralName
.getSpecificEpithet().equals(nonViralName
.getInfraSpecificEpithet())){
439 tags
.add(nonViralName
.getRank());
440 tags
.add(nonViralName
.getInfraSpecificEpithet());
443 if(! "".equals(nonViralName
.getAppendedPhrase())&& (nonViralName
.getAppendedPhrase() != null)){
444 tags
.add(nonViralName
.getAppendedPhrase());
451 //***************************** PRIVATES ***************************************/
453 protected String
getRanklessNameCache(NonViralName nonViralName
){
455 result
= (result
+ (CdmUtils
.Nz(nonViralName
.getGenusOrUninomial()))).trim().replace("null", "");
456 result
+= " " + (CdmUtils
.Nz(nonViralName
.getSpecificEpithet())).trim();
457 result
+= " " + (CdmUtils
.Nz(nonViralName
.getInfraSpecificEpithet())).trim();
458 result
= result
.trim().replace("null", "");
459 //result += " (rankless)";
460 result
= addAppendedPhrase(result
, nonViralName
);
465 protected String
getGenusOrUninomialNameCache(NonViralName nonViralName
){
467 result
= CdmUtils
.Nz(nonViralName
.getGenusOrUninomial()).trim();
468 result
= addAppendedPhrase(result
, nonViralName
).trim();
472 protected String
getInfraGenusNameCache(NonViralName nonViralName
){
474 Rank rank
= nonViralName
.getRank();
475 if (rank
.isSpeciesAggregate()){
476 return getSpeciesAggregateCache(nonViralName
);
478 String infraGenericMarker
= "'unhandled infrageneric rank'";
481 infraGenericMarker
= rank
.getInfraGenericMarker();
482 } catch (UnknownCdmTypeException e
) {
483 infraGenericMarker
= "'unhandled infrageneric rank'";
486 result
= CdmUtils
.Nz(nonViralName
.getGenusOrUninomial()).trim();
487 result
+= " " + infraGenericMarker
+ " " + (CdmUtils
.Nz(nonViralName
.getInfraGenericEpithet())).trim().replace("null", "");
488 result
= addAppendedPhrase(result
, nonViralName
).trim();
493 protected String
getSpeciesAggregateCache(NonViralName nonViralName
){
494 String result
= getGenusAndSpeciesPart(nonViralName
);
496 result
+= " " + getSpeciesAggregateEpithet(nonViralName
);
497 result
= addAppendedPhrase(result
, nonViralName
).trim();
501 private String
getSpeciesAggregateEpithet(NonViralName nonViralName
) {
504 marker
= nonViralName
.getRank().getInfraGenericMarker();
505 } catch (UnknownCdmTypeException e
) {
506 marker
= "'unknown aggregat type'";
511 protected String
getSpeciesNameCache(NonViralName nonViralName
){
512 String result
= getGenusAndSpeciesPart(nonViralName
);
513 result
= addAppendedPhrase(result
, nonViralName
).trim();
514 result
= result
.replace("\\s\\", " ");
519 protected String
getInfraSpeciesNameCache(NonViralName nonViralName
){
520 return getInfraSpeciesNameCache(nonViralName
, true);
523 protected String
getInfraSpeciesNameCache(NonViralName nonViralName
, boolean includeMarker
){
524 String result
= getGenusAndSpeciesPart(nonViralName
);
526 result
+= " " + (nonViralName
.getRank().getAbbreviation()).trim().replace("null", "");
528 result
+= " " + (CdmUtils
.Nz(nonViralName
.getInfraSpecificEpithet())).trim().replace("null", "");
529 result
= addAppendedPhrase(result
, nonViralName
).trim();
534 private String
getGenusAndSpeciesPart(NonViralName nonViralName
) {
536 result
= CdmUtils
.Nz(nonViralName
.getGenusOrUninomial()).trim();
537 if (StringUtils
.isNotBlank(nonViralName
.getInfraGenericEpithet()) ){
538 result
+= " (" + nonViralName
.getInfraGenericEpithet().trim() + ")";
540 result
+= " " + (CdmUtils
.Nz(nonViralName
.getSpecificEpithet()).trim()).replace("null", "");
548 * @return true, if name has Rank, Rank is below species and species epithet equals infraSpeciesEpithtet, else false
550 protected boolean isAutonym(NonViralName nonViralName
){
551 if (nonViralName
!= null && nonViralName
.getRank() != null && nonViralName
.getSpecificEpithet() != null && nonViralName
.getInfraSpecificEpithet() != null &&
552 nonViralName
.getRank().isInfraSpecific() && nonViralName
.getSpecificEpithet().trim().equals(nonViralName
.getInfraSpecificEpithet().trim())){
559 protected String
addAppendedPhrase(String resultString
, NonViralName nonViralName
){
560 String appendedPhrase
= nonViralName
==null ?
null : nonViralName
.getAppendedPhrase();
561 if (resultString
== null){
562 return appendedPhrase
;
563 }else if(appendedPhrase
== null || "".equals(appendedPhrase
.trim())) {
565 }else if ("".equals(resultString
)){
566 return resultString
+ appendedPhrase
;
568 return resultString
+ " " + appendedPhrase
;
573 public String
getLastEpithet(T taxonNameBase
) {
574 Rank rank
= taxonNameBase
.getRank();
575 if(rank
.isGenus() || rank
.isSupraGeneric()) {
576 return taxonNameBase
.getGenusOrUninomial();
577 } else if(rank
.isInfraGeneric()) {
578 return taxonNameBase
.getInfraGenericEpithet();
579 } else if(rank
.isSpecies()) {
580 return taxonNameBase
.getSpecificEpithet();
582 return taxonNameBase
.getInfraSpecificEpithet();