8829804e08a8baa9a4c7ce24f8a8de1924041778
[cdmlib-apps.git] / cdmlib-eflora / src / main / java / eu / etaxonomy / cdm / io / eflora / centralAfrica / ferns / CentralAfricaFernsTaxonImport.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.eflora.centralAfrica.ferns;
11
12 import java.sql.ResultSet;
13 import java.sql.SQLException;
14 import java.util.HashMap;
15 import java.util.HashSet;
16 import java.util.Map;
17 import java.util.Set;
18 import java.util.UUID;
19 import java.util.regex.Matcher;
20 import java.util.regex.Pattern;
21
22 import org.apache.commons.lang.StringUtils;
23 import org.apache.log4j.Logger;
24 import org.springframework.stereotype.Component;
25
26 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
27 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade.DerivedUnitType;
28 import eu.etaxonomy.cdm.common.CdmUtils;
29 import eu.etaxonomy.cdm.io.common.IOValidator;
30 import eu.etaxonomy.cdm.io.common.mapping.DbImportAnnotationMapper;
31 import eu.etaxonomy.cdm.io.common.mapping.DbImportMapping;
32 import eu.etaxonomy.cdm.io.common.mapping.DbImportMethodMapper;
33 import eu.etaxonomy.cdm.io.common.mapping.DbImportObjectCreationMapper;
34 import eu.etaxonomy.cdm.io.common.mapping.DbNotYetImplementedMapper;
35 import eu.etaxonomy.cdm.io.common.mapping.IMappingImport;
36 import eu.etaxonomy.cdm.io.common.mapping.UndefinedTransformerMethodException;
37 import eu.etaxonomy.cdm.io.eflora.centralAfrica.ferns.validation.CentralAfricaFernsTaxonImportValidator;
38 import eu.etaxonomy.cdm.model.agent.Team;
39 import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
40 import eu.etaxonomy.cdm.model.common.AnnotationType;
41 import eu.etaxonomy.cdm.model.common.CdmBase;
42 import eu.etaxonomy.cdm.model.common.ExtensionType;
43 import eu.etaxonomy.cdm.model.common.Language;
44 import eu.etaxonomy.cdm.model.common.TimePeriod;
45 import eu.etaxonomy.cdm.model.description.CommonTaxonName;
46 import eu.etaxonomy.cdm.model.description.Distribution;
47 import eu.etaxonomy.cdm.model.description.Feature;
48 import eu.etaxonomy.cdm.model.description.PresenceTerm;
49 import eu.etaxonomy.cdm.model.description.TaxonDescription;
50 import eu.etaxonomy.cdm.model.description.TextData;
51 import eu.etaxonomy.cdm.model.location.NamedArea;
52 import eu.etaxonomy.cdm.model.location.NamedAreaLevel;
53 import eu.etaxonomy.cdm.model.location.NamedAreaType;
54 import eu.etaxonomy.cdm.model.location.TdwgArea;
55 import eu.etaxonomy.cdm.model.location.WaterbodyOrCountry;
56 import eu.etaxonomy.cdm.model.name.BotanicalName;
57 import eu.etaxonomy.cdm.model.name.NonViralName;
58 import eu.etaxonomy.cdm.model.name.Rank;
59 import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
60 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
61 import eu.etaxonomy.cdm.model.reference.Reference;
62 import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
63 import eu.etaxonomy.cdm.model.taxon.Synonym;
64 import eu.etaxonomy.cdm.model.taxon.Taxon;
65 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
66
67
68 /**
69 * @author a.mueller
70 * @created 20.02.2010
71 * @version 1.0
72 */
73 @Component
74 public class CentralAfricaFernsTaxonImport extends CentralAfricaFernsImportBase<TaxonBase> implements IMappingImport<TaxonBase, CentralAfricaFernsImportState>{
75 private static final Logger logger = Logger.getLogger(CentralAfricaFernsTaxonImport.class);
76
77 public static final UUID TNS_EXT_UUID = UUID.fromString("41cb0450-ac84-4d73-905e-9c7773c23b05");
78
79
80 private DbImportMapping mapping;
81
82 //second path is not used anymore, there is now an ErmsTaxonRelationImport class instead
83 private boolean isSecondPath = false;
84
85 private static final String pluralString = "taxa";
86 private static final String dbTableName = "[African pteridophytes]";
87 private static final Class cdmTargetClass = TaxonBase.class;
88
89 public CentralAfricaFernsTaxonImport(){
90 super(pluralString, dbTableName, cdmTargetClass);
91 }
92
93
94
95 /* (non-Javadoc)
96 * @see eu.etaxonomy.cdm.io.erms.ErmsImportBase#getIdQuery()
97 */
98 @Override
99 protected String getIdQuery() {
100 String strQuery = " SELECT [Taxon number] FROM " + dbTableName ;
101 return strQuery;
102 }
103
104
105 /* (non-Javadoc)
106 * @see eu.etaxonomy.cdm.io.erms.ErmsImportBase#getMapping()
107 */
108 protected DbImportMapping getMapping() {
109 if (mapping == null){
110 mapping = new DbImportMapping();
111
112 mapping.addMapper(DbImportObjectCreationMapper.NewInstance(this, "Taxon number", TAXON_NAMESPACE)); //id + tu_status
113
114 // mapping.addMapper(DbImportMethodMapper.NewInstance(this, "makeTypes", ResultSet.class, TaxonBase.class, CentralAfricaFernsImportState.class));
115 mapping.addMapper(DbImportAnnotationMapper.NewInstance("Notes", AnnotationType.EDITORIAL()));
116
117 mapping.addMapper(DbImportMethodMapper.NewInstance(this, "makeCommonName", ResultSet.class, CentralAfricaFernsImportState.class));
118 mapping.addMapper(DbImportMethodMapper.NewInstance(this, "makeReferences", ResultSet.class, CentralAfricaFernsImportState.class));
119 mapping.addMapper(DbImportMethodMapper.NewInstance(this, "makeEcology", ResultSet.class, CentralAfricaFernsImportState.class));
120 mapping.addMapper(DbImportMethodMapper.NewInstance(this, "makeDistribution", ResultSet.class, CentralAfricaFernsImportState.class ));
121
122 // mapping.addMapper(DbImportTextDataCreationMapper.NewInstance(dbIdAttribute, objectToCreateNamespace, dbTaxonFkAttribute, taxonNamespace, dbTextAttribute, Language.ENGLISH(), Feature.ECOLOGY(), null));
123
124 //not yet implemented or ignore
125 mapping.addMapper(DbNotYetImplementedMapper.NewInstance("Types XXX", "Method Mapper does not work yet. Needs implementation for all 5 types. FIXMEs in implementation"));
126
127 mapping.addMapper(DbNotYetImplementedMapper.NewInstance("Basionym of", "Needs better understanding"));
128 mapping.addMapper(DbNotYetImplementedMapper.NewInstance("Synonym of", "Needs better understanding. Strange values like "));
129
130
131 mapping.addMapper(DbNotYetImplementedMapper.NewInstance("Chromosome number" , "Wrong data. Seems to be 'reference full'"));
132
133 mapping.addMapper(DbNotYetImplementedMapper.NewInstance("Book / Paper title" , "Needs implementation. Inreferences?"));
134
135 mapping.addMapper(DbNotYetImplementedMapper.NewInstance("Book Publisher & Place" , "How to access the reference via String mapper?"));
136
137 mapping.addMapper(DbNotYetImplementedMapper.NewInstance("Nom remarks" , "Needs parsing for status, homonyms etc., the rest goes to a name annotation"));
138
139 mapping.addMapper(DbNotYetImplementedMapper.NewInstance("Reprint no" , "What's this?"));
140 mapping.addMapper(DbNotYetImplementedMapper.NewInstance("Date verified" , "Needed?"));
141
142 mapping.addMapper(DbNotYetImplementedMapper.NewInstance("Illustrations - non-original" , "What's this?"));
143
144 //
145 // UUID credibilityUuid = ErmsTransformer.uuidCredibility;
146 // mapping.addMapper(DbImportExtensionMapper.NewInstance("tu_credibility", credibilityUuid, "credibility", "credibility", "credibility")); //Werte: null, unknown, marked for deletion
147 //
148 //ignore
149 // mapping.addMapper(DbIgnoreMapper.NewInstance("cache_citation", "citation cache not needed in PESI"));
150
151 //not yet implemented or ignore
152 // mapping.addMapper(DbNotYetImplementedMapper.NewInstance("tu_hidden", "Needs DbImportMarkerMapper implemented"));
153
154 }
155 return mapping;
156 }
157
158 /* (non-Javadoc)
159 * @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getRecordQuery(eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator)
160 */
161 @Override
162 protected String getRecordQuery(CentralAfricaFernsImportConfigurator config) {
163 String strSelect = " SELECT * ";
164 String strFrom = " FROM [African pteridophytes] as ap";
165 String strWhere = " WHERE ( ap.[taxon number] IN (" + ID_LIST_TOKEN + ") )";
166 String strOrderBy = " ORDER BY [Taxon number]";
167 String strRecordQuery = strSelect + strFrom + strWhere + strOrderBy;
168 return strRecordQuery;
169 }
170
171
172 /* (non-Javadoc)
173 * @see eu.etaxonomy.cdm.io.berlinModel.in.IPartitionedIO#getRelatedObjectsForPartition(java.sql.ResultSet)
174 */
175 public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs) {
176 String nameSpace;
177 Class cdmClass;
178 Set<String> idSet;
179 Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
180
181 try{
182 Set<String> nameIdSet = new HashSet<String>();
183 Set<String> referenceIdSet = new HashSet<String>();
184 while (rs.next()){
185 // handleForeignKey(rs, nameIdSet, "PTNameFk");
186 // handleForeignKey(rs, referenceIdSet, "PTRefFk");
187 }
188
189 //reference map
190 // nameSpace = "Reference";
191 // cdmClass = Reference.class;
192 // Map<String, Person> referenceMap = (Map<String, Person>)getCommonService().getSourcedObjectsByIdInSource(Person.class, teamIdSet, nameSpace);
193 // result.put(Reference.class, referenceMap);
194
195 } catch (SQLException e) {
196 throw new RuntimeException(e);
197 }
198 return result;
199 }
200
201 private TaxonBase makeTypes(ResultSet rs, TaxonBase taxonBase, CentralAfricaFernsImportState state) throws SQLException{
202 TaxonNameBase name = taxonBase.getName();
203 String typeString = rs.getString("Type");
204 String typeCollectorString = rs.getString("Type collector and number");
205 String typeLocationString = rs.getString("Type location");
206 makeSingleType(name, typeString, typeCollectorString, typeLocationString);
207 return taxonBase;
208 }
209
210
211 private void makeSingleType(TaxonNameBase name, String typeString, String typeCollectorString, String typeLocationString) {
212 DerivedUnitFacade type = DerivedUnitFacade.NewInstance(DerivedUnitType.Specimen);
213 makeTypeCollectorInfo(type, typeCollectorString);
214 type.setLocality(typeString);
215 //TODO
216 // type.addDuplicate(duplicateSpecimen);
217 //FIXME handle also NameTypeDesignations
218 SpecimenTypeDesignation designation = SpecimenTypeDesignation.NewInstance();
219 designation.setTypeSpecimen(type.innerDerivedUnit());
220 name.addTypeDesignation(designation, false);
221 }
222
223
224
225 private void makeTypeCollectorInfo(DerivedUnitFacade type, String collectorAndNumberString) {
226 String reNumber = "(s\\.n\\.|\\d.*)";
227 Pattern reNumberPattern = Pattern.compile(reNumber);
228 Matcher matcher = reNumberPattern.matcher(collectorAndNumberString);
229
230 if ( matcher.find()){
231 int numberStart = matcher.start();
232 String number = collectorAndNumberString.substring(numberStart).trim();
233 String collectorString = collectorAndNumberString.substring(0, numberStart -1).trim();
234 type.setCollectorsNumber(number);
235 Team team = Team.NewTitledInstance(collectorString, collectorString);
236 type.setCollector(team);
237
238 }else{
239 logger.warn("collector string did not match number pattern: " + collectorAndNumberString);
240
241 }
242 }
243
244 /**
245 * for internal use only, used by MethodMapper
246 */
247 private TaxonBase makeDistribution(ResultSet rs, CentralAfricaFernsImportState state) throws SQLException{
248 try {
249 String taxonNumber = state.getTaxonNumber();
250 // logger.info(taxonNumber);
251 TaxonBase<?> taxonBase = state.getRelatedObject(state.CURRENT_OBJECT_NAMESPACE, state.CURRENT_OBJECT_ID, TaxonBase.class);
252 String countriesString = rs.getString("Distribution - Country");
253 String province = rs.getString("Distribution - Province");
254 String distributionDetailed = rs.getString("Distribution - detailed");
255 TaxonNameBase nameUsedInSource = taxonBase.getName();
256 Taxon taxon;
257 if (taxonBase.isInstanceOf(Taxon.class)){
258 taxon = CdmBase.deproxy(taxonBase, Taxon.class);
259 }else{
260 logger.warn("Distributions for synonyms not yet supported");
261 taxon = Taxon.NewInstance(null, null);
262 }
263
264 if (StringUtils.isNotBlank(countriesString) ){
265 makeCountries(state, taxonNumber, taxon, nameUsedInSource, countriesString, province, distributionDetailed);
266 }
267 makeProvince(taxon, province);
268 makeDistributionDetailed(taxon, distributionDetailed);
269
270 return taxonBase;
271 } catch (Exception e) {
272 e.printStackTrace();
273 return null;
274 }
275 }
276
277
278
279 /**
280 * @param state
281 * @param taxonNumber
282 * @param taxonBase
283 * @param countriesString
284 */
285 private void makeCountries(CentralAfricaFernsImportState state, String taxonNumber, Taxon taxon, TaxonNameBase nameUsedInSource, String countriesString, String province, String distributionDetailed) {
286 countriesString = countriesString.replaceAll("\\*", "");
287 countriesString = countriesString.replace(" ", " ");
288 countriesString = countriesString.replace(", endemic", " - endemic");
289 countriesString = countriesString.replace("(endemic)", " - endemic");
290 countriesString = countriesString.replace("(introduced)", " - introduced");
291 countriesString = countriesString.replace("(naturalised)", " - naturalised");
292 countriesString = countriesString.replace("Madagascar-", "Madagascar -");
293 countriesString = countriesString.replace("Mahé", "Mahe");
294
295 String[] split = countriesString.split("[,;]");
296 String remainingString = null;
297 for (String countryString : split){
298 countryString = CdmUtils.concat(", ", remainingString , countryString);
299 if (countryString.matches(".*\\(.*") && ! countryString.matches(".*\\).*")){
300 remainingString = countryString;
301 continue;
302 }
303 remainingString = null;
304 try {
305 makeSingleCountry(state, taxonNumber, taxon, nameUsedInSource, countryString.trim());
306 } catch (UndefinedTransformerMethodException e) {
307 e.printStackTrace();
308 }
309 }
310 }
311
312 private void makeDistributionDetailed(Taxon taxon, String distributionDetailed) {
313 if (StringUtils.isNotBlank(distributionDetailed)){
314 TaxonDescription description = getTaxonDescription(taxon, false, true);
315 TextData distribution = TextData.NewInstance(Feature.DISTRIBUTION());
316 description.addElement(distribution);
317 distribution.putText(distributionDetailed, Language.ENGLISH());
318 }
319 }
320
321 private void makeProvince(Taxon taxon, String province) {
322 if (StringUtils.isNotBlank(province)){
323 TaxonDescription description = getTaxonDescription(taxon, false, true);
324 TextData distribution = TextData.NewInstance(Feature.DISTRIBUTION());
325 description.addElement(distribution);
326 distribution.putText(province, Language.ENGLISH());
327 }
328 }
329
330
331
332 private void makeSingleCountry(CentralAfricaFernsImportState state, String taxonNumber, Taxon taxon, TaxonNameBase nameUsedInSource, String country) throws UndefinedTransformerMethodException {
333 boolean areaDoubtful = false;
334 Distribution distribution = Distribution.NewInstance(null, PresenceTerm.PRESENT());
335 distribution.addSource(taxonNumber, "Distribution_Country", state.getConfig().getSourceReference(), null, nameUsedInSource, null);
336 NamedArea area = null;
337 //empty
338 if (StringUtils.isBlank(country)){
339 return;
340 }
341 country = country.trim();
342 //doubtful
343 if (country.startsWith("?")){
344 areaDoubtful = true;
345 country = country.substring(1).trim();
346 }
347 //status
348 country = makeCountryStatus(state, country, distribution);
349
350 //brackets
351 country = makeCountryBrackets(state, taxonNumber, taxon, nameUsedInSource, country);
352 String countryWithoutIslands = null;
353 String countryWithoutDot = null;
354 if (country.endsWith(" Isl.") || country.endsWith(" isl.") ){
355 countryWithoutIslands = country.substring(0, country.length()-5);
356 }
357 if (country.endsWith(".")){
358 countryWithoutDot = country.substring(0, country.length()-1);
359 }
360 if (country.endsWith("*")){
361 country = country.substring(0, country.length()-1);
362 }
363 if (country.endsWith("Islands")){
364 country = country.replace("Islands", "Is.");
365 }
366
367
368 //areas
369 if (TdwgArea.isTdwgAreaLabel(country)){
370 //tdwg
371 area = TdwgArea.getAreaByTdwgLabel(country);
372 }else if (TdwgArea.isTdwgAreaLabel(countryWithoutIslands)){
373 //tdwg
374 area = TdwgArea.getAreaByTdwgLabel(countryWithoutIslands);
375 }else if (TdwgArea.isTdwgAreaLabel(countryWithoutDot)){
376 //tdwg
377 area = TdwgArea.getAreaByTdwgLabel(countryWithoutDot);
378 }else if ( (area = state.getTransformer().getNamedAreaByKey(country)) != null) {
379 //area already set
380 }else if (WaterbodyOrCountry.isWaterbodyOrCountryLabel(country)){
381 //iso
382 area = WaterbodyOrCountry.getWaterbodyOrCountryByLabel(country);
383 }else{
384 //others
385 NamedAreaLevel level = null;
386 NamedAreaType areaType = null;
387
388 UUID uuid = state.getTransformer().getNamedAreaUuid(country);
389 if (uuid == null){
390 logger.error(taxonNumber + " - Unknown country: " + country);
391 }
392 area = getNamedArea(state, uuid, country, country, country, areaType, level);
393 }
394
395 distribution.setArea(area);
396 if (areaDoubtful == true){
397 if (distribution.getStatus().equals(PresenceTerm.PRESENT())){
398 distribution.setStatus(PresenceTerm.PRESENT_DOUBTFULLY());
399 }
400 }
401 TaxonDescription description = getTaxonDescription(taxon, false, true);
402 description.addElement(distribution);
403 }
404
405
406
407 private String makeCountryBrackets(CentralAfricaFernsImportState state, String taxonNumber, Taxon taxon, TaxonNameBase nameUsedInSource, String country) {
408 String[] split = (country + " ").split("\\(.*\\)");
409 if (split.length == 2){
410 String bracket = country.substring(split[0].length()+1, country.indexOf(")"));
411 country = split[0].trim();
412 makeCountries(state, taxonNumber, taxon, nameUsedInSource, bracket, null, null);
413 }else if (split.length ==1){
414 //do nothing
415 }else{
416 logger.warn("Illegal length");
417 }
418 return country;
419 }
420
421
422
423 private String makeCountryStatus(CentralAfricaFernsImportState state, String country, Distribution distribution) throws UndefinedTransformerMethodException {
424 PresenceTerm status = null;
425 String[] split = country.split(" - ");
426
427 if (split.length == 2){
428 country = split[0].trim();
429 String statusString = split[1];
430 statusString = statusString.replace(".", "");
431 status = state.getTransformer().getPresenceTermByKey(statusString);
432 if (status == null){
433 logger.warn("No status found: "+ statusString);
434 }
435 // UUID uuid = null;
436 // status = getPresenceTerm(state, uuid, statusString, statusString, null);
437 }else if (split.length == 1){
438 //nothing to do
439 }else{
440 logger.warn("Invalid length: " + split.length);
441 }
442 if (status != null){
443 distribution.setStatus(status);
444 }
445 return country;
446 }
447
448
449
450 /**
451 * for internal use only, used by MethodMapper
452 */
453 private TaxonBase makeReferences(ResultSet rs, CentralAfricaFernsImportState state) throws SQLException{
454 String taxonNumber = state.getTaxonNumber();
455 String referenceFullString = rs.getString("Reference full");
456 String referenceAbbreviatedString = rs.getString("Reference - abbreviated");
457 String volume = rs.getString("Book / Journal volume");
458 String pages = rs.getString("Book / Journal pages");
459 String illustrations = rs.getString("Illustration/s");
460
461 String fascicle = rs.getString("Book / Journal fascicle");
462 String part = rs.getString("Book / Journal part");
463 String paperTitle = rs.getString("Book / Paper title");
464
465
466
467 String datePublishedString = rs.getString("Date published");
468 String referenceString = referenceFullString;
469 if (StringUtils.isBlank(referenceString)){
470 referenceString = referenceAbbreviatedString;
471 }
472
473 TaxonBase<?> taxonBase = state.getRelatedObject(state.CURRENT_OBJECT_NAMESPACE, state.CURRENT_OBJECT_ID, TaxonBase.class);
474 if (StringUtils.isNotBlank(referenceString) || StringUtils.isNotBlank(volume) ||
475 StringUtils.isNotBlank(pages) || StringUtils.isNotBlank(illustrations) ||
476 StringUtils.isNotBlank(datePublishedString) || StringUtils.isNotBlank(paperTitle)){
477 NonViralName name = CdmBase.deproxy(taxonBase.getName(), NonViralName.class);
478 Reference reference = ReferenceFactory.newGeneric();
479 reference.setAuthorTeam((TeamOrPersonBase)name.getCombinationAuthorTeam());
480 reference.setTitle(referenceString);
481 reference.setVolume(volume);
482 reference.setEdition(part);
483 Reference inrefernce = null;
484 //TODO parser
485 TimePeriod datePublished = TimePeriod.parseString(datePublishedString);
486 reference.setDatePublished(datePublished);
487 if (StringUtils.isNotBlank(paperTitle)){
488 Reference innerReference = ReferenceFactory.newGeneric();
489 innerReference.setDatePublished(datePublished);
490 name.setNomenclaturalReference(innerReference);
491 innerReference.setInReference(reference);
492 reference = innerReference;
493 }else{
494 name.setNomenclaturalReference(reference);
495 }
496
497 //details
498 String details = CdmUtils.concat(", ", pages, illustrations);
499 details = StringUtils.isBlank(details) ? null : details.trim();
500 name.setNomenclaturalMicroReference(details);
501 try {
502 UUID uuidFascicle = state.getTransformer().getExtensionTypeUuid("fascicle");
503 ExtensionType extensionType = getExtensionType(state, uuidFascicle, "Fascicle", "Fascicle", null);
504 reference.addExtension(fascicle, extensionType);
505 } catch (UndefinedTransformerMethodException e) {
506 e.printStackTrace();
507 }
508
509 }else{
510 logger.warn(taxonNumber + " - Taxon has no reference");
511 }
512 return taxonBase;
513 }
514
515 /**
516 * for internal use only, used by MethodMapper
517 */
518 private TaxonBase makeCommonName(ResultSet rs, CentralAfricaFernsImportState state) throws SQLException{
519 String taxonNumber = state.getTaxonNumber();
520 String commonNames = rs.getString("Common names");
521 TaxonBase<?> taxonBase = state.getRelatedObject(state.CURRENT_OBJECT_NAMESPACE, state.CURRENT_OBJECT_ID, TaxonBase.class);
522 if (StringUtils.isNotBlank(commonNames)){
523 if (taxonBase.isInstanceOf(Taxon.class)){
524 Taxon taxon = (Taxon)taxonBase;
525 TaxonDescription description = getTaxonDescription(taxon, false, true);
526 String[] split = commonNames.split(",");
527 for (String commonNameString: split){
528 CommonTaxonName commonName = CommonTaxonName.NewInstance(commonNameString.trim(), Language.ENGLISH());
529 description.addElement(commonName);
530 }
531 }else{
532 logger.warn(taxonNumber + " - Taxon with common name is of type synonym but must be accepted taxon: " + taxonBase.getName().getTitleCache());
533 }
534 }
535 return taxonBase;
536 }
537
538 /**
539 * for internal use only, used by MethodMapper
540 * @param commonNames
541 */
542 private TaxonBase makeEcology(ResultSet rs, CentralAfricaFernsImportState state) throws SQLException{
543 String taxonNumber = state.getTaxonNumber();
544 String ecologyString = rs.getString("Ecology");
545 TaxonBase<?> taxonBase = state.getRelatedObject(state.CURRENT_OBJECT_NAMESPACE, state.CURRENT_OBJECT_ID, TaxonBase.class);
546 if (StringUtils.isNotBlank(ecologyString)){
547 if (taxonBase.isInstanceOf(Taxon.class)){
548 Taxon taxon = (Taxon)taxonBase;
549 TaxonDescription description = getTaxonDescription(taxon, false, true);
550 TextData ecology = TextData.NewInstance(Feature.ECOLOGY());
551 ecology.putText(ecologyString.trim(), Language.ENGLISH());
552 description.addElement(ecology);
553 }else{
554 logger.warn(taxonNumber + " - Taxon with ecology is of type synonym but must be accepted taxon: " + taxonBase.getName().getTitleCache());
555 }
556 }
557 return taxonBase;
558 }
559
560
561 /* (non-Javadoc)
562 * @see eu.etaxonomy.cdm.io.common.mapping.IMappingImport#createObject(java.sql.ResultSet)
563 */
564 public TaxonBase createObject(ResultSet rs, CentralAfricaFernsImportState state) throws SQLException {
565 BotanicalName taxonName = BotanicalName.NewInstance(null);
566 Reference sec = state.getConfig().getSourceReference();
567
568 String taxonNumber = rs.getString("Taxon number");
569 state.setTaxonNumber(taxonNumber);
570
571 String orderName = rs.getString("Order name");
572 String subOrderName = rs.getString("Suborder name");
573 String familyName = rs.getString("Family name");
574 String subFamilyName = rs.getString("Subfamily name");
575 String tribusName = rs.getString("Tribus name");
576 String subTribusName = rs.getString("Subtribus name");
577 String sectionName = rs.getString("Section name");
578 String genusName = rs.getString("Genus name");
579 String subGenusName = rs.getString("Subgenus name");
580 String seriesName = rs.getString("Series name");
581 String specificEpihet = rs.getString("Specific epihet");
582 String subspeciesName = rs.getString("Subspecies name");
583 String varietyName = rs.getString("Variety name");
584 String subFormaName = rs.getString("Subforma");
585 String subVariety = rs.getString("Subvariery");
586 String formaName = rs.getString("Forma name");
587 String subsectionName = rs.getString("Subsection name");
588
589 String status = rs.getString("Current/Synonym");
590
591 TaxonBase taxon = makeTaxon(taxonName, sec, taxonNumber, status);
592
593 // Integer parent3Rank = rs.getInt("parent3rank");
594
595 //rank and epithets
596 Rank lowestRank = setLowestUninomial(taxonName, orderName, subOrderName, familyName, subFamilyName, tribusName, subTribusName,genusName);
597 lowestRank = setLowestInfraGeneric(taxonName, lowestRank, subGenusName, sectionName, subsectionName, seriesName);
598 if (StringUtils.isNotBlank(specificEpihet)){
599 taxonName.setSpecificEpithet(specificEpihet);
600 lowestRank = Rank.SPECIES();
601 }
602 lowestRank = setLowestInfraSpecific(taxonName, lowestRank, subspeciesName, varietyName, subVariety, formaName,subFormaName);
603
604 taxonName.setRank(lowestRank);
605 setAuthor(taxonName, rs, taxonNumber, false);
606
607 //add original source for taxon name (taxon original source is added in mapper
608 Reference citation = state.getConfig().getSourceReference();
609 // addOriginalSource(taxonName, taxonNumber, TAXON_NAMESPACE, citation);
610 return taxon;
611
612 }
613
614
615
616 /**
617 * Creates the taxon object depending on name, sec and status
618 * @param taxonName
619 * @param sec
620 * @param taxonNumber
621 * @param status
622 * @return
623 */
624 private TaxonBase makeTaxon(BotanicalName taxonName, Reference sec, String taxonNumber, String status) {
625 TaxonBase taxon;
626 if ("c".equalsIgnoreCase(status)|| "incertus".equalsIgnoreCase(status) ){
627 taxon = Taxon.NewInstance(taxonName, sec);
628 if ("incertus".equalsIgnoreCase(status)){
629 taxon.setDoubtful(true);
630 }
631 }else if ("s".equalsIgnoreCase(status)){
632 taxon = Synonym.NewInstance(taxonName, sec);
633 }else{
634 logger.warn(taxonNumber + ": Status not given for taxon " );
635 taxon = Taxon.NewUnknownStatusInstance(taxonName, sec);
636 }
637 return taxon;
638 }
639
640
641 private Rank setLowestInfraSpecific(BotanicalName taxonName, Rank lowestRank, String subspeciesName, String varietyName,
642 String subVariety, String formaName, String subFormaName) {
643 if (StringUtils.isNotBlank(subFormaName)){
644 taxonName.setInfraSpecificEpithet(subFormaName);
645 return Rank.SUBFORM();
646 }else if (StringUtils.isNotBlank(formaName)){
647 taxonName.setInfraSpecificEpithet(formaName);
648 return Rank.FORM();
649 }else if (StringUtils.isNotBlank(subVariety)){
650 taxonName.setInfraSpecificEpithet(subVariety);
651 return Rank.SUBVARIETY();
652 }else if (StringUtils.isNotBlank(varietyName)){
653 taxonName.setInfraSpecificEpithet(varietyName);
654 return Rank.VARIETY();
655 }else if (StringUtils.isNotBlank(subspeciesName)){
656 taxonName.setInfraSpecificEpithet(subspeciesName);
657 return Rank.SUBSPECIES();
658 }else{
659 return lowestRank;
660 }
661 }
662
663
664
665 private Rank setLowestInfraGeneric(BotanicalName taxonName, Rank lowestRank, String subGenusName, String sectionName, String subSectionName, String seriesName) {
666 if (StringUtils.isNotBlank(seriesName)){
667 taxonName.setInfraGenericEpithet(seriesName);
668 return Rank.SERIES();
669 }else if (StringUtils.isNotBlank(subSectionName)){
670 taxonName.setInfraGenericEpithet(subSectionName);
671 return Rank.SUBSECTION_BOTANY();
672 }else if (StringUtils.isNotBlank(sectionName)){
673 taxonName.setInfraGenericEpithet(sectionName);
674 return Rank.SECTION_BOTANY();
675 }else if (StringUtils.isNotBlank(subGenusName)){
676 taxonName.setInfraGenericEpithet(subGenusName);
677 return Rank.SUBGENUS();
678 }else{
679 return lowestRank;
680 }
681 }
682
683
684
685 private Rank setLowestUninomial(BotanicalName taxonName, String orderName, String subOrderName, String familyName, String subFamilyName,
686 String tribusName, String subTribusName, String genusName) {
687
688 if (StringUtils.isNotBlank(genusName)){
689 taxonName.setGenusOrUninomial(genusName);
690 return Rank.GENUS();
691 }else if (StringUtils.isNotBlank(subTribusName)){
692 taxonName.setGenusOrUninomial(subTribusName);
693 return Rank.SUBTRIBE();
694 }else if (StringUtils.isNotBlank(tribusName)){
695 taxonName.setGenusOrUninomial(tribusName);
696 return Rank.TRIBE();
697 }else if (StringUtils.isNotBlank(subFamilyName)){
698 taxonName.setGenusOrUninomial(subFamilyName);
699 return Rank.SUBFAMILY();
700 }else if (StringUtils.isNotBlank(familyName)){
701 taxonName.setGenusOrUninomial(familyName);
702 return Rank.FAMILY();
703 }else if (StringUtils.isNotBlank(subOrderName)){
704 taxonName.setGenusOrUninomial(subOrderName);
705 return Rank.SUBORDER();
706 }else if (StringUtils.isNotBlank(orderName)){
707 taxonName.setGenusOrUninomial(orderName);
708 return Rank.ORDER();
709 }else{
710 return null;
711 }
712 }
713
714
715 /* (non-Javadoc)
716 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
717 */
718 @Override
719 protected boolean doCheck(CentralAfricaFernsImportState state){
720 IOValidator<CentralAfricaFernsImportState> validator = new CentralAfricaFernsTaxonImportValidator();
721 return validator.validate(state);
722 }
723
724
725 /* (non-Javadoc)
726 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IoStateBase)
727 */
728 @Override
729 protected boolean isIgnore(CentralAfricaFernsImportState state){
730 return ! state.getConfig().isDoTaxa();
731 }
732
733
734
735 }