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
.model
.agent
.INomenclaturalAuthor
;
21 import eu
.etaxonomy
.cdm
.model
.agent
.Team
;
22 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
23 import eu
.etaxonomy
.cdm
.model
.common
.Representation
;
24 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalStatus
;
25 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalStatusType
;
26 import eu
.etaxonomy
.cdm
.model
.name
.NonViralName
;
27 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
28 import eu
.etaxonomy
.cdm
.model
.reference
.INomenclaturalReference
;
32 * This class is a default implementation for the INonViralNameCacheStrategy<T extends NonViralName> interface.
33 * The method actually implements a cache strategy for botanical names so no method has to be overwritten by
34 * a subclass for botanic names.
35 * Where differing from this Default BotanicNameCacheStrategy other subclasses should overwrite the existing methods
36 * e.g. a CacheStrategy for zoological names should overwrite getAuthorAndExAuthor
44 public class NonViralNameDefaultCacheStrategy
<T
extends NonViralName
> extends NameCacheStrategyBase
<T
> implements INonViralNameCacheStrategy
<T
> {
45 private static final Logger logger
= Logger
.getLogger(NonViralNameDefaultCacheStrategy
.class);
47 final static UUID uuid
= UUID
.fromString("1cdda0d1-d5bc-480f-bf08-40a510a2f223");
49 protected String NameAuthorSeperator
= " ";
50 protected String BasionymStart
= "(";
51 protected String BasionymEnd
= ")";
52 protected String ExAuthorSeperator
= " ex ";
53 protected CharSequence BasionymAuthorCombinationAuthorSeperator
= " ";
56 public UUID
getUuid(){
63 * @return NonViralNameDefaultCacheStrategy A new instance of NonViralNameDefaultCacheStrategy
65 public static NonViralNameDefaultCacheStrategy
NewInstance(){
66 return new NonViralNameDefaultCacheStrategy();
72 protected NonViralNameDefaultCacheStrategy(){
76 /* **************** GETTER / SETTER **************************************/
79 * String that separates the NameCache part from the AuthorCache part
82 public String
getNameAuthorSeperator() {
83 return NameAuthorSeperator
;
87 public void setNameAuthorSeperator(String nameAuthorSeperator
) {
88 NameAuthorSeperator
= nameAuthorSeperator
;
93 * String the basionym author part starts with e.g. '('.
94 * This should correspond with the {@link NonViralNameDefaultCacheStrategy#getBasionymEnd() basionymEnd} attribute
97 public String
getBasionymStart() {
102 public void setBasionymStart(String basionymStart
) {
103 BasionymStart
= basionymStart
;
108 * String the basionym author part ends with e.g. ')'.
109 * This should correspond with the {@link NonViralNameDefaultCacheStrategy#getBasionymStart() basionymStart} attribute
112 public String
getBasionymEnd() {
117 public void setBasionymEnd(String basionymEnd
) {
118 BasionymEnd
= basionymEnd
;
123 * String to seperate ex author from author.
126 public String
getExAuthorSeperator() {
127 return ExAuthorSeperator
;
131 public void setExAuthorSeperator(String exAuthorSeperator
) {
132 ExAuthorSeperator
= exAuthorSeperator
;
137 * String that seperates the basionym/original_combination author part from the combination author part
140 public CharSequence
getBasionymAuthorCombinationAuthorSeperator() {
141 return BasionymAuthorCombinationAuthorSeperator
;
145 public void setBasionymAuthorCombinationAuthorSeperator(
146 CharSequence basionymAuthorCombinationAuthorSeperator
) {
147 BasionymAuthorCombinationAuthorSeperator
= basionymAuthorCombinationAuthorSeperator
;
151 //** *****************************************************************************************/
155 * @see eu.etaxonomy.cdm.strategy.INameCacheStrategy#getNameCache()
158 public String
getTitleCache(T nonViralName
) {
159 if (nonViralName
== null){
163 if (isAutonym(nonViralName
)){
164 String speciesPart
= getSpeciesNameCache(nonViralName
);
165 //TODO should this include basionym authors and ex authors
166 INomenclaturalAuthor author
= nonViralName
.getCombinationAuthorTeam();
167 String authorPart
= "";
169 authorPart
= CdmUtils
.Nz(author
.getNomenclaturalTitle());
171 INomenclaturalAuthor basAuthor
= nonViralName
.getBasionymAuthorTeam();
172 String basAuthorPart
= "";
173 if (basAuthor
!= null){
174 basAuthorPart
= CdmUtils
.Nz(basAuthor
.getNomenclaturalTitle());
176 if (! "".equals(basAuthorPart
)){
177 authorPart
= "("+ basAuthorPart
+")" + authorPart
;
179 String infraSpeciesPart
= (CdmUtils
.Nz(nonViralName
.getInfraSpecificEpithet()));
181 String infraSpeciesSeparator
= "";
182 if (nonViralName
.getRank() == null || !nonViralName
.getRank().isInfraSpecific()){
183 //TODO handle exception
184 logger
.warn("Rank for autonym does not exist or is not lower than species !!");
186 infraSpeciesSeparator
= nonViralName
.getRank().getAbbreviation();
189 result
= CdmUtils
.concat(" ", new String
[]{speciesPart
, authorPart
, infraSpeciesSeparator
, infraSpeciesPart
});
190 result
= result
.trim().replace("null", "");
192 String nameCache
= CdmUtils
.Nz(getNameCache(nonViralName
));
193 String authorCache
= CdmUtils
.Nz(getAuthorshipCache(nonViralName
));
194 result
= CdmUtils
.concat(NameAuthorSeperator
, nameCache
, authorCache
);
201 public String
getFullTitleCache(T nonViralName
) {
203 if (nonViralName
== null){
207 if (nonViralName
.isProtectedFullTitleCache() == true) {
208 return nonViralName
.getFullTitleCache();
213 String titleCache
= getTitleCache(nonViralName
);
215 String microReference
= nonViralName
.getNomenclaturalMicroReference();
216 INomenclaturalReference ref
= (INomenclaturalReference
)nonViralName
.getNomenclaturalReference();
217 String referenceBaseCache
= null;
219 referenceBaseCache
= ref
.getNomenclaturalCitation(microReference
);
222 //make nomenclatural status
223 String ncStatusCache
= "";
224 Set
<NomenclaturalStatus
> ncStati
= nonViralName
.getStatus();
225 Iterator
<NomenclaturalStatus
> iterator
= ncStati
.iterator();
226 while (iterator
.hasNext()) {
227 NomenclaturalStatus ncStatus
= (NomenclaturalStatus
)iterator
.next();
228 NomenclaturalStatusType statusType
= ncStatus
.getType();
229 Language lang
= Language
.LATIN();
230 Representation repr
= statusType
.getRepresentation(lang
);
231 ncStatusCache
= ", " + repr
.getAbbreviatedLabel();
233 String refConcat
= " ";
234 if (referenceBaseCache
!= null && ! referenceBaseCache
.trim().startsWith("in ")){
237 result
= CdmUtils
.concat(refConcat
, titleCache
, referenceBaseCache
);
238 result
= CdmUtils
.concat("", result
, ncStatusCache
);
244 * Generates and returns the "name cache" (only scientific name without author teams and year).
245 * @see eu.etaxonomy.cdm.strategy.cache.name.INameCacheStrategy#getNameCache(eu.etaxonomy.cdm.model.name.TaxonNameBase)
247 public String
getNameCache(T nonViralName
) {
248 if (nonViralName
== null){
252 Rank rank
= nonViralName
.getRank();
255 result
= getRanklessNameCache(nonViralName
);
256 }else if (rank
.isInfraSpecific()){
257 result
= getInfraSpeciesNameCache(nonViralName
);
258 }else if (rank
.isSpecies()){
259 result
= getSpeciesNameCache(nonViralName
);
260 }else if (rank
.isInfraGeneric()){
261 result
= getInfraGenusNameCache(nonViralName
);
262 }else if (rank
.isGenus()){
263 result
= getGenusOrUninomialNameCache(nonViralName
);
264 }else if (rank
.isSupraGeneric()){
265 result
= getGenusOrUninomialNameCache(nonViralName
);
267 logger
.warn("Name Strategy for Name (UUID: " + nonViralName
.getUuid() + ") not yet implemented");
275 * @see eu.etaxonomy.cdm.strategy.cache.INonViralNameCacheStrategy#getAuthorCache(eu.etaxonomy.cdm.model.name.NonViralName)
277 public String
getAuthorshipCache(T nonViralName
) {
278 if (nonViralName
== null){
282 if (nonViralName
.isProtectedAuthorshipCache() == true) {
283 return nonViralName
.getAuthorshipCache();
285 return getNonCacheAuthorshipCache(nonViralName
);
290 * Returns the authorshipcache string for the atomized authorship fields. Does not use the authorshipfield.
291 * @throws NullPointerException if nonViralName is null.
292 * @param nonViralName
295 protected String
getNonCacheAuthorshipCache(T nonViralName
){
297 INomenclaturalAuthor combinationAuthor
= nonViralName
.getCombinationAuthorTeam();
298 INomenclaturalAuthor exCombinationAuthor
= nonViralName
.getExCombinationAuthorTeam();
299 INomenclaturalAuthor basionymAuthor
= nonViralName
.getBasionymAuthorTeam();
300 INomenclaturalAuthor exBasionymAuthor
= nonViralName
.getExBasionymAuthorTeam();
301 String basionymPart
= "";
302 String authorPart
= "";
304 if (basionymAuthor
!= null || exBasionymAuthor
!= null){
305 basionymPart
= BasionymStart
+ getAuthorAndExAuthor(basionymAuthor
, exBasionymAuthor
) + BasionymEnd
;
307 if (combinationAuthor
!= null || exCombinationAuthor
!= null){
308 authorPart
= getAuthorAndExAuthor(combinationAuthor
, exCombinationAuthor
);
310 result
= CdmUtils
.concat(BasionymAuthorCombinationAuthorSeperator
, basionymPart
, authorPart
);
315 * Returns the AuthorCache part for a combination of an author and an ex author. This applies on combination authors
316 * as well as on basionym/orginal combination authors.
317 * @param author the author
318 * @param exAuthor the ex-author
321 protected String
getAuthorAndExAuthor(INomenclaturalAuthor author
, INomenclaturalAuthor exAuthor
){
323 String authorString
= "";
324 String exAuthorString
= "";
326 authorString
= CdmUtils
.Nz(author
.getNomenclaturalTitle());
328 if (exAuthor
!= null){
329 exAuthorString
= CdmUtils
.Nz(exAuthor
.getNomenclaturalTitle());
331 if (exAuthorString
.length() > 0 ){
332 exAuthorString
= exAuthorString
+ ExAuthorSeperator
;
334 result
= exAuthorString
+ authorString
;
341 * @see eu.etaxonomy.cdm.strategy.INameCacheStrategy#getTaggedName(eu.etaxonomy.cdm.model.common.CdmBase)
344 public List
<Object
> getTaggedName(T nonViralName
) {
345 List
<Object
> tags
= new ArrayList
<Object
>();
346 tags
.add(nonViralName
.getGenusOrUninomial());
347 if (nonViralName
.isSpecies() || nonViralName
.isInfraSpecific()){
348 tags
.add(nonViralName
.getSpecificEpithet());
352 if (nonViralName
.isInfraSpecific() && ! nonViralName
.getSpecificEpithet().equals(nonViralName
.getInfraSpecificEpithet())){
353 tags
.add(nonViralName
.getRank());
354 tags
.add(nonViralName
.getInfraSpecificEpithet());
357 if (nonViralName
.isInfraGeneric()){
358 //TODO choose right strategy or generic approach?
359 // --- strategy 1 ---
360 tags
.add(nonViralName
.getRank());
361 tags
.add(nonViralName
.getInfraGenericEpithet());
362 // --- strategy 2 ---
363 // tags.add('('+nvn.getInfraGenericEpithet()+')');
365 Team authorTeam
= Team
.NewInstance();
366 authorTeam
.setProtectedTitleCache(true);
367 authorTeam
.setTitleCache(nonViralName
.getAuthorshipCache());
368 tags
.add(authorTeam
);
370 // Name is an autonym. Rank and infraspecific eitheton follow the author
371 if (nonViralName
.isInfraSpecific() && nonViralName
.getSpecificEpithet().equals(nonViralName
.getInfraSpecificEpithet())){
372 tags
.add(nonViralName
.getRank());
373 tags
.add(nonViralName
.getInfraSpecificEpithet());
376 if(! "".equals(nonViralName
.getAppendedPhrase())){
377 tags
.add(nonViralName
.getAppendedPhrase());
384 /************** PRIVATES ****************/
386 protected String
getRanklessNameCache(NonViralName nonViralName
){
388 result
= (result
+ (nonViralName
.getGenusOrUninomial())).trim().replace("null", "");
389 result
+= " " + (CdmUtils
.Nz(nonViralName
.getSpecificEpithet())).trim();
390 result
+= " " + (CdmUtils
.Nz(nonViralName
.getInfraSpecificEpithet())).trim();
391 result
= result
.trim().replace("null", "");
392 //result += " (rankless)";
393 result
= addAppendedPhrase(result
, nonViralName
);
398 protected String
getGenusOrUninomialNameCache(NonViralName nonViralName
){
400 result
= CdmUtils
.Nz(nonViralName
.getGenusOrUninomial());
401 result
= addAppendedPhrase(result
, nonViralName
);
405 protected String
getInfraGenusNameCache(NonViralName nonViralName
){
407 result
= CdmUtils
.Nz(nonViralName
.getGenusOrUninomial());
408 result
+= " (" + (CdmUtils
.Nz(nonViralName
.getInfraGenericEpithet()) + ")").trim().replace("null", "");
409 result
= addAppendedPhrase(result
, nonViralName
);
414 protected String
getSpeciesNameCache(NonViralName nonViralName
){
416 result
= CdmUtils
.Nz(nonViralName
.getGenusOrUninomial());
417 result
+= " " + CdmUtils
.Nz(nonViralName
.getSpecificEpithet()).trim().replace("null", "");
418 result
= addAppendedPhrase(result
, nonViralName
);
423 protected String
getInfraSpeciesNameCache(NonViralName nonViralName
){
425 result
= CdmUtils
.Nz(nonViralName
.getGenusOrUninomial());
426 result
+= " " + (CdmUtils
.Nz(nonViralName
.getSpecificEpithet()).trim()).replace("null", "");
427 if (! isAutonym(nonViralName
)){
428 result
+= " " + (nonViralName
.getRank().getAbbreviation()).trim().replace("null", "");
430 result
+= " " + (CdmUtils
.Nz(nonViralName
.getInfraSpecificEpithet())).trim().replace("null", "");
431 result
= addAppendedPhrase(result
, nonViralName
);
438 * @return true, if name has Rank, Rank is below species and species epithet equals infraSpeciesEpithtet, else false
440 protected boolean isAutonym(NonViralName nonViralName
){
441 if (nonViralName
!= null && nonViralName
.getRank() != null && nonViralName
.getSpecificEpithet() != null && nonViralName
.getInfraSpecificEpithet() != null &&
442 nonViralName
.getRank().isInfraSpecific() && nonViralName
.getSpecificEpithet().trim().equals(nonViralName
.getInfraSpecificEpithet().trim())){
449 protected String
addAppendedPhrase(String resultString
, NonViralName nonViralName
){
450 String appendedPhrase
= nonViralName
==null ?
null : nonViralName
.getAppendedPhrase();
451 if (resultString
== null){
452 return appendedPhrase
;
453 }else if(appendedPhrase
== null || "".equals(appendedPhrase
.trim())) {
455 }else if ("".equals(resultString
)){
456 return resultString
+ appendedPhrase
;
458 return resultString
+ " " + appendedPhrase
;
463 public String
getLastEpithet(T taxonNameBase
) {
464 Rank rank
= taxonNameBase
.getRank();
465 if(rank
.isGenus() || rank
.isSupraGeneric()) {
466 return taxonNameBase
.getGenusOrUninomial();
467 } else if(rank
.isInfraGeneric()) {
468 return taxonNameBase
.getInfraGenericEpithet();
469 } else if(rank
.isSpecies()) {
470 return taxonNameBase
.getSpecificEpithet();
472 return taxonNameBase
.getInfraSpecificEpithet();