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.getDescriptionSources().size() > 0) {
393
						this.associateImageWithCdmBase(ref,(Reference) td.getDescriptionSources().toArray()[0]);
394
					} else {
395
						Reference descriptionSource = ReferenceFactory.newGeneric();
396
						sources.add(descriptionSource);
397
						td.addDescriptionSource(descriptionSource);
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.addDescriptionSource(publication);
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
			int j = 0;
753
			for (Element elSpecimen : listSpecimens) {
754
				String id = elSpecimen.getAttributeValue("id");
755
				Specimen specimen = null;
756
				if (!id.equals("")) {
757
					specimen = Specimen.NewInstance();
758
					specimens.put(id,specimen);
759
					importRepresentation(elSpecimen, sddNamespace, specimen, id, cdmState);
760
				}
761
			}
762

    
763
		}
764
	}
765

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

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

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

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

    
796
			}
797

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

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

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

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

    
832
			for (Element elIPRStatement : listIPRStatements){
833

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

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

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

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

    
865
			}
866
		}
867
	}
868

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

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

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

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

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

    
905
			}
906
		}
907
	}
908

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

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

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

    
928
	}
929

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

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

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

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

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

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

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

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

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

    
985
		}
986
		return;
987
	}
988

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

    
1002
			try {
1003

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

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

    
1013
				quantitativeCharacter.setSupportsQuantitativeData(true);
1014

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

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

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

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

    
1053
				features.put(idQC, quantitativeCharacter);
1054

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

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

    
1063
		}
1064
		return;
1065
	}
1066

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

    
1075
			try {
1076

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

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

    
1086
				textCharacter.setSupportsTextData(true);
1087

    
1088
				features.put(idTC, textCharacter);
1089

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

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

    
1098
		}
1099
		return;
1100
	}
1101

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1422
				try {
1423

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

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

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

    
1437
					if (elLinks != null) {
1438

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

    
1445
							try {
1446

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

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

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

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

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

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

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

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

    
1483
			}
1484
		}
1485
	}
1486

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

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

    
1508
				try {
1509

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

    
1514
					publications.put(idP,publication);
1515

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

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

    
1523
			}
1524
		}
1525
	}
1526

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

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

    
1539
				String id = "";
1540

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

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

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

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

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

    
1582
						media.addRepresentation(representation);
1583

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

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

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

    
1632
				}
1633
			}
1634
		}
1635
	}
1636

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

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

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

    
1699
			}
1700
		}
1701
	}
1702

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

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

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

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

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

    
1742
			}
1743

    
1744
		}
1745
	}
1746

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

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

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

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

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

    
1890
						classifications.add(classification);
1891
					}
1892

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

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

    
1901
			}
1902

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

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