Project

General

Profile

Download (49 KB) Statistics
| Branch: | Tag: | Revision:
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.specimen.abcd206.in;
11

    
12
import java.io.IOException;
13
import java.io.InputStream;
14
import java.net.URI;
15
import java.net.URL;
16
import java.util.ArrayList;
17
import java.util.HashMap;
18
import java.util.Iterator;
19
import java.util.List;
20
import java.util.Set;
21
import java.util.UUID;
22

    
23
import javax.xml.parsers.DocumentBuilder;
24
import javax.xml.parsers.DocumentBuilderFactory;
25
import javax.xml.parsers.ParserConfigurationException;
26

    
27
import org.apache.log4j.Logger;
28
import org.springframework.stereotype.Component;
29
import org.springframework.transaction.TransactionStatus;
30
import org.w3c.dom.Document;
31
import org.w3c.dom.Element;
32
import org.w3c.dom.Node;
33
import org.w3c.dom.NodeList;
34
import org.xml.sax.SAXException;
35

    
36
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
37
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade.DerivedUnitType;
38
import eu.etaxonomy.cdm.io.common.ICdmIO;
39
import eu.etaxonomy.cdm.io.specimen.SpecimenImportBase;
40
import eu.etaxonomy.cdm.io.specimen.UnitsGatheringArea;
41
import eu.etaxonomy.cdm.io.specimen.UnitsGatheringEvent;
42
import eu.etaxonomy.cdm.model.agent.Institution;
43
import eu.etaxonomy.cdm.model.agent.Team;
44
import eu.etaxonomy.cdm.model.common.DescriptionElementSource;
45
import eu.etaxonomy.cdm.model.description.Feature;
46
import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
47
import eu.etaxonomy.cdm.model.description.TaxonDescription;
48
import eu.etaxonomy.cdm.model.location.NamedArea;
49
import eu.etaxonomy.cdm.model.media.Media;
50
import eu.etaxonomy.cdm.model.name.BacterialName;
51
import eu.etaxonomy.cdm.model.name.BotanicalName;
52
import eu.etaxonomy.cdm.model.name.CultivarPlantName;
53
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
54
import eu.etaxonomy.cdm.model.name.NonViralName;
55
import eu.etaxonomy.cdm.model.name.Rank;
56
import eu.etaxonomy.cdm.model.name.ZoologicalName;
57
import eu.etaxonomy.cdm.model.occurrence.Collection;
58
import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
59
import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
60
import eu.etaxonomy.cdm.model.reference.Reference;
61
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
62
import eu.etaxonomy.cdm.model.taxon.Taxon;
63
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
64
import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
65

    
66

    
67
/**
68
 * @author p.kelbert
69
 * @created 20.10.2008
70
 * @version 1.0
71
 */
