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
.model
.reference
.ReferenceBase
;
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){
167 if (isAutonym(nonViralName
)){
168 String speciesPart
= getSpeciesNameCache(nonViralName
);
169 //TODO should this include basionym authors and ex authors
170 INomenclaturalAuthor author
= nonViralName
.getCombinationAuthorTeam();
171 String authorPart
= "";
173 authorPart
= CdmUtils
.Nz(author
.getNomenclaturalTitle());
175 INomenclaturalAuthor basAuthor
= nonViralName
.getBasionymAuthorTeam();
176 String basAuthorPart
= "";
177 if (basAuthor
!= null){
178 basAuthorPart
= CdmUtils
.Nz(basAuthor
.getNomenclaturalTitle());
180 if (! "".equals(basAuthorPart
)){
181 authorPart
= "("+ basAuthorPart
+")" + authorPart
;
183 String infraSpeciesPart
= (CdmUtils
.Nz(nonViralName
.getInfraSpecificEpithet()));
185 String infraSpeciesSeparator
= "";
186 if (nonViralName
.getRank() == null || !nonViralName
.getRank().isInfraSpecific()){
187 //TODO handle exception
188 logger
.warn("Rank for autonym does not exist or is not lower than species !!");
190 infraSpeciesSeparator
= nonViralName
.getRank().getAbbreviation();
193 result
= CdmUtils
.concat(" ", new String
[]{speciesPart
, authorPart
, infraSpeciesSeparator
, infraSpeciesPart
});
194 result
= result
.trim().replace("null", "");
196 String nameCache
= CdmUtils
.Nz(getNameCache(nonViralName
));
197 if (nameIncludesAuthorship(nonViralName
)){
198 String authorCache
= CdmUtils
.Nz(getAuthorshipCache(nonViralName
));
199 result
= CdmUtils
.concat(NameAuthorSeperator
, nameCache
, authorCache
);
207 protected boolean nameIncludesAuthorship(NonViralName nonViralName
){
208 Rank rank
= nonViralName
.getRank();
209 if (rank
!= null && rank
.isSpeciesAggregate()){
218 public String
getFullTitleCache(T nonViralName
) {
220 if (nonViralName
== null){
224 if (nonViralName
.isProtectedFullTitleCache() == true) {
225 return nonViralName
.getFullTitleCache();
230 String titleCache
= getTitleCache(nonViralName
);
232 String microReference
= nonViralName
.getNomenclaturalMicroReference();
233 ReferenceBase
<?
> ref
= nonViralName
.getNomenclaturalReference();
234 String referenceBaseCache
= null;
236 INomenclaturalReference nomRef
= HibernateProxyHelper
.deproxy(ref
, INomenclaturalReference
.class);
237 referenceBaseCache
= nomRef
.getNomenclaturalCitation(microReference
);
240 //make nomenclatural status
241 String ncStatusCache
= "";
242 Set
<NomenclaturalStatus
> ncStati
= nonViralName
.getStatus();
243 Iterator
<NomenclaturalStatus
> iterator
= ncStati
.iterator();
244 while (iterator
.hasNext()) {
245 NomenclaturalStatus ncStatus
= (NomenclaturalStatus
)iterator
.next();
246 NomenclaturalStatusType statusType
= ncStatus
.getType();
247 Language lang
= Language
.LATIN();
248 Representation repr
= statusType
.getRepresentation(lang
);
249 ncStatusCache
= ", " + repr
.getAbbreviatedLabel();
251 String refConcat
= " ";
252 if (referenceBaseCache
!= null && ! referenceBaseCache
.trim().startsWith("in ")){
255 result
= CdmUtils
.concat(refConcat
, titleCache
, referenceBaseCache
);
256 result
= CdmUtils
.concat("", result
, ncStatusCache
);
262 * Generates and returns the "name cache" (only scientific name without author teams and year).
263 * @see eu.etaxonomy.cdm.strategy.cache.name.INameCacheStrategy#getNameCache(eu.etaxonomy.cdm.model.name.TaxonNameBase)
265 public String
getNameCache(T nonViralName
) {
266 if (nonViralName
== null){
270 Rank rank
= nonViralName
.getRank();
273 result
= getRanklessNameCache(nonViralName
);
274 }else if (rank
.isInfraSpecific()){
275 result
= getInfraSpeciesNameCache(nonViralName
);
276 }else if (rank
.isSpecies()){
277 result
= getSpeciesNameCache(nonViralName
);
278 }else if (rank
.isInfraGeneric()){
279 result
= getInfraGenusNameCache(nonViralName
);
280 }else if (rank
.isGenus()){
281 result
= getGenusOrUninomialNameCache(nonViralName
);
282 }else if (rank
.isSupraGeneric()){
283 result
= getGenusOrUninomialNameCache(nonViralName
);
285 logger
.warn("Name Strategy for Name (UUID: " + nonViralName
.getUuid() + ") not yet implemented");
293 * @see eu.etaxonomy.cdm.strategy.cache.INonViralNameCacheStrategy#getAuthorCache(eu.etaxonomy.cdm.model.name.NonViralName)
295 public String
getAuthorshipCache(T nonViralName
) {
296 if (nonViralName
== null){
300 if (nonViralName
.isProtectedAuthorshipCache() == true) {
301 return nonViralName
.getAuthorshipCache();
303 return getNonCacheAuthorshipCache(nonViralName
);
308 * Returns the authorshipcache string for the atomized authorship fields. Does not use the authorshipfield.
309 * @throws NullPointerException if nonViralName is null.
310 * @param nonViralName
313 protected String
getNonCacheAuthorshipCache(T nonViralName
){
315 INomenclaturalAuthor combinationAuthor
= nonViralName
.getCombinationAuthorTeam();
316 INomenclaturalAuthor exCombinationAuthor
= nonViralName
.getExCombinationAuthorTeam();
317 INomenclaturalAuthor basionymAuthor
= nonViralName
.getBasionymAuthorTeam();
318 INomenclaturalAuthor exBasionymAuthor
= nonViralName
.getExBasionymAuthorTeam();
319 String basionymPart
= "";
320 String authorPart
= "";
322 if (basionymAuthor
!= null || exBasionymAuthor
!= null){
323 basionymPart
= BasionymStart
+ getAuthorAndExAuthor(basionymAuthor
, exBasionymAuthor
) + BasionymEnd
;
325 if (combinationAuthor
!= null || exCombinationAuthor
!= null){
326 authorPart
= getAuthorAndExAuthor(combinationAuthor
, exCombinationAuthor
);
328 result
= CdmUtils
.concat(BasionymAuthorCombinationAuthorSeperator
, basionymPart
, authorPart
);
333 * Returns the AuthorCache part for a combination of an author and an ex author. This applies on combination authors
334 * as well as on basionym/orginal combination authors.
335 * @param author the author
336 * @param exAuthor the ex-author
339 protected String
getAuthorAndExAuthor(INomenclaturalAuthor author
, INomenclaturalAuthor exAuthor
){
341 String authorString
= "";
342 String exAuthorString
= "";
344 authorString
= CdmUtils
.Nz(author
.getNomenclaturalTitle());
346 if (exAuthor
!= null){
347 exAuthorString
= CdmUtils
.Nz(exAuthor
.getNomenclaturalTitle());
349 if (exAuthorString
.length() > 0 ){
350 exAuthorString
= exAuthorString
+ ExAuthorSeperator
;
352 result
= exAuthorString
+ authorString
;
359 * @see eu.etaxonomy.cdm.strategy.INameCacheStrategy#getTaggedName(eu.etaxonomy.cdm.model.common.CdmBase)
362 public List
<Object
> getTaggedName(T nonViralName
) {
363 List
<Object
> tags
= new ArrayList
<Object
>();
364 tags
.add(nonViralName
.getGenusOrUninomial());
365 if (nonViralName
.isSpecies() || nonViralName
.isInfraSpecific()){
366 tags
.add(nonViralName
.getSpecificEpithet());
370 if (nonViralName
.isInfraSpecific() && ! nonViralName
.getSpecificEpithet().equals(nonViralName
.getInfraSpecificEpithet())){
371 tags
.add(nonViralName
.getRank());
372 tags
.add(nonViralName
.getInfraSpecificEpithet());
375 if (nonViralName
.isInfraGeneric()){
376 //TODO choose right strategy or generic approach?
377 // --- strategy 1 ---
378 tags
.add(nonViralName
.getRank());
379 tags
.add(nonViralName
.getInfraGenericEpithet());
380 // --- strategy 2 ---
381 // tags.add('('+nvn.getInfraGenericEpithet()+')');
383 Team authorTeam
= Team
.NewInstance();
384 authorTeam
.setProtectedTitleCache(true);
385 authorTeam
.setTitleCache(nonViralName
.getAuthorshipCache());
386 tags
.add(authorTeam
);
388 // Name is an autonym. Rank and infraspecific eitheton follow the author
389 if (nonViralName
.isInfraSpecific() && nonViralName
.getSpecificEpithet().equals(nonViralName
.getInfraSpecificEpithet())){
390 tags
.add(nonViralName
.getRank());
391 tags
.add(nonViralName
.getInfraSpecificEpithet());
394 if(! "".equals(nonViralName
.getAppendedPhrase())){
395 tags
.add(nonViralName
.getAppendedPhrase());
402 /************** PRIVATES ****************/
404 protected String
getRanklessNameCache(NonViralName nonViralName
){
406 result
= (result
+ (nonViralName
.getGenusOrUninomial())).trim().replace("null", "");
407 result
+= " " + (CdmUtils
.Nz(nonViralName
.getSpecificEpithet())).trim();
408 result
+= " " + (CdmUtils
.Nz(nonViralName
.getInfraSpecificEpithet())).trim();
409 result
= result
.trim().replace("null", "");
410 //result += " (rankless)";
411 result
= addAppendedPhrase(result
, nonViralName
);
416 protected String
getGenusOrUninomialNameCache(NonViralName nonViralName
){
418 result
= CdmUtils
.Nz(nonViralName
.getGenusOrUninomial());
419 result
= addAppendedPhrase(result
, nonViralName
);
423 protected String
getInfraGenusNameCache(NonViralName nonViralName
){
425 Rank rank
= nonViralName
.getRank();
426 if (rank
.isSpeciesAggregate()){
427 return getSpeciesAggregateCache(nonViralName
);
429 String infraGenericMarker
= "xxx.";
432 infraGenericMarker
= rank
.getInfraGenericMarker();
433 } catch (UnknownCdmTypeException e
) {
434 infraGenericMarker
= "'unhandled infrageneric rank'";
437 result
= CdmUtils
.Nz(nonViralName
.getGenusOrUninomial());
438 result
+= " " + infraGenericMarker
+ " " + (CdmUtils
.Nz(nonViralName
.getInfraGenericEpithet())).trim().replace("null", "");
439 result
= addAppendedPhrase(result
, nonViralName
);
444 protected String
getSpeciesAggregateCache(NonViralName nonViralName
){
446 result
= CdmUtils
.Nz(nonViralName
.getGenusOrUninomial());
447 result
+= " " + CdmUtils
.Nz(nonViralName
.getSpecificEpithet()).trim().replace("null", "");
450 marker
= nonViralName
.getRank().getInfraGenericMarker();
451 } catch (UnknownCdmTypeException e
) {
452 marker
= "'unknown aggregat type'";
454 result
+= " " + marker
;
455 result
= addAppendedPhrase(result
, nonViralName
);
459 protected String
getSpeciesNameCache(NonViralName nonViralName
){
461 result
= CdmUtils
.Nz(nonViralName
.getGenusOrUninomial());
462 result
+= " " + CdmUtils
.Nz(nonViralName
.getSpecificEpithet()).trim().replace("null", "");
463 result
= addAppendedPhrase(result
, nonViralName
);
468 protected String
getInfraSpeciesNameCache(NonViralName nonViralName
){
470 result
= CdmUtils
.Nz(nonViralName
.getGenusOrUninomial());
471 result
+= " " + (CdmUtils
.Nz(nonViralName
.getSpecificEpithet()).trim()).replace("null", "");
472 if (! isAutonym(nonViralName
)){
473 result
+= " " + (nonViralName
.getRank().getAbbreviation()).trim().replace("null", "");
475 result
+= " " + (CdmUtils
.Nz(nonViralName
.getInfraSpecificEpithet())).trim().replace("null", "");
476 result
= addAppendedPhrase(result
, nonViralName
);
483 * @return true, if name has Rank, Rank is below species and species epithet equals infraSpeciesEpithtet, else false
485 protected boolean isAutonym(NonViralName nonViralName
){
486 if (nonViralName
!= null && nonViralName
.getRank() != null && nonViralName
.getSpecificEpithet() != null && nonViralName
.getInfraSpecificEpithet() != null &&
487 nonViralName
.getRank().isInfraSpecific() && nonViralName
.getSpecificEpithet().trim().equals(nonViralName
.getInfraSpecificEpithet().trim())){
494 protected String
addAppendedPhrase(String resultString
, NonViralName nonViralName
){
495 String appendedPhrase
= nonViralName
==null ?
null : nonViralName
.getAppendedPhrase();
496 if (resultString
== null){
497 return appendedPhrase
;
498 }else if(appendedPhrase
== null || "".equals(appendedPhrase
.trim())) {
500 }else if ("".equals(resultString
)){
501 return resultString
+ appendedPhrase
;
503 return resultString
+ " " + appendedPhrase
;
508 public String
getLastEpithet(T taxonNameBase
) {
509 Rank rank
= taxonNameBase
.getRank();
510 if(rank
.isGenus() || rank
.isSupraGeneric()) {
511 return taxonNameBase
.getGenusOrUninomial();
512 } else if(rank
.isInfraGeneric()) {
513 return taxonNameBase
.getInfraGenericEpithet();
514 } else if(rank
.isSpecies()) {
515 return taxonNameBase
.getSpecificEpithet();
517 return taxonNameBase
.getInfraSpecificEpithet();