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
.List
;
13 import java
.util
.UUID
;
15 import org
.apache
.log4j
.Logger
;
17 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
18 import eu
.etaxonomy
.cdm
.model
.agent
.INomenclaturalAuthor
;
19 import eu
.etaxonomy
.cdm
.model
.agent
.Team
;
20 import eu
.etaxonomy
.cdm
.model
.name
.NonViralName
;
21 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
22 import eu
.etaxonomy
.cdm
.model
.reference
.INomenclaturalReference
;
26 * This class is a default implementation for the INonViralNameCacheStrategy<T extends NonViralName> interface.
27 * The method actually implements a cache strategy for botanical names so no method has to be overwritten by
28 * a subclass for botanic names.
29 * Where differing from this Default BotanicNameCacheStrategy other subclasses should overwrite the existing methods
30 * e.g. a CacheStrategy for zoological names should overwrite getAuthorAndExAuthor
38 public class NonViralNameDefaultCacheStrategy
<T
extends NonViralName
> extends NameCacheStrategyBase
<T
> implements INonViralNameCacheStrategy
<T
> {
39 private static final Logger logger
= Logger
.getLogger(NonViralNameDefaultCacheStrategy
.class);
41 final static UUID uuid
= UUID
.fromString("1cdda0d1-d5bc-480f-bf08-40a510a2f223");
43 protected String NameAuthorSeperator
= " ";
44 protected String BasionymStart
= "(";
45 protected String BasionymEnd
= ")";
46 protected String ExAuthorSeperator
= " ex ";
47 protected CharSequence BasionymAuthorCombinationAuthorSeperator
= " ";
50 public UUID
getUuid(){
57 * @return NonViralNameDefaultCacheStrategy A new instance of NonViralNameDefaultCacheStrategy
59 public static NonViralNameDefaultCacheStrategy
NewInstance(){
60 return new NonViralNameDefaultCacheStrategy();
66 protected NonViralNameDefaultCacheStrategy(){
70 /* **************** GETTER / SETTER **************************************/
73 * String that separates the NameCache part from the AuthorCache part
76 public String
getNameAuthorSeperator() {
77 return NameAuthorSeperator
;
81 public void setNameAuthorSeperator(String nameAuthorSeperator
) {
82 NameAuthorSeperator
= nameAuthorSeperator
;
87 * String the basionym author part starts with e.g. '('.
88 * This should correspond with the {@link NonViralNameDefaultCacheStrategy#getBasionymEnd() basionymEnd} attribute
91 public String
getBasionymStart() {
96 public void setBasionymStart(String basionymStart
) {
97 BasionymStart
= basionymStart
;
102 * String the basionym author part ends with e.g. ')'.
103 * This should correspond with the {@link NonViralNameDefaultCacheStrategy#getBasionymStart() basionymStart} attribute
106 public String
getBasionymEnd() {
111 public void setBasionymEnd(String basionymEnd
) {
112 BasionymEnd
= basionymEnd
;
117 * String to seperate ex author from author.
120 public String
getExAuthorSeperator() {
121 return ExAuthorSeperator
;
125 public void setExAuthorSeperator(String exAuthorSeperator
) {
126 ExAuthorSeperator
= exAuthorSeperator
;
131 * String that seperates the basionym/original_combination author part from the combination author part
134 public CharSequence
getBasionymAuthorCombinationAuthorSeperator() {
135 return BasionymAuthorCombinationAuthorSeperator
;
139 public void setBasionymAuthorCombinationAuthorSeperator(
140 CharSequence basionymAuthorCombinationAuthorSeperator
) {
141 BasionymAuthorCombinationAuthorSeperator
= basionymAuthorCombinationAuthorSeperator
;
145 //** *****************************************************************************************/
149 * @see eu.etaxonomy.cdm.strategy.INameCacheStrategy#getNameCache()
153 public String
getTitleCache(T nonViralName
) {
154 if (nonViralName
== null){
158 if (isAutonym(nonViralName
)){
159 String speciesPart
= getSpeciesNameCache(nonViralName
);
160 //TODO should this include basionym authors and ex authors
161 INomenclaturalAuthor author
= nonViralName
.getCombinationAuthorTeam();
162 String authorPart
= "";
164 authorPart
= CdmUtils
.Nz(author
.getNomenclaturalTitle());
166 INomenclaturalAuthor basAuthor
= nonViralName
.getBasionymAuthorTeam();
167 String basAuthorPart
= "";
168 if (basAuthor
!= null){
169 basAuthorPart
= CdmUtils
.Nz(basAuthor
.getNomenclaturalTitle());
171 if (! "".equals(basAuthorPart
)){
172 authorPart
= "("+ basAuthorPart
+")" + authorPart
;
174 String infraSpeciesPart
= (CdmUtils
.Nz(nonViralName
.getInfraSpecificEpithet()));
175 result
= CdmUtils
.concat(" ", new String
[]{speciesPart
, authorPart
, infraSpeciesPart
});
176 result
= result
.trim().replace("null", "");
178 String nameCache
= CdmUtils
.Nz(getNameCache(nonViralName
));
179 String authorCache
= CdmUtils
.Nz(getAuthorshipCache(nonViralName
));
180 result
= CdmUtils
.concat(NameAuthorSeperator
, nameCache
, authorCache
);
187 * Generates and returns the "name cache" (only scientific name without author teams and year).
188 * @see eu.etaxonomy.cdm.strategy.cache.name.INameCacheStrategy#getNameCache(eu.etaxonomy.cdm.model.name.TaxonNameBase)
190 public String
getNameCache(T nonViralName
) {
191 if (nonViralName
== null){
195 Rank rank
= nonViralName
.getRank();
198 result
= getRanklessNameCache(nonViralName
);
199 }else if (rank
.isInfraSpecific()){
200 result
= getInfraSpeciesNameCache(nonViralName
);
201 }else if (rank
.isSpecies()){
202 result
= getSpeciesNameCache(nonViralName
);
203 }else if (rank
.isInfraGeneric()){
204 result
= getInfraGenusNameCache(nonViralName
);
205 }else if (rank
.isGenus()){
206 result
= getGenusOrUninomialNameCache(nonViralName
);
207 }else if (rank
.isSupraGeneric()){
208 result
= getGenusOrUninomialNameCache(nonViralName
);
210 logger
.warn("Name Strategy for Name (UUID: " + nonViralName
.getUuid() + ") not yet implemented");
218 * @see eu.etaxonomy.cdm.strategy.cache.INonViralNameCacheStrategy#getAuthorCache(eu.etaxonomy.cdm.model.name.NonViralName)
220 public String
getAuthorshipCache(T nonViralName
) {
221 if (nonViralName
== null){
225 INomenclaturalAuthor combinationAuthor
= nonViralName
.getCombinationAuthorTeam();
226 INomenclaturalAuthor exCombinationAuthor
= nonViralName
.getExCombinationAuthorTeam();
227 INomenclaturalAuthor basionymAuthor
= nonViralName
.getBasionymAuthorTeam();
228 INomenclaturalAuthor exBasionymAuthor
= nonViralName
.getExBasionymAuthorTeam();
229 String basionymPart
= "";
230 String authorPart
= "";
232 if (basionymAuthor
!= null || exBasionymAuthor
!= null){
233 basionymPart
= BasionymStart
+ getAuthorAndExAuthor(basionymAuthor
, exBasionymAuthor
) + BasionymEnd
;
235 if (combinationAuthor
!= null || exCombinationAuthor
!= null){
236 authorPart
= getAuthorAndExAuthor(combinationAuthor
, exCombinationAuthor
);
238 result
= CdmUtils
.concat(BasionymAuthorCombinationAuthorSeperator
, basionymPart
, authorPart
);
243 * Returns the AuthorCache part for a combination of an author and an ex author. This applies on combination authors
244 * as well as on basionym/orginal combination authors.
245 * @param author the author
246 * @param exAuthor the ex-author
249 protected String
getAuthorAndExAuthor(INomenclaturalAuthor author
, INomenclaturalAuthor exAuthor
){
251 String authorString
= "";
252 String exAuthorString
= "";
254 authorString
= CdmUtils
.Nz(author
.getNomenclaturalTitle());
256 if (exAuthor
!= null){
257 exAuthorString
= CdmUtils
.Nz(exAuthor
.getNomenclaturalTitle());
259 if (exAuthorString
.length() > 0 ){
260 exAuthorString
= exAuthorString
+ ExAuthorSeperator
;
262 result
= exAuthorString
+ authorString
;
269 * @see eu.etaxonomy.cdm.strategy.INameCacheStrategy#getTaggedName(eu.etaxonomy.cdm.model.common.CdmBase)
272 public List
<Object
> getTaggedName(T nonViralName
) {
273 List
<Object
> tags
= new ArrayList
<Object
>();
274 tags
.add(nonViralName
.getGenusOrUninomial());
275 if (nonViralName
.isSpecies() || nonViralName
.isInfraSpecific()){
276 tags
.add(nonViralName
.getSpecificEpithet());
280 if (nonViralName
.isInfraSpecific() && ! nonViralName
.getSpecificEpithet().equals(nonViralName
.getInfraSpecificEpithet())){
281 tags
.add(nonViralName
.getRank());
282 tags
.add(nonViralName
.getInfraSpecificEpithet());
285 if (nonViralName
.isInfraGeneric()){
286 //TODO choose right strategy or generic approach?
287 // --- strategy 1 ---
288 tags
.add(nonViralName
.getRank());
289 tags
.add(nonViralName
.getInfraGenericEpithet());
290 // --- strategy 2 ---
291 // tags.add('('+nvn.getInfraGenericEpithet()+')');
293 Team authorTeam
= Team
.NewInstance();
294 authorTeam
.setProtectedTitleCache(true);
295 authorTeam
.setTitleCache(nonViralName
.getAuthorshipCache());
296 tags
.add(authorTeam
);
298 // Name is an autonym. Rank and infraspecific eitheton follow the author
299 if (nonViralName
.isInfraSpecific() && nonViralName
.getSpecificEpithet().equals(nonViralName
.getInfraSpecificEpithet())){
300 tags
.add(nonViralName
.getRank());
301 tags
.add(nonViralName
.getInfraSpecificEpithet());
304 if(! "".equals(nonViralName
.getAppendedPhrase())){
305 tags
.add(nonViralName
.getAppendedPhrase());
312 /************** PRIVATES ****************/
314 protected String
getRanklessNameCache(NonViralName nonViralName
){
316 result
= (result
+ (nonViralName
.getGenusOrUninomial())).trim().replace("null", "-");
317 result
+= " " + (CdmUtils
.Nz(nonViralName
.getSpecificEpithet())).trim();
318 result
+= " " + (CdmUtils
.Nz(nonViralName
.getInfraSpecificEpithet())).trim();
319 result
= result
.trim().replace("null", "-");
320 //result += " (rankless)";
321 result
= addAppendedPhrase(result
, nonViralName
);
326 protected String
getGenusOrUninomialNameCache(NonViralName nonViralName
){
328 result
= CdmUtils
.Nz(nonViralName
.getGenusOrUninomial());
329 result
= addAppendedPhrase(result
, nonViralName
);
333 protected String
getInfraGenusNameCache(NonViralName nonViralName
){
335 result
= CdmUtils
.Nz(nonViralName
.getGenusOrUninomial());
336 result
+= " (" + (CdmUtils
.Nz(nonViralName
.getInfraGenericEpithet()) + ")").trim().replace("null", "");
337 result
= addAppendedPhrase(result
, nonViralName
);
342 protected String
getSpeciesNameCache(NonViralName nonViralName
){
344 result
= CdmUtils
.Nz(nonViralName
.getGenusOrUninomial());
345 result
+= " " + CdmUtils
.Nz(nonViralName
.getSpecificEpithet()).trim().replace("null", "");
346 result
= addAppendedPhrase(result
, nonViralName
);
351 protected String
getInfraSpeciesNameCache(NonViralName nonViralName
){
353 result
= CdmUtils
.Nz(nonViralName
.getGenusOrUninomial());
354 result
+= " " + (CdmUtils
.Nz(nonViralName
.getSpecificEpithet()).trim()).replace("null", "");
355 if (! isAutonym(nonViralName
)){
356 result
+= " " + (nonViralName
.getRank().getAbbreviation()).trim().replace("null", "");
358 result
+= " " + (CdmUtils
.Nz(nonViralName
.getInfraSpecificEpithet())).trim().replace("null", "");
359 result
= addAppendedPhrase(result
, nonViralName
);
366 * @return true, if name has Rank, Rank is below species and species epithet equals infraSpeciesEpithtet, else false
368 protected boolean isAutonym(NonViralName nonViralName
){
369 if (nonViralName
!= null && nonViralName
.getRank() != null && nonViralName
.getSpecificEpithet() != null && nonViralName
.getInfraSpecificEpithet() != null &&
370 nonViralName
.getRank().isInfraSpecific() && nonViralName
.getSpecificEpithet().trim().equals(nonViralName
.getInfraSpecificEpithet().trim())){
377 protected String
addAppendedPhrase(String resultString
, NonViralName nonViralName
){
378 String appendedPhrase
= nonViralName
==null ?
null : nonViralName
.getAppendedPhrase();
379 if (resultString
== null){
380 return appendedPhrase
;
381 }else if(appendedPhrase
== null || "".equals(appendedPhrase
.trim())) {
383 }else if ("".equals(resultString
)){
384 return resultString
+ appendedPhrase
;
386 return resultString
+ " " + appendedPhrase
;