cleanup
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / tcsxml / TcsXmlTransformer.java
1 /**
2 * Copyright (C) 2009 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.io.tcsxml;
10
11 import org.apache.logging.log4j.LogManager;
12 import org.apache.logging.log4j.Logger;
13
14 import eu.etaxonomy.cdm.common.ResultWrapper;
15 import eu.etaxonomy.cdm.model.common.IRelationshipType;
16 import eu.etaxonomy.cdm.model.name.HybridRelationshipType;
17 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
18 import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
19 import eu.etaxonomy.cdm.model.name.Rank;
20 import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
21 /*import eu.etaxonomy.cdm.model.reference.Article;
22 import eu.etaxonomy.cdm.model.reference.Book;
23 import eu.etaxonomy.cdm.model.reference.BookSection;
24 import eu.etaxonomy.cdm.model.reference.Journal;
25 import eu.etaxonomy.cdm.model.reference.PersonalCommunication;
26 import eu.etaxonomy.cdm.model.reference.PrintSeries;*/
27 import eu.etaxonomy.cdm.model.reference.Reference;
28 import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
29 //import eu.etaxonomy.cdm.model.reference.WebPage;
30 import eu.etaxonomy.cdm.model.taxon.SynonymType;
31 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
32 import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;
33
34 public final class TcsXmlTransformer {
35
36 @SuppressWarnings("unused")
37 private static final Logger logger = LogManager.getLogger();
38
39 //TypeDesignation
40 public static SpecimenTypeDesignationStatus typeStatusId2TypeStatus (int typeStatusId) throws UnknownCdmTypeException{
41 switch (typeStatusId){
42 case 1: return SpecimenTypeDesignationStatus.HOLOTYPE();
43 case 2: return SpecimenTypeDesignationStatus.LECTOTYPE();
44 case 3: return SpecimenTypeDesignationStatus.NEOTYPE();
45 case 4: return SpecimenTypeDesignationStatus.EPITYPE();
46 case 5: return SpecimenTypeDesignationStatus.ISOLECTOTYPE();
47 case 6: return SpecimenTypeDesignationStatus.ISONEOTYPE();
48 case 7: return SpecimenTypeDesignationStatus.ISOTYPE();
49 case 8: return SpecimenTypeDesignationStatus.PARANEOTYPE();
50 case 9: return SpecimenTypeDesignationStatus.PARATYPE();
51 case 10: return SpecimenTypeDesignationStatus.SECOND_STEP_LECTOTYPE();
52 case 11: return SpecimenTypeDesignationStatus.SECOND_STEP_NEOTYPE();
53 case 12: return SpecimenTypeDesignationStatus.SYNTYPE();
54 case 21: return SpecimenTypeDesignationStatus.ICONOTYPE();
55 case 22: return SpecimenTypeDesignationStatus.PHOTOTYPE();
56 default: {
57 throw new UnknownCdmTypeException("Unknown TypeDesignationStatus (id=" + Integer.valueOf(typeStatusId).toString() + ")");
58 }
59 }
60 }
61
62
63
64
65 /** Creates an cdm-Rank by the tcs rank
66 */
67 public static Rank rankCode2Rank (String strRank) throws UnknownCdmTypeException{
68 if (strRank == null){return null;
69 //genus group
70 }else if (strRank.equals("infragen")){return Rank.INFRAGENUS();
71 }else if (strRank.equals("subgen")){return Rank.SUBGENUS();
72 }else if (strRank.equals("gen")){return Rank.GENUS();
73 //genus subdivision
74 //TODO
75 }else if (strRank.equals("aggr")){return Rank.SPECIESAGGREGATE();
76 }else if (strRank.equals("taxinfragen")){return Rank.INFRAGENERICTAXON();
77 }else if (strRank.equals("subser")){return Rank.SUBSERIES();
78 }else if (strRank.equals("ser")){return Rank.SERIES();
79 }else if (strRank.equals("subsect")){return Rank.SUBSECTION_BOTANY();
80 }else if (strRank.equals("sect")){return Rank.SECTION_BOTANY();
81 //species group
82 }else if (strRank.equals("subsp_aggr")){return Rank.SUBSPECIFICAGGREGATE();
83 }else if (strRank.equals("ssp")){return Rank.SUBSPECIES();
84 }else if (strRank.equals("sp")){return Rank.SPECIES();
85 //below subspecies
86 }else if (strRank.equals("cand")){return Rank.CANDIDATE();
87 }else if (strRank.equals("taxinfrasp")){return Rank.INFRASPECIFICTAXON();
88 }else if (strRank.equals("fsp")){return Rank.SPECIALFORM();
89 }else if (strRank.equals("subsubfm")){return Rank.SUBSUBFORM();
90 }else if (strRank.equals("subfm")){return Rank.SUBFORM();
91 }else if (strRank.equals("fm")){return Rank.FORM();
92 }else if (strRank.equals("subsubvar")){return Rank.SUBSUBVARIETY();
93 }else if (strRank.equals("subvar")){return Rank.SUBVARIETY();
94 }else if (strRank.equals("var")){return Rank.VARIETY();
95 //TODO -> see documentation, Bacteria status
96 // }else if (strRank.equals("pv")){return Rank;
97 // }else if (strRank.equals("bv")){return Rank.;
98 }else if (strRank.equals("infrasp")){return Rank.INFRASPECIES();
99 //above superfamily
100 }else if (strRank.equals("infraord")){return Rank.INFRAORDER();
101 }else if (strRank.equals("ord")){return Rank.ORDER();
102 }else if (strRank.equals("superord")){return Rank.SUPERORDER();
103 }else if (strRank.equals("infracl")){return Rank.INFRACLASS();
104 }else if (strRank.equals("subcl")){return Rank.SUBCLASS();
105 }else if (strRank.equals("cl")){return Rank.CLASS();
106 }else if (strRank.equals("supercl")){return Rank.SUPERCLASS();
107 }else if (strRank.equals("infraphyl_div")){return Rank.INFRAPHYLUM();
108 }else if (strRank.equals("subphyl_div")){return Rank.SUBPHYLUM();
109 }else if (strRank.equals("phyl_div")){return Rank.PHYLUM();
110 }else if (strRank.equals("superphyl_div")){return Rank.SUPERPHYLUM();
111 }else if (strRank.equals("infrareg")){return Rank.INFRAKINGDOM();
112 }else if (strRank.equals("subreg")){return Rank.SUBKINGDOM();
113 }else if (strRank.equals("reg")){return Rank.KINGDOM();
114 }else if (strRank.equals("superreg")){return Rank.SUPERKINGDOM();
115 }else if (strRank.equals("dom")){return Rank.DOMAIN();
116 }else if (strRank.equals("taxsupragen")){return Rank.SUPRAGENERICTAXON();
117 //family group
118 }else if (strRank.equals("infrafam")){return Rank.INFRAFAMILY();
119 }else if (strRank.equals("subfam")){return Rank.SUBFAMILY();
120 }else if (strRank.equals("fam")){return Rank.FAMILY();
121 }else if (strRank.equals("superfam")){return Rank.SUPERFAMILY();
122 //family subdivision
123 }else if (strRank.equals("intratrib")){return Rank.TRIBE();
124 }else if (strRank.equals("subtrib")){return Rank.SUBTRIBE();
125 }else if (strRank.equals("trib")){return Rank.TRIBE();
126 }else if (strRank.equals("supertrib")){return Rank.SUPERTRIBE();
127 }
128 else {
129 throw new UnknownCdmTypeException("Unknown Rank " + strRank);
130 }
131
132
133 }
134
135 public static Rank rankString2Rank (String strRank) throws UnknownCdmTypeException{
136
137 String tcsFamily = "family";
138 String tcsSubFamily = "subfamily";
139 String tcsTribe = "tribe";
140 String tcsSubtribe = "subtribe";
141 String tcsGenus = "genus";
142 String tcsSection = "section";
143 String tcsSpecies = "species";
144 String tcsSubSpecies = "subspecies";
145 String tcsVariety = "variety";
146 String tcsSubVariety = "subvariety";
147 String tcsForm = "form";
148
149
150 String tcsAbbFamily = "fam.";
151 String tcsAbbrSubFamily = "subfam.";
152 String tcsAbbrTribe = "trib.";
153 String tcsAbbrSubtribe = "subtrib.";
154 String tcsAbbrGenus = "gen.";
155 String tcsAbbrSubGenus = "subgen.";
156 String tcsAbbrSection = "sect.";
157 String tcsAbbrSubSection = "subsect.";
158 String tcsAbbrSeries = "ser.";
159 String tcsAbbrSpecies = "spec.";
160 String tcsAbbrSubSpecies = "subsp.";
161 String tcsAbbrVariety = "var.";
162 String tcsAbbrSubVariety ="subvar.";
163 String tcsAbbrForm = "f.";
164 String tcsAbbrForma = "forma";
165 String tcsAbbrSubForm = "subf.";
166 String tcsAbbrInfraspecUnranked ="[infrasp.unranked]";
167 String tcsAbbrInfragenUnranked ="[infragen.unranked]";
168 String tcsAbbrNothoSubSpecies = "nothosubsp.";
169
170 if (strRank == null){return null;
171 }else{
172 strRank = strRank.toLowerCase();
173 }
174 if (tcsFamily.equals(strRank) ){return Rank.FAMILY();
175 }else if (tcsSubFamily.equals(strRank)){return Rank.SUBFAMILY();
176 }else if (tcsTribe.equals(strRank)){return Rank.TRIBE();
177 }else if (tcsSubtribe.equals(strRank)){return Rank.SUBTRIBE();
178 }else if (tcsGenus.equals(strRank)){return Rank.GENUS();
179 }else if (tcsSection.equals(strRank)){return Rank.SECTION_BOTANY();
180 }else if (tcsSpecies.equals(strRank)){return Rank.SPECIES();
181 }else if (tcsVariety.equals(strRank)){return Rank.VARIETY();
182 }else if (tcsSubVariety.equals(strRank)){return Rank.SUBVARIETY();
183 }else if (tcsSubSpecies.equals(strRank) ){return Rank.SUBSPECIES();
184 }else if (tcsForm.equals(strRank)){return Rank.FORM();
185 }else if (tcsAbbFamily.equals(strRank)){return Rank.FAMILY();
186 }else if (tcsAbbrSubFamily.equals(strRank)){return Rank.SUBFAMILY();
187 }else if (tcsAbbrTribe.equals(strRank)){return Rank.TRIBE();
188 }else if (tcsAbbrSubtribe.equals(strRank)){return Rank.SUBTRIBE();
189 }else if (tcsAbbrGenus.equals(strRank)){return Rank.GENUS();
190 }else if (tcsAbbrSubGenus.equals(strRank)){return Rank.SUBGENUS();
191 }else if (tcsAbbrSection.equals(strRank)){return Rank.SECTION_BOTANY();
192 }else if (tcsAbbrSubSection.equals(strRank)){return Rank.SUBSECTION_BOTANY();
193 }else if (tcsAbbrSeries.equals(strRank)){return Rank.SERIES();
194 }else if (tcsAbbrSpecies.equals(strRank)){return Rank.SPECIES();
195 }else if (tcsAbbrSubSpecies.equals(strRank) || tcsAbbrNothoSubSpecies.equals(strRank)){return Rank.SUBSPECIES();
196 }else if (tcsAbbrVariety.equals(strRank)){return Rank.VARIETY();
197 }else if (tcsAbbrSubVariety.equals(strRank)){return Rank.SUBVARIETY();
198 }else if (tcsAbbrForm.equals(strRank) ||tcsAbbrForma.equals(strRank)){return Rank.FORM();
199 }else if (tcsAbbrSubForm.equals(strRank)){return Rank.SUBFORM();
200 }else if (tcsAbbrInfraspecUnranked.equals(strRank)){return Rank.UNRANKED_INFRASPECIFIC();
201 }else if (tcsAbbrInfragenUnranked.equals(strRank)){return Rank.UNRANKED_INFRAGENERIC();
202 }else{
203 throw new UnknownCdmTypeException("Unknown Rank " + strRank);
204 }
205 }
206
207 /** Creates an cdm-NomenclaturalCode by the tcs NomenclaturalCode
208 */
209 public static NomenclaturalCode nomCodeString2NomCode (String nomCode) throws UnknownCdmTypeException{
210 if (nomCode != null){
211 nomCode = nomCode.trim();
212 }
213 if (nomCode == null){ return null;
214 }else if (nomCode.equals("Botanical")){return NomenclaturalCode.ICNAFP;
215 }else if (nomCode.equals("Zoological")){return NomenclaturalCode.ICZN;
216 }else if (nomCode.equals("Viral")){return NomenclaturalCode.ICVCN;
217 }else if (nomCode.equals("Bacteriological")){return NomenclaturalCode.ICNP;
218 }else if (nomCode.equals("CultivatedPlant")){return NomenclaturalCode.ICNCP;
219 //TODO code Indeterminate
220 // }else if (nomCode.equals("Indeterminate")){return NomenclaturalCode.XXX();
221 }
222 else {
223 throw new UnknownCdmTypeException("Unknown Nomenclatural Code " + nomCode);
224 }
225 }
226
227 public static boolean isReverseRelationshipCategory (String tcsRelationshipCategory){
228 String str = tcsRelationshipCategory.replace("http://rs.tdwg.org/ontology/voc/TaxonConcept#", "");
229 if ("HasSynonym".equalsIgnoreCase(str)
230 || "IsParentTaxonOf".equalsIgnoreCase(str)
231 || "IsIncludedIn".equalsIgnoreCase(str)
232 || "DoesNotInclude".equalsIgnoreCase(str)
233 ){
234
235 return true;
236 }
237 return false;
238 }
239
240 /** Creates an cdm-Rank by the tcs rank
241 */
242 public static Reference pubTypeStr2PubType (String strPubType) throws UnknownCdmTypeException{
243 String tcsRoot = "http://rs.tdwg.org/ontology/voc/PublicationCitation#";
244 String tcsBook = tcsRoot + "Book";
245 String tcsJournal = tcsRoot + "Journal";
246 String tcsWebPage = tcsRoot + "WebPage";
247 String tcsCommunication = tcsRoot + "Communication";
248 String tcsBookSeries = tcsRoot + "BookSeries";
249 String tcsArticle = tcsRoot + "JournalArticle";
250 String tcsBookSection = tcsRoot + "BookSection";
251
252
253 // Artwork An Artwork type publication.
254 // AudiovisualMaterial A Audiovisual Material type publication.
255 // BookSeries A Book Series type publication.
256 // Commentary A Commentary type publication.
257 // Communication A Communication type publication.
258 // ComputerProgram A Computer Program type publication.
259 // ConferenceProceedings A Conference Proceedings type publication.
260 // Determination A Determination type publication.
261 // EditedBook A Edited Book type publication.
262 // Generic A generic publication.
263 // Journal A Journal type publication.
264 // MagazineArticle A Magazine Article type publication.
265 // Map A Map type publication.
266 // NewspaperArticle A Newspaper Article type publication.
267 // Patent A Patent type publication.
268 // Report A Report type publication.
269 // SubReference A Sub-Reference type publication.
270 // Thesis A Thesis type publication.
271
272 if (strPubType == null){return null;
273 }else if (tcsBookSection.equals(strPubType)){return ReferenceFactory.newBookSection();
274 }else if (tcsBook.equals(strPubType)){return ReferenceFactory.newBook();
275 }else if (tcsArticle.equals(strPubType)){return ReferenceFactory.newArticle();
276 }else if (tcsJournal.equals(strPubType)){return ReferenceFactory.newJournal();
277 }else if (tcsWebPage.equals(strPubType)){return ReferenceFactory.newWebPage();
278 }else if (tcsCommunication.equals(strPubType)){return ReferenceFactory.newPersonalCommunication();
279 }else if (tcsBookSeries.equals(strPubType)){return ReferenceFactory.newPrintSeries();
280 }
281 else {
282 throw new UnknownCdmTypeException("Unknown publication type " + strPubType);
283 }
284 }
285
286 /** Creates an cdm-RelationshipTermBase by the tcsRelationshipCategory
287 */
288 public static IRelationshipType tcsRelationshipType2Relationship (String tcsRelationshipType, ResultWrapper<Boolean> inverse) throws UnknownCdmTypeException{
289 if (tcsRelationshipType == null){ return null;
290
291 //Synonym relationships
292 // }else if (tcsRelationshipType.equals("is synonym for")){return SynonymType.SYNONYM_OF();
293 }else if (tcsRelationshipType.equals("has synonym")){inverse.setValue(true); return SynonymType.SYNONYM_OF;
294
295 //Taxon relationships
296 }else if (tcsRelationshipType.equals("is child taxon of")){return TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN();
297 }else if (tcsRelationshipType.equals("is parent taxon of")){inverse.setValue(true); return TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN();
298
299 //concept relationships
300 }else if (tcsRelationshipType.equals("does not overlap")){return TaxonRelationshipType.DOES_NOT_OVERLAP();
301 }else if (tcsRelationshipType.equals("excludes")){return TaxonRelationshipType.EXCLUDES();
302 }else if (tcsRelationshipType.equals("includes")){return TaxonRelationshipType.INCLUDES();
303 }else if (tcsRelationshipType.equals("is congruent to")){return TaxonRelationshipType.CONGRUENT_TO();
304 }else if (tcsRelationshipType.equals("is not congruent to")){return TaxonRelationshipType.NOT_CONGRUENT_TO();
305 }else if (tcsRelationshipType.equals("is not included in")){return TaxonRelationshipType.NOT_INCLUDED_IN();
306 }else if (tcsRelationshipType.equals("overlaps")){return TaxonRelationshipType.OVERLAPS();
307 //reverse concept relationships
308 }else if (tcsRelationshipType.equals("is included in")){inverse.setValue(true); return TaxonRelationshipType.INCLUDES();
309 }else if (tcsRelationshipType.equals("does not include")){inverse.setValue(true); return TaxonRelationshipType.NOT_INCLUDED_IN();
310
311 //TODO
312
313 // }else if (tcsRelationshipType.equals("has vernacular")){return TaxonRelationshipType.X;
314 // }else if (tcsRelationshipType.equals("is vernacular for")){return TaxonRelationshipType.X;
315 // }else if (tcsRelationshipType.equals("is ambiregnal of")){return TaxonRelationshipType.X;
316 }else if (tcsRelationshipType.equals("is hybrid child of")){return HybridRelationshipType.FIRST_PARENT();
317 // }else if (tcsRelationshipType.equals("is hybrid parent of")){return TaxonRelationshipType.X;
318 // }else if (tcsRelationshipType.equals("is male parent of")){return TaxonRelationshipType.X;
319 // }else if (tcsRelationshipType.equals("is first parent of")){return TaxonRelationshipType.X;
320 // }else if (tcsRelationshipType.equals("is female parent of")){return TaxonRelationshipType.X;
321 // }else if (tcsRelationshipType.equals("is second parent of")){return TaxonRelationshipType.X;
322 // }else if (tcsRelationshipType.equals("is teleomorph of")){return TaxonRelationshipType.X;
323 // }else if (tcsRelationshipType.equals("is anamorph of")){return TaxonRelationshipType.X;
324
325 }else {
326 throw new UnknownCdmTypeException("Unknown RelationshipCategory " + tcsRelationshipType);
327 }
328 }
329
330
331 /** Creates an cdm-NomenclaturalCode by the tcs NomenclaturalCode
332 */
333 public static NomenclaturalStatusType nomStatusString2NomStatus (String nomStatus) throws UnknownCdmTypeException{
334
335 if (nomStatus == null){ return null;
336 }else if ("Valid".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.VALID();
337
338 }else if ("Alternative".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.ALTERNATIVE();
339 }else if ("nom. altern.".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.ALTERNATIVE();
340
341 }else if ("Ambiguous".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.AMBIGUOUS();
342
343 }else if ("Doubtful".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.DOUBTFUL();
344
345 }else if ("Confusum".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.CONFUSUM();
346
347 }else if ("Illegitimate".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.ILLEGITIMATE();
348 }else if ("nom. illeg.".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.ILLEGITIMATE();
349
350 }else if ("Superfluous".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.SUPERFLUOUS();
351 }else if ("nom. superfl.".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.SUPERFLUOUS();
352
353 }else if ("Rejected".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.REJECTED();
354 }else if ("nom. rej.".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.REJECTED();
355
356 }else if ("Utique Rejected".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.UTIQUE_REJECTED();
357
358 }else if ("Conserved Prop".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.CONSERVED_PROP();
359
360 }else if ("Orthography Conserved Prop".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.ORTHOGRAPHY_CONSERVED_PROP();
361
362 }else if ("Legitimate".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.LEGITIMATE();
363
364 }else if ("Novum".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.NOVUM();
365 }else if ("nom. nov.".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.NOVUM();
366
367 }else if ("Utique Rejected Prop".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.UTIQUE_REJECTED_PROP();
368
369 }else if ("Orthography Conserved".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.ORTHOGRAPHY_CONSERVED();
370
371 }else if ("Rejected Prop".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.REJECTED_PROP();
372
373 }else if ("Conserved".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.CONSERVED();
374 }else if ("nom. cons.".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.CONSERVED();
375
376 }else if ("Sanctioned".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.SANCTIONED();
377
378 }else if ("Invalid".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.INVALID();
379 }else if ("nom. inval.".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.INVALID();
380
381 }else if ("Nudum".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.NUDUM();
382 }else if ("nom. nud.".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.NUDUM();
383
384 }else if ("Combination Invalid".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.COMBINATION_INVALID();
385
386 }else if ("Provisional".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.PROVISIONAL();
387 }else if ("nom. provis.".equalsIgnoreCase(nomStatus)){return NomenclaturalStatusType.PROVISIONAL();
388 }
389
390 else {
391 throw new UnknownCdmTypeException("Unknown Nomenclatural status type " + nomStatus);
392 }
393 }
394
395
396 }