Project

General

Profile

« Previous | Next » 

Revision 4d8fb1a4

Added by Alexander Oppermann about 9 years ago

Adapted Redlist Import to new XML Schema...

View differences:

app-import/src/main/java/eu/etaxonomy/cdm/io/redlist/bfnXml/BfnXmlImportTaxonName.java
10 10
package eu.etaxonomy.cdm.io.redlist.bfnXml;
11 11

  
12 12
import java.util.ArrayList;
13
import java.util.Arrays;
14 13
import java.util.Collection;
15
import java.util.HashMap;
16 14
import java.util.LinkedHashMap;
17 15
import java.util.List;
18 16
import java.util.Map;
19
import java.util.Set;
20 17
import java.util.UUID;
21 18

  
22 19
import org.apache.commons.lang.StringUtils;
23 20
import org.apache.log4j.Logger;
24
import org.apache.poi.ss.formula.functions.T;
25
import org.hibernate.Session;
26
import org.jdom.Attribute;
27 21
import org.jdom.Element;
28 22
import org.jdom.Namespace;
29 23
import org.springframework.stereotype.Component;
30 24
import org.springframework.transaction.TransactionStatus;
31 25

  
32
import com.google.common.base.CharMatcher;
33

  
34 26
import eu.etaxonomy.cdm.api.service.IClassificationService;
35 27
import eu.etaxonomy.cdm.api.service.ITaxonService;
36
import eu.etaxonomy.cdm.api.service.config.MatchingTaxonConfigurator;
37
import eu.etaxonomy.cdm.api.service.pager.Pager;
38
import eu.etaxonomy.cdm.api.service.util.TaxonRelationshipEdge;
39 28
import eu.etaxonomy.cdm.common.ResultWrapper;
40 29
import eu.etaxonomy.cdm.common.XmlHelp;
41 30
import eu.etaxonomy.cdm.io.common.ICdmIO;
42
import eu.etaxonomy.cdm.io.common.ImportHelper;
43
import eu.etaxonomy.cdm.io.common.MapWrapper;
44 31
import eu.etaxonomy.cdm.model.common.CdmBase;
45
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
46 32
import eu.etaxonomy.cdm.model.common.Language;
47
import eu.etaxonomy.cdm.model.common.RelationshipBase;
48
import eu.etaxonomy.cdm.model.common.RelationshipBase.Direction;
49 33
import eu.etaxonomy.cdm.model.description.CategoricalData;
34
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
50 35
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
51 36
import eu.etaxonomy.cdm.model.description.Feature;
52 37
import eu.etaxonomy.cdm.model.description.State;
53 38
import eu.etaxonomy.cdm.model.description.TaxonDescription;
54 39
import eu.etaxonomy.cdm.model.description.TextData;
40
import eu.etaxonomy.cdm.model.location.NamedArea;
55 41
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
56 42
import eu.etaxonomy.cdm.model.name.NomenclaturalStatus;
57 43
import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
58 44
import eu.etaxonomy.cdm.model.name.NonViralName;
59 45
import eu.etaxonomy.cdm.model.name.Rank;
60 46
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
61
import eu.etaxonomy.cdm.model.reference.Reference;
62
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
63 47
import eu.etaxonomy.cdm.model.taxon.Classification;
64 48
import eu.etaxonomy.cdm.model.taxon.Synonym;
65 49
import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
......
106 90

  
107 91
		BfnXmlImportConfigurator config = state.getConfig();
108 92
		Element elDataSet = getDataSetElement(config);
93
		//TODO set Namespace
109 94
		Namespace bfnNamespace = config.getBfnXmlNamespace();
110 95
		
111 96
		List<?> contentXML = elDataSet.getContent();
......
121 106
					createOrUdateClassification(config, taxonService, savedTaxonMap, currentElement, state);
122 107
					commitTransaction(tx);
123 108
				}//import concept relations of taxon lists
124
				else if(currentElement.getName().equalsIgnoreCase("KONZEPTBEZIEHUNGEN")){
125
					TransactionStatus tx = startTransaction();
126
					extractTaxonConceptRelationShips(bfnNamespace,currentElement);
127
					commitTransaction(tx);
109
				if(config.isHasSecondList()){
110
					if(currentElement.getName().equalsIgnoreCase("KONZEPTBEZIEHUNGEN")){
111
						TransactionStatus tx = startTransaction();
112
						extractTaxonConceptRelationShips(bfnNamespace,currentElement);
113
						commitTransaction(tx);
114
					}
128 115
				}
129 116
			}
130 117
		}
......
248 235
			Element elSynonyms = XmlHelp.getSingleChildElement(success, elTaxon, childName, bfnNamespace, obligatory);
249 236
			if(elSynonyms != null){
250 237
				childElementName = "SYNONYM";
251
				createOrUpdateSynonym(taxon, success, obligatory, bfnNamespace, childElementName,elSynonyms, taxonId, config);
238
				createOrUpdateSynonym(taxon, success, obligatory, bfnNamespace, childElementName,elSynonyms, taxonId, state);
239
			}
