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
.name
.NomenclaturalStatus
;
24 import eu
.etaxonomy
.cdm
.model
.name
.NonViralName
;
25 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
26 import eu
.etaxonomy
.cdm
.model
.reference
.INomenclaturalReference
;
30 * This class is a default implementation for the INonViralNameCacheStrategy<T extends NonViralName> interface.
31 * The method actually implements a cache strategy for botanical names so no method has to be overwritten by
32 * a subclass for botanic names.
33 * Where differing from this Default BotanicNameCacheStrategy other subclasses should overwrite the existing methods
34 * e.g. a CacheStrategy for zoological names should overwrite getAuthorAndExAuthor
42 public class NonViralNameDefaultCacheStrategy
<T
extends NonViralName
> extends NameCacheStrategyBase
<T
> implements INonViralNameCacheStrategy
<T
> {
43 private static final Logger logger
= Logger
.getLogger(NonViralNameDefaultCacheStrategy
.class);
45 final static UUID uuid
= UUID
.fromString("1cdda0d1-d5bc-480f-bf08-40a510a2f223");
47 protected String NameAuthorSeperator
= " ";
48 protected String BasionymStart
= "(";
49 protected String BasionymEnd
= ")";
50 protected String ExAuthorSeperator
= " ex ";
51 protected CharSequence BasionymAuthorCombinationAuthorSeperator
= " ";
54 public UUID
getUuid(){
61 * @return NonViralNameDefaultCacheStrategy A new instance of NonViralNameDefaultCacheStrategy
63 public static NonViralNameDefaultCacheStrategy
NewInstance(){
64 return new NonViralNameDefaultCacheStrategy();
70 protected NonViralNameDefaultCacheStrategy(){
74 /* **************** GETTER / SETTER **************************************/
77 * String that separates the NameCache part from the AuthorCache part
80 public String
getNameAuthorSeperator() {
81 return NameAuthorSeperator
;
85 public void setNameAuthorSeperator(String nameAuthorSeperator
) {
86 NameAuthorSeperator
= nameAuthorSeperator
;
91 * String the basionym author part starts with e.g. '('.
92 * This should correspond with the {@link NonViralNameDefaultCacheStrategy#getBasionymEnd() basionymEnd} attribute
95 public String
getBasionymStart() {
100 public void setBasionymStart(String basionymStart
) {
101 BasionymStart
= basionymStart
;
106 * String the basionym author part ends with e.g. ')'.
107 * This should correspond with the {@link NonViralNameDefaultCacheStrategy#getBasionymStart() basionymStart} attribute
110 public String
getBasionymEnd() {
115 public void setBasionymEnd(String basionymEnd
) {
116 BasionymEnd
= basionymEnd
;
121 * String to seperate ex author from author.
124 public String
getExAuthorSeperator() {
125 return ExAuthorSeperator
;
129 public void setExAuthorSeperator(String exAuthorSeperator
) {
130 ExAuthorSeperator
= exAuthorSeperator
;
135 * String that seperates the basionym/original_combination author part from the combination author part
138 public CharSequence
getBasionymAuthorCombinationAuthorSeperator() {
139 return BasionymAuthorCombinationAuthorSeperator
;
143 public void setBasionymAuthorCombinationAuthorSeperator(
144 CharSequence basionymAuthorCombinationAuthorSeperator
) {
145 BasionymAuthorCombinationAuthorSeperator
= basionymAuthorCombinationAuthorSeperator
;
149 //** *****************************************************************************************/
153 * @see eu.etaxonomy.cdm.strategy.INameCacheStrategy#getNameCache()
156 public String
getTitleCache(T nonViralName
) {
157 if (nonViralName
== null){
161 if (isAutonym(nonViralName
)){
162 String speciesPart
= getSpeciesNameCache(nonViralName
);
163 //TODO should this include basionym authors and ex authors
164 INomenclaturalAuthor author
= nonViralName
.getCombinationAuthorTeam();
165 String authorPart
= "";
167 authorPart
= CdmUtils
.Nz(author
.getNomenclaturalTitle());
169 INomenclaturalAuthor basAuthor
= nonViralName
.getBasionymAuthorTeam();
170 String basAuthorPart
= "";
171 if (basAuthor
!= null){
172 basAuthorPart
= CdmUtils
.Nz(basAuthor
.getNomenclaturalTitle());
174 if (! "".equals(basAuthorPart
)){
175 authorPart
= "("+ basAuthorPart
+")" + authorPart
;
177 String infraSpeciesPart
= (CdmUtils
.Nz(nonViralName
.getInfraSpecificEpithet()));
178 result
= CdmUtils
.concat(" ", new String
[]{speciesPart
, authorPart
, infraSpeciesPart
});
179 result
= result
.trim().replace("null", "");
181 String nameCache
= CdmUtils
.Nz(getNameCache(nonViralName
));
182 String authorCache
= CdmUtils
.Nz(getAuthorshipCache(nonViralName
));
183 result
= CdmUtils
.concat(NameAuthorSeperator
, nameCache
, authorCache
);
190 public String
getFullTitleCache(T nonViralName
) {
192 if (nonViralName
== null){
196 if (nonViralName
.isProtectedFullTitleCache() == true) {
197 return nonViralName
.getFullTitleCache();
202 String titleCache
= getTitleCache(nonViralName
);
204 String microReference
= nonViralName
.getNomenclaturalMicroReference();
205 INomenclaturalReference
<?
> ref
= nonViralName
.getNomenclaturalReference();
206 String referenceBaseCache
= null;
208 referenceBaseCache
= ref
.getNomenclaturalCitation(microReference
);
211 //make nomenclatural status
212 String ncStatusCache
= "";
213 Set
<NomenclaturalStatus
> ncStati
= nonViralName
.getStatus();
214 Iterator
<NomenclaturalStatus
> iterator
= ncStati
.iterator();
215 while (iterator
.hasNext()) {
216 NomenclaturalStatus ncStatus
= (NomenclaturalStatus
)iterator
.next();
217 ncStatusCache
= ", " + ncStatus
.getType().getRepresentation(Language
.LATIN()).getAbbreviatedLabel();
219 String refConcat
= " ";
220 if (referenceBaseCache
!= null && ! referenceBaseCache
.trim().startsWith("in ")){
223 result
= CdmUtils
.concat(refConcat
, titleCache
, referenceBaseCache
);
224 result
= CdmUtils
.concat("", result
, ncStatusCache
);
230 * Generates and returns the "name cache" (only scientific name without author teams and year).
231 * @see eu.etaxonomy.cdm.strategy.cache.name.INameCacheStrategy#getNameCache(eu.etaxonomy.cdm.model.name.TaxonNameBase)
233 public String
getNameCache(T nonViralName
) {
234 if (nonViralName
== null){
238 Rank rank
= nonViralName
.getRank();
241 result
= getRanklessNameCache(nonViralName
);
242 }else if (rank
.isInfraSpecific()){
243 result
= getInfraSpeciesNameCache(nonViralName
);
244 }else if (rank
.isSpecies()){
245 result
= getSpeciesNameCache(nonViralName
);
246 }else if (rank
.isInfraGeneric()){
247 result
= getInfraGenusNameCache(nonViralName
);
248 }else if (rank
.isGenus()){
249 result
= getGenusOrUninomialNameCache(nonViralName
);
250 }else if (rank
.isSupraGeneric()){
251 result
= getGenusOrUninomialNameCache(nonViralName
);
253 logger
.warn("Name Strategy for Name (UUID: " + nonViralName
.getUuid() + ") not yet implemented");
261 * @see eu.etaxonomy.cdm.strategy.cache.INonViralNameCacheStrategy#getAuthorCache(eu.etaxonomy.cdm.model.name.NonViralName)
263 public String
getAuthorshipCache(T nonViralName
) {
264 if (nonViralName
== null){
268 if (nonViralName
.isProtectedAuthorshipCache() == true) {
269 return nonViralName
.getAuthorshipCache();
273 INomenclaturalAuthor combinationAuthor
= nonViralName
.getCombinationAuthorTeam();
274 INomenclaturalAuthor exCombinationAuthor
= nonViralName
.getExCombinationAuthorTeam();
275 INomenclaturalAuthor basionymAuthor
= nonViralName
.getBasionymAuthorTeam();
276 INomenclaturalAuthor exBasionymAuthor
= nonViralName
.getExBasionymAuthorTeam();
277 String basionymPart
= "";
278 String authorPart
= "";
280 if (basionymAuthor
!= null || exBasionymAuthor
!= null){
281 basionymPart
= BasionymStart
+ getAuthorAndExAuthor(basionymAuthor
, exBasionymAuthor
) + BasionymEnd
;
283 if (combinationAuthor
!= null || exCombinationAuthor
!= null){
284 authorPart
= getAuthorAndExAuthor(combinationAuthor
, exCombinationAuthor
);
286 result
= CdmUtils
.concat(BasionymAuthorCombinationAuthorSeperator
, basionymPart
, authorPart
);
291 * Returns the AuthorCache part for a combination of an author and an ex author. This applies on combination authors
292 * as well as on basionym/orginal combination authors.
293 * @param author the author
294 * @param exAuthor the ex-author
297 protected String
getAuthorAndExAuthor(INomenclaturalAuthor author
, INomenclaturalAuthor exAuthor
){
299 String authorString
= "";
300 String exAuthorString
= "";
302 authorString
= CdmUtils
.Nz(author
.getNomenclaturalTitle());
304 if (exAuthor
!= null){
305 exAuthorString
= CdmUtils
.Nz(exAuthor
.getNomenclaturalTitle());
307 if (exAuthorString
.length() > 0 ){
308 exAuthorString
= exAuthorString
+ ExAuthorSeperator
;
310 result
= exAuthorString
+ authorString
;
317 * @see eu.etaxonomy.cdm.strategy.INameCacheStrategy#getTaggedName(eu.etaxonomy.cdm.model.common.CdmBase)
320 public List
<Object
> getTaggedName(T nonViralName
) {
321 List
<Object
> tags
= new ArrayList
<Object
>();
322 tags
.add(nonViralName
.getGenusOrUninomial());
323 if (nonViralName
.isSpecies() || nonViralName
.isInfraSpecific()){
324 tags
.add(nonViralName
.getSpecificEpithet());
328 if (nonViralName
.isInfraSpecific() && ! nonViralName
.getSpecificEpithet().equals(nonViralName
.getInfraSpecificEpithet())){
329 tags
.add(nonViralName
.getRank());
330 tags
.add(nonViralName
.getInfraSpecificEpithet());
333 if (nonViralName
.isInfraGeneric()){
334 //TODO choose right strategy or generic approach?
335 // --- strategy 1 ---
336 tags
.add(nonViralName
.getRank());
337 tags
.add(nonViralName
.getInfraGenericEpithet());
338 // --- strategy 2 ---
339 // tags.add('('+nvn.getInfraGenericEpithet()+')');
341 Team authorTeam
= Team
.NewInstance();
342 authorTeam
.setProtectedTitleCache(true);
343 authorTeam
.setTitleCache(nonViralName
.getAuthorshipCache());
344 tags
.add(authorTeam
);
346 // Name is an autonym. Rank and infraspecific eitheton follow the author
347 if (nonViralName
.isInfraSpecific() && nonViralName
.getSpecificEpithet().equals(nonViralName
.getInfraSpecificEpithet())){
348 tags
.add(nonViralName
.getRank());
349 tags
.add(nonViralName
.getInfraSpecificEpithet());
352 if(! "".equals(nonViralName
.getAppendedPhrase())){
353 tags
.add(nonViralName
.getAppendedPhrase());
360 /************** PRIVATES ****************/
362 protected String
getRanklessNameCache(NonViralName nonViralName
){
364 result
= (result
+ (nonViralName
.getGenusOrUninomial())).trim().replace("null", "");
365 result
+= " " + (CdmUtils
.Nz(nonViralName
.getSpecificEpithet())).trim();
366 result
+= " " + (CdmUtils
.Nz(nonViralName
.getInfraSpecificEpithet())).trim();
367 result
= result
.trim().replace("null", "");
368 //result += " (rankless)";
369 result
= addAppendedPhrase(result
, nonViralName
);
374 protected String
getGenusOrUninomialNameCache(NonViralName nonViralName
){
376 result
= CdmUtils
.Nz(nonViralName
.getGenusOrUninomial());
377 result
= addAppendedPhrase(result
, nonViralName
);
381 protected String
getInfraGenusNameCache(NonViralName nonViralName
){
383 result
= CdmUtils
.Nz(nonViralName
.getGenusOrUninomial());
384 result
+= " (" + (CdmUtils
.Nz(nonViralName
.getInfraGenericEpithet()) + ")").trim().replace("null", "");
385 result
= addAppendedPhrase(result
, nonViralName
);
390 protected String
getSpeciesNameCache(NonViralName nonViralName
){
392 result
= CdmUtils
.Nz(nonViralName
.getGenusOrUninomial());
393 result
+= " " + CdmUtils
.Nz(nonViralName
.getSpecificEpithet()).trim().replace("null", "");
394 result
= addAppendedPhrase(result
, nonViralName
);
399 protected String
getInfraSpeciesNameCache(NonViralName nonViralName
){
401 result
= CdmUtils
.Nz(nonViralName
.getGenusOrUninomial());
402 result
+= " " + (CdmUtils
.Nz(nonViralName
.getSpecificEpithet()).trim()).replace("null", "");
403 if (! isAutonym(nonViralName
)){
404 result
+= " " + (nonViralName
.getRank().getAbbreviation()).trim().replace("null", "");
406 result
+= " " + (CdmUtils
.Nz(nonViralName
.getInfraSpecificEpithet())).trim().replace("null", "");
407 result
= addAppendedPhrase(result
, nonViralName
);
414 * @return true, if name has Rank, Rank is below species and species epithet equals infraSpeciesEpithtet, else false
416 protected boolean isAutonym(NonViralName nonViralName
){
417 if (nonViralName
!= null && nonViralName
.getRank() != null && nonViralName
.getSpecificEpithet() != null && nonViralName
.getInfraSpecificEpithet() != null &&
418 nonViralName
.getRank().isInfraSpecific() && nonViralName
.getSpecificEpithet().trim().equals(nonViralName
.getInfraSpecificEpithet().trim())){
425 protected String
addAppendedPhrase(String resultString
, NonViralName nonViralName
){
426 String appendedPhrase
= nonViralName
==null ?
null : nonViralName
.getAppendedPhrase();
427 if (resultString
== null){
428 return appendedPhrase
;
429 }else if(appendedPhrase
== null || "".equals(appendedPhrase
.trim())) {
431 }else if ("".equals(resultString
)){
432 return resultString
+ appendedPhrase
;
434 return resultString
+ " " + appendedPhrase
;