(no commit message)
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / faunaEuropaea / FaunaEuropaeaTransformer.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
10 package eu.etaxonomy.cdm.io.faunaEuropaea;
11
12 import java.sql.ResultSet;
13 import java.sql.SQLException;
14 import java.util.Map;
15
16 import org.apache.log4j.Logger;
17
18 import eu.etaxonomy.cdm.model.description.AbsenceTerm;
19 import eu.etaxonomy.cdm.model.description.PresenceAbsenceTermBase;
20 import eu.etaxonomy.cdm.model.description.PresenceTerm;
21 import eu.etaxonomy.cdm.model.location.NamedArea;
22 import eu.etaxonomy.cdm.model.location.TdwgArea;
23 import eu.etaxonomy.cdm.model.name.Rank;
24 import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;
25
26 /**
27 * @author a.babadshanjan
28 * @created 12.05.2009
29 * @version 1.0
30 */
31
32 public final class FaunaEuropaeaTransformer {
33 private static final Logger logger = Logger.getLogger(FaunaEuropaeaTransformer.class);
34
35 // Query
36 public static final int Q_NO_RESTRICTION = -1;
37
38 // TaxonStatus
39 public static final int T_STATUS_ACCEPTED = 1;
40 public static final int T_STATUS_NOT_ACCEPTED = 0;
41
42 // Author
43 public static final int A_AUCT = 1;
44 public static final String A_AUCTNAME = "auct.";
45
46 // Parenthesis
47 public static final int P_PARENTHESIS = 1;
48
49 // Rank
50 public static final int R_KINGDOM = 1;
51 public static final int R_SUBKINGDOM = 2;
52 public static final int R_SUPERPHYLUM = 3;
53 public static final int R_PHYLUM = 4;
54 public static final int R_SUBPHYLUM = 5;
55 public static final int R_INFRAPHYLUM = 6;
56 public static final int R_CLASS = 7;
57 public static final int R_SUBCLASS = 8;
58 public static final int R_INFRACLASS = 9;
59 public static final int R_SUPERORDER = 10;
60 public static final int R_ORDER = 11;
61 public static final int R_SUBORDER = 12;
62 public static final int R_INFRAORDER = 13;
63 public static final int R_SUPERFAMILY = 14;
64 public static final int R_FAMILY = 15;
65 public static final int R_SUBFAMILY = 16;
66 public static final int R_TRIBE = 17;
67 public static final int R_SUBTRIBE = 18;
68 public static final int R_GENUS = 19;
69 public static final int R_SUBGENUS = 20;
70 public static final int R_SPECIES = 21;
71 public static final int R_SUBSPECIES = 22;
72
73 private static Map<String, String> tdwgAreas = null;
74
75 public static PresenceAbsenceTermBase<?> occStatus2PresenceAbsence(int occStatusId) throws UnknownCdmTypeException{
76 switch (occStatusId){
77 case 0: return AbsenceTerm.ABSENT();
78 case 2: return PresenceTerm.INTRODUCED();
79 case 1: return PresenceTerm.NATIVE();
80 default: {
81 throw new UnknownCdmTypeException("Unknown presence status (id=" + Integer.valueOf(occStatusId).toString() + ")");
82 }
83 }
84 }
85
86
87 public static PresenceAbsenceTermBase<?> occStatus2PresenceAbsence_ (int occStatusId) throws UnknownCdmTypeException{
88 switch (occStatusId){
89 case 0: return null;
90 case 110: return PresenceTerm.CULTIVATED_REPORTED_IN_ERROR();
91 case 120: return PresenceTerm.CULTIVATED();
92 case 210: return PresenceTerm.INTRODUCED_REPORTED_IN_ERROR();
93 case 220: return PresenceTerm.INTRODUCED_PRESENCE_QUESTIONABLE();
94 case 230: return PresenceTerm.INTRODUCED_FORMERLY_INTRODUCED();
95 case 240: return PresenceTerm.INTRODUCED_DOUBTFULLY_INTRODUCED();
96 case 250: return PresenceTerm.INTRODUCED();
97 case 260: return PresenceTerm.INTRODUCED_UNCERTAIN_DEGREE_OF_NATURALISATION();
98 case 270: return PresenceTerm.INTRODUCED_ADVENTITIOUS();
99 case 280: return PresenceTerm.INTRODUCED_NATURALIZED();
100 case 310: return PresenceTerm.NATIVE_REPORTED_IN_ERROR();
101 case 320: return PresenceTerm.NATIVE_PRESENCE_QUESTIONABLE();
102 case 330: return PresenceTerm.NATIVE_FORMERLY_NATIVE();
103 case 340: return PresenceTerm.NATIVE_DOUBTFULLY_NATIVE();
104 case 350: return PresenceTerm.NATIVE();
105 case 999: {
106 logger.warn("endemic for EM can not be transformed in legal status");
107 //TODO preliminary
108 return PresenceTerm.PRESENT();
109 }
110 default: {
111 throw new UnknownCdmTypeException("Unknown occurrence status (id=" + Integer.valueOf(occStatusId).toString() + ")");
112 }
113 }
114 }
115
116
117 public static Rank rankId2Rank (ResultSet rs, boolean useUnknown) throws UnknownCdmTypeException {
118 Rank result;
119 try {
120 int rankId = rs.getInt("rnk_id");
121 int parentRankId = rs.getInt("rnk_rnk_id");
122 String rankName = rs.getString("rnk_name");
123 String rankLatinName = rs.getString("rnk_latinname");
124 int rankCategory = rs.getInt("rnk_category");
125
126 if (logger.isDebugEnabled()) {
127 logger.debug(rankId + ", " + parentRankId + ", " + rankName + ", " + rankCategory);
128 }
129
130 try {
131 result = Rank.getRankByNameOrAbbreviation(rankName);
132 } catch (UnknownCdmTypeException e1) {
133
134 switch (rankId) {
135 case 0: return null;
136 case R_KINGDOM: return Rank.KINGDOM();
137 case R_SUBKINGDOM: return Rank.SUBKINGDOM();
138 case R_SUPERPHYLUM: return Rank.SUPERPHYLUM();
139 case R_PHYLUM: return Rank.PHYLUM();
140 case R_SUBPHYLUM: return Rank.SUBPHYLUM();
141 case R_INFRAPHYLUM: return Rank.INFRAPHYLUM();
142 case R_CLASS: return Rank.CLASS();
143 case R_SUBCLASS: return Rank.SUBCLASS();
144 case R_INFRACLASS: return Rank.INFRACLASS();
145 case R_SUPERORDER: return Rank.SUPERORDER();
146 case R_ORDER: return Rank.ORDER();
147 case R_SUBORDER: return Rank.SUBORDER();
148 case R_INFRAORDER: return Rank.INFRAORDER();
149 case R_SUPERFAMILY: return Rank.SUPERFAMILY();
150 case R_FAMILY: return Rank.FAMILY();
151 case R_SUBFAMILY: return Rank.SUBFAMILY();
152 case R_TRIBE: return Rank.TRIBE();
153 case R_SUBTRIBE: return Rank.SUBTRIBE();
154 case R_GENUS: return Rank.GENUS();
155 case R_SUBGENUS: return Rank.SUBGENUS();
156 case R_SPECIES: return Rank.SPECIES();
157 case R_SUBSPECIES: return Rank.SUBSPECIES();
158
159 default: {
160 if (useUnknown){
161 logger.error("Rank unknown. Created UNKNOWN_RANK");
162 return Rank.UNKNOWN_RANK();
163 }
164 throw new UnknownCdmTypeException("Unknown Rank id" + Integer.valueOf(rankId).toString());
165 }
166 }
167 }
168 return result;
169 } catch (SQLException e) {
170 e.printStackTrace();
171 logger.warn("Exception occurred. Created UNKNOWN_RANK instead");
172 return Rank.UNKNOWN_RANK();
173 }
174 }
175
176
177 public static NamedArea areaId2TdwgArea (FaunaEuropaeaDistribution fauEuDistribution)
178 throws UnknownCdmTypeException {
179
180 NamedArea tdwgArea = null;
181
182 try {
183 int areaId = fauEuDistribution.getAreaId();
184 String areaName = fauEuDistribution.getAreaName();
185 String areaCode = fauEuDistribution.getAreaCode();
186 int extraLimital = fauEuDistribution.getExtraLimital();
187
188 //TODO: Verify mappings with comments. Those don't map to TDWG areas.
189
190 if (areaCode.equals("AD")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("SPA-AN");
191 else if (areaCode.equals("AFR")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("24"); // Afro-tropical region - Northeast Tropical Africa
192 else if (areaCode.equals("AL")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("ALB-OO");
193 else if (areaCode.equals("AT")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("AUT-AU");
194 else if (areaCode.equals("AUS")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("50"); // Australian region - Australia
195 else if (areaCode.equals("BA")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("YUG-BH");
196 else if (areaCode.equals("BE")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("BGM-BE");
197 else if (areaCode.equals("BG")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("BUL-OO");
198 else if (areaCode.equals("BY")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("BLR-OO");
199 else if (areaCode.equals("CH")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("SWI-OO");
200 else if (areaCode.equals("CY")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("CYP-OO");
201 else if (areaCode.equals("CZ")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("CZE-CZ");
202 else if (areaCode.equals("DE")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("GER-OO");
203 else if (areaCode.equals("DK-DEN")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("DEN-OO");
204 else if (areaCode.equals("DK-FOR")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("FOR-OO");
205 else if (areaCode.equals("EE")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("BLT-ES");
206 else if (areaCode.equals("EPA")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("3"); // Palaearctic - Asia-Temperate
207 else if (areaCode.equals("ES-BAL")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("BLT-ES");
208 else if (areaCode.equals("ES-CNY")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("CNY-OO");
209 else if (areaCode.equals("ES-SPA")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("SPA-SP");
210 else if (areaCode.equals("FI")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("FIN-OO");
211 else if (areaCode.equals("FR-COR")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("COR-OO");
212 else if (areaCode.equals("FR-FRA")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("FRA-FR");
213 else if (areaCode.equals("GB-CI")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("FRA-CI");
214 else if (areaCode.equals("GB-GI")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("SPA-GI");
215 else if (areaCode.equals("GB-GRB")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("GRB-OO");
216 else if (areaCode.equals("GB-NI")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("IRE-NI");
217 else if (areaCode.equals("GR-AEG")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("EAI-OO"); // North Aegean Is. - East Aegean Is.
218 else if (areaCode.equals("GR-CYC")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("GRC-OO"); // Cyclades Is. - Greece
219 else if (areaCode.equals("GR-DOD")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("EAI-OO"); // Dodecanese Is. - East Aegean Is.
220 else if (areaCode.equals("GR-GRC")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("GRC-OO");
221 else if (areaCode.equals("GR-KRI")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("KRI-OO");
222 else if (areaCode.equals("HR")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("YUG-CR");
223 else if (areaCode.equals("HU")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("HUN-OO");
224 else if (areaCode.equals("IE")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("IRE-IR");
225 else if (areaCode.equals("IS")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("ICE-OO");
226 else if (areaCode.equals("IT-ITA")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("ITA-IT");
227 else if (areaCode.equals("IT-SAR")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("SAR-OO");
228 else if (areaCode.equals("IT-SI")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("SIC-SI");
229 else if (areaCode.equals("LI")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("AUT-LI");
230 else if (areaCode.equals("LT")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("BLT-LI");
231 else if (areaCode.equals("LU")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("BGM-LU");
232 else if (areaCode.equals("LV")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("BLT-LA");
233 else if (areaCode.equals("MC")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("FRA-MO");
234 else if (areaCode.equals("MD")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("UKR-MO");
235 else if (areaCode.equals("MK")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("YUG-MA");
236 else if (areaCode.equals("MT")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("SIC-MA");
237 else if (areaCode.equals("NAF")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("20"); // North Africa - Northern Africa
238 else if (areaCode.equals("NEA")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("7"); // Nearctic region - Northern America
239 else if (areaCode.equals("NEO")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("8"); // Neotropical region - Southern America
240 else if (areaCode.equals("NL")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("NET-OO");
241 else if (areaCode.equals("NO-NOR")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("NOR-OO");
242 else if (areaCode.equals("NO-SVA")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("SVA-OO");
243 else if (areaCode.equals("NRE")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("34"); // Near East - Western Asia
244 else if (areaCode.equals("ORR")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("35"); // Oriental region - Arabian Peninsula
245 else if (areaCode.equals("PL")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("POL-OO");
246 else if (areaCode.equals("PT-AZO")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("AZO-OO");
247 else if (areaCode.equals("PT-MDR")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("MDR-OO");
248 else if (areaCode.equals("PT-POR")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("POR-OO");
249 else if (areaCode.equals("PT-SEL")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("SEL-OO");
250 else if (areaCode.equals("RO")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("ROM-OO");
251 else if (areaCode.equals("RU-FJL")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("WSB-OO");
252 else if (areaCode.equals("RU-KGD")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("BLT-KA");
253 else if (areaCode.equals("RU-NOZ")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("WSB-OO");
254 else if (areaCode.equals("RU-RUC")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("RUC-OO");
255 else if (areaCode.equals("RU-RUE")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("RUE-OO");
256 else if (areaCode.equals("RU-RUN")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("RUN-OO");
257 else if (areaCode.equals("RU-RUS")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("RUS-OO");
258 else if (areaCode.equals("RU-RUW")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("RUW-OO");
259 else if (areaCode.equals("SE")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("SWE-OO");
260 else if (areaCode.equals("SI")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("YUG-SL");
261 else if (areaCode.equals("SK")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("CZE-SK");
262 else if (areaCode.equals("SM")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("ITA-SM");
263 else if (areaCode.equals("TR-TUE")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("TUE-OO");
264 else if (areaCode.equals("UA")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("UKR-UK");
265 else if (areaCode.equals("VA")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("ITA-VC");
266 else if (areaCode.equals("YU")) tdwgArea = TdwgArea.getAreaByTdwgAbbreviation("YUG");
267 else
268 throw new UnknownCdmTypeException("Unknown Area " + areaCode);
269
270 if (logger.isDebugEnabled()) {
271 logger.debug(areaId + ", " + areaName + ", " + areaCode + ", " + extraLimital);
272 }
273
274 } catch (Exception e) {
275 e.printStackTrace();
276 logger.warn("Exception occurred. Area could not be mapped.");
277 return null;
278 }
279
280 return tdwgArea;
281 }
282
283 }