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