(no commit message)
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / strategy / cache / name / NonViralNameDefaultCacheStrategy.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9 package eu.etaxonomy.cdm.strategy.cache.name;
10
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.UUID;
14
15 import org.apache.log4j.Logger;
16
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;
23
24
25 /**
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
31 * @author a.mueller
32 */
33 /**
34 * @author AM
35 *
36 * @param <T>
37 */
38 public class NonViralNameDefaultCacheStrategy<T extends NonViralName> extends NameCacheStrategyBase<T> implements INonViralNameCacheStrategy<T> {
39 private static final Logger logger = Logger.getLogger(NonViralNameDefaultCacheStrategy.class);
40
41 final static UUID uuid = UUID.fromString("1cdda0d1-d5bc-480f-bf08-40a510a2f223");
42
43 protected String NameAuthorSeperator = " ";
44 protected String BasionymStart = "(";
45 protected String BasionymEnd = ")";
46 protected String ExAuthorSeperator = " ex ";
47 protected CharSequence BasionymAuthorCombinationAuthorSeperator = " ";
48
49 @Override
50 public UUID getUuid(){
51 return uuid;
52 }
53
54
55 /**
56 * Factory method
57 * @return NonViralNameDefaultCacheStrategy A new instance of NonViralNameDefaultCacheStrategy
58 */
59 public static NonViralNameDefaultCacheStrategy NewInstance(){
60 return new NonViralNameDefaultCacheStrategy();
61 }
62
63 /**
64 * Constructor
65 */
66 protected NonViralNameDefaultCacheStrategy(){
67 super();
68 }
69
70 /* **************** GETTER / SETTER **************************************/
71
72 /**
73 * String that separates the NameCache part from the AuthorCache part
74 * @return
75 */
76 public String getNameAuthorSeperator() {
77 return NameAuthorSeperator;
78 }
79
80
81 public void setNameAuthorSeperator(String nameAuthorSeperator) {
82 NameAuthorSeperator = nameAuthorSeperator;
83 }
84
85
86 /**
87 * String the basionym author part starts with e.g. '('.
88 * This should correspond with the {@link NonViralNameDefaultCacheStrategy#getBasionymEnd() basionymEnd} attribute
89 * @return
90 */
91 public String getBasionymStart() {
92 return BasionymStart;
93 }
94
95
96 public void setBasionymStart(String basionymStart) {
97 BasionymStart = basionymStart;
98 }
99
100
101 /**
102 * String the basionym author part ends with e.g. ')'.
103 * This should correspond with the {@link NonViralNameDefaultCacheStrategy#getBasionymStart() basionymStart} attribute
104 * @return
105 */
106 public String getBasionymEnd() {
107 return BasionymEnd;
108 }
109
110
111 public void setBasionymEnd(String basionymEnd) {
112 BasionymEnd = basionymEnd;
113 }
114
115
116 /**
117 * String to seperate ex author from author.
118 * @return
119 */
120 public String getExAuthorSeperator() {
121 return ExAuthorSeperator;
122 }
123
124
125 public void setExAuthorSeperator(String exAuthorSeperator) {
126 ExAuthorSeperator = exAuthorSeperator;
127 }
128
129
130 /**
131 * String that seperates the basionym/original_combination author part from the combination author part
132 * @return
133 */
134 public CharSequence getBasionymAuthorCombinationAuthorSeperator() {
135 return BasionymAuthorCombinationAuthorSeperator;
136 }
137
138
139 public void setBasionymAuthorCombinationAuthorSeperator(
140 CharSequence basionymAuthorCombinationAuthorSeperator) {
141 BasionymAuthorCombinationAuthorSeperator = basionymAuthorCombinationAuthorSeperator;
142 }
143
144
145 //** *****************************************************************************************/
146
147
148 /* (non-Javadoc)
149 * @see eu.etaxonomy.cdm.strategy.INameCacheStrategy#getNameCache()
150 */
151 // just for testing
152 @Override
153 public String getTitleCache(T nonViralName) {
154 if (nonViralName == null){
155 return null;
156 }
157 String result = "";
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 = "";
163 if (author != null){
164 authorPart = CdmUtils.Nz(author.getNomenclaturalTitle());
165 }
166 INomenclaturalAuthor basAuthor = nonViralName.getBasionymAuthorTeam();
167 String basAuthorPart = "";
168 if (basAuthor != null){
169 basAuthorPart = CdmUtils.Nz(basAuthor.getNomenclaturalTitle());
170 }
171 if (! "".equals(basAuthorPart)){
172 authorPart = "("+ basAuthorPart +")" + authorPart;
173 }
174 String infraSpeciesPart = (CdmUtils.Nz(nonViralName.getInfraSpecificEpithet()));
175 result = CdmUtils.concat(" ", new String[]{speciesPart, authorPart, infraSpeciesPart});
176 result = result.trim().replace("null", "");
177 }else{
178 String nameCache = CdmUtils.Nz(getNameCache(nonViralName));
179 String authorCache = CdmUtils.Nz(getAuthorshipCache(nonViralName));
180 result = CdmUtils.concat(NameAuthorSeperator, nameCache, authorCache);
181 }
182 return result;
183 }
184
185
186 /**
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)
189 */
190 public String getNameCache(T nonViralName) {
191 if (nonViralName == null){
192 return null;
193 }
194 String result;
195 Rank rank = nonViralName.getRank();
196
197 if (rank == null){
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);
209 }else{
210 logger.warn("Name Strategy for Name (UUID: " + nonViralName.getUuid() + ") not yet implemented");
211 result = "";
212 }
213 return result;
214 }
215
216
217 /* (non-Javadoc)
218 * @see eu.etaxonomy.cdm.strategy.cache.INonViralNameCacheStrategy#getAuthorCache(eu.etaxonomy.cdm.model.name.NonViralName)
219 */
220 public String getAuthorshipCache(T nonViralName) {
221 if (nonViralName == null){
222 return null;
223 }
224 String result = "";
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 = "";
231 //basionym
232 if (basionymAuthor != null || exBasionymAuthor != null){
233 basionymPart = BasionymStart + getAuthorAndExAuthor(basionymAuthor, exBasionymAuthor) + BasionymEnd;
234 }
235 if (combinationAuthor != null || exCombinationAuthor != null){
236 authorPart = getAuthorAndExAuthor(combinationAuthor, exCombinationAuthor);
237 }
238 result = CdmUtils.concat(BasionymAuthorCombinationAuthorSeperator, basionymPart, authorPart);
239 return result;
240 }
241
242 /**
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
247 * @return
248 */
249 protected String getAuthorAndExAuthor(INomenclaturalAuthor author, INomenclaturalAuthor exAuthor){
250 String result = "";
251 String authorString = "";
252 String exAuthorString = "";
253 if (author != null){
254 authorString = CdmUtils.Nz(author.getNomenclaturalTitle());
255 }
256 if (exAuthor != null){
257 exAuthorString = CdmUtils.Nz(exAuthor.getNomenclaturalTitle());
258 }
259 if (exAuthorString.length() > 0 ){
260 exAuthorString = exAuthorString + ExAuthorSeperator;
261 }
262 result = exAuthorString + authorString;
263 return result;
264
265 }
266
267
268 /* (non-Javadoc)
269 * @see eu.etaxonomy.cdm.strategy.INameCacheStrategy#getTaggedName(eu.etaxonomy.cdm.model.common.CdmBase)
270 */
271 @Override
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());
277 }
278
279 // No autonym
280 if (nonViralName.isInfraSpecific() && ! nonViralName.getSpecificEpithet().equals(nonViralName.getInfraSpecificEpithet())){
281 tags.add(nonViralName.getRank());
282 tags.add(nonViralName.getInfraSpecificEpithet());
283 }
284
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()+')');
292 }
293 Team authorTeam = Team.NewInstance();
294 authorTeam.setProtectedTitleCache(true);
295 authorTeam.setTitleCache(nonViralName.getAuthorshipCache());
296 tags.add(authorTeam);
297
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());
302 }
303
304 if(! "".equals(nonViralName.getAppendedPhrase())){
305 tags.add(nonViralName.getAppendedPhrase());
306 }
307
308 return tags;
309 }
310
311
312 /************** PRIVATES ****************/
313
314 protected String getRanklessNameCache(NonViralName nonViralName){
315 String result = "";
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);
322 return result;
323 }
324
325
326 protected String getGenusOrUninomialNameCache(NonViralName nonViralName){
327 String result;
328 result = CdmUtils.Nz(nonViralName.getGenusOrUninomial());
329 result = addAppendedPhrase(result, nonViralName);
330 return result;
331 }
332
333 protected String getInfraGenusNameCache(NonViralName nonViralName){
334 String result;
335 result = CdmUtils.Nz(nonViralName.getGenusOrUninomial());
336 result += " (" + (CdmUtils.Nz(nonViralName.getInfraGenericEpithet()) + ")").trim().replace("null", "");
337 result = addAppendedPhrase(result, nonViralName);
338 return result;
339 }
340
341
342 protected String getSpeciesNameCache(NonViralName nonViralName){
343 String result;
344 result = CdmUtils.Nz(nonViralName.getGenusOrUninomial());
345 result += " " + CdmUtils.Nz(nonViralName.getSpecificEpithet()).trim().replace("null", "");
346 result = addAppendedPhrase(result, nonViralName);
347 return result;
348 }
349
350
351 protected String getInfraSpeciesNameCache(NonViralName nonViralName){
352 String result;
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", "");
357 }
358 result += " " + (CdmUtils.Nz(nonViralName.getInfraSpecificEpithet())).trim().replace("null", "");
359 result = addAppendedPhrase(result, nonViralName);
360 return result;
361 }
362
363
364 /**
365 * @param name
366 * @return true, if name has Rank, Rank is below species and species epithet equals infraSpeciesEpithtet, else false
367 */
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())){
371 return true;
372 }else{
373 return false;
374 }
375 }
376
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())) {
382 return resultString;
383 }else if ("".equals(resultString)){
384 return resultString + appendedPhrase;
385 }else {
386 return resultString + " " + appendedPhrase;
387 }
388 }
389
390 }