Project

General

Profile

Download (68.3 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.sdd.in;
11

    
12
import java.io.File;
13
import java.net.MalformedURLException;
14
import java.net.URI;
15
import java.net.URL;
16
import java.text.SimpleDateFormat;
17
import java.util.ArrayList;
18
import java.util.Date;
19
import java.util.HashMap;
20
import java.util.HashSet;
21
import java.util.List;
22
import java.util.Map;
23
import java.util.Set;
24
import java.util.UUID;
25

    
26
import org.apache.commons.lang.StringUtils;
27
import org.apache.log4j.Logger;
28
import org.jdom.Element;
29
import org.jdom.Namespace;
30
import org.joda.time.DateTime;
31
import org.springframework.stereotype.Component;
32
import org.springframework.transaction.TransactionStatus;
33

    
34
import eu.etaxonomy.cdm.api.service.IDescriptionService;
35
import eu.etaxonomy.cdm.common.media.ImageInfo;
36
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
37
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
38
import eu.etaxonomy.cdm.io.common.ICdmImport;
39
import eu.etaxonomy.cdm.io.common.ImportHelper;
40
import eu.etaxonomy.cdm.io.common.XmlImportBase;
41
import eu.etaxonomy.cdm.io.sdd.SDDTransformer;
42
import eu.etaxonomy.cdm.model.agent.Person;
43
import eu.etaxonomy.cdm.model.agent.Team;
44
import eu.etaxonomy.cdm.model.common.Annotation;
45
import eu.etaxonomy.cdm.model.common.AnnotationType;
46
import eu.etaxonomy.cdm.model.common.CdmBase;
47
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
48
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
49
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
50
import eu.etaxonomy.cdm.model.common.Language;
51
import eu.etaxonomy.cdm.model.common.LanguageString;
52
import eu.etaxonomy.cdm.model.common.Marker;
53
import eu.etaxonomy.cdm.model.common.MarkerType;
54
import eu.etaxonomy.cdm.model.common.Representation;
55
import eu.etaxonomy.cdm.model.common.TermBase;
56
import eu.etaxonomy.cdm.model.common.TermVocabulary;
57
import eu.etaxonomy.cdm.model.common.VersionableEntity;
58
import eu.etaxonomy.cdm.model.description.CategoricalData;
59
import eu.etaxonomy.cdm.model.description.Feature;
60
import eu.etaxonomy.cdm.model.description.FeatureNode;
61
import eu.etaxonomy.cdm.model.description.FeatureTree;
62
import eu.etaxonomy.cdm.model.description.MeasurementUnit;
63
import eu.etaxonomy.cdm.model.description.Modifier;
64
import eu.etaxonomy.cdm.model.description.QuantitativeData;
65
import eu.etaxonomy.cdm.model.description.State;
66
import eu.etaxonomy.cdm.model.description.StateData;
67
import eu.etaxonomy.cdm.model.description.StatisticalMeasure;
68
import eu.etaxonomy.cdm.model.description.StatisticalMeasurementValue;
69
import eu.etaxonomy.cdm.model.description.TaxonDescription;
70
import eu.etaxonomy.cdm.model.description.TextData;
71
import eu.etaxonomy.cdm.model.description.WorkingSet;
72
import eu.etaxonomy.cdm.model.location.NamedArea;
73
import eu.etaxonomy.cdm.model.media.IdentifiableMediaEntity;
74
import eu.etaxonomy.cdm.model.media.ImageFile;
75
import eu.etaxonomy.cdm.model.media.Media;
76
import eu.etaxonomy.cdm.model.media.MediaRepresentation;
77
import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
78
import eu.etaxonomy.cdm.model.media.Rights;
79
import eu.etaxonomy.cdm.model.name.NonViralName;
80
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
81
import eu.etaxonomy.cdm.model.occurrence.Specimen;
82
import eu.etaxonomy.cdm.model.reference.Reference;
83
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
84
import eu.etaxonomy.cdm.model.taxon.Classification;
85
import eu.etaxonomy.cdm.model.taxon.Synonym;
86
import eu.etaxonomy.cdm.model.taxon.Taxon;
87
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
88

    
89
/**
90
 * @author h.fradin
91
 * @created 24.10.2008
92
 * @version 1.0
93
 */
94
@Component("sddImport")
95
public class SDDImport extends XmlImportBase<SDDImportConfigurator, SDDImportState> implements ICdmImport<SDDImportConfigurator, SDDImportState> {
96
	private static final Logger logger = Logger.getLogger(SDDImport.class);
97

    
98
	private static int modCount = 1000;
99

    
100
	private UUID uuidAnnotationTypeLocation = UUID.fromString("a3737e07-72e3-46d2-986d-fa4cf5de0b63");
101
	
102
	
103
	private Map<String,Person> authors = new HashMap<String,Person>();
104
	private Map<String,String> citations = new HashMap<String,String>();
105
	private Map<String,String> defaultUnitPrefixes = new HashMap<String,String>();
106
	private Map<String,Person> editors = new HashMap<String,Person>();
107
	private Map<String,FeatureNode> featureNodes = new HashMap<String,FeatureNode>();
108
	private Map<String,Feature> features = new HashMap<String,Feature>();
109
	private Map<String,String> locations = new HashMap<String,String>();
110
	private Map<String,List<CdmBase>> mediaObject_ListCdmBase = new HashMap<String,List<CdmBase>>();
111
	private Map<String,String> mediaObject_Role = new HashMap<String,String>();
112
	private Map<String,Reference<?>> publications = new HashMap<String,Reference<?>>();
113
	private Map<String,State> states = new HashMap<String,State>();
114
	private Map<String,TaxonDescription> taxonDescriptions = new HashMap<String,TaxonDescription>();
115
	private Map<String,NonViralName<?>> taxonNameBases = new HashMap<String,NonViralName<?>>();
116
	private Map<String,MeasurementUnit> units = new HashMap<String,MeasurementUnit>();
117
	private Map<String,TaxonNode> taxonNodes = new HashMap<String,TaxonNode>();
118
	private Map<String,NamedArea> namedAreas = new HashMap<String,NamedArea>();
119
	private Map<String,Specimen> specimens = new HashMap<String,Specimen>();
120
	private Map<String,Modifier> modifiers = new HashMap<String,Modifier>();
121
	
122
	private Set<MarkerType> markerTypes = new HashSet<MarkerType>();
123
	private Set<TermVocabulary<?>> vocabularies = new HashSet<TermVocabulary<?>>();
124

    
125
	private Set<Feature> descriptiveConcepts = new HashSet<Feature>();
126
	private Set<AnnotationType> annotationTypes = new HashSet<AnnotationType>();
127
//	private Set<Feature> featureSet = new HashSet<Feature>();
128
	private Set<Reference<?>> sources = new HashSet<Reference<?>>();
129
	private Reference<?> sec = ReferenceFactory.newDatabase();
130
	private Reference<?> sourceReference = null;
131

    
132
	private Language datasetLanguage = null;
133
	private WorkingSet workingSet = null;
134

    
135
	private Namespace xmlNamespace = Namespace.getNamespace("xml","http://www.w3.org/XML/1998/namespace");
136

    
137
	private String generatorName = "";
138
	private String generatorVersion = "";
139
	
140

    
141
	private Set<StatisticalMeasure> statisticalMeasures = new HashSet<StatisticalMeasure>();
142
	private Set<VersionableEntity> featureData = new HashSet<VersionableEntity>();
143
	private Set<FeatureTree> featureTrees = new HashSet<FeatureTree>();
144
	private Set<Classification> classifications = new HashSet<Classification>();
145

    
146
	private Rights copyright = null;
147

    
148
	private int taxonNamesCount = 0;
149
	
150
	public SDDImport(){
151
		super();
152
	}
153

    
154
	@Override
155
	public boolean doCheck(SDDImportState state){
156
		boolean result = true;
157
		logger.warn("No check implemented for SDD");
158
		return result;
159
	}
160

    
161
	//	@Override
162
	//	public boolean doInvoke(IImportConfigurator config, Map<String, MapWrapper<? extends CdmBase>> stores){
163
	@Override
164
	public void doInvoke(SDDImportState state){
165
		
166
		TransactionStatus ts = startTransaction();
167
		SDDImportConfigurator sddConfig = state.getConfig();
168
		IProgressMonitor progressMonitor = sddConfig.getProgressMonitor();
169
				
170
		logger.info("start Datasets ...");
171
		
172
		// <Datasets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://rs.tdwg.org/UBIF/2006/" xsi:schemaLocation="http://rs.tdwg.org/UBIF/2006/ ../SDD.xsd">
173
		Element root = sddConfig.getSourceRoot();
174
		Namespace sddNamespace = sddConfig.getSddNamespace();
175

    
176
		logger.info("start TechnicalMetadata ...");
177
		// <TechnicalMetadata created="2006-04-20T10:00:00">
178
		importTechnicalMetadata(root, sddNamespace, sddConfig);
179
		List<Element> elDatasets = root.getChildren("Dataset",sddNamespace);
180
//		int i = 0;
181

    
182
		//for each Dataset
183
		logger.info("start Dataset ...");
184
		progressMonitor.beginTask("Importing SDD data", elDatasets.size());
185
		for (Element elDataset : elDatasets){
186
			importDataset(elDataset, sddNamespace, state);			
187
//			if ((++i % modCount) == 0){ logger.info("dataset(s) handled: " + i);}
188
//			logger.info(i + " dataset(s) handled");
189
			progressMonitor.worked(1);
190
		}
191
		commitTransaction(ts);
192
		progressMonitor.done();
193
		logger.info("End of transaction");
194
		return;
195
	}
196

    
197
	/* (non-Javadoc)
198
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
199
	 */
200
	protected boolean isIgnore(SDDImportState state){
201
		return false;
202
	}
203

    
204

    
205
	// associates the reference of a media object in SDD with a CdmBase Object
206
	protected void associateImageWithCdmBase(String refMO, CdmBase cb){
207
		if ((refMO != null) && (cb!=null)) {
208
			if (! refMO.equals("")) {
209
				if (! mediaObject_ListCdmBase.containsKey(refMO)) {
210
					List<CdmBase> lcb = new ArrayList<CdmBase>();
211
					lcb.add(cb);
212
					mediaObject_ListCdmBase.put(refMO,lcb);
213
				} else {
214
					List<CdmBase> lcb = mediaObject_ListCdmBase.get(refMO);
215
					lcb.add(cb);
216
					mediaObject_ListCdmBase.put(refMO,lcb);
217
				}
218
			}
219
		}
220
	}
221

    
222
	// imports information about the Dataset
223
	protected void importDatasetRepresentation(Element parent, Namespace sddNamespace){
224
		logger.info("start Representation ...");
225
		/* <Representation>
226
			<Label>The Genus Viola</Label>
227
			<Detail>This is an example for a very simple SDD file, representing a single description with categorical, quantitative, and text character. Compare also the "Fragment*" examples, which contain more complex examples in the form of document fragments. Intended for version="SDD 1.1".</Detail>
228
	       </Representation>
229
		 */
230

    
231
		
232
		
233
		Element elRepresentation = parent.getChild("Representation",sddNamespace);
234
		String label = (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace);
235
		String detail = (String)ImportHelper.getXmlInputValue(elRepresentation, "Detail",sddNamespace);
236
		
237
		//new
238
		Representation representation = Representation.NewInstance(detail, label, null, datasetLanguage);
239
		workingSet.addRepresentation(representation);
240
		
241

    
242
		//old
243
//		sec.setTitleCache(label, true);
244
//
245
//		if (detail != null) {
246
//			Annotation annotation = Annotation.NewInstance(detail, datasetLanguage);
247
//			annotation.setAnnotationType(AnnotationType.EDITORIAL());
248
//			sec.addAnnotation(annotation);
249
//		}
250
		
251

    
252
		List<Element> listMediaObjects = elRepresentation.getChildren("MediaObject",sddNamespace);
253

    
254
		for (Element elMediaObject : listMediaObjects) {
255
			String ref = null;
256
			String role = null;
257
			if (elMediaObject != null) {
258
				ref = elMediaObject.getAttributeValue("ref");
259
				role = elMediaObject.getAttributeValue("role");
260
			}
261
			if (ref != null) {
262
				if (!ref.equals("")) {
263
					this.associateImageWithCdmBase(ref,sourceReference);
264
					this.associateImageWithCdmBase(ref,sec);
265
					mediaObject_Role.put(ref,role);
266
				}
267
			}
268
		}
269
	}
270

    
271
	// imports the representation (label, detail, lang) of a particular SDD element
272
	protected void importRepresentation(Element parent, Namespace sddNamespace, VersionableEntity ve, String id, SDDImportState state){
273
		Element elRepresentation = parent.getChild("Representation",sddNamespace);
274
		
275
		Map<Language,List<String>> langLabDet = new HashMap<Language,List<String>>();
276

    
277
		handleRepresentationLabels(sddNamespace, elRepresentation, langLabDet);
278
		handleRepresentationDetails(sddNamespace, elRepresentation, langLabDet);
279

    
280
		if (ve instanceof TermBase) {
281
			makeRepresentationForTerms((TermBase)ve, langLabDet);
282
		}else if (ve instanceof Media) {
283
			makeRepresentationForMedia((Media)ve, langLabDet);
284
		}else if (ve instanceof IdentifiableEntity<?>) {
285
			IdentifiableEntity<?> ie = (IdentifiableEntity<?>)ve;
286
			makeRepresentationForIdentifiableEntity(sddNamespace, ie, elRepresentation, langLabDet);
287
			if (ve instanceof IdentifiableMediaEntity<?>){
288
				makeRepresentationForIdentifiableMediaEntity(parent, sddNamespace, (IdentifiableMediaEntity<?>)ve);
289
			}
290
		}
291

    
292
		makeRepresentationMediaObjects(sddNamespace, ve, elRepresentation);//FIXME
293

    
294
	}
295

    
296

    
297
	/**
298
	 * Handles the "Detail" children of representations. Adds the result to the langLabDet.
299
	 * @param sddNamespace
300
	 * @param elRepresentation
301
	 * @param langLabDet
302
	 */
303
	private void handleRepresentationDetails(Namespace sddNamespace,
304
			Element elRepresentation, Map<Language, List<String>> langLabDet) {
305
		List<Element> listDetails = elRepresentation.getChildren("Detail",sddNamespace);
306
		for (Element elDetail : listDetails){
307
			Language language = getLanguage(elDetail);
308
			String role = elDetail.getAttributeValue("role");
309
			String detail = elDetail.getText();
310
			List<String> labDet = langLabDet.get(language);
311
			labDet.add(detail);
312
			labDet.add(role);
313
			langLabDet.put(language, labDet);
314
		}
315
	}
316

    
317
	/**
318
	 * Handles the "Label" children of representations. Adds the result to the langLabDet.
319
	 * @param sddNamespace
320
	 * @param elRepresentation
321
	 * @param langLabDet
322
	 */
323
	private void handleRepresentationLabels(Namespace sddNamespace,
324
				Element elRepresentation, Map<Language, List<String>> langLabDet) {
325
		// <Label xml:lang="la">Viola hederacea Labill.</Label>
326
		List<Element> listLabels = elRepresentation.getChildren("Label",sddNamespace);
327
		for (Element elLabel : listLabels){
328
			Language language = getLanguage(elLabel);
329
			String label = elLabel.getText();
330
			List<String> labDet = new ArrayList<String>(3);
331
			labDet.add(label);
332
			langLabDet.put(language, labDet);
333
		}
334
	}
335
	
336
	/**
337
	 * 
338
	 * @param ve
339
	 * @param langLabDet
340
	 */
341
	private void makeRepresentationForMedia(Media m, Map<Language, List<String>> langLabDet) {
342
		for (Language lang : langLabDet.keySet()){
343
			List<String> labDet = langLabDet.get(lang);
344
			if (labDet.get(0) != null){
345
				m.putTitle(LanguageString.NewInstance(labDet.get(0), lang));
346
			}
347
			if (labDet.size()>1) {
348
				m.addDescription(labDet.get(1), lang);
349
			}
350
		}
351
	}
352

    
353
	/**
354
	 * Handles representations for terms. Adds one representation per language in langLabDet.
355
	 * 
356
	 * @param ve
357
	 * @param langLabDet
358
	 */
359
	private void makeRepresentationForTerms(TermBase tb, Map<Language, List<String>> langLabDet) {
360
			for (Language lang : langLabDet.keySet()){
361
				List<String> labDet = langLabDet.get(lang);
362
				if (labDet.size()>0){
363
					if (labDet.size()>1) {
364
						tb.addRepresentation(Representation.NewInstance(labDet.get(1), labDet.get(0), labDet.get(0), lang));
365
					} else {
366
						tb.addRepresentation(Representation.NewInstance(labDet.get(0), labDet.get(0), labDet.get(0), lang));
367
					}
368
				}
369
			}
370
	}
371

    
372

    
373
	/**
374
	 * Handles the "MediaObject" children of representations.
375
	 * @param sddNamespace
376
	 * @param ve
377
	 * @param elRepresentation
378
	 */
379
	private void makeRepresentationMediaObjects(Namespace sddNamespace,	VersionableEntity ve, Element elRepresentation) {
380
		List <Element> listMediaObjects = elRepresentation.getChildren("MediaObject", sddNamespace);
381
		for (Element elMediaObject : listMediaObjects) {
382
			String ref = null;
383
			//TODO
384
			String role = null;
385
			if (elMediaObject != null) {
386
				ref = elMediaObject.getAttributeValue("ref");
387
				role = elMediaObject.getAttributeValue("role");
388
			}
389
			if (StringUtils.isNotBlank(ref)) {
390
				if (ve instanceof TaxonDescription) {
391
					TaxonDescription td = (TaxonDescription) ve;
392
					if (td.getSources().size() > 0) {
393
						this.associateImageWithCdmBase(ref, td.getSources().iterator().next().getCitation());
394
					} else {
395
						Reference<?> descriptionSource = ReferenceFactory.newGeneric();
396
						sources.add(descriptionSource);
397
						td.addSource(null, null, descriptionSource, null);
398
						this.associateImageWithCdmBase(ref,descriptionSource);
399
					}
400
				} else {
401
					this.associateImageWithCdmBase(ref,ve);
402
				}
403
			}
404
		}
405
	}
406

    
407
	/**
408
	 * Handles the "Links" element
409
	 * @param parent
410
	 * @param sddNamespace
411
	 * @param ve
412
	 */
413
	private void makeRepresentationForIdentifiableMediaEntity(Element parent,
414
			Namespace sddNamespace, IdentifiableMediaEntity ime) {
415
		Element elLinks = parent.getChild("Links",sddNamespace);
416

    
417
		if (elLinks != null) {
418

    
419
			//  <Link rel="Alternate" href="http://www.diversitycampus.net/people/hagedorn"/>
420
			List<Element> listLinks = elLinks.getChildren("Link", sddNamespace);
421
			Media link = Media.NewInstance();
422
			MediaRepresentation mr = MediaRepresentation.NewInstance();
423
			int k = 0;
424
			//for each Link
425
			for (Element elLink : listLinks){
426

    
427
				try {
428
					//TODO
429
					String rel = elLink.getAttributeValue("rel");
430
					String href = elLink.getAttributeValue("href");
431
					URI uri = new URI(href);
432
					mr.addRepresentationPart(MediaRepresentationPart.NewInstance(uri, null));
433
					link.addRepresentation(mr);
434
					ime.addMedia(link);
435

    
436
				} catch (Exception e) {
437
					//FIXME
438
					logger.warn("Import of Link " + k + " failed.");
439
				}
440

    
441
				if ((++k % modCount) == 0){ logger.info("Links handled: " + k);}
442

    
443
			}
444
		}
445
	}
446

    
447
	/**
448
	 * @param sddNamespace
449
	 * @param ve
450
	 * @param elRepresentation
451
	 * @param langLabDet
452
	 * @return
453
	 */
454
	private void makeRepresentationForIdentifiableEntity(Namespace sddNamespace, IdentifiableEntity<?> ie, 
455
					Element elRepresentation, Map<Language, List<String>> langLabDet) {
456
		List<String> labDet = null;
457

    
458
		if (ie instanceof TaxonNameBase) {
459
			if (langLabDet.keySet().contains(getTermService().getLanguageByIso("la"))) {
460
				labDet = langLabDet.get(getTermService().getLanguageByIso("la"));
461
			} else if (langLabDet.keySet().contains(datasetLanguage)) {
462
				labDet = langLabDet.get(datasetLanguage);
463
				logger.info("TaxonName " + (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace) + " is not specified as a latin name.");
464
			} else {
465
				labDet = langLabDet.get(langLabDet.keySet().iterator().next());
466
				logger.info("TaxonName " + (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace) + " is not specified as a latin name.");
467
			}
468
		} else {
469
			labDet = langLabDet.get(langLabDet.keySet().iterator().next());
470
		}
471

    
472
		//FIXME labDet is != null only for TaxonNameBase
473
		ie.setTitleCache(labDet.get(0), true);
474

    
475
		if (labDet.size()>1) {
476
			Annotation annotation = null;
477
			if (labDet.get(1) != null) {
478
				if (labDet.get(2) != null) {
479
					annotation = Annotation.NewInstance(labDet.get(2) + " - " + labDet.get(1), datasetLanguage);
480
				} else {
481
					annotation = Annotation.NewInstance(labDet.get(1), datasetLanguage);
482
				}
483
			}
484
			ie.addAnnotation(annotation);
485
		}
486
		return;
487
	}
488

    
489
	/**
490
	 * @param elLabel
491
	 * @return
492
	 */
493
	private Language getLanguage(Element elLanguage) {
494
		String lang = elLanguage.getAttributeValue("lang",xmlNamespace);
495
		Language language = null;
496
		if (StringUtils.isNotBlank(lang)) {
497
			language = getTermService().getLanguageByIso(lang.substring(0, 2));
498
		} else {
499
			language = datasetLanguage;
500
		}
501
		return language;
502
	}
503
	
504

    
505
	// imports the representation (label, detail, lang) of a particular SDD element
506
	protected void importTechnicalMetadata(Element root, Namespace sddNamespace, SDDImportConfigurator sddConfig){
507
		Element elTechnicalMetadata = root.getChild("TechnicalMetadata", sddNamespace);
508
		String nameCreated = elTechnicalMetadata.getAttributeValue("created");
509
		sourceReference = sddConfig.getSourceReference();
510

    
511
		if (nameCreated != null) {
512
			if (!nameCreated.equals("")) {
513
				int year = Integer.parseInt(nameCreated.substring(0,4));
514
				int monthOfYear = Integer.parseInt(nameCreated.substring(5,7));
515
				int dayOfMonth = Integer.parseInt(nameCreated.substring(8,10));
516
				int hourOfDay = Integer.parseInt(nameCreated.substring(11,13));
517
				int minuteOfHour = Integer.parseInt(nameCreated.substring(14,16));
518
				int secondOfMinute = Integer.parseInt(nameCreated.substring(17,19));
519
				DateTime created = new DateTime(year,monthOfYear,dayOfMonth,hourOfDay,minuteOfHour,secondOfMinute,0);
520
				sourceReference.setCreated(created);
521
				sec.setCreated(created);
522
			}
523
		}
524

    
525
		// <Generator name="n/a, handcrafted instance document" version="n/a"/>
526
		Element elGenerator = elTechnicalMetadata.getChild("Generator", sddNamespace);
527
		generatorName = elGenerator.getAttributeValue("name");
528
		generatorVersion = elGenerator.getAttributeValue("version");
529

    
530
		sec.addAnnotation(Annotation.NewDefaultLanguageInstance(generatorName + " - " + generatorVersion));
531
		sourceReference.addAnnotation(Annotation.NewDefaultLanguageInstance(generatorName + " - " + generatorVersion));
532

    
533
	}
534

    
535
	// imports the complete dataset information
536
	protected void importDataset(Element elDataset, Namespace sddNamespace, SDDImportState state){			// <Dataset xml:lang="en-us">
537
		
538
		workingSet = WorkingSet.NewInstance();
539
		importDatasetLanguage(elDataset,state);
540
		importDatasetRepresentation(elDataset, sddNamespace);
541
		importRevisionData(elDataset, sddNamespace);
542
		importIPRStatements(elDataset, sddNamespace, state);
543
		importTaxonNames(elDataset, sddNamespace, state);
544

    
545
		importDescriptiveConcepts(elDataset, sddNamespace, state);
546
		importCharacters(elDataset, sddNamespace, state);
547
		importCharacterTrees(elDataset, sddNamespace, state);
548
		
549
		MarkerType editorMarkerType = getMarkerType(state, SDDTransformer.uuidMarkerEditor, "editor", "Editor", "edt");
550
		MarkerType geographicAreaMarkerType = getMarkerType(state, SDDTransformer.uuidMarkerSDDGeographicArea, "SDDGeographicArea", "SDDGeographicArea", "ga"); 
551
		MarkerType descriptiveConceptMarkerType = getMarkerType(state, SDDTransformer.uuidMarkerDescriptiveConcept, "DescriptiveConcept", "Descriptive Concept", "DC");
552
		markerTypes.add(editorMarkerType);
553
		markerTypes.add(geographicAreaMarkerType);
554
		markerTypes.add(descriptiveConceptMarkerType);
555
		
556
		//saving of all imported data into the CDM db
557
		saveVocabularies();
558
		saveFeatures();
559
		saveModifiers();
560
		saveStates();
561
		saveMarkerType();
562
		saveAreas(geographicAreaMarkerType);		
563
		saveUnits();
564
		saveStatisticalMeasure();		
565
		saveAnnotationType();
566

    
567
		importCodedDescriptions(elDataset, sddNamespace, state);
568
		importAgents(elDataset, sddNamespace, state);
569
		importPublications(elDataset, sddNamespace, state);
570
		importMediaObjects(elDataset, sddNamespace, state);
571
		importTaxonHierarchies(elDataset, sddNamespace, state);
572
		importGeographicAreas(elDataset, sddNamespace, state);
573
		importSpecimens(elDataset,sddNamespace, state);
574
		
575
		
576
		if ((authors != null)||(editors != null)) {
577
			Team team = Team.NewInstance();
578
			if (authors != null) {
579
				for (Person author : authors.values()){
580
					team.addTeamMember(author);
581
				}
582
			}
583
			if (editors != null) {
584
				Marker marker = Marker.NewInstance();
585
				marker.setMarkerType(editorMarkerType);
586
				for (Person editor : editors.values()){
587
					Person edit = editor;
588
					edit.addMarker(marker);
589
					team.addTeamMember(edit);
590
				}
591
			}
592
			sec.setAuthorTeam(team);
593
			sourceReference.setAuthorTeam(team);
594
		}
595

    
596
		if (copyright != null) {
597
			sourceReference.addRights(copyright);
598
			sec.addRights(copyright);
599
		}
600
		
601
		// Returns a CdmApplicationController created by the values of this configuration.
602
		IDescriptionService descriptionService = getDescriptionService();
603
		
604
		for (TaxonDescription taxonDescription : taxonDescriptions.values()){
605
			// Persists a Description
606
			descriptionService.save(taxonDescription);
607
		}
608
		
609
		for (String ref : taxonDescriptions.keySet()){
610
			TaxonDescription td = taxonDescriptions.get(ref);
611
			if (citations.containsKey(ref)) {
612
				Reference<?> publication = publications.get(citations.get(ref));
613
				if (locations.containsKey(ref)) {
614
					Annotation location = Annotation.NewInstance(locations.get(ref), datasetLanguage);
615
					//TODO move to a generic place (implemented in hurry therefore dirty)
616
					AnnotationType annotationType = getAnnotationType(state, uuidAnnotationTypeLocation, "location", "location", "location", null);
617
//					annotationTypes.add(annotationType);  TODO necessary??
618
					location.setAnnotationType(annotationType);
619
					(publication).addAnnotation(location);
620
				}
621
				td.addSource(null, null,publication, null);
622
			}
623
		}
624
		logger.info("end makeTaxonDescriptions ...");
625

    
626
		if (descriptiveConcepts != null) {
627
			for (Feature feature : descriptiveConcepts) {
628
				Marker marker = Marker.NewInstance();
629
				marker.setMarkerType(descriptiveConceptMarkerType);
630
				feature.addMarker(marker);
631
			}
632
		}
633
		saveFeatures();
634
		
635
		for (Reference<?> publication : publications.values()){
636
			getReferenceService().save(publication); 
637
		}
638
		
639
		for (Reference<?> source : sources){
640
			getReferenceService().save(source); 
641
		}
642

    
643
		for (FeatureTree featureTree : featureTrees) {
644
			getFeatureTreeService().save(featureTree);
645
		}
646
		getWorkingSetService().save(workingSet);
647
		for (Classification classification : classifications) {
648
			getClassificationService().save(classification);
649
		}
650
		for (Specimen specimen : specimens.values()) {
651
			getOccurrenceService().save(specimen);
652
		}
653
		logger.info("end of persistence ...");
654
		
655
		return;
656
	}
657

    
658
	/**
659
	 * 
660
	 */
661
	private void saveVocabularies() {
662
		for (TermVocabulary<?> vocabulary : vocabularies ){
663
			getVocabularyService().save(vocabulary);
664
		}
665
		
666
	}
667

    
668
	private void saveAnnotationType() {
669
		for (AnnotationType annotationType: annotationTypes){
670
			getTermService().saveOrUpdate(annotationType); 
671
		}
672
	}
673

    
674
	private void saveStatisticalMeasure() {
675
		for (StatisticalMeasure sm : statisticalMeasures){
676
			getTermService().save(sm); 
677
		}
678
	}
679

    
680
	private void saveUnits() {
681
		if (units != null) {
682
			for (MeasurementUnit unit : units.values()){
683
				if (unit != null) {
684
					getTermService().save(unit); 
685
				}
686
			}
687
		}
688
	}
689

    
690
	private void saveAreas(MarkerType geographicAreaMarkerType) {
691
		for (NamedArea area : namedAreas.values() ){
692
			Marker marker = Marker.NewInstance();
693
			marker.setMarkerType(geographicAreaMarkerType);
694
			area.addMarker(marker);
695
			getTermService().save(area);
696
		}
697
	}
698

    
699
	private void saveStates() {
700
		for (State state : states.values() ){
701
			getTermService().save(state);
702
		}
703
	}
704

    
705
	private void saveMarkerType() {
706
		for (MarkerType markerType : markerTypes){
707
			getTermService().save(markerType);
708
		}
709
	}
710

    
711
	private void saveModifiers() {
712
		for (Modifier modifier : modifiers.values() ){
713
			getTermService().save(modifier);
714
		}
715
	}
716

    
717
	private void saveFeatures() {
718
		for (Feature feature : features.values() ){
719
			getTermService().save(feature);
720
		}
721
	}
722

    
723
	// imports the default language of the dataset
724
	protected void importDatasetLanguage(Element elDataset, SDDImportState state){
725
		String nameLang = elDataset.getAttributeValue("lang",xmlNamespace);
726

    
727
		if (StringUtils.isNotBlank(nameLang)) {
728
			String iso = nameLang.substring(0, 2);
729
			datasetLanguage = getTermService().getLanguageByIso(iso);
730
		} else {
731
			datasetLanguage = Language.DEFAULT();
732
		}
733
		if (datasetLanguage == null) {
734
			datasetLanguage = Language.DEFAULT();
735
		}
736
	}
737
	
738
	// imports the specimens
739
	protected void importSpecimens(Element elDataset, Namespace sddNamespace, SDDImportState cdmState) {
740
		logger.info("start Specimens ...");
741
		/*	<Specimens>
742
        		<Specimen id="sp1">
743
           			<Representation>
744
              			<Label>TJM45337</Label>
745
           			</Representation>
746
        		</Specimen>
747
     		</Specimens>
748
		 */
749
		Element elSpecimens = elDataset.getChild("Specimens",sddNamespace);
750
		if (elSpecimens != null){
751
			List<Element> listSpecimens = elSpecimens.getChildren("Specimen", sddNamespace);
752
			for (Element elSpecimen : listSpecimens) {
753
				String id = elSpecimen.getAttributeValue("id");
754
				Specimen specimen = null;
755
				if (!id.equals("")) {
756
					specimen = Specimen.NewInstance();
757
					specimens.put(id,specimen);
758
					importRepresentation(elSpecimen, sddNamespace, specimen, id, cdmState);
759
				}
760
			}
761

    
762
		}
763
	}
764

    
765
	// imports the revision data associated with the Dataset (authors, modifications)
766
	protected void importRevisionData(Element elDataset, Namespace sddNamespace){
767
		// <RevisionData>
768
		logger.info("start RevisionData ...");
769
		Element elRevisionData = elDataset.getChild("RevisionData",sddNamespace);
770
		if (elRevisionData != null){
771
			// <Creators>
772
			Element elCreators = elRevisionData.getChild("Creators",sddNamespace);
773

    
774
			// <Agent role="aut" ref="a1"/>
775
			List<Element> listAgents = elCreators.getChildren("Agent", sddNamespace);
776

    
777
			int j = 0;
778
			//for each Agent
779
			for (Element elAgent : listAgents){
780

    
781
				String role = elAgent.getAttributeValue("role");
782
				String ref = elAgent.getAttributeValue("ref");
783
				if (role.equals("aut")) {
784
					if(!ref.equals("")) {
785
						authors.put(ref, null);
786
					}
787
				}
788
				if (role.equals("edt")) {
789
					if(!ref.equals("")) {
790
						editors.put(ref, null);
791
					}
792
				}
793
				if ((++j % modCount) == 0){ logger.info("Agents handled: " + j);}
794

    
795
			}
796

    
797
			// <DateModified>2006-04-08T00:00:00</DateModified>
798
			String stringDateModified = (String)ImportHelper.getXmlInputValue(elRevisionData, "DateModified",sddNamespace);
799

    
800
			if (stringDateModified != null) {
801
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");
802
				Date d = null;
803
				try {
804
					d = sdf.parse(stringDateModified);
805
				} catch(Exception e) {
806
					System.err.println("Exception :");
807
					e.printStackTrace();
808
				}
809

    
810
				DateTime updated = null;
811
				if (d != null) {
812
					updated = new DateTime(d);
813
					sourceReference.setUpdated(updated);
814
					sec.setUpdated(updated);
815
				}
816
			}
817
		}
818
	}
819

    
820
	// imports ipr statements associated with a dataset
821
	protected void importIPRStatements(Element elDataset, Namespace sddNamespace, SDDImportState state){
822
		// <IPRStatements>
823
		logger.info("start IPRStatements ...");
824
		Element elIPRStatements = elDataset.getChild("IPRStatements",sddNamespace);
825
		// <IPRStatement role="Copyright">
826
		if (elIPRStatements != null) {
827
			List<Element> listIPRStatements = elIPRStatements.getChildren("IPRStatement", sddNamespace);
828
			int j = 0;
829
			//for each IPRStatement
830

    
831
			for (Element elIPRStatement : listIPRStatements){
832

    
833
				String role = elIPRStatement.getAttributeValue("role");
834
				// <Label xml:lang="en-au">(c) 2003-2006 Centre for Occasional Botany.</Label>
835
				Element elLabel = elIPRStatement.getChild("Label",sddNamespace);
836
				String lang = "";
837
				if (elLabel != null) {
838
					lang = elLabel.getAttributeValue("lang",xmlNamespace);
839
				}
840
				String label = (String)ImportHelper.getXmlInputValue(elIPRStatement, "Label",sddNamespace);
841

    
842
				if (role.equals("Copyright")) {
843
					Language iprLanguage = null;
844
					if (lang != null) {
845
						if (!lang.equals("")) {
846
							iprLanguage = getTermService().getLanguageByIso(lang.substring(0, 2));
847
						} else {
848
							iprLanguage = datasetLanguage;
849
						}
850
					}
851
					if (iprLanguage == null) {
852
						iprLanguage = datasetLanguage;
853
					}
854
					copyright = Rights.NewInstance(label, iprLanguage);
855
				}
856

    
857
				if (copyright != null) {
858
					sourceReference.addRights(copyright);
859
					sec.addRights(copyright);
860
				}
861

    
862
				if ((++j % modCount) == 0){ logger.info("IPRStatements handled: " + j);}
863

    
864
			}
865
		}
866
	}
867

    
868
	// imports the taxon names
869
	protected void importTaxonNames(Element elDataset, Namespace sddNamespace, SDDImportState state){
870
		// <TaxonNames>
871
		logger.info("start TaxonNames ...");
872
		Element elTaxonNames = elDataset.getChild("TaxonNames",sddNamespace);
873
		// <TaxonName id="t1" uri="urn:lsid:authority:namespace:my-own-id">
874
		if (elTaxonNames != null) {
875
			List<Element> listTaxonNames = elTaxonNames.getChildren("TaxonName", sddNamespace);
876
			int j = 0;
877
			//for each TaxonName
878
			for (Element elTaxonName : listTaxonNames){
879

    
880
				String id = elTaxonName.getAttributeValue("id");
881
				String uri = elTaxonName.getAttributeValue("uri");
882

    
883
				NonViralName<?> tnb = null;
884
				if (!id.equals("")) {
885
					tnb = NonViralName.NewInstance(null);
886
					IdentifiableSource source = null;
887
					if (uri != null) {
888
						if (!uri.equals("")) {
889
							source = IdentifiableSource.NewInstance(id, "TaxonName", ReferenceFactory.newGeneric(), uri);
890
						}
891
					} else {
892
						source = IdentifiableSource.NewInstance(id, "TaxonName");
893
					}
894
					tnb.addSource(source);
895
					taxonNameBases.put(id,tnb);
896
				}
897

    
898
				// <Representation>
899
				// <Label xml:lang="la">Viola hederacea Labill.</Label>
900
				importRepresentation(elTaxonName, sddNamespace, tnb, id, state);
901

    
902
				if ((++j % modCount) == 0){ logger.info("TaxonNames handled: " + j);}
903

    
904
			}
905
		}
906
	}
907

    
908
	// imports the characters (categorical, quantitative and text ; sequence characters not supported) which correspond to CDM Features
909
	protected void importCharacters(Element elDataset, Namespace sddNamespace, SDDImportState cdmState){
910
		// <Characters>
911
		logger.info("start Characters ...");
912
		Element elCharacters = elDataset.getChild("Characters", sddNamespace);
913

    
914
		// <CategoricalCharacter id="c1">
915
		if (elCharacters != null) {
916
			handleCategoricalData(sddNamespace, cdmState, elCharacters);
917
			handleQuantitativeData(sddNamespace, cdmState, elCharacters);
918
			handleTextCharacters(sddNamespace, cdmState, elCharacters);
919
		}
920

    
921
		/*for (Iterator<Feature> f = features.values().iterator() ; f.hasNext() ;){
922
			featureSet.add(f.next()); //XIM Why this line ?
923
		}*/
924
		
925
		return;
926

    
927
	}
928

    
929
	/**
930
	 * @param sddNamespace
931
	 * @param sddConfig
932
	 * @param elCharacters
933
	 * @return
934
	 */
935
	private void handleCategoricalData(Namespace sddNamespace, SDDImportState cdmState, Element elCharacters) {
936
		List<Element> elCategoricalCharacters = elCharacters.getChildren("CategoricalCharacter", sddNamespace);
937
		int j = 0;
938
		for (Element elCategoricalCharacter : elCategoricalCharacters){
939
			try {
940

    
941
				String idCC = elCategoricalCharacter.getAttributeValue("id");
942
				Feature categoricalCharacter = Feature.NewInstance();
943
				categoricalCharacter.setKindOf(Feature.DESCRIPTION());
944
				importRepresentation(elCategoricalCharacter, sddNamespace, categoricalCharacter, idCC, cdmState);
945
				categoricalCharacter.setSupportsCategoricalData(true);
946

    
947
				// <States>
948
				Element elStates = elCategoricalCharacter.getChild("States",sddNamespace);
949

    
950
				// <StateDefinition id="s1">
951
				List<Element> elStateDefinitions = elStates.getChildren("StateDefinition",sddNamespace);
952
				TermVocabulary<State> termVocabularyState = TermVocabulary.NewInstance(null, null, null, null);
953
				
954
				vocabularies.add(termVocabularyState);
955
				
956
				int k = 0;
957
				//for each StateDefinition
958
				for (Element elStateDefinition : elStateDefinitions){
959

    
960
					if ((++k % modCount) == 0){ logger.info("StateDefinitions handled: " + (k-1));}
961

    
962
					String idS = elStateDefinition.getAttributeValue("id");
963
					State state = states.get(idS);
964
					if (state == null){
965
						state = State.NewInstance();
966
					}else{
967
						logger.debug("State duplicate found");
968
					}
969
					importRepresentation(elStateDefinition, sddNamespace, state, idS, cdmState);
970

    
971
					termVocabularyState.addTerm(state);
972
					states.put(idS,state);
973
				}
974
				categoricalCharacter.addSupportedCategoricalEnumeration(termVocabularyState);
975
				features.put(idCC, categoricalCharacter);
976

    
977
			} catch (Exception e) {
978
				logger.warn("Import of CategoricalCharacter " + j + " failed.");
979
				cdmState.setUnsuccessfull(); 
980
			}
981

    
982
			if ((++j % modCount) == 0){ logger.info("CategoricalCharacters handled: " + j);}
983

    
984
		}
985
		return;
986
	}
987

    
988
	/**
989
	 * @param sddNamespace
990
	 * @param sddConfig
991
	 * @param elCharacters
992
	 */
993
	private void handleQuantitativeData(Namespace sddNamespace,	SDDImportState cdmState, Element elCharacters) {
994
		int j;
995
		// <QuantitativeCharacter id="c2">
996
		List<Element> elQuantitativeCharacters = elCharacters.getChildren("QuantitativeCharacter", sddNamespace);
997
		j = 0;
998
		//for each QuantitativeCharacter
999
		for (Element elQuantitativeCharacter : elQuantitativeCharacters){
1000

    
1001
			try {
1002

    
1003
				String idQC = elQuantitativeCharacter.getAttributeValue("id");
1004

    
1005
				// <Representation>
1006
				//  <Label>Leaf length</Label>
1007
				// </Representation>
1008
				Feature quantitativeCharacter = Feature.NewInstance();
1009
				quantitativeCharacter.setKindOf(Feature.DESCRIPTION());
1010
				importRepresentation(elQuantitativeCharacter, sddNamespace, quantitativeCharacter, idQC, cdmState);
1011

    
1012
				quantitativeCharacter.setSupportsQuantitativeData(true);
1013

    
1014
				// <MeasurementUnit>
1015
				//  <Label role="Abbrev">m</Label>
1016
				// </MeasurementUnit>
1017
				Element elMeasurementUnit = elQuantitativeCharacter.getChild("MeasurementUnit",sddNamespace);
1018
				String label = "";
1019
				String role = "";
1020
				if (elMeasurementUnit != null) {
1021
					Element elLabel = elMeasurementUnit.getChild("Label",sddNamespace);
1022
					role = elLabel.getAttributeValue("role");
1023
					label = (String)ImportHelper.getXmlInputValue(elMeasurementUnit, "Label",sddNamespace);
1024
				}
1025

    
1026
				MeasurementUnit unit = null;
1027
				if (!label.equals("")){
1028
					if (role != null) {
1029
						if (role.equals("Abbrev")){
1030
							unit = MeasurementUnit.NewInstance(label,label,label);
1031
						}
1032
					} else {
1033
						unit = MeasurementUnit.NewInstance(label,label,label);
1034
					}
1035
				}
1036

    
1037
				if (unit != null) {
1038
					units.put(idQC, unit);
1039
				}
1040

    
1041
				//<Default>
1042
				//  <MeasurementUnitPrefix>milli</MeasurementUnitPrefix>
1043
				//</Default>
1044
				Element elDefault = elQuantitativeCharacter.getChild("Default",sddNamespace);
1045
				if (elDefault != null) {
1046
					String measurementUnitPrefix = (String)ImportHelper.getXmlInputValue(elDefault, "MeasurementUnitPrefix",sddNamespace);
1047
					if (! measurementUnitPrefix.equals("")){
1048
						defaultUnitPrefixes.put(idQC, measurementUnitPrefix);
1049
					}
1050
				}
1051

    
1052
				features.put(idQC, quantitativeCharacter);
1053

    
1054
			} catch (Exception e) {
1055
				//FIXME
1056
				logger.warn("Import of QuantitativeCharacter " + j + " failed.");
1057
				cdmState.setUnsuccessfull();
1058
			}
1059

    
1060
			if ((++j % modCount) == 0){ logger.info("QuantitativeCharacters handled: " + j);}
1061

    
1062
		}
1063
		return;
1064
	}
1065

    
1066
	private void handleTextCharacters(Namespace sddNamespace, SDDImportState cdmState, Element elCharacters) {
1067
		int j;
1068
		// <TextCharacter id="c3">
1069
		List<Element> elTextCharacters = elCharacters.getChildren("TextCharacter", sddNamespace);
1070
		j = 0;
1071
		//for each TextCharacter
1072
		for (Element elTextCharacter : elTextCharacters){
1073

    
1074
			try {
1075

    
1076
				String idTC = elTextCharacter.getAttributeValue("id");
1077

    
1078
				// <Representation>
1079
				//  <Label xml:lang="en">Leaf features not covered by other characters</Label>
1080
				// </Representation>
1081
				Feature textCharacter = Feature.NewInstance();
1082
				textCharacter.setKindOf(Feature.DESCRIPTION());
1083
				importRepresentation(elTextCharacter, sddNamespace, textCharacter, idTC, cdmState);
1084

    
1085
				textCharacter.setSupportsTextData(true);
1086

    
1087
				features.put(idTC, textCharacter);
1088

    
1089
			} catch (Exception e) {
1090
				//FIXME
1091
				logger.warn("Import of TextCharacter " + j + " failed.");
1092
				cdmState.setUnsuccessfull();
1093
			}
1094

    
1095
			if ((++j % modCount) == 0){ logger.info("TextCharacters handled: " + j);}
1096

    
1097
		}
1098
		return;
1099
	}
1100

    
1101
	// imports the descriptions of taxa
1102
	protected void importCodedDescriptions(Element elDataset, Namespace sddNamespace, SDDImportState cdmState){
1103
				
1104
		// <CodedDescriptions>
1105
		logger.info("start CodedDescriptions ...");
1106
		Element elCodedDescriptions = elDataset.getChild("CodedDescriptions",sddNamespace);
1107
		
1108
		// <CodedDescription id="D101">
1109
		if (elCodedDescriptions != null) {
1110
			List<Element> listCodedDescriptions = elCodedDescriptions.getChildren("CodedDescription", sddNamespace);
1111
			int j = 0;
1112
			//for each CodedDescription
1113
			for (Element elCodedDescription : listCodedDescriptions){
1114
				handleCodedDescription(sddNamespace, cdmState, elCodedDescription, j);
1115
				if ((++j % modCount) == 0){ logger.info("CodedDescriptions handled: " + j);}
1116
			}
1117
		}
1118
		return;
1119
	}
1120

    
1121
	/**
1122
	 * @param sddNamespace
1123
	 * @param sddConfig
1124
	 * @param j
1125
	 * @param elCodedDescription
1126
	 * @return
1127
	 */
1128
	private void handleCodedDescription(Namespace sddNamespace, SDDImportState cdmState, Element elCodedDescription, int j) {
1129
		try {
1130

    
1131
			String idCD = elCodedDescription.getAttributeValue("id");
1132

    
1133
			// <Representation>
1134
			//  <Label>&lt;i&gt;Viola hederacea&lt;/i&gt; Labill. as revised by R. Morris April 8, 2006</Label>
1135
			// </Representation>
1136
			TaxonDescription taxonDescription = TaxonDescription.NewInstance();
1137
			if (!generatorName.isEmpty()){
1138
				Annotation annotation = Annotation.NewInstance(generatorName, AnnotationType.TECHNICAL(),Language.DEFAULT());
1139
				taxonDescription.addAnnotation(annotation);
1140
			}
1141
			importRepresentation(elCodedDescription, sddNamespace, taxonDescription, idCD, cdmState);
1142

    
1143
			// <Scope>
1144
			//  <TaxonName ref="t1"/>
1145
			//  <Citation ref="p1" location="p. 30"/>
1146
			// </Scope>
1147
			Element elScope = elCodedDescription.getChild("Scope", sddNamespace);
1148
			Taxon taxon;
1149
			if (elScope != null) {
1150
				taxon = handleCDScope(sddNamespace, cdmState, idCD, elScope);
1151
			} else {//in case no taxon is linked to the description, a new one is created
1152
				taxon = handleCDNoScope(sddNamespace, cdmState, elCodedDescription);
1153
			}
1154

    
1155
			// <SummaryData>
1156
			Element elSummaryData = elCodedDescription.getChild("SummaryData",sddNamespace);
1157
			if (elSummaryData != null) {
1158
				handleSummaryCategoricalData(sddNamespace, taxonDescription, elSummaryData);
1159
				handleSummaryQuantitativeData(sddNamespace, taxonDescription, elSummaryData);
1160
				handleSummaryTextData(sddNamespace, taxonDescription, elSummaryData);
1161
			}
1162

    
1163
			if (taxon != null) {
1164
				taxon.addDescription(taxonDescription);
1165
			}
1166
//			
1167
			workingSet.addDescription(taxonDescription);
1168
			
1169
//OLD			taxonDescription.setDescriptiveSystem(featureSet);
1170

    
1171
			taxonDescriptions.put(idCD, taxonDescription);//FIXME
1172

    
1173
		} catch (Exception e) {
1174
			//FIXME
1175
			logger.warn("Import of CodedDescription " + j + " failed.", e);
1176
			cdmState.setUnsuccessfull();
1177
		}
1178
		return;
1179
	}
1180

    
1181
	/**
1182
	 * @param sddNamespace
1183
	 * @param sddConfig
1184
	 * @param elCodedDescription
1185
	 * @param taxon
1186
	 * @return
1187
	 */
1188
	private Taxon handleCDNoScope(Namespace sddNamespace, SDDImportState cdmState, Element elCodedDescription	) {
1189
		Taxon taxon = null;
1190
		NonViralName<?> nonViralName = NonViralName.NewInstance(null);
1191
		String id = new String("" + taxonNamesCount);
1192
		IdentifiableSource source = IdentifiableSource.NewInstance(id, "TaxonName");
1193
		importRepresentation(elCodedDescription, sddNamespace, nonViralName, id, cdmState);
1194
		
1195
		if(cdmState.getConfig().isDoMatchTaxa()){
1196
			taxon = getTaxonService().findBestMatchingTaxon(nonViralName.getTitleCache());
1197
		}
1198
		
1199
		if(taxon != null){
1200
			nonViralName = HibernateProxyHelper.deproxy(taxon.getName(), NonViralName.class);
1201
//							taxonNameBases.put(id ,tnb);
1202
//							taxonNamesCount++;
1203
			logger.info("using existing Taxon " + taxon.getTitleCache());
1204
		} else {
1205
			nonViralName.addSource(source);
1206
			taxonNameBases.put(id ,nonViralName);
1207
			taxonNamesCount++;						
1208
			logger.info("creating new Taxon from TaxonName " + nonViralName.getTitleCache());
1209
			taxon = Taxon.NewInstance(nonViralName, sec);
1210
		}
1211
		return taxon;
1212
	}
1213

    
1214
	/**
1215
	 * @param sddNamespace
1216
	 * @param sddConfig
1217
	 * @param idCD
1218
	 * @param elScope
1219
	 * @param taxon
1220
	 * @return
1221
	 */
1222
	private Taxon handleCDScope(Namespace sddNamespace, SDDImportState cdmState, 
1223
			String idCD, Element elScope) {
1224
		Taxon taxon = null;
1225
		Element elTaxonName = elScope.getChild("TaxonName", sddNamespace);
1226
		String ref = elTaxonName.getAttributeValue("ref");
1227
		NonViralName<?> nonViralName = taxonNameBases.get(ref);
1228
		
1229
		if(cdmState.getConfig().isDoMatchTaxa()){
1230
			taxon = getTaxonService().findBestMatchingTaxon(nonViralName.getTitleCache());
1231
		}
1232
		
1233
		if(taxon != null){
1234
			logger.info("using existing Taxon" + taxon.getTitleCache());
1235
			if(!nonViralName.getUuid().equals(taxon.getName().getUuid())){
1236
				logger.warn("TaxonNameBase entity of existing taxon does not match Name in list -> replacing Name in list");
1237
				nonViralName = HibernateProxyHelper.deproxy(taxon.getName(), NonViralName.class);
1238
			}				
1239
		} else {							
1240
			logger.info("creating new Taxon from TaxonName '" + nonViralName.getTitleCache()+"'");
1241
			taxon = Taxon.NewInstance(nonViralName, sec);
1242
		}
1243
		
1244
		//citation
1245
		Element elCitation = elScope.getChild("Citation",sddNamespace);
1246
		if (elCitation != null) {
1247
			String refCitation = elCitation.getAttributeValue("ref");
1248
			if (! refCitation.equals("")){
1249
				citations.put(idCD, refCitation);
1250
			}
1251
			String location = elCitation.getAttributeValue("location");
1252
			if (! location.equals("")){
1253
				locations.put(idCD, location);
1254
			}
1255
		}
1256
		return taxon;
1257
	}
1258

    
1259
	/**
1260
	 * @param sddNamespace
1261
	 * @param taxonDescription
1262
	 * @param elSummaryData
1263
	 */
1264
	private void handleSummaryTextData(Namespace sddNamespace,
1265
			TaxonDescription taxonDescription, Element elSummaryData) {
1266
		String ref;
1267
		int k;
1268
		// <TextChar ref="c3">
1269
		List<Element> elTextChars = elSummaryData.getChildren("TextChar", sddNamespace);
1270
		k = 0;
1271
		//for each TextChar
1272
		for (Element elTextChar : elTextChars){
1273
			if ((++k % modCount) == 0){ logger.info("TextChar handled: " + (k-1));}
1274
			ref = elTextChar.getAttributeValue("ref");
1275
			Feature feature = features.get(ref);
1276
			TextData textData = TextData.NewInstance();
1277
			textData.setFeature(feature);
1278

    
1279
			// <Content>Free form text</Content>
1280
			String content = (String)ImportHelper.getXmlInputValue(elTextChar, "Content",sddNamespace);
1281
			textData.putText(datasetLanguage, content);
1282
			taxonDescription.addElement(textData);
1283
		}
1284
	}
1285

    
1286
	/**
1287
	 * @param sddNamespace
1288
	 * @param taxonDescription
1289
	 * @param elSummaryData
1290
	 */
1291
	private void handleSummaryQuantitativeData(Namespace sddNamespace,
1292
			TaxonDescription taxonDescription, Element elSummaryData) {
1293
		String ref;
1294
		int k;
1295
		// <Quantitative ref="c2">
1296
		List<Element> elQuantitatives = elSummaryData.getChildren("Quantitative", sddNamespace);
1297
		k = 0;
1298
		//for each Quantitative
1299
		for (Element elQuantitative : elQuantitatives){
1300
			if ((++k % modCount) == 0){ logger.warn("Quantitative handled: " + (k-1));}
1301
			ref = elQuantitative.getAttributeValue("ref");
1302
			Feature feature = features.get(ref);
1303
			QuantitativeData quantitativeData = QuantitativeData.NewInstance();
1304
			quantitativeData.setFeature(feature);
1305

    
1306
			MeasurementUnit unit = units.get(ref);
1307
			String prefix = defaultUnitPrefixes.get(ref);
1308
			if (unit != null) {
1309
				String u = unit.getLabel();
1310
				if (prefix != null) {
1311
					u = prefix + u;
1312
				}
1313
				unit.setLabel(u);
1314
				quantitativeData.setUnit(unit);
1315
			}
1316

    
1317
			// <Measure type="Min" value="2.3"/>
1318
			List<Element> elMeasures = elQuantitative.getChildren("Measure", sddNamespace);
1319
			int l = 0;
1320
			
1321
			//for each State
1322
			for (Element elMeasure : elMeasures){
1323
				if ((++l % modCount) == 0){ logger.info("States handled: " + (l-1));}
1324
				String type = elMeasure.getAttributeValue("type");
1325
				String value = elMeasure.getAttributeValue("value");
1326
				if (value.contains(",")) {
1327
					value = value.replace(',', '.');
1328
				}
1329
				Float v = Float.parseFloat(value);
1330
				//Float v = new Float(0);
1331
				StatisticalMeasure t = null;
1332
				if (type.equals("Min")) {
1333
					t = StatisticalMeasure.MIN();
1334
				} else if (type.equals("Mean")) {
1335
					t = StatisticalMeasure.AVERAGE();
1336
				} else if (type.equals("Max")) {
1337
					t = StatisticalMeasure.MAX();
1338
				} else if (type.equals("SD")) {
1339
					t = StatisticalMeasure.STANDARD_DEVIATION();
1340
				} else if (type.equals("N")) {
1341
					t = StatisticalMeasure.SAMPLE_SIZE();
1342
				} else if (type.equals("UMethLower")) {
1343
					t = StatisticalMeasure.TYPICAL_LOWER_BOUNDARY();
1344
				} else if (type.equals("UMethUpper")) {
1345
					t = StatisticalMeasure.TYPICAL_UPPER_BOUNDARY();
1346
				} else if (type.equals("Var")) {
1347
					t = StatisticalMeasure.VARIANCE();
1348
				} else {
1349
					t = StatisticalMeasure.NewInstance(type,type,type);
1350
					statisticalMeasures.add(t);
1351
				}
1352

    
1353
				StatisticalMeasurementValue statisticalValue = StatisticalMeasurementValue.NewInstance();
1354
				statisticalValue.setValue(v);
1355
				statisticalValue.setType(t);
1356
				quantitativeData.addStatisticalValue(statisticalValue);
1357
				featureData.add(statisticalValue);
1358
			}
1359
			taxonDescription.addElement(quantitativeData);
1360
		}
1361
	}
1362

    
1363
	/**
1364
	 * @param sddNamespace
1365
	 * @param taxonDescription
1366
	 * @param elSummaryData
1367
	 */
1368
	private void handleSummaryCategoricalData(Namespace sddNamespace,
1369
			TaxonDescription taxonDescription, Element elSummaryData) {
1370
		String ref;
1371
		// <Categorical ref="c4">
1372
		List<Element> elCategoricals = elSummaryData.getChildren("Categorical", sddNamespace);
1373
		int k = 0;
1374
		//for each Categorical
1375
		for (Element elCategorical : elCategoricals){
1376
			if ((++k % modCount) == 0){ logger.warn("Categorical handled: " + (k-1));}
1377
			ref = elCategorical.getAttributeValue("ref");
1378
			Feature feature = features.get(ref);
1379
			CategoricalData categoricalData = CategoricalData.NewInstance();
1380
			categoricalData.setFeature(feature);
1381

    
1382
			// <State ref="s3"/>
1383
			List<Element> elStates = elCategorical.getChildren("State", sddNamespace);
1384
			int l = 0;
1385
			
1386
			//for each State
1387
			for (Element elState : elStates){
1388
				if ((++l % modCount) == 0){ logger.info("States handled: " + (l-1));}
1389
				ref = elState.getAttributeValue("ref");
1390
				State state = states.get(ref);
1391
				if (state != null) {
1392
					StateData stateData = StateData.NewInstance();
1393
					stateData.setState(state);
1394
					List<Element> elModifiers = elState.getChildren("Modifier", sddNamespace);
1395
					for (Element elModifier : elModifiers){
1396
						ref = elModifier.getAttributeValue("ref");
1397
						Modifier modifier = modifiers.get(ref);
1398
						if (modifier != null) {
1399
							stateData.addModifier(modifier);
1400
						}
1401
					}
1402
					categoricalData.addState(stateData);
1403
				}
1404
				taxonDescription.addElement(categoricalData);
1405
			}
1406
		}
1407
	}
1408

    
1409
	// imports the persons associated with the dataset creation, modification, related publications
1410
	protected void importAgents(Element elDataset, Namespace sddNamespace, SDDImportState cdmState){
1411
		// <Agents>
1412
		logger.info("start Agents ...");
1413
		Element elAgents = elDataset.getChild("Agents",sddNamespace);
1414
		if (elAgents != null) {
1415
			// <Agent id="a1">
1416
			List <Element> listAgents = elAgents.getChildren("Agent", sddNamespace);
1417
			int j = 0;
1418
			//for each Agent
1419
			for (Element elAgent : listAgents){
1420

    
1421
				try {
1422

    
1423
					String idA = elAgent.getAttributeValue("id");
1424

    
1425
					//  <Representation>
1426
					//   <Label>Kevin Thiele</Label>
1427
					//   <Detail role="Description">Ali Baba is also known as r.a.m.</Detail>
1428
					//  </Representation>
1429
					Person person = Person.NewInstance();
1430
					importRepresentation(elAgent, sddNamespace, person, idA, cdmState);
1431
					person.addSource(IdentifiableSource.NewInstance(idA, "Agent"));
1432

    
1433
					/*XIM <Links>
1434
					Element elLinks = elAgent.getChild("Links",sddNamespace);
1435

    
1436
					if (elLinks != null) {
1437

    
1438
						//  <Link rel="Alternate" href="http://www.diversitycampus.net/people/hagedorn"/>
1439
						List<Element> listLinks = elLinks.getChildren("Link", sddNamespace);
1440
						int k = 0;
1441
						//for each Link
1442
						for (Element elLink : listLinks){
1443

    
1444
							try {
1445

    
1446
								String rel = elLink.getAttributeValue("rel");
1447
								String href = elLink.getAttributeValue("href");
1448

    
1449
								Media link = Media.NewInstance();
1450
								MediaRepresentation mr = MediaRepresentation.NewInstance();
1451
								mr.addRepresentationPart(MediaRepresentationPart.NewInstance(href, null));
1452
								link.addRepresentation(mr);
1453
								person.addMedia(link);
1454

    
1455
							} catch (Exception e) {
1456
								//FIXME
1457
								logger.warn("Import of Link " + k + " failed.");
1458
								success = false; 
1459
							}
1460

    
1461
							if ((++k % modCount) == 0){ logger.info("Links handled: " + k);}
1462

    
1463
						}
1464
					}
1465
					*/
1466
					if (authors.containsKey(idA)) {
1467
						authors.put(idA,person);
1468
					}
1469

    
1470
					if (editors.containsKey(idA)) {
1471
						editors.put(idA, person);
1472
					}
1473

    
1474
				} catch (Exception e) {
1475
					//FIXME
1476
					logger.warn("Import of Agent " + j + " failed.");
1477
					cdmState.setUnsuccessfull(); 
1478
				}
1479

    
1480
				if ((++j % modCount) == 0){ logger.info("Agents handled: " + j);}
1481

    
1482
			}
1483
		}
1484
	}
1485

    
1486
	// imports publications related with the data set
1487
	protected void importPublications(Element elDataset, Namespace sddNamespace, SDDImportState cdmState){
1488
		/* <Publications>
1489
			  <Publication id="p112">
1490
			    <Representation>
1491
			      <Label>Gee, X. & Haa, Y. (2003). How to be happy in five minutes. Instant Gratifications, Palm Beach.</Label>
1492
			    </Representation>
1493
			    <Links>
1494
			    <Link rel="BasedOn" href="doi:10.1992/32311"/>
1495
			    <Link rel="Alternate" href="http://some.service.net/providing/bibliographic.data"/>
1496
			    </Links>
1497
			</Publications>
1498
*/
1499
		logger.info("start Publications ...");
1500
		Element elPublications = elDataset.getChild("Publications",sddNamespace);
1501

    
1502
		if (elPublications != null) {
1503
			List<Element> listPublications = elPublications.getChildren("Publication", sddNamespace);
1504
			int j = 0;
1505
			for (Element elPublication : listPublications){
1506

    
1507
				try {
1508

    
1509
					String idP = elPublication.getAttributeValue("id");
1510
					Reference<?> publication = ReferenceFactory.newArticle();
1511
					importRepresentation(elPublication, sddNamespace, publication, idP, cdmState);
1512

    
1513
					publications.put(idP,publication);
1514

    
1515
				} catch (Exception e) {
1516
					logger.warn("Import of Publication " + j + " failed.");
1517
					cdmState.setUnsuccessfull();
1518
				}
1519

    
1520
				if ((++j % modCount) == 0){ logger.info("Publications handled: " + j);}
1521

    
1522
			}
1523
		}
1524
	}
1525

    
1526
	// imports media objects such as images //FIXME check mediaobj
1527
	protected void importMediaObjects(Element elDataset, Namespace sddNamespace, SDDImportState cdmState){
1528
		// <MediaObjects>
1529
		logger.info("start MediaObjects ...");
1530
		Element elMediaObjects = elDataset.getChild("MediaObjects",sddNamespace);
1531

    
1532
		if (elMediaObjects != null) {
1533
			// <MediaObject id="m1">
1534
			List<Element> listMediaObjects = elMediaObjects.getChildren("MediaObject", sddNamespace);
1535
			int j = 0;
1536
			for (Element elMO : listMediaObjects){
1537

    
1538
				String id = "";
1539

    
1540
				try {
1541
					String idMO = elMO.getAttributeValue("id");
1542
					id = idMO;
1543

    
1544
					//  <Representation>
1545
					//   <Label>Image description, e.g. to be used for alt-attribute in html.</Label>
1546
					//  </Representation>
1547
					Media media = Media.NewInstance();
1548
					importRepresentation(elMO, sddNamespace, media, idMO, cdmState);
1549

    
1550
					// <Type>Image</Type>
1551
					// <Source href="http://test.edu/test.jpg"/>
1552
					String type = (String)ImportHelper.getXmlInputValue(elMO,"Type",sddNamespace);
1553

    
1554
					if ((type != null) && (type.equals("Image"))) {
1555
						Element elSource = elMO.getChild("Source",sddNamespace);
1556
						String href = elSource.getAttributeValue("href");
1557

    
1558
						ImageInfo imageMetaData = null;
1559
						ImageFile image = null;
1560
						if (href.substring(0,7).equals("http://")) {
1561
							try{
1562
								URL url = new URL(href);
1563
								
1564
								imageMetaData = ImageInfo.NewInstance(url.toURI(), 0);
1565
								image = ImageFile.NewInstance(url.toURI(), null, imageMetaData);
1566
							} catch (MalformedURLException e) {
1567
								logger.error("Malformed URL", e);
1568
							}
1569
						} else {
1570
							String sns = cdmState.getConfig().getSourceNameString();
1571
							File f = new File(sns);
1572
							File parent = f.getParentFile();
1573
							String fi = parent.toString() + File.separator + href;
1574
							File file = new File(fi);
1575
							imageMetaData  = ImageInfo.NewInstance(new URI(fi), 0); //file
1576
							image = ImageFile.NewInstance(file.toURI(), null, imageMetaData);
1577
						}
1578
						MediaRepresentation representation = MediaRepresentation.NewInstance(imageMetaData.getMimeType(), null);
1579
						representation.addRepresentationPart(image);
1580

    
1581
						media.addRepresentation(representation);
1582

    
1583
						ArrayList<CdmBase> lcb = (ArrayList<CdmBase>) mediaObject_ListCdmBase.get(idMO);
1584
						if (lcb != null) {
1585
							for (int k = 0; k < lcb.size(); k++) {
1586
								if (lcb.get(k) instanceof DefinedTermBase) {
1587
									DefinedTermBase<?> dtb = (DefinedTermBase<?>) lcb.get(k);
1588
									// if (lcb.get(0) instanceof DefinedTermBase) {
1589
									// DefinedTermBase dtb = (DefinedTermBase) lcb.get(0);
1590
									//									if (dtb!=null) {
1591
									//										if (k == 0) {
1592
									dtb.addMedia(media);
1593
									//System.out.println(dtb.getLabel());
1594
									//										} else {
1595
									//											Media me = (Media) media.clone();
1596
									//											dtb.addMedia(me);
1597
									//										}
1598
									//									}
1599
								} else if (lcb.get(k) instanceof Reference) {
1600
									Reference<?> rb = (Reference<?>) lcb.get(k);
1601
									//} else if (lcb.get(0) instanceof Reference) {
1602
									//Reference rb = (Reference) lcb.get(0);
1603
									// rb.setTitleCache(label);
1604
									//									if (rb!=null) {
1605
									//										if (k == 0) {
1606
									rb.addMedia(media);
1607
									//System.out.println(rb.getTitle());
1608
									//										} else {
1609
									//											Media me = (Media) media.clone();
1610
									//											rb.addMedia(me);
1611
									//										}
1612
									//									}
1613
//								 else if (lcb.get(k) instanceof TaxonNameBase){
1614
//									TaxonNameBase tb = (TaxonNameBase) lcb.get(k);
1615
//									tb.addMedia(media);
1616
								} else {
1617
									logger.warn("Can't handle associated media for " + lcb.get(k).getId() + "(" +  lcb.get(k).getClass().getSimpleName()+")"  );
1618
								}
1619
							}
1620
						}
1621
					}
1622

    
1623
				} catch (Exception e) {
1624
					//FIXME
1625
					logger.warn("Could not attach MediaObject " + j + "(SDD: " + id + ") to several objects: " + e.getMessage());
1626
					cdmState.setUnsuccessfull();
1627
				}
1628

    
1629
				if ((++j % modCount) == 0){ logger.info("MediaObjects handled: " + j);
1630

    
1631
				}
1632
			}
1633
		}
1634
	}
1635

    
1636
	// imports the <DescriptiveConcepts> block ; DescriptiveConcepts are used as nodes in CharacterTrees and Characters as leaves
1637
	// but since Modifiers can be linked to DescriptiveConcepts they are stored as features with a particular Marker
1638
	protected void importDescriptiveConcepts(Element elDataset, Namespace sddNamespace, SDDImportState state){
1639
		/* <DescriptiveConcepts>
1640
		      <DescriptiveConcept id="dc0">
1641
			        <Representation>
1642
			          <Label>Fixed set of modifiers supported in Lucid3</Label>
1643
			        </Representation>
1644
			        <Modifiers>
1645
			          <Modifier id="mod1">
1646
			            <Representation>
1647
			              <Label>rarely</Label>
1648
			            </Representation>
1649
			            <ModifierClass>Frequency</ModifierClass>
1650
			            <ProportionRange lowerestimate="0.0" upperestimate="0.25"/>
1651
			          </Modifier>
1652
		          </Modifiers>
1653
		        </DescriptiveConcept>
1654
	         </DescriptiveConcepts>
1655
		 */
1656
		logger.info("start DescriptiveConcepts ...");
1657
		Element elDescriptiveConcepts = elDataset.getChild("DescriptiveConcepts",sddNamespace);
1658
		if (elDescriptiveConcepts != null) {
1659
			List<Element> listDescriptiveConcepts = elDescriptiveConcepts.getChildren("DescriptiveConcept", sddNamespace);
1660
			int j = 0;
1661

    
1662
			for (Element elDescriptiveConcept : listDescriptiveConcepts){
1663
				try {
1664
				String id = elDescriptiveConcept.getAttributeValue("id");
1665
					Feature feature = Feature.NewInstance();
1666
					feature.setKindOf(Feature.DESCRIPTION());
1667
					if (!id.equals("")) {
1668
					//	 <Representation>
1669
					//       <Label>Body</Label>
1670
					importRepresentation(elDescriptiveConcept, sddNamespace, feature, id, state);
1671
						features.put(id, feature);
1672
						getTermService().save(feature);//XIM
1673
						descriptiveConcepts.add(feature);
1674
						// imports the modifiers
1675
						Element elModifiers = elDescriptiveConcept.getChild("Modifiers", sddNamespace);
1676
					if (elModifiers !=null){
1677
						List<Element> listModifiers = elModifiers.getChildren("Modifier", sddNamespace);
1678
							TermVocabulary<Modifier> termVocabularyState = TermVocabulary.NewInstance(null, null, null, null);
1679
						for (Element elModifier : listModifiers) {
1680
								Modifier modif = Modifier.NewInstance();
1681
								String idmod = elModifier.getAttributeValue("id");
1682
								importRepresentation(elModifier, sddNamespace, modif, idmod, state);
1683
								termVocabularyState.addTerm(modif);
1684
								//termVocabularyStates.add(termVocabularyState);
1685
								getVocabularyService().save(termVocabularyState);//XIM
1686
								modifiers.put(idmod, modif);
1687
						}
1688
							feature.addRecommendedModifierEnumeration(termVocabularyState);
1689
				}
1690

    
1691
					}
1692
				}
1693
				catch (Exception e) {
1694
					logger.warn("Import of DescriptiveConcept " + j + " failed: " + e.getMessage());
1695
				}
1696
				if ((++j % modCount) == 0){ logger.info("DescriptiveConcepts handled: " + j);}
1697

    
1698
			}
1699
		}
1700
	}
1701

    
1702
	// imports the <CharacterTrees> block
1703
	protected void importCharacterTrees(Element elDataset, Namespace sddNamespace, SDDImportState cdmState){
1704
		// <CharacterTrees>
1705
		logger.info("start CharacterTrees ...");
1706
		Element elCharacterTrees = elDataset.getChild("CharacterTrees",sddNamespace);
1707

    
1708
		if (elCharacterTrees != null) {
1709
			List<Element> listCharacterTrees = elCharacterTrees.getChildren("CharacterTree", sddNamespace);
1710
			int j = 0;
1711
			for (Element elCharacterTree : listCharacterTrees){
1712
				try {
1713
					Element elRepresentation = elCharacterTree.getChild("Representation",sddNamespace);
1714
					String label = (String)ImportHelper.getXmlInputValue(elRepresentation,"Label",sddNamespace);
1715
					//Element elDesignedFor = elCharacterTree.getChild("DesignedFor",sddNamespace);//TODO ?
1716

    
1717
					FeatureTree featureTree =  FeatureTree.NewInstance();
1718
					importRepresentation(elCharacterTree, sddNamespace, featureTree, "", cdmState);
1719
					FeatureNode root = featureTree.getRoot();
1720
					List<Element> listeOfNodes = elCharacterTree.getChildren("Nodes", sddNamespace);
1721

    
1722
					//Nodes of CharacterTrees in SDD always refer to DescriptiveConcepts
1723
					for (Element elNodes : listeOfNodes) {
1724
						handleCharacterNodes(sddNamespace, root, elNodes);
1725
					}
1726
					featureTrees.add(featureTree);
1727
					if (workingSet.getDescriptiveSystem() != null){
1728
						//TODO how to handle multiple 
1729
						logger.warn("Multiple feature trees not yet supported");
1730
					}else{
1731
						workingSet.setDescriptiveSystem(featureTree);
1732
					}
1733
				}
1734

    
1735
				catch (Exception e) {
1736
					logger.warn("Import of Character tree " + j + " failed.");
1737
					cdmState.setUnsuccessfull();
1738
				}
1739
				if ((++j % modCount) == 0){ logger.info("CharacterTrees handled: " + j);}
1740

    
1741
			}
1742

    
1743
		}
1744
	}
1745

    
1746
	/**
1747
	 * @param sddNamespace
1748
	 * @param root
1749
	 * @param elNodes
1750
	 */
1751
	private void handleCharacterNodes(Namespace sddNamespace, FeatureNode root, Element elNodes) {
1752
		List<Element> listNodes = elNodes.getChildren("Node", sddNamespace);
1753
		if (listNodes != null) {
1754
			for (Element elNode : listNodes){
1755
				String idN = elNode.getAttributeValue("id");
1756
				FeatureNode fn = null;
1757
				Feature dc = null;
1758
				if (idN!=null) {
1759
					// DescriptiveConcepts are used as nodes in CharacterTrees
1760
					Element elDescriptiveConcept = elNode.getChild("DescriptiveConcept", sddNamespace);
1761
					if (elDescriptiveConcept != null){
1762
						String refDC = elDescriptiveConcept.getAttributeValue("ref");
1763
						dc = features.get(refDC);
1764
						fn = FeatureNode.NewInstance(dc);
1765
					}
1766
					if (fn==null){
1767
						fn = FeatureNode.NewInstance();
1768
					}
1769
					Element elParent = elNode.getChild("Parent", sddNamespace);
1770
					// in SDD links between Nodes are referenced by the <Parent> tag
1771
					if (elParent!=null){
1772
						String refP = elParent.getAttributeValue("ref");
1773
						if (refP!=null) {
1774
							FeatureNode parent = featureNodes.get(refP);
1775
							if (parent==null){
1776
								root.addChild(fn); // if no parent found or the reference is broken, add the node to the root of the tree
1777
							}
1778
							else {
1779
								parent.addChild(fn);
1780
							}
1781
						}
1782
					}
1783
					else {
1784
						root.addChild(fn); // if no parent found or the reference is broken, add the node to the root of the tree
1785
					}
1786
				}
1787
				featureNodes.put(idN, fn);
1788
			}
1789
		}
1790

    
1791
		// Leaves of CharacterTrees in SDD are always CharNodes (referring to Characters)
1792
		List<Element> listCharNodes = elNodes.getChildren("CharNode", sddNamespace);
1793
		if (listCharNodes != null) {
1794
			for (Element elCharNode : listCharNodes){
1795
				Element elParent = elCharNode.getChild("Parent", sddNamespace);
1796
				Element elCharacter = elCharNode.getChild("Character", sddNamespace);
1797
				Element elDependencyRules = elCharNode.getChild("DependencyRules", sddNamespace);
1798
				FeatureNode fn = FeatureNode.NewInstance();
1799
				
1800
				if (elDependencyRules!=null){
1801
					Element elInapplicableIf = elCharNode.getChild("InapplicableIf", sddNamespace);
1802
					if (elInapplicableIf!=null){
1803
						List<Element> listStates = elInapplicableIf.getChildren("State", sddNamespace);
1804
						for (Element stateElement : listStates) {
1805
							String refState = stateElement.getAttributeValue("ref");
1806
							if ((refState!=null)&&(!refState.equals(""))) {
1807
								State state = states.get(refState);
1808
								fn.addInapplicableState(state);
1809
							}
1810
						}
1811
					}
1812
					Element elOnlyapplicableIf = elCharNode.getChild("OnlyApplicableIf", sddNamespace);
1813
					if (elOnlyapplicableIf!=null){
1814
						List<Element> listStates = elInapplicableIf.getChildren("State", sddNamespace);
1815
						for (Element stateElement : listStates) {
1816
							String refState = stateElement.getAttributeValue("ref");
1817
							if ((refState!=null)&&(!refState.equals(""))) {
1818
								State state = states.get(refState);
1819
								fn.addApplicableState(state);
1820
							}
1821
						}
1822
					}
1823
				}
1824
				
1825
				if (elParent!=null){
1826
					String refP = elParent.getAttributeValue("ref");
1827
					if ((refP!=null)&&(!refP.equals(""))) {
1828
					FeatureNode parent = featureNodes.get(refP);
1829
						if (parent==null){
1830
						parent = root; // if no parent found or the reference is broken, add the node to the root of the tree
1831
						}
1832
						parent.addChild(fn);
1833
					}
1834
				}
1835
				String refC = elCharacter.getAttributeValue("ref");
1836
				if ((refC!=null)&&(!refC.equals(""))){
1837
					Feature character = features.get(refC);
1838
					fn.setFeature(character);
1839
					featureNodes.put(refC, fn);
1840
				}
1841
			}		
1842
		}
1843
	}
1844

    
1845
	// imports the <TaxonHierarchies> block
1846
	protected void importTaxonHierarchies(Element elDataset, Namespace sddNamespace, SDDImportState cdmState){
1847

    
1848
		logger.info("start TaxonHierarchies ...");
1849
		Element elTaxonHierarchies = elDataset.getChild("TaxonHierarchies",sddNamespace);
1850

    
1851
		if (elTaxonHierarchies != null) {
1852
			List<Element> listTaxonHierarchies = elTaxonHierarchies.getChildren("TaxonHierarchy", sddNamespace);
1853
			int j = 0;
1854
			for (Element elTaxonHierarchy : listTaxonHierarchies){
1855
				try {
1856
					Element elRepresentation = elTaxonHierarchy.getChild("Representation",sddNamespace);
1857
					String label = (String)ImportHelper.getXmlInputValue(elRepresentation,"Label",sddNamespace);
1858
						Classification classification =  Classification.NewInstance(label);
1859
						importRepresentation(elTaxonHierarchy, sddNamespace, classification, "", cdmState);
1860
					
1861
						Element elNodes = elTaxonHierarchy.getChild("Nodes", sddNamespace); // There can be only one <Nodes> block for TaxonHierarchies
1862
						List<Element> listNodes = elNodes.getChildren("Node", sddNamespace);
1863
						
1864
						for (Element elNode : listNodes){
1865
							String idN = elNode.getAttributeValue("id");
1866
							TaxonNameBase<?,?> tnb = null;
1867
							if (!idN.equals("")) {
1868
								Element elTaxonName = elNode.getChild("TaxonName", sddNamespace);
1869
								String refTN = elTaxonName.getAttributeValue("ref");
1870
								tnb = taxonNameBases.get(refTN);
1871
								Taxon taxon = (Taxon) tnb.getTaxa().iterator().next() ;
1872
								Element elParent = elNode.getChild("Parent", sddNamespace);
1873
								if (elParent!=null){
1874
									String refP = elParent.getAttributeValue("ref");
1875
									if (!refP.equals("")) {
1876
										TaxonNode parent = taxonNodes.get(refP);
1877
										TaxonNode child = parent.addChildTaxon(taxon, sec, "", Synonym.NewInstance(tnb, sec));
1878
										taxonNodes.put(idN,child);
1879
									}
1880
								}
1881
								else {
1882
									TaxonNode tn = classification.addChildTaxon(taxon, sec, "", Synonym.NewInstance(tnb, sec)); // if no parent found or the reference is broken, add the node to the root of the tree
1883
									taxonNodes.put(idN,tn);
1884
								}
1885
							}
1886
						}
1887

    
1888
						classifications.add(classification);
1889
					}
1890

    
1891
				catch (Exception e) {
1892
					//FIXME
1893
					logger.warn("Import of Taxon Hierarchy " + j + " failed.");
1894
					cdmState.setUnsuccessfull();
1895
				}
1896

    
1897
				if ((++j % modCount) == 0){ logger.info("TaxonHierarchies handled: " + j);}
1898

    
1899
			}
1900

    
1901
		}
1902
	}
1903
	
1904
	
1905
	// imports the <GeographicAreas> block 
1906
	protected void importGeographicAreas(Element elDataset, Namespace sddNamespace, SDDImportState cdmState) {
1907
		Element elGeographicAreas = elDataset.getChild("GeographicAreas",sddNamespace);
1908
		if (elGeographicAreas != null) {
1909
			List<Element> listGeographicAreas = elGeographicAreas.getChildren("GeographicArea", sddNamespace);
1910
			int j = 0;
1911
						
1912
			for (Element elGeographicArea : listGeographicAreas){
1913

    
1914
				String id = elGeographicArea.getAttributeValue("id");
1915
				NamedArea na = new NamedArea();
1916
				importRepresentation(elGeographicArea, sddNamespace, na, id, cdmState);
1917
				namedAreas.put(id,na);
1918
								}
1919
			if ((++j % modCount) == 0){ logger.info("GeographicAreas handled: " + j);}
1920
							}
1921
							}
1922
}
(2-2/4)