240
			//for vernacular name
241
			childName = "DEUTSCHENAMEN";
242
			Element elVernacularName = XmlHelp.getSingleChildElement(success, elTaxon, childName, bfnNamespace, obligatory);
243
			if(elVernacularName != null){
244
				childElementName = "DNAME";
245
				createOrUpdateVernacularName(taxon, bfnNamespace, childElementName, elVernacularName, state);
252 246
			}
253 247
			//for each information concerning the taxon element
254 248
			//TODO Information block
......
278 272
	}
279 273

  
280 274

  
275

  
276

  
281 277
	/**
282 278
	 * This will put the prior imported list into a classification
283 279
	 * 
......
292 288
	@SuppressWarnings("rawtypes")
293 289
	private boolean createOrUdateClassification(BfnXmlImportConfigurator config, ITaxonService taxonService, Map<UUID, TaxonBase> savedTaxonMap, Element currentElement, BfnXmlImportState state) {
294 290
		boolean isNewClassification = true;
291
		String classificationName = state.getFirstClassificationName();
292
		if(config.isFillSecondList()){
293
			classificationName = state.getSecondClassificationName();
294
		}
295
//		if(classificationName == null){
296
//			classificationName = config.getClassificationName();
297
//		}
295 298
		//TODO make classification name dynamically depending on its value in the XML.
296
		Classification classification = Classification.NewInstance(config.getClassificationName()+" "+currentElement.getAttributeValue("inhalt"), state.getCurrentRef());
299
		Classification classification = Classification.NewInstance(classificationName+" "+currentElement.getAttributeValue("inhalt"), state.getCompleteSourceRef());
300
		classification.addImportSource(Integer.toString(classification.getId()), classification.getTitleCache(), state.getCompleteSourceRef(), state.getCurrentMicroRef().toString());
297 301
//		List<Classification> classificationList = getClassificationService().list(Classification.class, null, null, null, VOC_CLASSIFICATION_INIT_STRATEGY);
298 302
//		for(Classification c : classificationList){
299 303
//			if(c.getTitleCache().equalsIgnoreCase(classification.getTitleCache())){
......
348 352
		String strAuthor = null;
349 353
		String strSupplement = null;
350 354
		Taxon taxon = null;
351
		Integer uniqueID = null;
355
		String uniqueID = null;
356
		String uriNameSpace = null;
352 357
//		Long uniqueID = null;
353 358
		for(Element elWissName:elWissNameList){
354 359

  
355 360
			if(elWissName.getAttributeValue("bereich", bfnNamespace).equalsIgnoreCase("Eindeutiger Code")){
361
				uriNameSpace = elWissName.getAttributeValue("bereich");
356 362
				String textNormalize = elWissName.getTextNormalize();
357 363
				if(StringUtils.isBlank(textNormalize)){
358
					uniqueID = -1;
364
					uniqueID = "";
359 365
				}else{
360
					uniqueID = Integer.valueOf(textNormalize);
366
					uniqueID = textNormalize;
361 367
				}
362 368
			}
363 369
			if(elWissName.getAttributeValue("bereich", bfnNamespace).equalsIgnoreCase("Autoren")){
......
397 403
//						if (nameList.size()>1){
398 404
//							logger.warn("More than 1 matching taxon name found for " + nameBase.getTitleCache());
399 405
//						}
400
//					}
406
//					}CurrentMicroRef
401 407

  
402 408
//					taxon = (Taxon) taxonBase;
403
					state.setCurrentRef(state.getFirstListSecRef());
409
					state.setCurrentMicroRef(state.getFirstListSecRef());
404 410
					if(config.isFillSecondList()){
405
						state.setCurrentRef(state.getSecondListSecRef());
411
						state.setCurrentMicroRef(state.getSecondListSecRef());
406 412
					}
407
					taxon = Taxon.NewInstance(nameBase, state.getCurrentRef());
408
					taxon.addImportSource(uniqueID.toString(), config.getBfnXmlNamespace().toString(), state.getCurrentRef(), null);
413
					taxon = Taxon.NewInstance(nameBase, state.getCurrentMicroRef());
414
//					logger.info("Taxon Reference" + taxon.getSec().getTitle());
415
					//set NameSpace
416
					Element parentElement = elWissName.getParentElement();
417
					Element grandParentElement = parentElement.getParentElement();
418
//					Element newElement = new Element("prefix", parentElement.getName()+":"+parentElement.getAttribute("taxNr").getName());
419
//					Element newElement = new Element("element",grandParentElement.getName()+"-"+parentElement.getName()+"-"+elWissName.getName() , uriNameSpace);
420
//					config.setBfnXmlNamespace(newElement.getNamespace());
421
					
422
					taxon.addImportSource(uniqueID, grandParentElement.getName()+":"+parentElement.getName()+":"+elWissName.getName()+":"+uriNameSpace, state.getCompleteSourceRef(), state.getCurrentMicroRef().getTitle());
409 423
				} catch (UnknownCdmTypeException e) {
410 424
					success.setValue(false); 
411 425
				}
......
427 441
	 * @param elSynonyms
428 442
	 * @param taxonId
429 443
	 * @param config
444
	 * @param state 
430 445
	 */
