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
.DefinedTermBase
;
23 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
24 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalStatus
;
25 import eu
.etaxonomy
.cdm
.model
.name
.NonViralName
;
26 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
27 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
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()));
180 result
= CdmUtils
.concat(" ", new String
[]{speciesPart
, authorPart
, infraSpeciesPart
});
181 result
= result
.trim().replace("null", "");
183 String nameCache
= CdmUtils
.Nz(getNameCache(nonViralName
));
184 String authorCache
= CdmUtils
.Nz(getAuthorshipCache(nonViralName
));
185 result
= CdmUtils
.concat(NameAuthorSeperator
, nameCache
, authorCache
);
192 public String
getFullTitleCache(T nonViralName
) {
193 if (nonViralName
== null){
197 String titleCache
= getTitleCache(nonViralName
);
198 String microReference
= nonViralName
.getNomenclaturalMicroReference();
199 INomenclaturalReference ref
= nonViralName
.getNomenclaturalReference();
200 String referenceBaseCache
= null;
202 referenceBaseCache
= ref
.getNomenclaturalCitation(microReference
);
205 String ncStatusCache
= "";
206 // NomenclaturalStatus ncStatus = (NomenclaturalStatus)nonViralName.getStatus().iterator().next();
207 // ncStatusCache = ncStatus.getType().getRepresentation(Language.LATIN()).getAbbreviatedLabel();
209 Set
<NomenclaturalStatus
> ncStati
= nonViralName
.getStatus();
210 Iterator
<NomenclaturalStatus
> iterator
= ncStati
.iterator();
211 while (iterator
.hasNext()) {
212 NomenclaturalStatus ncStatus
= (NomenclaturalStatus
)iterator
.next();
213 ncStatusCache
= ", " + ncStatus
.getType().getRepresentation(Language
.LATIN()).getAbbreviatedLabel();
215 String refConcat
= " ";
216 if (referenceBaseCache
!= null && ! referenceBaseCache
.trim().startsWith("in ")){
219 result
= CdmUtils
.concat(refConcat
, titleCache
, referenceBaseCache
);
220 result
= CdmUtils
.concat("", result
, ncStatusCache
);
226 * Generates and returns the "name cache" (only scientific name without author teams and year).
227 * @see eu.etaxonomy.cdm.strategy.cache.name.INameCacheStrategy#getNameCache(eu.etaxonomy.cdm.model.name.TaxonNameBase)
229 public String
getNameCache(T nonViralName
) {
230 if (nonViralName
== null){
234 Rank rank
= nonViralName
.getRank();
237 result
= getRanklessNameCache(nonViralName
);
238 }else if (rank
.isInfraSpecific()){
239 result
= getInfraSpeciesNameCache(nonViralName
);
240 }else if (rank
.isSpecies()){
241 result
= getSpeciesNameCache(nonViralName
);
242 }else if (rank
.isInfraGeneric()){
243 result
= getInfraGenusNameCache(nonViralName
);
244 }else if (rank
.isGenus()){
245 result
= getGenusOrUninomialNameCache(nonViralName
);
246 }else if (rank
.isSupraGeneric()){
247 result
= getGenusOrUninomialNameCache(nonViralName
);
249 logger
.warn("Name Strategy for Name (UUID: " + nonViralName
.getUuid() + ") not yet implemented");
257 * @see eu.etaxonomy.cdm.strategy.cache.INonViralNameCacheStrategy#getAuthorCache(eu.etaxonomy.cdm.model.name.NonViralName)
259 public String
getAuthorshipCache(T nonViralName
) {
260 if (nonViralName
== null){
264 INomenclaturalAuthor combinationAuthor
= nonViralName
.getCombinationAuthorTeam();
265 INomenclaturalAuthor exCombinationAuthor
= nonViralName
.getExCombinationAuthorTeam();
266 INomenclaturalAuthor basionymAuthor
= nonViralName
.getBasionymAuthorTeam();
267 INomenclaturalAuthor exBasionymAuthor
= nonViralName
.getExBasionymAuthorTeam();
268 String basionymPart
= "";
269 String authorPart
= "";
271 if (basionymAuthor
!= null || exBasionymAuthor
!= null){
272 basionymPart
= BasionymStart
+ getAuthorAndExAuthor(basionymAuthor
, exBasionymAuthor
) + BasionymEnd
;
274 if (combinationAuthor
!= null || exCombinationAuthor
!= null){
275 authorPart
= getAuthorAndExAuthor(combinationAuthor
, exCombinationAuthor
);
277 result
= CdmUtils
.concat(BasionymAuthorCombinationAuthorSeperator
, basionymPart
, authorPart
);
282 * Returns the AuthorCache part for a combination of an author and an ex author. This applies on combination authors
283 * as well as on basionym/orginal combination authors.
284 * @param author the author
285 * @param exAuthor the ex-author
288 protected String
getAuthorAndExAuthor(INomenclaturalAuthor author
, INomenclaturalAuthor exAuthor
){
290 String authorString
= "";
291 String exAuthorString
= "";
293 authorString
= CdmUtils
.Nz(author
.getNomenclaturalTitle());
295 if (exAuthor
!= null){
296 exAuthorString
= CdmUtils
.Nz(exAuthor
.getNomenclaturalTitle());
298 if (exAuthorString
.length() > 0 ){
299 exAuthorString
= exAuthorString
+ ExAuthorSeperator
;
301 result
= exAuthorString
+ authorString
;
308 * @see eu.etaxonomy.cdm.strategy.INameCacheStrategy#getTaggedName(eu.etaxonomy.cdm.model.common.CdmBase)
311 public List
<Object
> getTaggedName(T nonViralName
) {
312 List
<Object
> tags
= new ArrayList
<Object
>();
313 tags
.add(nonViralName
.getGenusOrUninomial());
314 if (nonViralName
.isSpecies() || nonViralName
.isInfraSpecific()){
315 tags
.add(nonViralName
.getSpecificEpithet());
319 if (nonViralName
.isInfraSpecific() && ! nonViralName
.getSpecificEpithet().equals(nonViralName
.getInfraSpecificEpithet())){
320 tags
.add(nonViralName
.getRank());
321 tags
.add(nonViralName
.getInfraSpecificEpithet());
324 if (nonViralName
.isInfraGeneric()){
325 //TODO choose right strategy or generic approach?
326 // --- strategy 1 ---
327 tags
.add(nonViralName
.getRank());
328 tags
.add(nonViralName
.getInfraGenericEpithet());
329 // --- strategy 2 ---
330 // tags.add('('+nvn.getInfraGenericEpithet()+')');
332 Team authorTeam
= Team
.NewInstance();
333 authorTeam
.setProtectedTitleCache(true);
334 authorTeam
.setTitleCache(nonViralName
.getAuthorshipCache());
335 tags
.add(authorTeam
);
337 // Name is an autonym. Rank and infraspecific eitheton follow the author
338 if (nonViralName
.isInfraSpecific() && nonViralName
.getSpecificEpithet().equals(nonViralName
.getInfraSpecificEpithet())){
339 tags
.add(nonViralName
.getRank());
340 tags
.add(nonViralName
.getInfraSpecificEpithet());
343 if(! "".equals(nonViralName
.getAppendedPhrase())){
344 tags
.add(nonViralName
.getAppendedPhrase());
351 /************** PRIVATES ****************/
353 protected String
getRanklessNameCache(NonViralName nonViralName
){
355 result
= (result
+ (nonViralName
.getGenusOrUninomial())).trim().replace("null", "-");
356 result
+= " " + (CdmUtils
.Nz(nonViralName
.getSpecificEpithet())).trim();
357 result
+= " " + (CdmUtils
.Nz(nonViralName
.getInfraSpecificEpithet())).trim();
358 result
= result
.trim().replace("null", "-");
359 //result += " (rankless)";
360 result
= addAppendedPhrase(result
, nonViralName
);
365 protected String
getGenusOrUninomialNameCache(NonViralName nonViralName
){
367 result
= CdmUtils
.Nz(nonViralName
.getGenusOrUninomial());
368 result
= addAppendedPhrase(result
, nonViralName
);
372 protected String
getInfraGenusNameCache(NonViralName nonViralName
){
374 result
= CdmUtils
.Nz(nonViralName
.getGenusOrUninomial());
375 result
+= " (" + (CdmUtils
.Nz(nonViralName
.getInfraGenericEpithet()) + ")").trim().replace("null", "");
376 result
= addAppendedPhrase(result
, nonViralName
);
381 protected String
getSpeciesNameCache(NonViralName nonViralName
){
383 result
= CdmUtils
.Nz(nonViralName
.getGenusOrUninomial());
384 result
+= " " + CdmUtils
.Nz(nonViralName
.getSpecificEpithet()).trim().replace("null", "");
385 result
= addAppendedPhrase(result
, nonViralName
);
390 protected String
getInfraSpeciesNameCache(NonViralName nonViralName
){
392 result
= CdmUtils
.Nz(nonViralName
.getGenusOrUninomial());
393 result
+= " " + (CdmUtils
.Nz(nonViralName
.getSpecificEpithet()).trim()).replace("null", "");
394 if (! isAutonym(nonViralName
)){
395 result
+= " " + (nonViralName
.getRank().getAbbreviation()).trim().replace("null", "");
397 result
+= " " + (CdmUtils
.Nz(nonViralName
.getInfraSpecificEpithet())).trim().replace("null", "");
398 result
= addAppendedPhrase(result
, nonViralName
);
405 * @return true, if name has Rank, Rank is below species and species epithet equals infraSpeciesEpithtet, else false
407 protected boolean isAutonym(NonViralName nonViralName
){
408 if (nonViralName
!= null && nonViralName
.getRank() != null && nonViralName
.getSpecificEpithet() != null && nonViralName
.getInfraSpecificEpithet() != null &&
409 nonViralName
.getRank().isInfraSpecific() && nonViralName
.getSpecificEpithet().trim().equals(nonViralName
.getInfraSpecificEpithet().trim())){
416 protected String
addAppendedPhrase(String resultString
, NonViralName nonViralName
){
417 String appendedPhrase
= nonViralName
==null ?
null : nonViralName
.getAppendedPhrase();
418 if (resultString
== null){
419 return appendedPhrase
;
420 }else if(appendedPhrase
== null || "".equals(appendedPhrase
.trim())) {
422 }else if ("".equals(resultString
)){
423 return resultString
+ appendedPhrase
;
425 return resultString
+ " " + appendedPhrase
;