View Javadoc

1   package org.cateproject.service.format.name;
2   
3   import java.io.Serializable;
4   import java.util.List;
5   
6   import eu.etaxonomy.cdm.model.common.Language;
7   import eu.etaxonomy.cdm.model.name.BotanicalName;
8   import eu.etaxonomy.cdm.model.name.Rank;
9   import eu.etaxonomy.cdm.strategy.cache.name.INonViralNameCacheStrategy;
10  
11  public class BotanicalNameFormatter implements INonViralNameCacheStrategy<BotanicalName>, Serializable {
12  	/**
13  	 * 
14  	 */
15  	private static final long serialVersionUID = 1L;
16  	
17  	private String getValueOrToken(String string) {
18  		if(string != null) {
19  			return string;
20  		} else { 
21  			return "???";
22  		}
23  	}
24  
25  	public String getLastEpithet(BotanicalName botanicalName) {
26  		Rank rank = botanicalName.getRank();
27  		
28  		if(rank.isInfraSpecific()) {
29  			StringBuilder name = new StringBuilder();
30  			if(botanicalName.isHybridFormula()) {
31  			    name.append("notho");
32  			}
33  			    
34  			 name.append(rank.getRepresentation(Language.DEFAULT()).getAbbreviatedLabel() + " " + getValueOrToken(botanicalName.getInfraSpecificEpithet()));
35  			return name.toString();
36  		} else if (rank.isSpecies()) {
37  			StringBuilder name = new StringBuilder();
38  			if(botanicalName.isHybridFormula()) {
39  				name.append("x ");
40  			}
41  			name.append(getValueOrToken(botanicalName.getSpecificEpithet()));
42  			return name.toString();
43  		} else if(rank.isInfraGeneric()) {
44  			return botanicalName.getInfraGenericEpithet();
45  		} else {
46  			return botanicalName.getGenusOrUninomial();
47  		}
48  	}
49  
50  	public String getAuthorshipCache(BotanicalName botanicalName) {
51  		StringBuilder authorship = new StringBuilder();
52  		if(botanicalName.getSpecificEpithet() != null && botanicalName.getInfraSpecificEpithet() != null) {
53  			if(botanicalName.getSpecificEpithet().equals(botanicalName.getInfraSpecificEpithet())) {
54                  return null; // Autonyms don't have authors
55  			} 
56  		}
57  				
58  	    if(botanicalName.getCombinationAuthorTeam() == null && botanicalName.getExCombinationAuthorTeam() == null) {
59  	    	if(botanicalName.getBasionymAuthorTeam() != null) {
60  		        authorship.append(botanicalName.getBasionymAuthorTeam().getNomenclaturalTitle());
61  	    	} else {
62  	    		authorship.append("???");
63  	    	}
64  	    	
65  	    	if(botanicalName.getExBasionymAuthorTeam() != null) {
66  		        authorship.append(" ex " +  botanicalName.getExBasionymAuthorTeam().getNomenclaturalTitle());
67  	    	} 
68  	    } else {
69  	    	if(botanicalName.getBasionymAuthorTeam() != null) {
70  		        authorship.append("(" + botanicalName.getBasionymAuthorTeam().getNomenclaturalTitle());
71  	    	} else {
72  	    		authorship.append("(???");
73  	    	}
74  	    	
75  	    	if(botanicalName.getExBasionymAuthorTeam() != null) {
76  		        authorship.append(" ex " +  botanicalName.getExBasionymAuthorTeam().getNomenclaturalTitle() + ") ");
77  	    	} else {
78  	    		authorship.append(") ");
79  	    	}
80  	    	
81  	    	if(botanicalName.getCombinationAuthorTeam() != null) {
82  		        authorship.append(botanicalName.getCombinationAuthorTeam().getNomenclaturalTitle());
83  	    	} else {
84  	    		authorship.append("???");
85  	    	}
86  	    	
87  	    	if(botanicalName.getExCombinationAuthorTeam() != null) {
88  		        authorship.append(" ex " +  botanicalName.getExCombinationAuthorTeam().getNomenclaturalTitle());
89  	    	} 
90  	    }
91  	    
92  	    return authorship.toString();
93      }
94  
95  	public String getNameCache(BotanicalName botanicalName) {
96  		Rank rank = botanicalName.getRank();
97  		if(rank == null) {
98  			return null;
99  		}
100 		if(rank.isInfraSpecific()) {
101 			StringBuilder name = new StringBuilder();
102 			
103 			name.append(getValueOrToken(botanicalName.getGenusOrUninomial()) + " ");
104 			
105 			if(botanicalName.isHybridFormula()) {
106 				name.append("x ");
107 			}
108 			name.append(getValueOrToken(botanicalName.getSpecificEpithet()) + " ");
109 			
110 			name.append(getLastEpithet(botanicalName));
111 			
112 			return name.toString();
113 		} else if(rank.isSpecies()) {
114 			StringBuilder name = new StringBuilder();
115 			name.append(getValueOrToken(botanicalName.getGenusOrUninomial()) + " ");
116 			name.append(getLastEpithet(botanicalName));
117 			return name.toString();
118 		} else {
119 			return getValueOrToken(botanicalName.getGenusOrUninomial());
120 		}
121 	}
122 
123 	public String getFullTitleCache(BotanicalName taxonNameBase) {
124 		return getTitleCache(taxonNameBase);
125 	}
126 
127 	public List<Object> getTaggedName(BotanicalName taxonNameBase) {
128 		// TODO Auto-generated method stub
129 		return null;
130 	}
131 
132 	public String getTitleCache(BotanicalName botanicalName) {
133     	StringBuilder name = new StringBuilder();
134     	name.append("<i>");
135     	if(botanicalName.getNameCache() != null) {
136     		name.append(botanicalName.getNameCache());
137     	} else {
138     	    name.append(getNameCache(botanicalName));
139     	}
140     	
141     	name.append("</i> ");
142     	
143     	if(botanicalName.getAuthorshipCache() != null) {
144     		name.append(botanicalName.getAuthorshipCache());
145     	} else {
146     		String authorshipCache = getAuthorshipCache(botanicalName);
147     		if(authorshipCache != null) {
148     	      name.append(authorshipCache);
149     		}
150     	}
151     	
152     	return name.toString();
153 	}
154 
155 }