431 446

  
432 447
	@SuppressWarnings({ "unchecked" })
433 448
	private void createOrUpdateSynonym(Taxon taxon, ResultWrapper<Boolean> success, boolean obligatory, Namespace bfnNamespace, 
434
			     String childElementName, Element elSynonyms, String taxonId, BfnXmlImportConfigurator config) {
449
			     String childElementName, Element elSynonyms, String taxonId, BfnXmlImportState state) {
435 450
		
436 451
		String childName;
437 452
		List<Element> elSynonymList = (List<Element>)elSynonyms.getChildren(childElementName, bfnNamespace);
......
462 477
						TaxonNameBase<?, ?> nameBase = parseNonviralNames(rank,strAuthor,strSupplement,elSynDetail);
463 478

  
464 479
						//TODO find best matching Taxa
465
						Synonym synonym = Synonym.NewInstance(nameBase, config.getSourceReference());
480
						Synonym synonym = Synonym.NewInstance(nameBase, state.getCurrentMicroRef());
466 481
						taxon.addSynonym(synonym, SynonymRelationshipType.SYNONYM_OF());
467 482
						
468 483
					} catch (UnknownCdmTypeException e) {
......
476 491
		}
477 492
	}
478 493

  
494
	
495
	/**
496
	 * 
497
	 * @param taxon
498
	 * @param bfnNamespace
499
	 * @param childElementName
500
	 * @param elVernacularName
501
	 * @param state
502
	 */
503
	private void createOrUpdateVernacularName(Taxon taxon,
504
			Namespace bfnNamespace, String childElementName,
505
			Element elVernacularName, BfnXmlImportState state) {
506
		
507
		List<Element> elVernacularNameList = (List<Element>)elVernacularName.getChildren(childElementName, bfnNamespace);
508
		
509
		TaxonDescription taxonDescription = getTaxonDescription(taxon, false, true);
510
		
511
		for(Element elVernacular : elVernacularNameList){
512
			Element child = elVernacular.getChild("TRIVIALNAME");
513
			if(child != null){
514
				makeCommonName(taxonDescription, child, state);
515
			}
516
		}
517
		
518
	}
519
	
479 520
	/**
480 521
	 * 
481 522
	 * @param taxon
......
537 578
				if(elInfoDetail.getAttributeValue("standardname").equalsIgnoreCase("Kommentar zur Gefährdung")){
538 579
					makeFeatures(taxonDescription, elInfoDetail, state, true);
539 580
				}
581
				if(elInfoDetail.getAttributeValue("standardname").equalsIgnoreCase("Sonderfälle")){
582
					makeFeatures(taxonDescription, elInfoDetail, state, false);
583
				}
584
				if(elInfoDetail.getAttributeValue("standardname").equalsIgnoreCase("Letzter Nachweis")){
585
					makeFeatures(taxonDescription, elInfoDetail, state, true);
586
				}
587
				if(elInfoDetail.getAttributeValue("standardname").equalsIgnoreCase("Weitere Kommentare")){
588
					makeFeatures(taxonDescription, elInfoDetail, state, true);
589
				}
540 590
			}
541 591
		}
542 592
	}
543 593

  
594
	
595
	
596
	private void makeCommonName(TaxonDescription taxonDescription,
597
			Element child, BfnXmlImportState state) {
598
		String commonNameValue = child.getValue();
599
		NamedArea area = getTermService().getAreaByTdwgAbbreviation("GER");
600
		CommonTaxonName commonName = CommonTaxonName.NewInstance(commonNameValue, Language.GERMAN(), area);
601
		taxonDescription.addElement(commonName);
602
	}
603

  
604
	
544 605
	/**
545 606
	 * 
546 607
	 * @param taxonDescription
......
553 614
			Element elInfoDetail,
554 615
			BfnXmlImportState state,
555 616
			boolean isTextData) {
617
		
556 618
		String transformedRlKatValue = null;
557 619
		UUID featureUUID = null;
558 620
		UUID stateTermUUID = null;
......
641 703
		TaxonNameBase<?,?> taxonNameBase = null;
642 704

  
643 705
		NomenclaturalCode nomCode = BfnXmlTransformer.nomCodeString2NomCode(strNomenclaturalCode);
644
		
706
		//Todo check author
645 707
		String strScientificName = elWissName.getTextNormalize();
646 708
		if(strSupplement != null && !strSupplement.isEmpty()){
647 709
			strScientificName = StringUtils.remove(strScientificName, strSupplement);

Also available in: Unified diff