72
@Component
73
public class Abcd206Import extends SpecimenImportBase<Abcd206ImportConfigurator, Abcd206ImportState> implements ICdmIO<Abcd206ImportState> {
74
	private static final Logger logger = Logger.getLogger(Abcd206Import.class);
75

    
76

    
77
	public Abcd206Import() {
78
		super();
79
	}
80

    
81
	
82
	@Override
83
	protected boolean doCheck(Abcd206ImportState state) {
84
		logger.warn("Checking not yet implemented for " + this.getClass().getSimpleName());
85
		return true;
86
	}
87
	
88
	
89
	@Override
90
	public void doInvoke(Abcd206ImportState state){
91
		logger.info("INVOKE Specimen Import from ABCD2.06 XML File");
92
		//AbcdIO test = new AbcdIO();
93
		URI sourceName = state.getConfig().getSource();
94
		NodeList unitsList = getUnitsNodeList(sourceName);
95
		if (unitsList != null){
96
			String message = "nb units to insert: "+unitsList.getLength();
97
			logger.info(message);
98
			updateProgress(state, message);
99
			
100
			Abcd206DataHolder dataHolder = new Abcd206DataHolder();
101
			
102
			for (int i=0 ; i<unitsList.getLength() ; i++){
103
				this.setUnitPropertiesXML((Element)unitsList.item(i), dataHolder);
104
				this.handleSingleUnit(state, dataHolder);
105
				
106
				//compare the ABCD elements added in to the CDM and the unhandled ABCD elements
107
				compareABCDtoCDM(sourceName, dataHolder.knownABCDelements, dataHolder);
108
								
109
				//reset the ABCD elements added in CDM
110
				//knownABCDelements = new ArrayList<String>();
111
				dataHolder.allABCDelements = new HashMap<String,String>();
112
			}
113
		}
114

    
115
		return;
116

    
117
	}
118
	
119
	/*
120
	 * Store the unit with its Gathering informations in the CDM
121
	 */
122
	private void handleSingleUnit(Abcd206ImportState state, Abcd206DataHolder dataHolder){
123
		
124
		Abcd206ImportConfigurator config = state.getConfig();
125
		
126
		TransactionStatus tx = startTransaction();
127
		try {
128
			updateProgress(state, "Importing data for unit: " + dataHolder.unitID);
129
			
130
//			Reference sec = Database.NewInstance();
131
//			sec.setTitleCache("XML DATA");
132
			Reference sec = config.getTaxonReference();
133

    
134
			//create facade
135
			DerivedUnitFacade derivedUnitFacade = getFacade(dataHolder);
136
			
137
			
138
			//handle identifications
139
			handleIdentifications(config, derivedUnitFacade, sec, dataHolder);
140

    
141
			//handle collection data
142
			setCollectionData(config, dataHolder, derivedUnitFacade);
143

    
144
			/**
145
			 * GATHERING EVENT
146
			 */
147

    
148
			//gathering event
149
			UnitsGatheringEvent unitsGatheringEvent = new UnitsGatheringEvent(
150
					getTermService(), dataHolder.locality, dataHolder.languageIso, dataHolder.longitude, 
151
					dataHolder.latitude, dataHolder.gatheringAgentList);
152
			
153
			//country
154
			UnitsGatheringArea unitsGatheringArea = 
155
				new UnitsGatheringArea(dataHolder.isocountry, dataHolder.country, getOccurrenceService());
156
			NamedArea areaCountry = unitsGatheringArea.getArea();
157
			
158
			//other areas
159
			unitsGatheringArea = new UnitsGatheringArea(dataHolder.namedAreaList);
160
			ArrayList<NamedArea> nas = unitsGatheringArea.getAreas();
161
			for (NamedArea namedArea : nas){
162
				unitsGatheringEvent.addArea(namedArea);
163
			}
164
			
165
			//copy gathering event to facade
166
			GatheringEvent gatheringEvent = unitsGatheringEvent.getGatheringEvent();
167
			derivedUnitFacade.setLocality(gatheringEvent.getLocality());
168
			derivedUnitFacade.setExactLocation(gatheringEvent.getExactLocation());
169
			derivedUnitFacade.setCollector(gatheringEvent.getCollector());
170
			derivedUnitFacade.setCountry(areaCountry);
171
			derivedUnitFacade.addCollectingAreas(unitsGatheringArea.getAreas());
172
			
173
			//TODO exsiccatum
174
			
175
			
176
			//add fieldNumber
177
			derivedUnitFacade.setFieldNumber(dataHolder.fieldNumber);
178
			
179
			//join gatheringEvent to fieldObservation
180

    
181
//			//add Multimedia URLs
182
			if(dataHolder.multimediaObjects.size() > 0){
183
				for (String multimediaObject : dataHolder.multimediaObjects){
184
					Media media = getImageMedia(multimediaObject, true);
185
					derivedUnitFacade.addDerivedUnitMedia(media);
186
				}
187
			}
188
			
189
			/**
190
			 * SAVE AND STORE DATA
191
			 */			
192
			getTermService().save(areaCountry);//TODO save area sooner
193
			for (NamedArea area : nas){
194
				getTermService().save(area);//save it sooner (foreach area)
195
			}
196
			getTermService().saveLanguageData(unitsGatheringEvent.getLocality());//TODO needs to be saved ?? save it sooner
197
			
198
			getOccurrenceService().save(derivedUnitFacade.innerDerivedUnit());
199
			logger.info("saved ABCD specimen ...");
200

    
201

    
202
		} catch (Exception e) {
203
			logger.warn("Error when reading record!!");
204
			e.printStackTrace();
205
			state.setUnsuccessfull();
206
		}
207
		commitTransaction(tx);
208

    
209
		return;
210
	}
211

    
212
	private void setCollectionData(Abcd206ImportConfigurator config,
213
			Abcd206DataHolder dataHolder, DerivedUnitFacade derivedUnitFacade) {
214
		//set catalogue number (unitID)
215
		derivedUnitFacade.setCatalogNumber(dataHolder.unitID);
216
		derivedUnitFacade.setAccessionNumber(dataHolder.accessionNumber);
217
//		derivedUnitFacade.setCollectorsNumber(dataHolder.collectorsNumber);
218

    
219

    
220
		/**
221
		 * INSTITUTION & COLLECTION
222
		 */
223
		//manage institution
224
		Institution institution = this.getInstitution(dataHolder.institutionCode, config, dataHolder);
225
		//manage collection
226
		Collection collection = this.getCollection(dataHolder.collectionCode, institution, config, dataHolder); 
227
		//link specimen & collection
228
		derivedUnitFacade.setCollection(collection);
229
	}
230

    
231

    
232
	private DerivedUnitFacade getFacade(Abcd206DataHolder dataHolder) {
233
		/**
234
		 * SPECIMEN OR OBSERVATION OR LIVING
235
		 */
236
//			DerivedUnitBase derivedThing = null;
237
		DerivedUnitType type = null;
238
		
239
		//create specimen
240
		if (dataHolder.recordBasis != null){
241
			if (dataHolder.recordBasis.toLowerCase().startsWith("s")) {//specimen
242
				type = DerivedUnitType.Specimen;
243
			}else if (dataHolder.recordBasis.toLowerCase().startsWith("o")) {//observation
244
				type = DerivedUnitType.Observation;	
245
			}else if (dataHolder.recordBasis.toLowerCase().startsWith("l")) {//living -> fossil, herbarium sheet....???
246
				type = DerivedUnitType.LivingBeing;
247
			}
248
			if (type == null){
249
				logger.info("The basis of record does not seem to be known: " + dataHolder.recordBasis);
250
				type = DerivedUnitType.DerivedUnit;
251
			}
252
			//TODO fossils?
253
		}else{
254
			logger.info("The basis of record is null");
255
			type = DerivedUnitType.DerivedUnit;
256
		}
257
		DerivedUnitFacade derivedUnitFacade = DerivedUnitFacade.NewInstance(type);
258
		return derivedUnitFacade;
259
	}
260

    
261

    
262
	/*
263
	 * Return the list of root nodes for an ABCD 2.06 XML file
264
	 * @param fileName: the file's location
265
	 * @return the list of root nodes ("Unit")
266
	 */
267
	private static NodeList getUnitsNodeList(URI urlFileName){
268
		NodeList unitList = null;
269
		try {
270
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
271
			DocumentBuilder builder = factory.newDocumentBuilder();
272
			URL url = urlFileName.toURL();
273
			Object o = url.getContent();
274
			InputStream is = (InputStream)o;
275
			Document document = builder.parse(is);
276
			Element root = document.getDocumentElement();
277
			unitList = root.getElementsByTagName("Unit");
278

    
279
		}catch(Exception e){
280
			logger.warn(e);
281
		}
282
		return unitList;
283
	}
284

    
285

    
286
	/*
287
	 * Store the unit's properties into variables
288
	 * Look which unit is the preferred one
289
	 * Look what kind of name it is supposed to be, for the parsing (Botanical, Zoological)
290
	 * @param racine: the root node for a single unit
291
	 */
292
	private void setUnitPropertiesXML(Element root, Abcd206DataHolder dataHolder){
293
		try{
294
			NodeList group;
295
			
296
//			try{afficherInfos(racine, 0);}catch (Exception e) {logger.info(e);}
297
			group = root.getChildNodes();
298
//			logger.info("ABCD ELEMENT not stored: "+group.item(i).getNodeName().toString()+" - value: "+group.item(i).getTextContent());
299
			for (int i = 0; i < group.getLength(); i++){
300
				if (group.item(i).getNodeName().equals("Identifications")){
301
					group = group.item(i).getChildNodes();
302
					break;
303
				}
304
			}
305
			dataHolder.identificationList = new ArrayList<String>();
306
			dataHolder.atomisedIdentificationList = new ArrayList<HashMap<String, String>>();
307
			dataHolder.referenceList = new ArrayList<String>();
308
			dataHolder.multimediaObjects = new ArrayList<String>();
309

    
310
			this.getScientificNames(group, dataHolder);
311

    
312
//			logger.info("this.identificationList "+this.identificationList.toString());
313
			this.getIDs(root, dataHolder);
314
			this.getRecordBasis(root, dataHolder);
315
			this.getMultimedia(root, dataHolder);
316
			this.getNumbers(root, dataHolder);
317
			this.getGeolocation(root, dataHolder);
318
			this.getGatheringPeople(root, dataHolder);
319

    
320
		} catch (Exception e) {
321
			logger.info("Error occured while parsing XML file"+e);
322
		}
323
	}
324

    
325
	String path= "";
326
	private void getHierarchie(Node node){
327
		while (node != null && node.getNodeName() != "DataSets"){
328
//			logger.info(node.getParentNode().getNodeName());
329
			path = node.getParentNode().getNodeName()+"/"+path; 
330
			node = node.getParentNode();
331
		}
332
//		logger.info("path: "+path);
333
	}
334

    
335
	private void getScientificNames(NodeList group, Abcd206DataHolder dataHolder){
336
		NodeList identifications,results;
337
		String tmpName = null;
338
		for (int j=0; j< group.getLength(); j++){
339
			if(group.item(j).getNodeName().equals("Identification")){
340
				dataHolder.nomenclatureCode ="";
341
				identifications = group.item(j).getChildNodes();
342
				for (int m=0; m<identifications.getLength();m++){
343
					if(identifications.item(m).getNodeName().equals("Result")){
344
						results = identifications.item(m).getChildNodes();
345
						for(int k=0; k<results.getLength();k++){
346
							if (results.item(k).getNodeName().equals("TaxonIdentified")){
347
								tmpName=this.getScientificName(results.item(k), dataHolder);
348
							}
349
						}
350
					}else if(identifications.item(m).getNodeName().equals("PreferredFlag")){
351
						if (dataHolder.nomenclatureCode != null && dataHolder.nomenclatureCode !=""){
352
							dataHolder.identificationList.add(tmpName+"_preferred_"+identifications.item(m).getTextContent()+"_code_" + dataHolder.nomenclatureCode);
353
						}else{
354
							dataHolder.identificationList.add(tmpName+"_preferred_"+identifications.item(m).getTextContent());
355
						}
356
						path=identifications.item(m).getNodeName();
357
						getHierarchie(identifications.item(m));
358
						dataHolder.knownABCDelements.add(path);
359
						path="";
360
					}else if (identifications.item(m).getNodeName().equals("References")){
361
						this.getReferences(identifications.item(m), dataHolder);
362
					}
363
				}
364
			}
365
		}
366
		boolean hasPref=false;
367
		for (int j=0; j< group.getLength(); j++){
368
			if(group.item(j).getNodeName().equals("Identification")){
369
				dataHolder.nomenclatureCode ="";
370
				identifications = group.item(j).getChildNodes();
371
				for (int m=0; m<identifications.getLength();m++){
372
					if(identifications.item(m).getNodeName().equals("Result")){
373
						results = identifications.item(m).getChildNodes();
374
						for(int k=0; k<results.getLength();k++){
375
							if (results.item(k).getNodeName().equals("TaxonIdentified")){
376
								tmpName=this.getScientificName(results.item(k), dataHolder);
377
							}
378
						}
379
					}
380
					if(identifications.item(m).getNodeName().equals("PreferredFlag")){
381
						hasPref=true;
382
					}
383
				}
384
				if ( !hasPref && tmpName != null){
385
					if (dataHolder.nomenclatureCode != null && dataHolder.nomenclatureCode !=""){
386
						dataHolder.identificationList.add(tmpName+"_preferred_"+"0"+"_code_" + dataHolder.nomenclatureCode);
387
					} else {
388
						dataHolder.identificationList.add(tmpName+"_preferred_"+"0");
389
					}
390
				}
391
			}
392
		}
393
	}
394

    
395

    
396

    
397
	private void getReferences(Node result, Abcd206DataHolder dataHolder){
398
		NodeList results,reference;
399
		results = result.getChildNodes();
400
		for(int k=0; k<results.getLength();k++){
401
			if (results.item(k).getNodeName().equals("Reference")){
402
				reference = results.item(k).getChildNodes();
403
				for(int l=0;l<reference.getLength();l++){
404
					if (reference.item(l).getNodeName().equals("TitleCitation")){
405
						path = reference.item(l).getNodeName();
406
						dataHolder.referenceList.add(reference.item(l).getTextContent());
407
						getHierarchie(reference.item(l));
408
						dataHolder.knownABCDelements.add(path);
409
						path="";
410
					}
411
				}
412
			}
413
		}
414
	}
415

    
416
	private String getScientificName(Node result, Abcd206DataHolder dataHolder){
417
		NodeList taxonsIdentified, scnames, atomised;
418
		String tmpName = "";
419
		dataHolder.atomisedStr = "";
420
		taxonsIdentified = result.getChildNodes();
421
		for (int l=0; l<taxonsIdentified.getLength(); l++){
422
			if (taxonsIdentified.item(l).getNodeName().equals("ScientificName")){
423
				scnames = taxonsIdentified.item(l).getChildNodes();
424
				for (int n=0;n<scnames.getLength();n++){
425
					if (scnames.item(n).getNodeName().equals("FullScientificNameString")){
426
						path=scnames.item(n).getNodeName();
427
						tmpName = scnames.item(n).getTextContent();
428
						getHierarchie(scnames.item(n));
429
						dataHolder.knownABCDelements.add(path);
430
						path="";
431
					}
432
					if (scnames.item(n).getNodeName().equals("NameAtomised")){
433
						try {
434
							if (scnames.item(n).hasChildNodes()){
435
								dataHolder.nomenclatureCode = scnames.item(n).getChildNodes().item(1).getNodeName();
436
							}
437
						} catch (Exception e) {
438
							dataHolder.nomenclatureCode ="";
439
						}
440
						atomised = scnames.item(n).getChildNodes().item(1).getChildNodes();
441
						dataHolder.atomisedIdentificationList.add(this.getAtomisedNames(dataHolder.nomenclatureCode, atomised, dataHolder));
442
					}
443
				}
444
			}
445
		}
446
		return tmpName;
447
	}
448

    
449
	private HashMap<String,String> getAtomisedNames(String code, NodeList atomised, Abcd206DataHolder dataHolder){
450
		if (code.equals("Botanical")){
451
			return this.getAtomisedBotanical(atomised, dataHolder);
452
		}
453
		if (code.equals("Bacterial")){
454
			return this.getAtomisedBacterial(atomised, dataHolder);
455
		}
456
		if (code.equals("NameViral")){
457
			return this.getAtomisedViral(atomised, dataHolder);
458
		}
459
		if (code.equals("NameZoological")){
460
			return this.getAtomisedZoological(atomised,dataHolder);
461
		}
462
		return new HashMap<String,String>();
463
	}
464

    
465
	private HashMap<String,String> getAtomisedZoological(NodeList atomised, Abcd206DataHolder dataHolder){
466
		HashMap<String,String> atomisedMap = new HashMap<String,String>();
467
		for (int i=0;i<atomised.getLength();i++){
468
			if(atomised.item(i).getNodeName().equals("GenusOrMonomial")){
469
				atomisedMap.put("Genus",atomised.item(i).getTextContent()); 
470
				path=atomised.item(i).getNodeName();
471
				getHierarchie(atomised.item(i));
472
				dataHolder.knownABCDelements.add(path);
473
				path="";	
474
			}
475
			if(atomised.item(i).getNodeName().equals("Subgenus")){
476
				atomisedMap.put("Subgenus",atomised.item(i).getTextContent());  
477
				path=atomised.item(i).getNodeName();
478
				getHierarchie(atomised.item(i));
479
				dataHolder.knownABCDelements.add(path);
480
				path="";
481
			}
482
			if(atomised.item(i).getNodeName().equals("SpeciesEpithet")){
483
				atomisedMap.put("SpeciesEpithet",atomised.item(i).getTextContent()); 
484
				path=atomised.item(i).getNodeName();
485
				getHierarchie(atomised.item(i));
486
				dataHolder.knownABCDelements.add(path);
487
				path="";
488
			}
489
			if(atomised.item(i).getNodeName().equals("SubspeciesEpithet")){
490
				atomisedMap.put("SubspeciesEpithet",atomised.item(i).getTextContent()); 
491
				path=atomised.item(i).getNodeName();
492
				getHierarchie(atomised.item(i));
493
				dataHolder.knownABCDelements.add(path);
494
				path="";
495
			}
496
			if(atomised.item(i).getNodeName().equals("AuthorTeamOriginalAndYear")){
497
				atomisedMap.put("AuthorTeamOriginalAndYear",atomised.item(i).getTextContent());  
498
				path=atomised.item(i).getNodeName();
499
				getHierarchie(atomised.item(i));
500
				dataHolder.knownABCDelements.add(path);
501
				path="";
502
			}
503
			if(atomised.item(i).getNodeName().equals("AuthorTeamParenthesisAndYear")){
504
				atomisedMap.put("AuthorTeamParenthesisAndYear",atomised.item(i).getTextContent());  
505
				path=atomised.item(i).getNodeName();
506
				getHierarchie(atomised.item(i));
507
				dataHolder.knownABCDelements.add(path);
508
				path="";
509
			}
510
			if(atomised.item(i).getNodeName().equals("CombinationAuthorTeamAndYear")){
511
				atomisedMap.put("CombinationAuthorTeamAndYear",atomised.item(i).getTextContent());  
512
				path=atomised.item(i).getNodeName();
513
				getHierarchie(atomised.item(i));
514
				dataHolder.knownABCDelements.add(path);
515
				path="";
516
			}
517
			if(atomised.item(i).getNodeName().equals("Breed")){
518
				atomisedMap.put("Breed",atomised.item(i).getTextContent());  
519
				path=atomised.item(i).getNodeName();
520
				getHierarchie(atomised.item(i));
521
				dataHolder.knownABCDelements.add(path);
522
				path="";
523
			}
524
			if(atomised.item(i).getNodeName().equals("NamedIndividual")){
525
				atomisedMap.put("NamedIndividual",atomised.item(i).getTextContent());  
526
				path=atomised.item(i).getNodeName();
527
				getHierarchie(atomised.item(i));
528
				dataHolder.knownABCDelements.add(path);
529
				path="";
530
			}
531
		}
532
		return atomisedMap;
533
	}
534

    
535
	private HashMap<String,String> getAtomisedViral(NodeList atomised, Abcd206DataHolder dataHolder){
536
		HashMap<String,String> atomisedMap = new HashMap<String,String>();
537
		for (int i=0;i<atomised.getLength();i++){
538
			if(atomised.item(i).getNodeName().equals("GenusOrMonomial")){
539
				atomisedMap.put("Genus",atomised.item(i).getTextContent());  
540
				path=atomised.item(i).getNodeName();
541
				getHierarchie(atomised.item(i));
542
				dataHolder.knownABCDelements.add(path);
543
				path="";
544
			}
545
			if(atomised.item(i).getNodeName().equals("ViralSpeciesDesignation")){
546
				atomisedMap.put("ViralSpeciesDesignation", atomised.item(i).getTextContent());  
547
				path=atomised.item(i).getNodeName();
548
				getHierarchie(atomised.item(i));
549
				dataHolder.knownABCDelements.add(path);
550
				path="";
551
			}
552
			if(atomised.item(i).getNodeName().equals("Acronym")){
553
				atomisedMap.put("Acronym",atomised.item(i).getTextContent()); 
554
				path=atomised.item(i).getNodeName();
555
				getHierarchie(atomised.item(i));
556
				dataHolder.knownABCDelements.add(path);
557
				path="";
558
			}
559
		}
560
		return atomisedMap;
561
	}
562

    
563
	private HashMap<String,String> getAtomisedBotanical(NodeList atomised, Abcd206DataHolder dataHolder){
564
		HashMap<String,String> atomisedMap = new HashMap<String,String>();
565
		for (int i=0;i<atomised.getLength();i++){
566
			if(atomised.item(i).getNodeName().equals("GenusOrMonomial")){
567
				atomisedMap.put("Genus",atomised.item(i).getTextContent());  
568
				path=atomised.item(i).getNodeName();
569
				getHierarchie(atomised.item(i));
570
				dataHolder.knownABCDelements.add(path);
571
				path="";
572
			}
573
			if(atomised.item(i).getNodeName().equals("FirstEpithet")){
574
				atomisedMap.put("FirstEpithet",atomised.item(i).getTextContent()); 
575
				path=atomised.item(i).getNodeName();
576
				getHierarchie(atomised.item(i));
577
				dataHolder.knownABCDelements.add(path);
578
				path="";
579
			}
580
			if(atomised.item(i).getNodeName().equals("InfraspecificEpithet")){
581
				atomisedMap.put("InfraSpeEpithet", atomised.item(i).getTextContent()); 
582
				path=atomised.item(i).getNodeName();
583
				getHierarchie(atomised.item(i));
584
				dataHolder.knownABCDelements.add(path);
585
				path="";
586
			}
587
			if(atomised.item(i).getNodeName().equals("Rank")){
588
				atomisedMap.put("Rank",atomised.item(i).getTextContent()); 
589
				path=atomised.item(i).getNodeName();
590
				getHierarchie(atomised.item(i));
591
				dataHolder.knownABCDelements.add(path);
592
				path="";
593
			}
594
			if(atomised.item(i).getNodeName().equals("HybridFlag")){
595
				atomisedMap.put("HybridFlag",atomised.item(i).getTextContent()); 
596
				path=atomised.item(i).getNodeName();
597
				getHierarchie(atomised.item(i));
598
				dataHolder.knownABCDelements.add(path);
599
				path="";
600
			}
601
			if(atomised.item(i).getNodeName().equals("AuthorTeamParenthesis")){
602
				atomisedMap.put("AuthorTeamParenthesis",atomised.item(i).getTextContent()); 
603
				path=atomised.item(i).getNodeName();
604
				getHierarchie(atomised.item(i));
605
				dataHolder.knownABCDelements.add(path);
606
				path="";
607
			}
608
			if(atomised.item(i).getNodeName().equals("AuthorTeam")){
609
				atomisedMap.put("AuthorTeam",atomised.item(i).getTextContent()); 
610
				path=atomised.item(i).getNodeName();
611
				getHierarchie(atomised.item(i));
612
				dataHolder.knownABCDelements.add(path);
613
				path="";
614
			}
615
			if(atomised.item(i).getNodeName().equals("CultivarGroupName")){
616
				atomisedMap.put("CultivarGroupName",atomised.item(i).getTextContent()); 
617
				path=atomised.item(i).getNodeName();
618
				getHierarchie(atomised.item(i));
619
				dataHolder.knownABCDelements.add(path);
620
				path="";
621
			}
622
			if(atomised.item(i).getNodeName().equals("CultivarName")){
623
				atomisedMap.put("CultivarName",atomised.item(i).getTextContent()); 
624
				path=atomised.item(i).getNodeName();
625
				getHierarchie(atomised.item(i));
626
				dataHolder.knownABCDelements.add(path);
627
				path="";
628
			}
629
			if(atomised.item(i).getNodeName().equals("TradeDesignationNames")){
630
				atomisedMap.put("Trade",atomised.item(i).getTextContent()); 
631
				path=atomised.item(i).getNodeName();
632
				getHierarchie(atomised.item(i));
633
				dataHolder.knownABCDelements.add(path);
634
				path="";
635
			}
636
		}
637
		return atomisedMap;
638
	}
639

    
640
	private HashMap<String,String> getAtomisedBacterial(NodeList atomised, Abcd206DataHolder dataHolder){
641
		HashMap<String,String> atomisedMap = new HashMap<String,String>();
642
		for (int i=0;i<atomised.getLength();i++){
643
			if(atomised.item(i).getNodeName().equals("GenusOrMonomial")){
644
				atomisedMap.put("Genus",atomised.item(i).getTextContent());  
645
				path=atomised.item(i).getNodeName();
646
				getHierarchie(atomised.item(i));
647
				dataHolder.knownABCDelements.add(path);
648
				path="";
649
			}
650
			if(atomised.item(i).getNodeName().equals("Subgenus")){
651
				atomisedMap.put("SubGenus",atomised.item(i).getTextContent());  
652
				path=atomised.item(i).getNodeName();
653
				getHierarchie(atomised.item(i));
654
				dataHolder.knownABCDelements.add(path);
655
				path="";
656
			}
657
			if(atomised.item(i).getNodeName().equals("SubgenusAuthorAndYear")){
658
				atomisedMap.put("SubgenusAuthorAndYear",atomised.item(i).getTextContent());  
659
				path=atomised.item(i).getNodeName();
660
				getHierarchie(atomised.item(i));
661
				dataHolder.knownABCDelements.add(path);
662
				path="";
663
			}
664
			if(atomised.item(i).getNodeName().equals("SpeciesEpithet")){
665
				atomisedMap.put("SpeciesEpithet",atomised.item(i).getTextContent());  
666
				path=atomised.item(i).getNodeName();
667
				getHierarchie(atomised.item(i));
668
				dataHolder.knownABCDelements.add(path);
669
				path="";
670
			}
671
			if(atomised.item(i).getNodeName().equals("SubspeciesEpithet")){
672
				atomisedMap.put("SubspeciesEpithet",atomised.item(i).getTextContent());  
673
				path=atomised.item(i).getNodeName();
674
				getHierarchie(atomised.item(i));
675
				dataHolder.knownABCDelements.add(path);
676
				path="";
677
			}
678
			if(atomised.item(i).getNodeName().equals("ParentheticalAuthorTeamAndYear")){
679
				atomisedMap.put("ParentheticalAuthorTeamAndYear",atomised.item(i).getTextContent());  
680
				path=atomised.item(i).getNodeName();
681
				getHierarchie(atomised.item(i));
682
				dataHolder.knownABCDelements.add(path);
683
				path="";
684
			}
685
			if(atomised.item(i).getNodeName().equals("AuthorTeamAndYear")){
686
				atomisedMap.put("AuthorTeamAndYear",atomised.item(i).getTextContent());  
687
				path=atomised.item(i).getNodeName();
688
				getHierarchie(atomised.item(i));
689
				dataHolder.knownABCDelements.add(path);
690
				path="";
691
			}
692
			if(atomised.item(i).getNodeName().equals("NameApprobation")){
693
				atomisedMap.put("NameApprobation",atomised.item(i).getTextContent());  
694
				path=atomised.item(i).getNodeName();
695
				getHierarchie(atomised.item(i));
696
				dataHolder.knownABCDelements.add(path);
697
				path="";
698
			}
699
		}
700
		return atomisedMap;
701
	}
702

    
703
	private void getIDs(Element root, Abcd206DataHolder dataHolder){
704
		NodeList group;
705
		try {
706
			group = root.getElementsByTagName("SourceInstitutionID");
707
			path=group.item(0).getNodeName();
708
			getHierarchie(group.item(0));
709
			dataHolder.knownABCDelements.add(path);
710
			path="";
711
			dataHolder.institutionCode = group.item(0).getTextContent();
712
		} catch (NullPointerException e) {
713
			dataHolder.institutionCode= "";
714
		}
715
		try {
716
			group = root.getElementsByTagName("SourceID");
717
			path=group.item(0).getNodeName();
718
			getHierarchie(group.item(0));
719
			dataHolder.knownABCDelements.add(path);
720
			path="";
721
			dataHolder.collectionCode = group.item(0).getTextContent();
722
		} catch (NullPointerException e) {
723
			dataHolder.collectionCode = "";
724
		}
725
		try {
726
			group = root.getElementsByTagName("UnitID");
727
			path=group.item(0).getNodeName();
728
			getHierarchie(group.item(0));
729
			dataHolder.knownABCDelements.add(path);
730
			path="";
731
			dataHolder.unitID = group.item(0).getTextContent();
732
		} catch (NullPointerException e) {
733
			dataHolder.unitID = "";
734
		}
735
	}
736

    
737
	private void getRecordBasis(Element root, Abcd206DataHolder dataHolder){
738
		NodeList group;
739
		try {
740
			group = root.getElementsByTagName("RecordBasis");
741
			path=group.item(0).getNodeName();
742
			getHierarchie(group.item(0));
743
			dataHolder.knownABCDelements.add(path);
744
			path="";
745
			dataHolder.recordBasis = group.item(0).getTextContent();
746
		} catch (NullPointerException e) {
747
			dataHolder.recordBasis = "";
748
		}
749
	}
750

    
751
	private void getMultimedia(Element root, Abcd206DataHolder dataHolder){
752
		NodeList group, multimedias, multimedia;
753
		try {
754
			group = root.getElementsByTagName("MultiMediaObjects");
755
			for(int i=0;i<group.getLength();i++){
756
				multimedias = group.item(i).getChildNodes();
757
				for (int j=0;j<multimedias.getLength();j++){
758
					if (multimedias.item(j).getNodeName().equals("MultiMediaObject")){	
759
						multimedia = multimedias.item(j).getChildNodes();
760
						for (int k=0;k<multimedia.getLength();k++){
761
							if(multimedia.item(k).getNodeName().equals("FileURI")){
762
								dataHolder.multimediaObjects.add(multimedia.item(k).getTextContent());
763
								path = multimedia.item(k).getNodeName();
764
								getHierarchie(multimedia.item(k));
765
								dataHolder.knownABCDelements.add(path);
766
								path="";	
767
							}
768
						}
769
					}
770
				}
771
			}
772
		} catch (NullPointerException e) {
773
			logger.info(e);
774
		}
775
	}
776

    
777
	private void getNumbers(Element root, Abcd206DataHolder dataHolder){
778
		NodeList group;
779
		try {
780
			group = root.getElementsByTagName("AccessionNumber");
781
			path=group.item(0).getNodeName();
782
			getHierarchie(group.item(0));
783
			dataHolder.knownABCDelements.add(path);
784
			path="";	
785
			dataHolder.accessionNumber = group.item(0).getTextContent();
786
		} catch (NullPointerException e) {
787
			dataHolder.accessionNumber = "";
788
		}
789
		try {
790
			group = root.getElementsByTagName("CollectorsFieldNumber");
791
			path=group.item(0).getNodeName();
792
			getHierarchie(group.item(0));
793
			dataHolder.knownABCDelements.add(path);
794
			path="";
795
			dataHolder.fieldNumber = group.item(0).getTextContent();
796
		} catch (NullPointerException e) {
797
			dataHolder.fieldNumber = "";
798
		}
799

    
800
//		try {
801
//			group = root.getElementsByTagName("CollectorsNumber");
802
//			path=group.item(0).getNodeName();
803
//			getHierarchie(group.item(0));
804
//			dataHolder.knownABCDelements.add(path);
805
//			path="";
806
//			dataHolder.collectorsNumber = group.item(0).getTextContent();
807
//		} catch (NullPointerException e) {
808
//			dataHolder.collectorsNumber = "";
809
//		}
810

    
811
		try {
812
			group = root.getElementsByTagName("AccessionNumber");
813
			path=group.item(0).getNodeName();
814
			getHierarchie(group.item(0));
815
			dataHolder.knownABCDelements.add(path);
816
			path="";
817
			dataHolder.accessionNumber = group.item(0).getTextContent();
818
		} catch (NullPointerException e) {
819
			dataHolder.accessionNumber = "";
820
		}
821
	}
822

    
823
	private void getGeolocation(Element root, Abcd206DataHolder dataHolder){
824
		NodeList group, childs;
825
		try {
826
			group = root.getElementsByTagName("LocalityText");
827
			path=group.item(0).getNodeName();
828
			getHierarchie(group.item(0));
829
			dataHolder.knownABCDelements.add(path);
830
			path="";
831
			dataHolder.locality = group.item(0).getTextContent();
832
			if (group.item(0).hasAttributes())
833
				if (group.item(0).getAttributes().getNamedItem("lang") != null)
834
					dataHolder.languageIso = group.item(0).getAttributes().getNamedItem("lang").getTextContent();
835
		} catch (NullPointerException e) {
836
			dataHolder.locality = "";
837
		}
838
		try {
839
			group = root.getElementsByTagName("LongitudeDecimal");
840
			path=group.item(0).getNodeName();
841
			getHierarchie(group.item(0));
842
			dataHolder.knownABCDelements.add(path);
843
			path="";
844
			dataHolder.longitude = Double.valueOf(group.item(0).getTextContent());
845
		} catch (NullPointerException e) {
846
			dataHolder.longitude=null;
847
		}
848
		try {
849
			group = root.getElementsByTagName("LatitudeDecimal");
850
			path=group.item(0).getNodeName();
851
			getHierarchie(group.item(0));
852
			dataHolder.knownABCDelements.add(path);
853
			path="";
854
			dataHolder.latitude = Double.valueOf(group.item(0).getTextContent());
855
		} catch (NullPointerException e) {
856
			dataHolder.latitude=null;
857
		}
858
		try {
859
			group = root.getElementsByTagName("Country");
860
			childs = group.item(0).getChildNodes();
861
			for (int i=0;i<childs.getLength(); i++){
862
				if(childs.item(i).getNodeName() == "Name"){
863
					path=childs.item(i).getNodeName();
864
					getHierarchie(childs.item(i));
865
					dataHolder.knownABCDelements.add(path);
866
					path="";
867
					dataHolder.country = childs.item(i).getTextContent();
868
				}
869
			}
870
		} catch (NullPointerException e) {
871
			dataHolder.country = "";
872
		}
873
		try {
874
			group = root.getElementsByTagName("Country");
875
			childs = group.item(0).getChildNodes();
876
			for (int i=0;i<childs.getLength(); i++){
877
				if(childs.item(i).getNodeName() == "ISO3166Code"){
878
					path=childs.item(i).getNodeName();
879
					getHierarchie(childs.item(i));
880
					dataHolder.knownABCDelements.add(path);
881
					path="";
882
					dataHolder.isocountry = childs.item(i).getTextContent();
883
				}
884
			}
885
		} catch (NullPointerException e) {
886
			dataHolder.isocountry = "";
887
		}
888
		try {
889
			group = root.getElementsByTagName("Altitude");
890
			for (int i=0;i<group.getLength();i++){
891
				childs = group.item(i).getChildNodes();
892
				for (int j=0;j<childs.getLength();j++){
893
					if (childs.item(j).getNodeName().equals("MeasurementOrFactText")){
894
						path=childs.item(j).getNodeName();
895
						getHierarchie(childs.item(j));
896
						dataHolder.knownABCDelements.add(path);
897
						path="";
898
						dataHolder.altitude = Integer.valueOf(childs.item(j).getTextContent());
899
					}
900
				}
901
			}
902
		} catch (NullPointerException e) {
903
			dataHolder.altitude = -9999;
904
		}
905

    
906
		try {
907
			group = root.getElementsByTagName("Depth");
908
			path=group.item(0).getNodeName();
909
			getHierarchie(group.item(0));
910
			dataHolder.knownABCDelements.add(path);
911
			path="";
912
			dataHolder.depth = Integer.valueOf(group.item(0).getTextContent());
913
		} catch (NullPointerException e) {
914
			dataHolder.depth = -9999;
915
		}
916

    
917
		try{
918
			group = root.getElementsByTagName("NamedArea");
919
			dataHolder.namedAreaList = new ArrayList<String>();
920
			for (int i=0;i<group.getLength();i++){
921
				childs = group.item(i).getChildNodes();
922
				for (int j=0; j<childs.getLength();j++){
923
					if (childs.item(j).getNodeName().equals("AreaName")){
924
						path = childs.item(j).getNodeName();
925
						getHierarchie(childs.item(j));
926
						dataHolder.knownABCDelements.add(path);
927
						path="";
928
						dataHolder.namedAreaList.add(childs.item(j).getTextContent());
929
					}
930
				}
931
			}
932
		}catch(NullPointerException e){
933
			dataHolder.namedAreaList = new ArrayList<String>();
934
		}
935
	}
936

    
937
	private void getGatheringPeople(Element root, Abcd206DataHolder dataHolder){
938
		NodeList group, childs, person;
939
		try {
940
			group = root.getElementsByTagName("GatheringAgent");
941
			dataHolder.gatheringAgentList = new ArrayList<String>();
942
			for (int i=0; i< group.getLength(); i++){
943
				childs = group.item(i).getChildNodes();
944
				for (int j=0; j<childs.getLength();j++){
945
					if (childs.item(j).getNodeName().equals("Person")){
946
						person = childs.item(j).getChildNodes();
947
						for (int k=0; k<person.getLength(); k++){
948
							if (person.item(k).getNodeName().equals("FullName")){
949
								path=person.item(k).getNodeName();
950
								getHierarchie(person.item(k));
951
								dataHolder.knownABCDelements.add(path);
952
								path="";
953
								dataHolder.gatheringAgentList.add(person.item(k).getTextContent());
954
							}
955
						}
956
					}
957

    
958
				}
959
			}
960
		} catch (NullPointerException e) {
961
			dataHolder.gatheringAgentList = new ArrayList<String>();
962
		}
963
	}
964

    
965
	private Institution getInstitution(String institutionCode, Abcd206ImportConfigurator config, Abcd206DataHolder dataHolder){
966
		Institution institution;
967
		List<Institution> institutions;
968
		try{
969
			logger.info(dataHolder.institutionCode);
970
			institutions = getAgentService().searchInstitutionByCode(dataHolder.institutionCode);
971
		}catch(Exception e){
972
			institutions=new ArrayList<Institution>();
973
		}
974
		if (institutions.size() ==0 || !config.isReUseExistingMetadata()){
975
			logger.info("Institution (agent) unknown or not allowed to reuse existing metadata");
976
			//create institution
977
			institution = Institution.NewInstance();
978
			institution.setCode(dataHolder.institutionCode);				
979
		}
980
		else{
981
			logger.info("Institution (agent) already in the db");
982
			institution = institutions.get(0);
983
		}
984
		return institution;
985
	}
986

    
987
	/*
988
	 * Look if the Collection does already exists
989
	 * @param collectionCode: a string
990
	 * @param institution: the current Institution
991
	 * @param app
992
	 * @return the Collection (existing or new)
993
	 */
994
	private Collection getCollection(String collectionCode, Institution institution, Abcd206ImportConfigurator config, Abcd206DataHolder dataHolder){
995
		Collection collection = Collection.NewInstance();
996
		List<Collection> collections;
997
		try{
998
			collections = getCollectionService().searchByCode(dataHolder.collectionCode);
999
		}catch(Exception e){
1000
			collections=new ArrayList<Collection>();
1001
		}
1002
		if (collections.size() ==0 || !config.isReUseExistingMetadata()){
1003
			logger.info("Collection not found or do not reuse existing metadata  " + dataHolder.collectionCode);
1004
			//create new collection
1005
			collection.setCode(dataHolder.collectionCode);
1006
			collection.setCodeStandard("GBIF");
1007
			collection.setInstitute(institution);
1008
		}
1009
		else{
1010
			boolean collectionFound=false;
1011
			for (int i=0; i<collections.size(); i++){
1012
				collection = collections.get(i);
1013
				try {
1014
					if (collection.getInstitute().getCode().equalsIgnoreCase(institution.getCode())){ 
1015
						//found a collection with the same code and the same institution
1016
						collectionFound=true;
1017
					}
1018
				} catch (NullPointerException e) {}
1019
			}
1020
			if (!collectionFound){ 
1021
				collection.setCode(dataHolder.collectionCode);
1022
				collection.setCodeStandard("GBIF");
1023
				collection.setInstitute(institution);
1024
			}
1025

    
1026
		}
1027
		return collection;
1028
	}
1029

    
1030
	/*
1031
	 * 
1032
	 * @param app
1033
	 * @param derivedThing
1034
	 * @param sec
1035
	 */
1036
	private void handleIdentifications(Abcd206ImportConfigurator config, DerivedUnitFacade facade, Reference sec, Abcd206DataHolder dataHolder){
1037
		NonViralName<?> taxonName = null;
1038
		String fullScientificNameString;
1039
		Taxon taxon = null;
1040
		DeterminationEvent determinationEvent = null;
1041
		List<TaxonBase> names = null;
1042

    
1043
		String scientificName="";
1044
		boolean preferredFlag=false;
1045

    
1046
		for (int i = 0; i < dataHolder.identificationList.size(); i++) {
1047
			fullScientificNameString = dataHolder.identificationList.get(i);
1048
			fullScientificNameString = fullScientificNameString.replaceAll(" et ", " & ");
1049
			if (fullScientificNameString.indexOf("_preferred_") != -1){
1050
				scientificName = fullScientificNameString.split("_preferred_")[0];
1051
				String pTmp = fullScientificNameString.split("_preferred_")[1].split("_code_")[0];
1052
				if (pTmp.equals("1") || pTmp.toLowerCase().indexOf("true") != -1){
1053
					preferredFlag=true;
1054
				} else { 
1055
					preferredFlag=false;
1056
				}
1057
			}
1058
			else{ 
1059
				scientificName = fullScientificNameString;
1060
			}
1061
			logger.info(fullScientificNameString);
1062
			if (fullScientificNameString.indexOf("_code_") != -1){	
1063
				dataHolder.nomenclatureCode = fullScientificNameString.split("_code_")[1];
1064
			}
1065
			if (config.isDoAutomaticParsing() || dataHolder.atomisedIdentificationList == null || dataHolder.atomisedIdentificationList.size()==0){	
1066
				taxonName = this.parseScientificName(scientificName, dataHolder);	
1067
			} else {
1068
				if (dataHolder.atomisedIdentificationList != null || dataHolder.atomisedIdentificationList.size()>0){
1069
					taxonName = this.setTaxonNameByType(dataHolder.atomisedIdentificationList.get(i), scientificName, dataHolder);
1070
				}
1071
			}
1072
			if(taxonName == null){
1073
				taxonName = NonViralName.NewInstance(null);
1074
				taxonName.setFullTitleCache(scientificName);
1075
			}
1076
			
1077
			// --- cascade through several options in order to find an appropriate taxon ---
1078
			
1079
			if (config.isDoMatchTaxa()){
1080
				taxon = getTaxonService().findBestMatchingTaxon(scientificName);
1081
			} 
1082
			
1083
			if (taxon == null && config.isDoReUseTaxon()){
1084
				try{
1085
					names = getTaxonService().searchTaxaByName(scientificName, sec);
1086
					taxon = (Taxon)names.get(0);
1087
				} catch(Exception e){
1088
					taxon=null;
1089
				}
1090
			} else {			
1091
				logger.info("Matching to existing Taxon : " + taxon.getTitleCache());
1092
			}
1093

    
1094
			if (!config.isDoReUseTaxon() || taxon == null){
1095
				getNameService().save(taxonName);
1096
				taxon = Taxon.NewInstance(taxonName, sec); //TODO sec set null
1097
			}
1098

    
1099
//			taxonName = NonViralName.NewInstance(null);
1100
//			taxonName.setFullTitleCache(scientificName);
1101
			
1102
			// --- taxon is found now ---
1103
			
1104
			determinationEvent = DeterminationEvent.NewInstance();
1105
			determinationEvent.setTaxon(taxon);
1106
			determinationEvent.setPreferredFlag(preferredFlag);
1107
			
1108
			for (String strReference : dataHolder.referenceList){
1109
				
1110
				Reference reference = ReferenceFactory.newGeneric();
1111
				reference.setTitleCache(strReference, true);
1112
				determinationEvent.addReference(reference);
1113
			}
1114
			facade.addDetermination(determinationEvent);
1115
			
1116
			if(config.isDoCreateIndividualsAssociations()){
1117
				TaxonDescription taxonDescription = null;
1118
				if(config.isDoMatchToExistingDescription()){
1119
					logger.error("The import option 'DoMatchToExistingDescription' is not yet implemented.");
1120
				} else {
1121
					UUID taxonDescriptionUUID = config.getTaxonToDescriptionMap().get(taxon.getUuid()); // rather put in state
1122
					taxonDescription = (TaxonDescription) getDescriptionService().load(taxonDescriptionUUID);
1123
					if(taxonDescription == null){
1124
						taxonDescription = TaxonDescription.NewInstance(taxon);
1125
						config.getTaxonToDescriptionMap().put(taxon.getUuid(), taxonDescription.getUuid());
1126
						if(taxonDescriptionUUID == null){
1127
							logger.info("Creating new TaxonDescription for " + taxon.getTitleCache());
1128
						} else {
1129
							logger.fatal("TaxonDescription with UUID " + taxonDescriptionUUID + " not found --> creating a new one.");					
1130
						}
1131
					}
1132
				}
1133
				IndividualsAssociation individualsAssociation = IndividualsAssociation.NewInstance();
1134
				individualsAssociation.setAssociatedSpecimenOrObservation(facade.innerDerivedUnit());
1135
				individualsAssociation.setFeature(Feature.INDIVIDUALS_ASSOCIATION());
1136
				for(Reference citation : determinationEvent.getReferences()){
1137
					individualsAssociation.addSource(DescriptionElementSource.NewInstance(null, null, citation, null));
1138
				}
1139
				taxonDescription.addElement(individualsAssociation);
1140
				getDescriptionService().save(taxonDescription);
1141
			}
1142
		}
1143

    
1144
	}
1145

    
1146
	private NonViralName<?> parseScientificName(String scientificName, Abcd206DataHolder dataHolder){
1147
		NonViralNameParserImpl nvnpi = NonViralNameParserImpl.NewInstance();
1148
		NonViralName<?>taxonName = null;
1149
		boolean problem=false;
1150

    
1151
		if (dataHolder.nomenclatureCode.toString().equals("Zoological")){
1152
			taxonName = (ZoologicalName)nvnpi.parseFullName(scientificName,NomenclaturalCode.ICZN,null);
1153
			if (taxonName.hasProblem()){
1154
				problem=true;
1155
			}
1156
		}
1157
		if (dataHolder.nomenclatureCode.toString().equals("Botanical")){
1158
			taxonName  = (BotanicalName)nvnpi.parseFullName(scientificName,NomenclaturalCode.ICBN,null);
1159
			if (taxonName.hasProblem()){
1160
				problem=true;;
1161
			}
1162
		}
1163
		if (dataHolder.nomenclatureCode.toString().equals("Bacterial")){
1164
			taxonName = (BacterialName)nvnpi.parseFullName(scientificName,NomenclaturalCode.ICNB, null);
1165
			if (taxonName.hasProblem()){
1166
				problem=true;
1167
			}
1168
		}
1169
		if (dataHolder.nomenclatureCode.toString().equals("Cultivar")){
1170
			taxonName = (CultivarPlantName)nvnpi.parseFullName(scientificName,NomenclaturalCode.ICNCP, null);
1171
			if (taxonName.hasProblem()){
1172
				problem=true;
1173
			}
1174
		}
1175
//		if (this.nomenclatureCode.toString().equals("Viral")){
1176
//		ViralName taxonName = (ViralName)nvnpi.parseFullName(scientificName,NomenclaturalCode.ICVCN(), null);
1177
//		if (taxonName.hasProblem())
1178
//		logger.info("pb ICVCN");
1179
//		}
1180
		//TODO: parsing of ViralNames?
1181
		if(problem){
1182
			taxonName = NonViralName.NewInstance(null);
1183
			taxonName.setTitleCache(scientificName, true);
1184
		}
1185
		return taxonName;
1186

    
1187
	}
1188

    
1189
	private NonViralName<?> setTaxonNameByType(HashMap<String, String> atomisedMap,String fullName, Abcd206DataHolder dataHolder){
1190
		if (dataHolder.nomenclatureCode.equals("Zoological")){
1191
			NonViralName<ZoologicalName> taxonName  = ZoologicalName.NewInstance(null); 
1192
			taxonName.setFullTitleCache(fullName, true);
1193
			taxonName.setGenusOrUninomial(getFromMap(atomisedMap,"Genus"));
1194
			taxonName.setInfraGenericEpithet(getFromMap(atomisedMap,"SubGenus"));
1195
			taxonName.setSpecificEpithet(getFromMap(atomisedMap,"SpeciesEpithet"));
1196
			taxonName.setInfraSpecificEpithet(getFromMap(atomisedMap,"SubspeciesEpithet"));
1197
			Team team = null;
1198
			if(getFromMap(atomisedMap,"AuthorTeamParenthesis") != null){
1199
				team = Team.NewInstance();
1200
				team.setTitleCache(getFromMap(atomisedMap,"AuthorTeamParenthesis"), true);
1201
			}else{
1202
				if (getFromMap(atomisedMap,"AuthorTeamAndYear") != null){
1203
					team = Team.NewInstance();
1204
					team.setTitleCache(getFromMap(atomisedMap,"AuthorTeamAndYear"), true);
1205
				}
1206
			}
1207
			if(team != null){
1208
				taxonName.setBasionymAuthorTeam(team);
1209
			}else{
1210
				if(getFromMap(atomisedMap,"AuthorTeamParenthesis") != null){
1211
					taxonName.setAuthorshipCache(getFromMap(atomisedMap,"AuthorTeamParenthesis"));
1212
				} else if (getFromMap(atomisedMap,"AuthorTeamAndYear") != null){
1213
					taxonName.setAuthorshipCache(getFromMap(atomisedMap,"AuthorTeamAndYear"));
1214
				}
1215
			}
1216
			if(getFromMap(atomisedMap,"CombinationAuthorTeamAndYear") != null){
1217
				team = Team.NewInstance();
1218
				team.setTitleCache(getFromMap(atomisedMap,"CombinationAuthorTeamAndYear"), true);
1219
				taxonName.setCombinationAuthorTeam(team);
1220
			}
1221
			if (taxonName.hasProblem()){
1222
				logger.info("pb ICZN");
1223
			}else{
1224
				return taxonName;
1225
			}
1226
		}
1227
		if (dataHolder.nomenclatureCode.equals("Botanical")){
1228
			NonViralName<BotanicalName> taxonName  = BotanicalName.NewInstance(null);
1229
			taxonName.setFullTitleCache(fullName, true);
1230
			taxonName.setGenusOrUninomial(getFromMap(atomisedMap,"Genus"));
1231
			taxonName.setInfraGenericEpithet(getFromMap(atomisedMap,"FirstEpithet"));
1232
			taxonName.setInfraSpecificEpithet(getFromMap(atomisedMap,"InfraSpeEpithet"));
1233
			try{taxonName.setRank(Rank.getRankByName(getFromMap(atomisedMap,"Rank")));
1234
			}catch(Exception e){}
1235
			Team team = null;
1236
			if(getFromMap(atomisedMap,"AuthorTeamParenthesis") != null){
1237
				team = Team.NewInstance();
1238
				team.setTitleCache(getFromMap(atomisedMap,"AuthorTeamParenthesis"), true);
1239
				if(team != null){
1240
					taxonName.setBasionymAuthorTeam(team);
1241
				}
1242
			}
1243
			if (getFromMap(atomisedMap,"AuthorTeam") != null){
1244
				team = Team.NewInstance();
1245
				team.setTitleCache(getFromMap(atomisedMap,"AuthorTeam"), true);
1246
				if(team != null){
1247
					taxonName.setCombinationAuthorTeam(team);
1248
				}
1249
			}
1250
			if (team == null)	{
1251
				if(getFromMap(atomisedMap,"AuthorTeamParenthesis") != null){
1252
					taxonName.setAuthorshipCache(getFromMap(atomisedMap,"AuthorTeamParenthesis"));
1253
				}else if (getFromMap(atomisedMap,"AuthorTeam") != null){
1254
					taxonName.setAuthorshipCache(getFromMap(atomisedMap,"AuthorTeam"));
1255
				}
1256
			}
1257
			if(getFromMap(atomisedMap,"CombinationAuthorTeamAndYear") != null){
1258
				team = Team.NewInstance();
1259
				team.setTitleCache(getFromMap(atomisedMap,"CombinationAuthorTeamAndYear"), true);
1260
				taxonName.setCombinationAuthorTeam(team);
1261
			}
1262
			if (taxonName.hasProblem()){
1263
				logger.info("pb ICBN");
1264
			}else {
1265
				return taxonName;
1266
			}
1267
		}
1268
		if (dataHolder.nomenclatureCode.equals("Bacterial")){
1269
			NonViralName<BacterialName> taxonName = BacterialName.NewInstance(null);
1270
			taxonName.setFullTitleCache(fullName, true);
1271
			taxonName.setGenusOrUninomial(getFromMap(atomisedMap,"Genus"));
1272
			taxonName.setInfraGenericEpithet(getFromMap(atomisedMap,"SubGenus"));
1273
			taxonName.setSpecificEpithet(getFromMap(atomisedMap,"Species"));
1274
			taxonName.setInfraSpecificEpithet(getFromMap(atomisedMap,"SubspeciesEpithet"));
1275
			if(getFromMap(atomisedMap,"AuthorTeamAndYear") != null){
1276
				Team team = Team.NewInstance();
1277
				team.setTitleCache(getFromMap(atomisedMap,"AuthorTeamAndYear"), true);
1278
				taxonName.setCombinationAuthorTeam(team);
1279
			}
1280
			if(getFromMap(atomisedMap,"ParentheticalAuthorTeamAndYear") != null){
1281
				Team team = Team.NewInstance();
1282
				team.setTitleCache(getFromMap(atomisedMap,"ParentheticalAuthorTeamAndYear"), true);
1283
				taxonName.setBasionymAuthorTeam(team);
1284
			}
1285
			if (taxonName.hasProblem()){
1286
				logger.info("pb ICNB");
1287
			}else{
1288
				return taxonName;
1289
			}
1290
		}
1291
		if (dataHolder.nomenclatureCode.equals("Cultivar")){
1292
			CultivarPlantName taxonName = CultivarPlantName.NewInstance(null);
1293

    
1294
			if (taxonName.hasProblem()){
1295
				logger.info("pb ICNCP");
1296
			}else {
1297
				return taxonName;
1298
			}
1299
		}
1300
//		if (this.nomenclatureCode.equals("Viral")){
1301
//		ViralName taxonName = ViralName.NewInstance(null);
1302
//		taxonName.setFullTitleCache(fullName, true);
1303
//		taxonName.setAcronym(getFromMap(atomisedMap,"Acronym"));
1304
//		if (taxonName.hasProblem())
1305
//		logger.info("pb ICVCN");
1306
//		else return taxonName;
1307
//		}
1308
		//TODO ViralName
1309
		NonViralName<?>taxonName = NonViralName.NewInstance(null);
1310
		taxonName.setFullTitleCache(fullName, true);
1311
		return taxonName;
1312
	}
1313

    
1314
	private String getFromMap(HashMap<String, String> atomisedMap, String key){
1315
		String value = null;
1316
		if (atomisedMap.containsKey(key)){
1317
			value = atomisedMap.get(key);
1318
		}
1319
		try{
1320
			if (value != null && key.matches(".*Year.*")){
1321
				value=value.trim();
1322
				if (value.matches("[a-z A-Z ]*[0-9]{4}$")){
1323
					String tmp=value.split("[0-9]{4}$")[0];
1324
					int year = Integer.parseInt(value.split(tmp)[1]);
1325
					if (year >= 1752){
1326
						value=tmp;
1327
					}else{
1328
						value=null;
1329
					}
1330
				}else{
1331
					value=null;
1332
				}
1333
			}
1334
		}catch(Exception e){value=null;}
1335

    
1336
		return value;
1337
	}
1338

    
1339
	private void compareABCDtoCDM(URI urlFileName, ArrayList<String> knownElts, Abcd206DataHolder dataHolder){
1340

    
1341
		try {
1342
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
1343
			DocumentBuilder constructeur = factory.newDocumentBuilder();
1344
			URL url = urlFileName.toURL();
1345
			Object o = url.getContent();
1346
			InputStream is = (InputStream)o;
1347
			Document document = constructeur.parse(is);
1348
			Element root = document.getDocumentElement();
1349
			traverse(root, dataHolder);
1350
		} catch (ParserConfigurationException e) {
1351
			e.printStackTrace();
1352
		} catch (SAXException e) {
1353
			e.printStackTrace();
1354
		} catch (IOException e) {
1355
			e.printStackTrace();
1356
		}
1357
		Set<String> elts = dataHolder.allABCDelements.keySet();
1358
		Iterator< String>it = elts.iterator();
1359
		String elt;
1360
		while (it.hasNext()){
1361
			elt = it.next();
1362
			if (knownElts.indexOf(elt) == -1){
1363
				logger.info("Unsaved ABCD element: " + elt + " - " + dataHolder.allABCDelements.get(elt));
1364
			}
1365
		}
1366
	}
1367
	
1368
	
1369

    
1370
	/**
1371
	 * Traverses the tree for compareABCDtoCDM
1372
	 * @param node
1373
	 * @param dataHolder
1374
	 */
1375
	private void traverse(Node node, Abcd206DataHolder dataHolder){
1376
		// Extract node info:
1377
		String test = node.getTextContent();
1378

    
1379
		// Print and continue traversing.
1380
		if(test != null && test != "#text" && node.getNodeName() != "#text" && test.split("\n").length==1 && test.length()>0){
1381
			path=node.getNodeName();
1382
			getHierarchie(node);
1383
			dataHolder.allABCDelements.put(path,test);
1384
			path="";
1385
		}
1386
		// Now traverse the rest of the tree in depth-first order.
1387
		if (node.hasChildNodes()) {
1388
			// Get the children in a list.
1389
			NodeList nl = node.getChildNodes();
1390
			// How many of them?
1391
			int size = nl.getLength();
1392
			for (int i=0; i<size; i++){
1393
				// Recursively traverse each of the children.
1394
				traverse (nl.item(i), dataHolder);
1395
			}
1396
		}
1397
	}
1398

    
1399

    
1400

    
1401
	@Override
1402
	protected boolean isIgnore(Abcd206ImportState state) {
1403
		//return ! config.isDoNameFacts();
1404
		return false;
1405
	}
1406

    
1407

    
1408

    
1409
}
(2-2/4)