abd8eced8a8ed6a4b660d79d77d5a151de4989ff
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / strategy / cache / name / NonViralNameDefaultCacheStrategy.java
1 /**
2 *
3 */
4 package eu.etaxonomy.cdm.strategy.cache.name;
5
6 import java.util.ArrayList;
7 import java.util.List;
8 import java.util.UUID;
9
10 import org.apache.log4j.Logger;
11
12 import eu.etaxonomy.cdm.common.CdmUtils;
13 import eu.etaxonomy.cdm.model.agent.INomenclaturalAuthor;
14 import eu.etaxonomy.cdm.model.agent.Team;
15 import eu.etaxonomy.cdm.model.name.NonViralName;
16 import eu.etaxonomy.cdm.model.name.Rank;
17 import eu.etaxonomy.cdm.model.reference.INomenclaturalReference;
18 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
19
20
21 /**
22 * This class is a default implementation for the INonViralNameCacheStrategy<T extends NonViralName> interface.
23 * The method actually implements a cache strategy for botanical names so no method has to be overwritten by
24 * a subclass for botanic names.
25 * Where differing from this Default BotanicNameCacheStrategy other subclasses should overwrite the existing methods
26 * e.g. a CacheStrategy for zoological names should overwrite getAuthorAndExAuthor
27 * @author a.mueller
28 */
29 /**
30 * @author AM
31 *
32 * @param <T>
33 */
34 public class NonViralNameDefaultCacheStrategy<T extends NonViralName> extends NameCacheStrategyBase<T> implements INonViralNameCacheStrategy<T> {
35 private static final Logger logger = Logger.getLogger(NonViralNameDefaultCacheStrategy.class);
36
37 final static UUID uuid = UUID.fromString("1cdda0d1-d5bc-480f-bf08-40a510a2f223");
38
39 protected String NameAuthorSeperator = " ";
40 protected String BasionymStart = "(";
41 protected String BasionymEnd = ")";
42 protected String ExAuthorSeperator = " ex ";
43 protected CharSequence BasionymAuthorCombinationAuthorSeperator = " ";
44
45 public UUID getUuid(){
46 return uuid;
47 }
48
49
50 /**
51 * Factory method
52 * @return NonViralNameDefaultCacheStrategy A new instance of NonViralNameDefaultCacheStrategy
53 */
54 public static NonViralNameDefaultCacheStrategy NewInstance(){
55 return new NonViralNameDefaultCacheStrategy();
56 }
57
58 /**
59 * Constructor
60 */
61 protected NonViralNameDefaultCacheStrategy(){
62 super();
63 }
64
65 /* **************** GETTER / SETTER **************************************/
66
67 /**
68 * String that separates the NameCache part from the AuthorCache part
69 * @return
70 */
71 public String getNameAuthorSeperator() {
72 return NameAuthorSeperator;
73 }
74
75
76 public void setNameAuthorSeperator(String nameAuthorSeperator) {
77 NameAuthorSeperator = nameAuthorSeperator;
78 }
79
80
81 /**
82 * String the basionym author part starts with e.g. '('.
83 * This should correspond with the {@link NonViralNameDefaultCacheStrategy#getBasionymEnd() basionymEnd} attribute
84 * @return
85 */
86 public String getBasionymStart() {
87 return BasionymStart;
88 }
89
90
91 public void setBasionymStart(String basionymStart) {
92 BasionymStart = basionymStart;
93 }
94
95
96 /**
97 * String the basionym author part ends with e.g. ')'.
98 * This should correspond with the {@link NonViralNameDefaultCacheStrategy#getBasionymStart() basionymStart} attribute
99 * @return
100 */
101 public String getBasionymEnd() {
102 return BasionymEnd;
103 }
104
105
106 public void setBasionymEnd(String basionymEnd) {
107 BasionymEnd = basionymEnd;
108 }
109
110
111 /**
112 * String to seperate ex author from author.
113 * @return
114 */
115 public String getExAuthorSeperator() {
116 return ExAuthorSeperator;
117 }
118
119
120 public void setExAuthorSeperator(String exAuthorSeperator) {
121 ExAuthorSeperator = exAuthorSeperator;
122 }
123
124
125 /**
126 * String that seperates the basionym/original_combination author part from the combination author part
127 * @return
128 */
129 public CharSequence getBasionymAuthorCombinationAuthorSeperator() {
130 return BasionymAuthorCombinationAuthorSeperator;
131 }
132
133
134 public void setBasionymAuthorCombinationAuthorSeperator(
135 CharSequence basionymAuthorCombinationAuthorSeperator) {
136 BasionymAuthorCombinationAuthorSeperator = basionymAuthorCombinationAuthorSeperator;
137 }
138
139
140 //** *****************************************************************************************/
141
142
143 /* (non-Javadoc)
144 * @see eu.etaxonomy.cdm.strategy.INameCacheStrategy#getNameCache()
145 */
146 // just for testing
147 @Override
148 public String getTitleCache(T nonViralName) {
149 if (nonViralName == null){
150 return null;
151 }
152 String result = "";
153 if (isAutonym(nonViralName)){
154 String speciesPart = getSpeciesNameCache(nonViralName);
155 //TODO should this include basionym authors and ex authors
156 INomenclaturalAuthor author = nonViralName.getCombinationAuthorTeam();
157 String authorPart = "";
158 if (author != null){
159 authorPart = CdmUtils.Nz(author.getNomenclaturalTitle());
160 }
161 INomenclaturalAuthor basAuthor = nonViralName.getBasionymAuthorTeam();
162 String basAuthorPart = "";
163 if (basAuthor != null){
164 basAuthorPart = CdmUtils.Nz(basAuthor.getNomenclaturalTitle());
165 }
166 if (! "".equals(basAuthorPart)){
167 authorPart = "("+ basAuthorPart +")" + authorPart;
168 }
169 String infraSpeciesPart = (CdmUtils.Nz(nonViralName.getInfraSpecificEpithet()));
170 result = CdmUtils.concat(" ", new String[]{speciesPart, authorPart, infraSpeciesPart});
171 result = result.trim().replace("null", "");
172 }else{
173 String nameCache = CdmUtils.Nz(getNameCache(nonViralName));
174 String authorCache = CdmUtils.Nz(getAuthorshipCache(nonViralName));
175 result = CdmUtils.concat(NameAuthorSeperator, nameCache, authorCache);
176 }
177 return result;
178 }
179
180
181 /**
182 * Generates and returns the "name cache" (only scientific name without author teams and year).
183 * @see eu.etaxonomy.cdm.strategy.cache.name.INameCacheStrategy#getNameCache(eu.etaxonomy.cdm.model.name.TaxonNameBase)
184 */
185 public String getNameCache(T nonViralName) {
186 if (nonViralName == null){
187 return null;
188 }
189 String result;
190 Rank rank = nonViralName.getRank();
191
192 if (rank == null){
193 result = getRanklessNameCache(nonViralName);
194 }else if (rank.isInfraSpecific()){
195 result = getInfraSpeciesNameCache(nonViralName);
196 }else if (rank.isSpecies()){
197 result = getSpeciesNameCache(nonViralName);
198 }else if (rank.isInfraGeneric()){
199 result = getInfraGenusNameCache(nonViralName);
200 }else if (rank.isGenus()){
201 result = getGenusOrUninomialNameCache(nonViralName);
202 }else if (rank.isSupraGeneric()){
203 result = getGenusOrUninomialNameCache(nonViralName);
204 }else{
205 logger.warn("Name Strategy for Name (UUID: " + nonViralName.getUuid() + ") not yet implemented");
206 result = "";
207 }
208 return result;
209 }
210
211
212 /* (non-Javadoc)
213 * @see eu.etaxonomy.cdm.strategy.cache.INonViralNameCacheStrategy#getAuthorCache(eu.etaxonomy.cdm.model.name.NonViralName)
214 */
215 public String getAuthorshipCache(T nonViralName) {
216 if (nonViralName == null){
217 return null;
218 }
219 String result = "";
220 INomenclaturalAuthor combinationAuthor = nonViralName.getCombinationAuthorTeam();
221 INomenclaturalAuthor exCombinationAuthor = nonViralName.getExCombinationAuthorTeam();
222 INomenclaturalAuthor basionymAuthor = nonViralName.getBasionymAuthorTeam();
223 INomenclaturalAuthor exBasionymAuthor = nonViralName.getExBasionymAuthorTeam();
224 String basionymPart = "";
225 String authorPart = "";
226 //basionym
227 if (basionymAuthor != null || exBasionymAuthor != null){
228 basionymPart = BasionymStart + getAuthorAndExAuthor(basionymAuthor, exBasionymAuthor) + BasionymEnd;
229 }
230 if (combinationAuthor != null || exCombinationAuthor != null){
231 authorPart = getAuthorAndExAuthor(combinationAuthor, exCombinationAuthor);
232 }
233 result = CdmUtils.concat(BasionymAuthorCombinationAuthorSeperator, basionymPart, authorPart);
234 return result;
235 }
236
237 /**
238 * Returns the AuthorCache part for a combination of an author and an ex author. This applies on combination authors
239 * as well as on basionym/orginal combination authors.
240 * @param author the author
241 * @param exAuthor the ex-author
242 * @return
243 */
244 protected String getAuthorAndExAuthor(INomenclaturalAuthor author, INomenclaturalAuthor exAuthor){
245 String result = "";
246 String authorString = "";
247 String exAuthorString = "";
248 if (author != null){
249 authorString = CdmUtils.Nz(author.getNomenclaturalTitle());
250 }
251 if (exAuthor != null){
252 exAuthorString = CdmUtils.Nz(exAuthor.getNomenclaturalTitle());
253 }
254 if (exAuthorString.length() > 0 ){
255 exAuthorString = exAuthorString + ExAuthorSeperator;
256 }
257 result = exAuthorString + authorString;
258 return result;
259
260 }
261
262
263 /* (non-Javadoc)
264 * @see eu.etaxonomy.cdm.strategy.INameCacheStrategy#getTaggedName(eu.etaxonomy.cdm.model.common.CdmBase)
265 */
266 @Override
267 public List<Object> getTaggedName(T nonViralName) {
268 List<Object> tags = new ArrayList<Object>();
269 tags.add(nonViralName.getGenusOrUninomial());
270 if (nonViralName.isSpecies() || nonViralName.isInfraSpecific()){
271 tags.add(nonViralName.getSpecificEpithet());
272 }
273 if (nonViralName.isInfraSpecific()){
274 tags.add(nonViralName.getRank());
275 tags.add(nonViralName.getInfraSpecificEpithet());
276 }
277 if (nonViralName.isInfraGeneric()){
278 //TODO choose right strategy or generic approach?
279 // --- strategy 1 ---
280 tags.add(nonViralName.getRank());
281 tags.add(nonViralName.getInfraGenericEpithet());
282 // --- strategy 2 ---
283 // tags.add('('+nvn.getInfraGenericEpithet()+')');
284 }
285 Team authorTeam = Team.NewInstance();
286 authorTeam.setProtectedTitleCache(true);
287 authorTeam.setTitleCache(nonViralName.getAuthorshipCache());
288 tags.add(authorTeam);
289 if (nonViralName.getNomenclaturalReference() != null){
290 INomenclaturalReference nomenclaturalReference = nonViralName.getNomenclaturalReference();
291 tags.add(nomenclaturalReference);//nonViralName.getNomenclaturalReference().getNomenclaturalCitation(nonViralName.getNomenclaturalMicroReference()));
292 }else{
293 tags.add("");
294 }
295 return tags;
296 }
297
298
299 /************** PRIVATES ****************/
300
301 protected String getRanklessNameCache(NonViralName nonViralName){
302 String result = "";
303 result = (result + (nonViralName.getGenusOrUninomial())).trim().replace("null", "-");
304 result += " " + (CdmUtils.Nz(nonViralName.getSpecificEpithet())).trim();
305 result += " " + (CdmUtils.Nz(nonViralName.getInfraSpecificEpithet())).trim();
306 result = result.trim().replace("null", "-");
307 //result += " (rankless)";
308 return result;
309 }
310
311
312 protected String getGenusOrUninomialNameCache(NonViralName nonViralName){
313 String result;
314 result = CdmUtils.Nz(nonViralName.getGenusOrUninomial());
315 return result;
316 }
317
318 protected String getInfraGenusNameCache(NonViralName nonViralName){
319 String result;
320 result = CdmUtils.Nz(nonViralName.getGenusOrUninomial());
321 result += " (" + (CdmUtils.Nz(nonViralName.getInfraGenericEpithet()) + ")").trim().replace("null", "");
322 return result;
323 }
324
325
326 protected String getSpeciesNameCache(NonViralName nonViralName){
327 String result;
328 result = CdmUtils.Nz(nonViralName.getGenusOrUninomial());
329 result += " " + CdmUtils.Nz(nonViralName.getSpecificEpithet()).trim().replace("null", "");
330 return result;
331 }
332
333
334 protected String getInfraSpeciesNameCache(NonViralName nonViralName){
335 String result;
336 result = CdmUtils.Nz(nonViralName.getGenusOrUninomial());
337 result += " " + (CdmUtils.Nz(nonViralName.getSpecificEpithet()).trim()).replace("null", "");
338 if (! isAutonym(nonViralName)){
339 result += " " + (nonViralName.getRank().getAbbreviation()).trim().replace("null", "");
340 }
341 result += " " + (CdmUtils.Nz(nonViralName.getInfraSpecificEpithet())).trim().replace("null", "");
342 return result;
343 }
344
345
346 /**
347 * @param name
348 * @return true, if name has Rank, Rank is below species and species epithet equals infraSpeciesEpithtet, else false
349 */
350 protected boolean isAutonym(NonViralName nonViralName){
351 if (nonViralName != null && nonViralName.getRank() != null && nonViralName.getSpecificEpithet() != null && nonViralName.getInfraSpecificEpithet() != null &&
352 nonViralName.getRank().isInfraSpecific() && nonViralName.getSpecificEpithet().trim().equals(nonViralName.getInfraSpecificEpithet().trim())){
353 return true;
354 }else{
355 return false;
356 }
357 }
358
359 }