Project

General

Profile

Download (68.8 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

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

    
33
import eu.etaxonomy.cdm.api.service.IDescriptionService;
34
import eu.etaxonomy.cdm.common.media.ImageInfo;
35
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
36
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
37
import eu.etaxonomy.cdm.io.common.CdmImportBase;
38
import eu.etaxonomy.cdm.io.common.ICdmIO;
39
import eu.etaxonomy.cdm.io.common.ICdmImport;
40
import eu.etaxonomy.cdm.io.common.ImportHelper;
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
 * This class is not used yet. It is meant for refactoring the SDD import.
91
 * 
92
 * @author a.mueller
93
 * @created 06.01.2011
94
 * @version 1.0
95
 */
96
@Component
97
public class SDDDataSetImport extends CdmImportBase<SDDImportConfigurator, SDDImportState> implements ICdmImport<SDDImportConfigurator, SDDImportState> {
98
	private static final Logger logger = Logger.getLogger(SDDDataSetImport.class);
99

    
100
	private static int modCount = 1000;
101

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

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

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

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

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

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

    
145
	private Rights copyright = null;
146

    
147
	private int taxonNamesCount = 0;
148

    
149
	protected Class<ICdmIO>[] ioClassList;
150
	
151
	public SDDDataSetImport(){
152
		super();
153
	}
154

    
155
	protected void makeIoClassList(){
156
		ioClassList = new Class[]{
157
				SDDImport.class
158
		};
159
	};
160
	
161
	@Override
162
	public boolean doCheck(SDDImportState state){
163
		boolean result = true;
164
		makeIoClassList();
165

    
166
//		for (Class<ICdmIO> ioClass: ioClassList){
167
//			try {
168
//				String ioBeanName = CdmApplicationAwareDefaultImport.getComponentBeanName(ioClass);
169
//				ICdmIO cdmIo = (ICdmIO)getTermService().getBean(ioBeanName, ICdmIO.class);
170
//				if (cdmIo != null){
171
////					result &= cdmIo.invoke(config, stores);
172
//					state.setCurrentIO(cdmIo);
173
//					result &= cdmIo.invoke(state);
174
//				}else{
175
//					logger.error("cdmIO was null");
176
//					result = false;
177
//				}
178
//			} catch (Exception e) {
179
//					logger.error(e);
180
//					e.printStackTrace();
181
//					result = false;
182
//			}
183
//		}
184
		logger.warn("No check implemented for SDD");
185
		return result;
186
	}
187

    
188
	//	@Override
189
	//	public boolean doInvoke(IImportConfigurator config, Map<String, MapWrapper<? extends CdmBase>> stores){
190
	@Override
191
	public void doInvoke(SDDImportState state){
192
		
193
		TransactionStatus ts = startTransaction();
194
		SDDImportConfigurator sddConfig = state.getConfig();
195
		IProgressMonitor progressMonitor = sddConfig.getProgressMonitor();
196
				
197
		logger.info("start Datasets ...");
198
		
199
		// <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">
200
		Element root = sddConfig.getSourceRoot();
201
		Namespace sddNamespace = sddConfig.getSddNamespace();
202

    
203
		logger.info("start TechnicalMetadata ...");
204
		// <TechnicalMetadata created="2006-04-20T10:00:00">
205
		importTechnicalMetadata(root, sddNamespace, sddConfig);
206
		List<Element> elDatasets = root.getChildren("Dataset",sddNamespace);
207
		int i = 0;
208

    
209
		//for each Dataset
210
		logger.info("start Dataset ...");
211
		progressMonitor.beginTask("Importing SDD data", elDatasets.size());
212
		for (Element elDataset : elDatasets){
213
			importDataset(elDataset, sddNamespace, state);			
214
//			if ((++i % modCount) == 0){ logger.info("dataset(s) handled: " + i);}
215
//			logger.info(i + " dataset(s) handled");
216
			progressMonitor.worked(1);
217
		}
218
		commitTransaction(ts);
219
		progressMonitor.done();
220
		logger.info("End of transaction");
221
		return;
222
	}
223

    
224
	/* (non-Javadoc)
225
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
226
	 */
227
	protected boolean isIgnore(SDDImportState state){
228
		return false;
229
	}
230

    
231

    
232
	// associates the reference of a media object in SDD with a CdmBase Object
233
	protected void associateImageWithCdmBase(String refMO, CdmBase cb){
234
		if ((refMO != null) && (cb!=null)) {
235
			if (! refMO.equals("")) {
236
				if (! mediaObject_ListCdmBase.containsKey(refMO)) {
237
					List<CdmBase> lcb = new ArrayList<CdmBase>();
238
					lcb.add(cb);
239
					mediaObject_ListCdmBase.put(refMO,lcb);
240
				} else {
241
					List<CdmBase> lcb = mediaObject_ListCdmBase.get(refMO);
242
					lcb.add(cb);
243
					mediaObject_ListCdmBase.put(refMO,lcb);
244
				}
245
			}
246
		}
247
	}
248

    
249
	// imports information about the Dataset
250
	protected void importDatasetRepresentation(Element parent, Namespace sddNamespace){
251
		logger.info("start Representation ...");
252
		/* <Representation>
253
			<Label>The Genus Viola</Label>
254
			<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>
255
	       </Representation>
256
		 */
257

    
258
		
259
		
260
		Element elRepresentation = parent.getChild("Representation",sddNamespace);
261
		String label = (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace);
262
		String detail = (String)ImportHelper.getXmlInputValue(elRepresentation, "Detail",sddNamespace);
263
		
264
		//new
265
		Representation representation = Representation.NewInstance(detail, label, null, datasetLanguage);
266
		workingSet.addRepresentation(representation);
267
		
268

    
269
		//old
270
//		sec.setTitleCache(label, true);
271
//
272
//		if (detail != null) {
273
//			Annotation annotation = Annotation.NewInstance(detail, datasetLanguage);
274
//			annotation.setAnnotationType(AnnotationType.EDITORIAL());
275
//			sec.addAnnotation(annotation);
276
//		}
277
		
278

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

    
281
		for (Element elMediaObject : listMediaObjects) {
282
			String ref = null;
283
			String role = null;
284
			if (elMediaObject != null) {
285
				ref = elMediaObject.getAttributeValue("ref");
286
				role = elMediaObject.getAttributeValue("role");
287
			}
288
			if (ref != null) {
289
				if (!ref.equals("")) {
290
					this.associateImageWithCdmBase(ref,sourceReference);
291
					this.associateImageWithCdmBase(ref,sec);
292
					mediaObject_Role.put(ref,role);
293
				}
294
			}
295
		}
296
	}
297

    
298
	// imports the representation (label, detail, lang) of a particular SDD element
299
	protected void importRepresentation(Element parent, Namespace sddNamespace, VersionableEntity ve, String id, SDDImportState state){
300
		Element elRepresentation = parent.getChild("Representation",sddNamespace);
301
		
302
		Map<Language,List<String>> langLabDet = new HashMap<Language,List<String>>();
303

    
304
		handleRepresentationLabels(sddNamespace, elRepresentation, langLabDet);
305
		handleRepresentationDetails(sddNamespace, elRepresentation, langLabDet);
306

    
307
		if (ve instanceof TermBase) {
308
			makeRepresentationForTerms((TermBase)ve, langLabDet);
309
		}else if (ve instanceof Media) {
310
			makeRepresentationForMedia((Media)ve, langLabDet);
311
		}else if (ve instanceof IdentifiableEntity<?>) {
312
			IdentifiableEntity<?> ie = (IdentifiableEntity<?>)ve;
313
			makeRepresentationForIdentifiableEntity(sddNamespace, ie, elRepresentation, langLabDet);
314
			if (ve instanceof IdentifiableMediaEntity<?>){
315
				makeRepresentationForIdentifiableMediaEntity(parent, sddNamespace, (IdentifiableMediaEntity<?>)ve);
316
			}
317
		}
318

    
319
		makeRepresentationMediaObjects(sddNamespace, ve, elRepresentation);//FIXME
320

    
321
	}
322

    
323

    
324
	/**
325
	 * Handles the "Detail" children of representations. Adds the result to the langLabDet.
326
	 * @param sddNamespace
327
	 * @param elRepresentation
328
	 * @param langLabDet
329
	 */
330
	private void handleRepresentationDetails(Namespace sddNamespace,
331
			Element elRepresentation, Map<Language, List<String>> langLabDet) {
332
		List<Element> listDetails = elRepresentation.getChildren("Detail",sddNamespace);
333
		for (Element elDetail : listDetails){
334
			Language language = getLanguage(elDetail);
335
			String role = elDetail.getAttributeValue("role");
336
			String detail = elDetail.getText();
337
			List<String> labDet = langLabDet.get(language);
338
			labDet.add(detail);
339
			labDet.add(role);
340
			langLabDet.put(language, labDet);
341
		}
342
	}
343

    
344
	/**
345
	 * Handles the "Label" children of representations. Adds the result to the langLabDet.
346
	 * @param sddNamespace
347
	 * @param elRepresentation
348
	 * @param langLabDet
349
	 */
350
	private void handleRepresentationLabels(Namespace sddNamespace,
351
				Element elRepresentation, Map<Language, List<String>> langLabDet) {
352
		// <Label xml:lang="la">Viola hederacea Labill.</Label>
353
		List<Element> listLabels = elRepresentation.getChildren("Label",sddNamespace);
354
		for (Element elLabel : listLabels){
355
			Language language = getLanguage(elLabel);
356
			String label = elLabel.getText();
357
			List<String> labDet = new ArrayList<String>(3);
358
			labDet.add(label);
359
			langLabDet.put(language, labDet);
360
		}
361
	}
362
	
363
	/**
364
	 * 
365
	 * @param ve
366
	 * @param langLabDet
367
	 */
368
	private void makeRepresentationForMedia(Media m, Map<Language, List<String>> langLabDet) {
369
		for (Language lang : langLabDet.keySet()){
370
			List<String> labDet = langLabDet.get(lang);
371
			if (labDet.get(0) != null){
372
				m.putTitle(LanguageString.NewInstance(labDet.get(0), lang));
373
			}
374
			if (labDet.size()>1) {
375
				m.addDescription(labDet.get(1), lang);
376
			}
377
		}
378
	}
379

    
380
	/**
381
	 * Handles representations for terms. Adds one representation per language in langLabDet.
382
	 * 
383
	 * @param ve
384
	 * @param langLabDet
385
	 */
386
	private void makeRepresentationForTerms(TermBase tb, Map<Language, List<String>> langLabDet) {
387
			for (Language lang : langLabDet.keySet()){
388
				List<String> labDet = langLabDet.get(lang);
389
				if (labDet.size()>0){
390
					if (labDet.size()>1) {
391
						tb.addRepresentation(Representation.NewInstance(labDet.get(1), labDet.get(0), labDet.get(0), lang));
392
					} else {
393
						tb.addRepresentation(Representation.NewInstance(labDet.get(0), labDet.get(0), labDet.get(0), lang));
394
					}
395
				}
396
			}
397
	}
398

    
399

    
400
	/**
401
	 * Handles the "MediaObject" children of representations.
402
	 * @param sddNamespace
403
	 * @param ve
404
	 * @param elRepresentation
405
	 */
406
	private void makeRepresentationMediaObjects(Namespace sddNamespace,	VersionableEntity ve, Element elRepresentation) {
407
		List <Element> listMediaObjects = elRepresentation.getChildren("MediaObject", sddNamespace);
408
		for (Element elMediaObject : listMediaObjects) {
409
			String ref = null;
410
			//TODO
411
			String role = null;
412
			if (elMediaObject != null) {
413
				ref = elMediaObject.getAttributeValue("ref");
414
				role = elMediaObject.getAttributeValue("role");
415
			}
416
			if (StringUtils.isNotBlank(ref)) {
417
				if (ve instanceof TaxonDescription) {
418
					TaxonDescription td = (TaxonDescription) ve;
419
					if (td.getSources().size() > 0) {
420
						this.associateImageWithCdmBase(ref, td.getSources().iterator().next().getCitation());
421
					} else {
422
						Reference<?> descriptionSource = ReferenceFactory.newGeneric();
423
						sources.add(descriptionSource);
424
						td.addSource(null, null, descriptionSource, null);
425
						this.associateImageWithCdmBase(ref,descriptionSource);
426
					}
427
				} else {
428
					this.associateImageWithCdmBase(ref,ve);
429
				}
430
			}
431
		}
432
	}
433

    
434
	/**
435
	 * Handles the "Links" element
436
	 * @param parent
437
	 * @param sddNamespace
438
	 * @param ve
439
	 */
440
	private void makeRepresentationForIdentifiableMediaEntity(Element parent,
441
			Namespace sddNamespace, IdentifiableMediaEntity ime) {
442
		Element elLinks = parent.getChild("Links",sddNamespace);
443

    
444
		if (elLinks != null) {
445

    
446
			//  <Link rel="Alternate" href="http://www.diversitycampus.net/people/hagedorn"/>
447
			List<Element> listLinks = elLinks.getChildren("Link", sddNamespace);
448
			Media link = Media.NewInstance();
449
			MediaRepresentation mr = MediaRepresentation.NewInstance();
450
			int k = 0;
451
			//for each Link
452
			for (Element elLink : listLinks){
453

    
454
				try {
455
					//TODO
456
					String rel = elLink.getAttributeValue("rel");
457
					String href = elLink.getAttributeValue("href");
458
					URI uri = new URI(href);
459
					mr.addRepresentationPart(MediaRepresentationPart.NewInstance(uri, null));
460
					link.addRepresentation(mr);
461
					ime.addMedia(link);
462

    
463
				} catch (Exception e) {
464
					//FIXME
465
					logger.warn("Import of Link " + k + " failed.");
466
				}
467

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

    
470
			}
471
		}
472
	}
473

    
474
	/**
475
	 * @param sddNamespace
476
	 * @param ve
477
	 * @param elRepresentation
478
	 * @param langLabDet
479
	 * @return
480
	 */
481
	private void makeRepresentationForIdentifiableEntity(Namespace sddNamespace, IdentifiableEntity<?> ie, 
482
					Element elRepresentation, Map<Language, List<String>> langLabDet) {
483
		List<String> labDet = null;
484

    
485
		if (ie instanceof TaxonNameBase) {
486
			if (langLabDet.keySet().contains(getTermService().getLanguageByIso("la"))) {
487
				labDet = langLabDet.get(getTermService().getLanguageByIso("la"));
488
			} else if (langLabDet.keySet().contains(datasetLanguage)) {
489
				labDet = langLabDet.get(datasetLanguage);
490
				logger.info("TaxonName " + (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace) + " is not specified as a latin name.");
491
			} else {
492
				labDet = langLabDet.get(langLabDet.keySet().iterator().next());
493
				logger.info("TaxonName " + (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace) + " is not specified as a latin name.");
494
			}
495
		} else {
496
			labDet = langLabDet.get(langLabDet.keySet().iterator().next());
497
		}
498

    
499
		//FIXME labDet is != null only for TaxonNameBase
500
		ie.setTitleCache(labDet.get(0), true);
501

    
502
		if (labDet.size()>1) {
503
			Annotation annotation = null;
504
			if (labDet.get(1) != null) {
505
				if (labDet.get(2) != null) {
506
					annotation = Annotation.NewInstance(labDet.get(2) + " - " + labDet.get(1), datasetLanguage);
507
				} else {
508
					annotation = Annotation.NewInstance(labDet.get(1), datasetLanguage);
509
				}
510
			}
511
			ie.addAnnotation(annotation);
512
		}
513
		return;
514
	}
515

    
516
	/**
517
	 * @param elLabel
518
	 * @return
519
	 */
520
	private Language getLanguage(Element elLanguage) {
521
		String lang = elLanguage.getAttributeValue("lang",xmlNamespace);
522
		Language language = null;
523
		if (StringUtils.isNotBlank(lang)) {
524
			language = getTermService().getLanguageByIso(lang.substring(0, 2));
525
		} else {
526
			language = datasetLanguage;
527
		}
528
		return language;
529
	}
530
	
531

    
532
	// imports the representation (label, detail, lang) of a particular SDD element
533
	protected void importTechnicalMetadata(Element root, Namespace sddNamespace, SDDImportConfigurator sddConfig){
534
		Element elTechnicalMetadata = root.getChild("TechnicalMetadata", sddNamespace);
535
		String nameCreated = elTechnicalMetadata.getAttributeValue("created");
536
		sourceReference = sddConfig.getSourceReference();
537

    
538
		if (nameCreated != null) {
539
			if (!nameCreated.equals("")) {
540
				int year = Integer.parseInt(nameCreated.substring(0,4));
541
				int monthOfYear = Integer.parseInt(nameCreated.substring(5,7));
542
				int dayOfMonth = Integer.parseInt(nameCreated.substring(8,10));
543
				int hourOfDay = Integer.parseInt(nameCreated.substring(11,13));
544
				int minuteOfHour = Integer.parseInt(nameCreated.substring(14,16));
545
				int secondOfMinute = Integer.parseInt(nameCreated.substring(17,19));
546
				DateTime created = new DateTime(year,monthOfYear,dayOfMonth,hourOfDay,minuteOfHour,secondOfMinute,0);
547
				sourceReference.setCreated(created);
548
				sec.setCreated(created);
549
			}
550
		}
551

    
552
		// <Generator name="n/a, handcrafted instance document" version="n/a"/>
553
		Element elGenerator = elTechnicalMetadata.getChild("Generator", sddNamespace);
554
		generatorName = elGenerator.getAttributeValue("name");
555
		generatorVersion = elGenerator.getAttributeValue("version");
556

    
557
		sec.addAnnotation(Annotation.NewDefaultLanguageInstance(generatorName + " - " + generatorVersion));
558
		sourceReference.addAnnotation(Annotation.NewDefaultLanguageInstance(generatorName + " - " + generatorVersion));
559

    
560
	}
561

    
562
	// imports the complete dataset information
563
	protected void importDataset(Element elDataset, Namespace sddNamespace, SDDImportState state){			// <Dataset xml:lang="en-us">
564
//		SDDImportConfigurator sddConfig = state.getConfig();
565

    
566
		workingSet = WorkingSet.NewInstance();
567
		importDatasetLanguage(elDataset,state);
568
		importDatasetRepresentation(elDataset, sddNamespace);
569
		importRevisionData(elDataset, sddNamespace);
570
		importIPRStatements(elDataset, sddNamespace, state);
571
		importTaxonNames(elDataset, sddNamespace, state);
572

    
573
		importDescriptiveConcepts(elDataset, sddNamespace, state);
574
		importCharacters(elDataset, sddNamespace, state);
575
		importCharacterTrees(elDataset, sddNamespace, state);
576
		
577
		MarkerType editorMarkerType = getMarkerType(state, SDDTransformer.uuidMarkerEditor, "editor", "Editor", "edt");
578
		MarkerType geographicAreaMarkerType = getMarkerType(state, SDDTransformer.uuidMarkerSDDGeographicArea, "SDDGeographicArea", "SDDGeographicArea", "ga"); 
579
		MarkerType descriptiveConceptMarkerType = getMarkerType(state, SDDTransformer.uuidMarkerDescriptiveConcept, "DescriptiveConcept", "Descriptive Concept", "DC");
580
		markerTypes.add(editorMarkerType);
581
		markerTypes.add(geographicAreaMarkerType);
582
		markerTypes.add(descriptiveConceptMarkerType);
583
		
584
		//saving of all imported data into the CDM db
585
		saveVocabularies();
586
		saveFeatures();
587
		saveModifiers();
588
		saveStates();
589
		saveMarkerType();
590
		saveAreas(geographicAreaMarkerType);		
591
		saveUnits();
592
		saveStatisticalMeasure();		
593
		saveAnnotationType();
594

    
595
		importCodedDescriptions(elDataset, sddNamespace, state);
596
		importAgents(elDataset, sddNamespace, state);
597
		importPublications(elDataset, sddNamespace, state);
598
		importMediaObjects(elDataset, sddNamespace, state);
599
		importTaxonHierarchies(elDataset, sddNamespace, state);
600
		importGeographicAreas(elDataset, sddNamespace, state);
601
		importSpecimens(elDataset,sddNamespace, state);
602
		
603
		
604
		if ((authors != null)||(editors != null)) {
605
			Team team = Team.NewInstance();
606
			if (authors != null) {
607
				for (Person author : authors.values()){
608
					team.addTeamMember(author);
609
				}
610
			}
611
			if (editors != null) {
612
				Marker marker = Marker.NewInstance();
613
				marker.setMarkerType(editorMarkerType);
614
				for (Person editor : editors.values()){
615
					Person edit = editor;
616
					edit.addMarker(marker);
617
					team.addTeamMember(edit);
618
				}
619
			}
620
			sec.setAuthorTeam(team);
621
			sourceReference.setAuthorTeam(team);
622
		}
623

    
624
		if (copyright != null) {
625
			sourceReference.addRights(copyright);
626
			sec.addRights(copyright);
627
		}
628
		
629
		// Returns a CdmApplicationController created by the values of this configuration.
630
		IDescriptionService descriptionService = getDescriptionService();
631
		
632
		for (TaxonDescription taxonDescription : taxonDescriptions.values()){
633
			// Persists a Description
634
			descriptionService.save(taxonDescription);
635
		}
636
		
637
		for (String ref : taxonDescriptions.keySet()){
638
			TaxonDescription td = taxonDescriptions.get(ref);
639
			if (citations.containsKey(ref)) {
640
				Reference publication = publications.get(citations.get(ref));
641
				if (locations.containsKey(ref)) {
642
					Annotation location = Annotation.NewInstance(locations.get(ref), datasetLanguage);
643
					AnnotationType annotationType = AnnotationType.NewInstance("", "location", "");
644
					annotationTypes.add(annotationType);
645
					location.setAnnotationType(annotationType);
646
					(publication).addAnnotation(location);
647
				}
648
				td.addSource(null, null, publication, null);
649
			}
650
		}
651
		logger.info("end makeTaxonDescriptions ...");
652

    
653
		if (descriptiveConcepts != null) {
654
			for (Feature feature : descriptiveConcepts) {
655
				Marker marker = Marker.NewInstance();
656
				marker.setMarkerType(descriptiveConceptMarkerType);
657
				feature.addMarker(marker);
658
			}
659
		}
660
		saveFeatures();
661
		
662
		for (Reference publication : publications.values()){
663
			getReferenceService().save(publication); 
664
		}
665
		
666
		for (Reference source : sources){
667
			getReferenceService().save(source); 
668
		}
669

    
670
		for (FeatureTree featureTree : featureTrees) {
671
			getFeatureTreeService().save(featureTree);
672
		}
673
		getWorkingSetService().save(workingSet);
674
		for (Classification classification : classifications) {
675
			getClassificationService().save(classification);
676
		}
677
		for (Specimen specimen : specimens.values()) {
678
			getOccurrenceService().save(specimen);
679
		}
680
		logger.info("end of persistence ...");
681
		
682
		return;
683
	}
684

    
685
	/**
686
	 * 
687
	 */
688
	private void saveVocabularies() {
689
		for (TermVocabulary vocabulary : vocabularies ){
690
			getVocabularyService().save(vocabulary);
691
		}
692
		
693
	}
694

    
695
	private void saveAnnotationType() {
696
		for (AnnotationType annotationType: annotationTypes){
697
			getTermService().save(annotationType); 
698
		}
699
	}
700

    
701
	private void saveStatisticalMeasure() {
702
		for (StatisticalMeasure sm : statisticalMeasures){
703
			getTermService().save(sm); 
704
		}
705
	}
706

    
707
	private void saveUnits() {
708
		if (units != null) {
709
			for (MeasurementUnit unit : units.values()){
710
				if (unit != null) {
711
					getTermService().save(unit); 
712
				}
713
			}
714
		}
715
	}
716

    
717
	private void saveAreas(MarkerType geographicAreaMarkerType) {
718
		for (NamedArea area : namedAreas.values() ){
719
			Marker marker = Marker.NewInstance();
720
			marker.setMarkerType(geographicAreaMarkerType);
721
			area.addMarker(marker);
722
			getTermService().save(area);
723
		}
724
	}
725

    
726
	private void saveStates() {
727
		for (State state : states.values() ){
728
			getTermService().save(state);
729
		}
730
	}
731

    
732
	private void saveMarkerType() {
733
		for (MarkerType markerType : markerTypes){
734
			getTermService().save(markerType);
735
		}
736
	}
737

    
738
	private void saveModifiers() {
739
		for (Modifier modifier : modifiers.values() ){
740
			getTermService().save(modifier);
741
		}
742
	}
743

    
744
	private void saveFeatures() {
745
		for (Feature feature : features.values() ){
746
			getTermService().save(feature);
747
		}
748
	}
749

    
750
	// imports the default language of the dataset
751
	protected void importDatasetLanguage(Element elDataset, SDDImportState state){
752
		String nameLang = elDataset.getAttributeValue("lang",xmlNamespace);
753

    
754
		if (StringUtils.isNotBlank(nameLang)) {
755
			String iso = nameLang.substring(0, 2);
756
			datasetLanguage = getTermService().getLanguageByIso(iso);
757
		} else {
758
			datasetLanguage = Language.DEFAULT();
759
		}
760
		if (datasetLanguage == null) {
761
			datasetLanguage = Language.DEFAULT();
762
		}
763
	}
764
	
765
	// imports the specimens
766
	protected void importSpecimens(Element elDataset, Namespace sddNamespace, SDDImportState cdmState) {
767
		logger.info("start Specimens ...");
768
		/*	<Specimens>
769
        		<Specimen id="sp1">
770
           			<Representation>
771
              			<Label>TJM45337</Label>
772
           			</Representation>
773
        		</Specimen>
774
     		</Specimens>
775
		 */
776
		Element elSpecimens = elDataset.getChild("Specimens",sddNamespace);
777
		if (elSpecimens != null){
778
			List<Element> listSpecimens = elSpecimens.getChildren("Specimen", sddNamespace);
779
			int j = 0;
780
			for (Element elSpecimen : listSpecimens) {
781
				String id = elSpecimen.getAttributeValue("id");
782
				Specimen specimen = null;
783
				if (!id.equals("")) {
784
					specimen = Specimen.NewInstance();
785
					specimens.put(id,specimen);
786
					importRepresentation(elSpecimen, sddNamespace, specimen, id, cdmState);
787
				}
788
			}
789

    
790
		}
791
	}
792

    
793
	// imports the revision data associated with the Dataset (authors, modifications)
794
	protected void importRevisionData(Element elDataset, Namespace sddNamespace){
795
		// <RevisionData>
796
		logger.info("start RevisionData ...");
797
		Element elRevisionData = elDataset.getChild("RevisionData",sddNamespace);
798
		if (elRevisionData != null){
799
			// <Creators>
800
			Element elCreators = elRevisionData.getChild("Creators",sddNamespace);
801

    
802
			// <Agent role="aut" ref="a1"/>
803
			List<Element> listAgents = elCreators.getChildren("Agent", sddNamespace);
804

    
805
			int j = 0;
806
			//for each Agent
807
			for (Element elAgent : listAgents){
808

    
809
				String role = elAgent.getAttributeValue("role");
810
				String ref = elAgent.getAttributeValue("ref");
811
				if (role.equals("aut")) {
812
					if(!ref.equals("")) {
813
						authors.put(ref, null);
814
					}
815
				}
816
				if (role.equals("edt")) {
817
					if(!ref.equals("")) {
818
						editors.put(ref, null);
819
					}
820
				}
821
				if ((++j % modCount) == 0){ logger.info("Agents handled: " + j);}
822

    
823
			}
824

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

    
828
			if (stringDateModified != null) {
829
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");
830
				Date d = null;
831
				try {
832
					d = sdf.parse(stringDateModified);
833
				} catch(Exception e) {
834
					System.err.println("Exception :");
835
					e.printStackTrace();
836
				}
837

    
838
				DateTime updated = null;
839
				if (d != null) {
840
					updated = new DateTime(d);
841
					sourceReference.setUpdated(updated);
842
					sec.setUpdated(updated);
843
				}
844
			}
845
		}
846
	}
847

    
848
	// imports ipr statements associated with a dataset
849
	protected void importIPRStatements(Element elDataset, Namespace sddNamespace, SDDImportState state){
850
		// <IPRStatements>
851
		logger.info("start IPRStatements ...");
852
		Element elIPRStatements = elDataset.getChild("IPRStatements",sddNamespace);
853
		// <IPRStatement role="Copyright">
854
		if (elIPRStatements != null) {
855
			List<Element> listIPRStatements = elIPRStatements.getChildren("IPRStatement", sddNamespace);
856
			int j = 0;
857
			//for each IPRStatement
858

    
859
			for (Element elIPRStatement : listIPRStatements){
860

    
861
				String role = elIPRStatement.getAttributeValue("role");
862
				// <Label xml:lang="en-au">(c) 2003-2006 Centre for Occasional Botany.</Label>
863
				Element elLabel = elIPRStatement.getChild("Label",sddNamespace);
864
				String lang = "";
865
				if (elLabel != null) {
866
					lang = elLabel.getAttributeValue("lang",xmlNamespace);
867
				}
868
				String label = (String)ImportHelper.getXmlInputValue(elIPRStatement, "Label",sddNamespace);
869

    
870
				if (role.equals("Copyright")) {
871
					Language iprLanguage = null;
872
					if (lang != null) {
873
						if (!lang.equals("")) {
874
							iprLanguage = getTermService().getLanguageByIso(lang.substring(0, 2));
875
						} else {
876
							iprLanguage = datasetLanguage;
877
						}
878
					}
879
					if (iprLanguage == null) {
880
						iprLanguage = datasetLanguage;
881
					}
882
					copyright = Rights.NewInstance(label, iprLanguage);
883
				}
884

    
885
				if (copyright != null) {
886
					sourceReference.addRights(copyright);
887
					sec.addRights(copyright);
888
				}
889

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

    
892
			}
893
		}
894
	}
895

    
896
	// imports the taxon names
897
	protected void importTaxonNames(Element elDataset, Namespace sddNamespace, SDDImportState cdmState){
898
		// <TaxonNames>
899
		logger.info("start TaxonNames ...");
900
		Element elTaxonNames = elDataset.getChild("TaxonNames",sddNamespace);
901
		// <TaxonName id="t1" uri="urn:lsid:authority:namespace:my-own-id">
902
		if (elTaxonNames != null) {
903
			List<Element> listTaxonNames = elTaxonNames.getChildren("TaxonName", sddNamespace);
904
			int j = 0;
905
			//for each TaxonName
906
			for (Element elTaxonName : listTaxonNames){
907

    
908
				String id = elTaxonName.getAttributeValue("id");
909
				String uri = elTaxonName.getAttributeValue("uri");
910

    
911
				NonViralName tnb = null;
912
				if (!id.equals("")) {
913
					tnb = NonViralName.NewInstance(null);
914
					IdentifiableSource source = null;
915
					if (uri != null) {
916
						if (!uri.equals("")) {
917
							source = IdentifiableSource.NewInstance(id, "TaxonName", ReferenceFactory.newGeneric(), uri);
918
						}
919
					} else {
920
						source = IdentifiableSource.NewInstance(id, "TaxonName");
921
					}
922
					tnb.addSource(source);
923
					taxonNameBases.put(id,tnb);
924
				}
925

    
926
				// <Representation>
927
				// <Label xml:lang="la">Viola hederacea Labill.</Label>
928
				importRepresentation(elTaxonName, sddNamespace, tnb, id, cdmState);
929

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

    
932
			}
933
		}
934
	}
935

    
936
	// imports the characters (categorical, quantitative and text ; sequence characters not supported) which correspond to CDM Features
937
	protected void importCharacters(Element elDataset, Namespace sddNamespace, SDDImportState state){
938
		// <Characters>
939
		logger.info("start Characters ...");
940
		Element elCharacters = elDataset.getChild("Characters", sddNamespace);
941

    
942
		// <CategoricalCharacter id="c1">
943
		if (elCharacters != null) {
944
			handleCategoricalData(sddNamespace, state, elCharacters);
945
			handleQuantitativeData(sddNamespace, state, elCharacters);
946
			handleTextCharacters(sddNamespace, state, elCharacters);
947
		}
948

    
949
		/*for (Iterator<Feature> f = features.values().iterator() ; f.hasNext() ;){
950
			featureSet.add(f.next()); //XIM Why this line ?
951
		}*/
952
		
953
		return;
954

    
955
	}
956

    
957
	/**
958
	 * @param sddNamespace
959
	 * @param cdmState
960
	 * @param elCharacters
961
	 * @return
962
	 */
963
	private void handleCategoricalData(Namespace sddNamespace, SDDImportState cdmState, Element elCharacters) {
964
		List<Element> elCategoricalCharacters = elCharacters.getChildren("CategoricalCharacter", sddNamespace);
965
		int j = 0;
966
		for (Element elCategoricalCharacter : elCategoricalCharacters){
967
			try {
968

    
969
				String idCC = elCategoricalCharacter.getAttributeValue("id");
970
				Feature categoricalCharacter = Feature.NewInstance();
971
				categoricalCharacter.setKindOf(Feature.DESCRIPTION());
972
				importRepresentation(elCategoricalCharacter, sddNamespace, categoricalCharacter, idCC, cdmState);
973
				categoricalCharacter.setSupportsCategoricalData(true);
974

    
975
				// <States>
976
				Element elStates = elCategoricalCharacter.getChild("States",sddNamespace);
977

    
978
				// <StateDefinition id="s1">
979
				List<Element> elStateDefinitions = elStates.getChildren("StateDefinition",sddNamespace);
980
				TermVocabulary<State> termVocabularyState = TermVocabulary.NewInstance(null, null, null, null);
981
				
982
				vocabularies.add(termVocabularyState);
983
				
984
				int k = 0;
985
				//for each StateDefinition
986
				for (Element elStateDefinition : elStateDefinitions){
987

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

    
990
					String idS = elStateDefinition.getAttributeValue("id");
991
					State state = states.get(idS);
992
					if (state == null){
993
						state = State.NewInstance();
994
					}else{
995
						logger.debug("State duplicate found");
996
					}
997
					importRepresentation(elStateDefinition, sddNamespace, state, idS, cdmState);
998

    
999
					termVocabularyState.addTerm(state);
1000
					states.put(idS,state);
1001
				}
1002
				categoricalCharacter.addSupportedCategoricalEnumeration(termVocabularyState);
1003
				features.put(idCC, categoricalCharacter);
1004

    
1005
			} catch (Exception e) {
1006
				logger.warn("Import of CategoricalCharacter " + j + " failed.");
1007
				cdmState.setUnsuccessfull();
1008
			}
1009

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

    
1012
		}
1013
		return;
1014
	}
1015

    
1016
	/**
1017
	 * @param sddNamespace
1018
	 * @param sddConfig
1019
	 * @param elCharacters
1020
	 */
1021
	private void handleQuantitativeData(Namespace sddNamespace,	SDDImportState cdmState, Element elCharacters) {
1022
		int j;
1023
		// <QuantitativeCharacter id="c2">
1024
		List<Element> elQuantitativeCharacters = elCharacters.getChildren("QuantitativeCharacter", sddNamespace);
1025
		j = 0;
1026
		//for each QuantitativeCharacter
1027
		for (Element elQuantitativeCharacter : elQuantitativeCharacters){
1028

    
1029
			try {
1030

    
1031
				String idQC = elQuantitativeCharacter.getAttributeValue("id");
1032

    
1033
				// <Representation>
1034
				//  <Label>Leaf length</Label>
1035
				// </Representation>
1036
				Feature quantitativeCharacter = Feature.NewInstance();
1037
				quantitativeCharacter.setKindOf(Feature.DESCRIPTION());
1038
				importRepresentation(elQuantitativeCharacter, sddNamespace, quantitativeCharacter, idQC, cdmState);
1039

    
1040
				quantitativeCharacter.setSupportsQuantitativeData(true);
1041

    
1042
				// <MeasurementUnit>
1043
				//  <Label role="Abbrev">m</Label>
1044
				// </MeasurementUnit>
1045
				Element elMeasurementUnit = elQuantitativeCharacter.getChild("MeasurementUnit",sddNamespace);
1046
				String label = "";
1047
				String role = "";
1048
				if (elMeasurementUnit != null) {
1049
					Element elLabel = elMeasurementUnit.getChild("Label",sddNamespace);
1050
					role = elLabel.getAttributeValue("role");
1051
					label = (String)ImportHelper.getXmlInputValue(elMeasurementUnit, "Label",sddNamespace);
1052
				}
1053

    
1054
				MeasurementUnit unit = null;
1055
				if (!label.equals("")){
1056
					if (role != null) {
1057
						if (role.equals("Abbrev")){
1058
							unit = MeasurementUnit.NewInstance(label,label,label);
1059
						}
1060
					} else {
1061
						unit = MeasurementUnit.NewInstance(label,label,label);
1062
					}
1063
				}
1064

    
1065
				if (unit != null) {
1066
					units.put(idQC, unit);
1067
				}
1068

    
1069
				//<Default>
1070
				//  <MeasurementUnitPrefix>milli</MeasurementUnitPrefix>
1071
				//</Default>
1072
				Element elDefault = elQuantitativeCharacter.getChild("Default",sddNamespace);
1073
				if (elDefault != null) {
1074
					String measurementUnitPrefix = (String)ImportHelper.getXmlInputValue(elDefault, "MeasurementUnitPrefix",sddNamespace);
1075
					if (! measurementUnitPrefix.equals("")){
1076
						defaultUnitPrefixes.put(idQC, measurementUnitPrefix);
1077
					}
1078
				}
1079

    
1080
				features.put(idQC, quantitativeCharacter);
1081

    
1082
			} catch (Exception e) {
1083
				//FIXME
1084
				logger.warn("Import of QuantitativeCharacter " + j + " failed.");
1085
				cdmState.setUnsuccessfull();
1086
			}
1087

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

    
1090
		}
1091
		return;
1092
	}
1093

    
1094
	private void handleTextCharacters(Namespace sddNamespace, SDDImportState cdmState, Element elCharacters) {
1095
		int j;
1096
		// <TextCharacter id="c3">
1097
		List<Element> elTextCharacters = elCharacters.getChildren("TextCharacter", sddNamespace);
1098
		j = 0;
1099
		//for each TextCharacter
1100
		for (Element elTextCharacter : elTextCharacters){
1101

    
1102
			try {
1103

    
1104
				String idTC = elTextCharacter.getAttributeValue("id");
1105

    
1106
				// <Representation>
1107
				//  <Label xml:lang="en">Leaf features not covered by other characters</Label>
1108
				// </Representation>
1109
				Feature textCharacter = Feature.NewInstance();
1110
				textCharacter.setKindOf(Feature.DESCRIPTION());
1111
				importRepresentation(elTextCharacter, sddNamespace, textCharacter, idTC, cdmState);
1112

    
1113
				textCharacter.setSupportsTextData(true);
1114

    
1115
				features.put(idTC, textCharacter);
1116

    
1117
			} catch (Exception e) {
1118
				//FIXME
1119
				logger.warn("Import of TextCharacter " + j + " failed.");
1120
				cdmState.setUnsuccessfull();
1121
			}
1122

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

    
1125
		}
1126
		return;
1127
	}
1128

    
1129
	// imports the descriptions of taxa
1130
	protected void importCodedDescriptions(Element elDataset, Namespace sddNamespace, SDDImportState cdmState){
1131
		
1132
		// <CodedDescriptions>
1133
		logger.info("start CodedDescriptions ...");
1134
		Element elCodedDescriptions = elDataset.getChild("CodedDescriptions",sddNamespace);
1135
		
1136
		// <CodedDescription id="D101">
1137
		if (elCodedDescriptions != null) {
1138
			List<Element> listCodedDescriptions = elCodedDescriptions.getChildren("CodedDescription", sddNamespace);
1139
			int j = 0;
1140
			//for each CodedDescription
1141
			for (Element elCodedDescription : listCodedDescriptions){
1142
				handleCodedDescription(sddNamespace, cdmState, elCodedDescription, j);
1143
				if ((++j % modCount) == 0){ logger.info("CodedDescriptions handled: " + j);}
1144
			}
1145
		}
1146
		return;
1147
	}
1148

    
1149
	/**
1150
	 * @param sddNamespace
1151
	 * @param sddConfig
1152
	 * @param j
1153
	 * @param elCodedDescription
1154
	 * @return
1155
	 */
1156
	private void handleCodedDescription(Namespace sddNamespace, SDDImportState cdmState, Element elCodedDescription, int j) {
1157
		try {
1158

    
1159
			String idCD = elCodedDescription.getAttributeValue("id");
1160

    
1161
			// <Representation>
1162
			//  <Label>&lt;i&gt;Viola hederacea&lt;/i&gt; Labill. as revised by R. Morris April 8, 2006</Label>
1163
			// </Representation>
1164
			TaxonDescription taxonDescription = TaxonDescription.NewInstance();
1165
			if (!generatorName.isEmpty()){
1166
				Annotation annotation = Annotation.NewInstance(generatorName, AnnotationType.TECHNICAL(),Language.DEFAULT());
1167
				taxonDescription.addAnnotation(annotation);
1168
			}
1169
			importRepresentation(elCodedDescription, sddNamespace, taxonDescription, idCD, cdmState);
1170

    
1171
			// <Scope>
1172
			//  <TaxonName ref="t1"/>
1173
			//  <Citation ref="p1" location="p. 30"/>
1174
			// </Scope>
1175
			Element elScope = elCodedDescription.getChild("Scope", sddNamespace);
1176
			Taxon taxon;
1177
			if (elScope != null) {
1178
				taxon = handleCDScope(sddNamespace, cdmState, idCD, elScope);
1179
			} else {//in case no taxon is linked to the description, a new one is created
1180
				taxon = handleCDNoScope(sddNamespace, cdmState, elCodedDescription);
1181
			}
1182

    
1183
			// <SummaryData>
1184
			Element elSummaryData = elCodedDescription.getChild("SummaryData",sddNamespace);
1185
			if (elSummaryData != null) {
1186
				handleSummaryCategoricalData(sddNamespace, taxonDescription, elSummaryData);
1187
				handleSummaryQuantitativeData(sddNamespace, taxonDescription, elSummaryData);
1188
				handleSummaryTextData(sddNamespace, taxonDescription, elSummaryData);
1189
			}
1190

    
1191
			if (taxon != null) {
1192
				taxon.addDescription(taxonDescription);
1193
			}
1194
//			
1195
			workingSet.addDescription(taxonDescription);
1196
			
1197
//OLD			taxonDescription.setDescriptiveSystem(featureSet);
1198

    
1199
			taxonDescriptions.put(idCD, taxonDescription);//FIXME
1200

    
1201
		} catch (Exception e) {
1202
			//FIXME
1203
			logger.warn("Import of CodedDescription " + j + " failed.", e);
1204
			cdmState.setUnsuccessfull();
1205
		}
1206
		return;
1207
	}
1208

    
1209
	/**
1210
	 * @param sddNamespace
1211
	 * @param sddConfig
1212
	 * @param elCodedDescription
1213
	 * @param taxon
1214
	 * @return
1215
	 */
1216
	private Taxon handleCDNoScope(Namespace sddNamespace,
1217
			SDDImportState cdmState, Element elCodedDescription	) {
1218
		Taxon taxon = null;
1219
		NonViralName nonViralName = NonViralName.NewInstance(null);
1220
		String id = new String("" + taxonNamesCount);
1221
		IdentifiableSource source = IdentifiableSource.NewInstance(id, "TaxonName");
1222
		importRepresentation(elCodedDescription, sddNamespace, nonViralName, id, cdmState);
1223
		
1224
		if(cdmState.getConfig().isDoMatchTaxa()){
1225
			taxon = getTaxonService().findBestMatchingTaxon(nonViralName.getTitleCache());
1226
		}
1227
		
1228
		if(taxon != null){
1229
			nonViralName = HibernateProxyHelper.deproxy(taxon.getName(), NonViralName.class);
1230
//							taxonNameBases.put(id ,tnb);
1231
//							taxonNamesCount++;
1232
			logger.info("using existing Taxon " + taxon.getTitleCache());
1233
		} else {
1234
			nonViralName.addSource(source);
1235
			taxonNameBases.put(id ,nonViralName);
1236
			taxonNamesCount++;						
1237
			logger.info("creating new Taxon from TaxonName " + nonViralName.getTitleCache());
1238
			taxon = Taxon.NewInstance(nonViralName, sec);
1239
		}
1240
		return taxon;
1241
	}
1242

    
1243
	/**
1244
	 * @param sddNamespace
1245
	 * @param sddConfig
1246
	 * @param idCD
1247
	 * @param elScope
1248
	 * @param taxon
1249
	 * @return
1250
	 */
1251
	private Taxon handleCDScope(Namespace sddNamespace, SDDImportState cdmState, 
1252
			String idCD, Element elScope) {
1253
		Taxon taxon = null;
1254
		Element elTaxonName = elScope.getChild("TaxonName", sddNamespace);
1255
		String ref = elTaxonName.getAttributeValue("ref");
1256
		NonViralName nonViralName = taxonNameBases.get(ref);
1257
		
1258
		if(cdmState.getConfig().isDoMatchTaxa()){
1259
			taxon = getTaxonService().findBestMatchingTaxon(nonViralName.getTitleCache());
1260
		}
1261
		
1262
		if(taxon != null){
1263
			logger.info("using existing Taxon" + taxon.getTitleCache());
1264
			if(!nonViralName.getUuid().equals(taxon.getName().getUuid())){
1265
				logger.warn("TaxonNameBase entity of existing taxon does not match Name in list -> replacing Name in list");
1266
				nonViralName = HibernateProxyHelper.deproxy(taxon.getName(), NonViralName.class);
1267
			}				
1268
		} else {							
1269
			logger.info("creating new Taxon from TaxonName '" + nonViralName.getTitleCache()+"'");
1270
			taxon = Taxon.NewInstance(nonViralName, sec);
1271
		}
1272
		
1273
		//citation
1274
		Element elCitation = elScope.getChild("Citation",sddNamespace);
1275
		if (elCitation != null) {
1276
			String refCitation = elCitation.getAttributeValue("ref");
1277
			if (! refCitation.equals("")){
1278
				citations.put(idCD, refCitation);
1279
			}
1280
			String location = elCitation.getAttributeValue("location");
1281
			if (! location.equals("")){
1282
				locations.put(idCD, location);
1283
			}
1284
		}
1285
		return taxon;
1286
	}
1287

    
1288
	/**
1289
	 * @param sddNamespace
1290
	 * @param taxonDescription
1291
	 * @param elSummaryData
1292
	 */
1293
	private void handleSummaryTextData(Namespace sddNamespace,
1294
			TaxonDescription taxonDescription, Element elSummaryData) {
1295
		String ref;
1296
		int k;
1297
		// <TextChar ref="c3">
1298
		List<Element> elTextChars = elSummaryData.getChildren("TextChar", sddNamespace);
1299
		k = 0;
1300
		//for each TextChar
1301
		for (Element elTextChar : elTextChars){
1302
			if ((++k % modCount) == 0){ logger.info("TextChar handled: " + (k-1));}
1303
			ref = elTextChar.getAttributeValue("ref");
1304
			Feature feature = features.get(ref);
1305
			TextData textData = TextData.NewInstance();
1306
			textData.setFeature(feature);
1307

    
1308
			// <Content>Free form text</Content>
1309
			String content = (String)ImportHelper.getXmlInputValue(elTextChar, "Content",sddNamespace);
1310
			textData.putText(datasetLanguage, content);
1311
			taxonDescription.addElement(textData);
1312
		}
1313
	}
1314

    
1315
	/**
1316
	 * @param sddNamespace
1317
	 * @param taxonDescription
1318
	 * @param elSummaryData
1319
	 */
1320
	private void handleSummaryQuantitativeData(Namespace sddNamespace,
1321
			TaxonDescription taxonDescription, Element elSummaryData) {
1322
		String ref;
1323
		int k;
1324
		// <Quantitative ref="c2">
1325
		List<Element> elQuantitatives = elSummaryData.getChildren("Quantitative", sddNamespace);
1326
		k = 0;
1327
		//for each Quantitative
1328
		for (Element elQuantitative : elQuantitatives){
1329
			if ((++k % modCount) == 0){ logger.warn("Quantitative handled: " + (k-1));}
1330
			ref = elQuantitative.getAttributeValue("ref");
1331
			Feature feature = features.get(ref);
1332
			QuantitativeData quantitativeData = QuantitativeData.NewInstance();
1333
			quantitativeData.setFeature(feature);
1334

    
1335
			MeasurementUnit unit = units.get(ref);
1336
			String prefix = defaultUnitPrefixes.get(ref);
1337
			if (unit != null) {
1338
				String u = unit.getLabel();
1339
				if (prefix != null) {
1340
					u = prefix + u;
1341
				}
1342
				unit.setLabel(u);
1343
				quantitativeData.setUnit(unit);
1344
			}
1345

    
1346
			// <Measure type="Min" value="2.3"/>
1347
			List<Element> elMeasures = elQuantitative.getChildren("Measure", sddNamespace);
1348
			int l = 0;
1349
			
1350
			//for each State
1351
			for (Element elMeasure : elMeasures){
1352
				if ((++l % modCount) == 0){ logger.info("States handled: " + (l-1));}
1353
				String type = elMeasure.getAttributeValue("type");
1354
				String value = elMeasure.getAttributeValue("value");
1355
				if (value.contains(",")) {
1356
					value = value.replace(',', '.');
1357
				}
1358
				Float v = Float.parseFloat(value);
1359
				//Float v = new Float(0);
1360
				StatisticalMeasure t = null;
1361
				if (type.equals("Min")) {
1362
					t = StatisticalMeasure.MIN();
1363
				} else if (type.equals("Mean")) {
1364
					t = StatisticalMeasure.AVERAGE();
1365
				} else if (type.equals("Max")) {
1366
					t = StatisticalMeasure.MAX();
1367
				} else if (type.equals("SD")) {
1368
					t = StatisticalMeasure.STANDARD_DEVIATION();
1369
				} else if (type.equals("N")) {
1370
					t = StatisticalMeasure.SAMPLE_SIZE();
1371
				} else if (type.equals("UMethLower")) {
1372
					t = StatisticalMeasure.TYPICAL_LOWER_BOUNDARY();
1373
				} else if (type.equals("UMethUpper")) {
1374
					t = StatisticalMeasure.TYPICAL_UPPER_BOUNDARY();
1375
				} else if (type.equals("Var")) {
1376
					t = StatisticalMeasure.VARIANCE();
1377
				} else {
1378
					t = StatisticalMeasure.NewInstance(type,type,type);
1379
					statisticalMeasures.add(t);
1380
				}
1381

    
1382
				StatisticalMeasurementValue statisticalValue = StatisticalMeasurementValue.NewInstance();
1383
				statisticalValue.setValue(v);
1384
				statisticalValue.setType(t);
1385
				quantitativeData.addStatisticalValue(statisticalValue);
1386
				featureData.add(statisticalValue);
1387
			}
1388
			taxonDescription.addElement(quantitativeData);
1389
		}
1390
	}
1391

    
1392
	/**
1393
	 * @param sddNamespace
1394
	 * @param taxonDescription
1395
	 * @param elSummaryData
1396
	 */
1397
	private void handleSummaryCategoricalData(Namespace sddNamespace,
1398
			TaxonDescription taxonDescription, Element elSummaryData) {
1399
		String ref;
1400
		// <Categorical ref="c4">
1401
		List<Element> elCategoricals = elSummaryData.getChildren("Categorical", sddNamespace);
1402
		int k = 0;
1403
		//for each Categorical
1404
		for (Element elCategorical : elCategoricals){
1405
			if ((++k % modCount) == 0){ logger.warn("Categorical handled: " + (k-1));}
1406
			ref = elCategorical.getAttributeValue("ref");
1407
			Feature feature = features.get(ref);
1408
			CategoricalData categoricalData = CategoricalData.NewInstance();
1409
			categoricalData.setFeature(feature);
1410

    
1411
			// <State ref="s3"/>
1412
			List<Element> elStates = elCategorical.getChildren("State", sddNamespace);
1413
			int l = 0;
1414
			
1415
			//for each State
1416
			for (Element elState : elStates){
1417
				if ((++l % modCount) == 0){ logger.info("States handled: " + (l-1));}
1418
				ref = elState.getAttributeValue("ref");
1419
				State state = states.get(ref);
1420
				if (state != null) {
1421
					StateData stateData = StateData.NewInstance();
1422
					stateData.setState(state);
1423
					List<Element> elModifiers = elState.getChildren("Modifier", sddNamespace);
1424
					for (Element elModifier : elModifiers){
1425
						ref = elModifier.getAttributeValue("ref");
1426
						Modifier modifier = modifiers.get(ref);
1427
						if (modifier != null) {
1428
							stateData.addModifier(modifier);
1429
						}
1430
					}
1431
					categoricalData.addState(stateData);
1432
				}
1433
				taxonDescription.addElement(categoricalData);
1434
			}
1435
		}
1436
	}
1437

    
1438
	// imports the persons associated with the dataset creation, modification, related publications
1439
	protected void importAgents(Element elDataset, Namespace sddNamespace, SDDImportState cdmState){
1440
		// <Agents>
1441
		logger.info("start Agents ...");
1442
		Element elAgents = elDataset.getChild("Agents",sddNamespace);
1443
		if (elAgents != null) {
1444
			// <Agent id="a1">
1445
			List <Element> listAgents = elAgents.getChildren("Agent", sddNamespace);
1446
			int j = 0;
1447
			//for each Agent
1448
			for (Element elAgent : listAgents){
1449

    
1450
				try {
1451

    
1452
					String idA = elAgent.getAttributeValue("id");
1453

    
1454
					//  <Representation>
1455
					//   <Label>Kevin Thiele</Label>
1456
					//   <Detail role="Description">Ali Baba is also known as r.a.m.</Detail>
1457
					//  </Representation>
1458
					Person person = Person.NewInstance();
1459
					importRepresentation(elAgent, sddNamespace, person, idA, cdmState);
1460
					person.addSource(IdentifiableSource.NewInstance(idA, "Agent"));
1461

    
1462
					/*XIM <Links>
1463
					Element elLinks = elAgent.getChild("Links",sddNamespace);
1464

    
1465
					if (elLinks != null) {
1466

    
1467
						//  <Link rel="Alternate" href="http://www.diversitycampus.net/people/hagedorn"/>
1468
						List<Element> listLinks = elLinks.getChildren("Link", sddNamespace);
1469
						int k = 0;
1470
						//for each Link
1471
						for (Element elLink : listLinks){
1472

    
1473
							try {
1474

    
1475
								String rel = elLink.getAttributeValue("rel");
1476
								String href = elLink.getAttributeValue("href");
1477

    
1478
								Media link = Media.NewInstance();
1479
								MediaRepresentation mr = MediaRepresentation.NewInstance();
1480
								mr.addRepresentationPart(MediaRepresentationPart.NewInstance(href, null));
1481
								link.addRepresentation(mr);
1482
								person.addMedia(link);
1483

    
1484
							} catch (Exception e) {
1485
								//FIXME
1486
								logger.warn("Import of Link " + k + " failed.");
1487
								success = false; 
1488
							}
1489

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

    
1492
						}
1493
					}
1494
					*/
1495
					if (authors.containsKey(idA)) {
1496
						authors.put(idA,person);
1497
					}
1498

    
1499
					if (editors.containsKey(idA)) {
1500
						editors.put(idA, person);
1501
					}
1502

    
1503
				} catch (Exception e) {
1504
					//FIXME
1505
					logger.warn("Import of Agent " + j + " failed.");
1506
					cdmState.setUnsuccessfull();
1507
				}
1508

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

    
1511
			}
1512
		}
1513
	}
1514

    
1515
	// imports publications related with the data set
1516
	protected void importPublications(Element elDataset, Namespace sddNamespace, SDDImportState cdmState){
1517
		/* <Publications>
1518
			  <Publication id="p112">
1519
			    <Representation>
1520
			      <Label>Gee, X. & Haa, Y. (2003). How to be happy in five minutes. Instant Gratifications, Palm Beach.</Label>
1521
			    </Representation>
1522
			    <Links>
1523
			    <Link rel="BasedOn" href="doi:10.1992/32311"/>
1524
			    <Link rel="Alternate" href="http://some.service.net/providing/bibliographic.data"/>
1525
			    </Links>
1526
			</Publications>
1527
*/
1528
		logger.info("start Publications ...");
1529
		Element elPublications = elDataset.getChild("Publications",sddNamespace);
1530

    
1531
		if (elPublications != null) {
1532
			List<Element> listPublications = elPublications.getChildren("Publication", sddNamespace);
1533
			int j = 0;
1534
			for (Element elPublication : listPublications){
1535

    
1536
				try {
1537

    
1538
					String idP = elPublication.getAttributeValue("id");
1539
					Reference publication = ReferenceFactory.newArticle();
1540
					importRepresentation(elPublication, sddNamespace, publication, idP, cdmState);
1541

    
1542
					publications.put(idP,publication);
1543

    
1544
				} catch (Exception e) {
1545
					logger.warn("Import of Publication " + j + " failed.");
1546
					cdmState.setUnsuccessfull();
1547
				}
1548

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

    
1551
			}
1552
		}
1553
	}
1554

    
1555
	// imports media objects such as images //FIXME check mediaobj
1556
	protected void importMediaObjects(Element elDataset, Namespace sddNamespace, SDDImportState cdmState){
1557
		// <MediaObjects>
1558
		logger.info("start MediaObjects ...");
1559
		Element elMediaObjects = elDataset.getChild("MediaObjects",sddNamespace);
1560

    
1561
		if (elMediaObjects != null) {
1562
			// <MediaObject id="m1">
1563
			List<Element> listMediaObjects = elMediaObjects.getChildren("MediaObject", sddNamespace);
1564
			int j = 0;
1565
			for (Element elMO : listMediaObjects){
1566

    
1567
				String id = "";
1568

    
1569
				try {
1570
					String idMO = elMO.getAttributeValue("id");
1571
					id = idMO;
1572

    
1573
					//  <Representation>
1574
					//   <Label>Image description, e.g. to be used for alt-attribute in html.</Label>
1575
					//  </Representation>
1576
					Media media = Media.NewInstance();
1577
					importRepresentation(elMO, sddNamespace, media, idMO, cdmState);
1578

    
1579
					// <Type>Image</Type>
1580
					// <Source href="http://test.edu/test.jpg"/>
1581
					String type = (String)ImportHelper.getXmlInputValue(elMO,"Type",sddNamespace);
1582

    
1583
					if ((type != null) && (type.equals("Image"))) {
1584
						Element elSource = elMO.getChild("Source",sddNamespace);
1585
						String href = elSource.getAttributeValue("href");
1586

    
1587
						ImageInfo imageMetaData = null;
1588
						ImageFile image = null;
1589
						if (href.substring(0,7).equals("http://")) {
1590
							try{
1591
								URL url = new URL(href);
1592
								
1593
								imageMetaData = ImageInfo.NewInstance(url.toURI(), 0);
1594
								image = ImageFile.NewInstance(url.toURI(), null, imageMetaData);
1595
							} catch (MalformedURLException e) {
1596
								logger.error("Malformed URL", e);
1597
							}
1598
						} else {
1599
							String sns = cdmState.getConfig().getSourceNameString();
1600
							File f = new File(sns);
1601
							File parent = f.getParentFile();
1602
							String fi = parent.toString() + File.separator + href;
1603
							File file = new File(fi);
1604
							imageMetaData = ImageInfo.NewInstance(new URI(fi), 0); //file
1605
							image = ImageFile.NewInstance(file.toURI(), null, imageMetaData);
1606
						}
1607
						MediaRepresentation representation = MediaRepresentation.NewInstance(imageMetaData.getMimeType(), null);
1608
						representation.addRepresentationPart(image);
1609

    
1610
						media.addRepresentation(representation);
1611

    
1612
						ArrayList<CdmBase> lcb = (ArrayList<CdmBase>) mediaObject_ListCdmBase.get(idMO);
1613
						if (lcb != null) {
1614
							for (int k = 0; k < lcb.size(); k++) {
1615
								if (lcb.get(k) instanceof DefinedTermBase) {
1616
									DefinedTermBase dtb = (DefinedTermBase) lcb.get(k);
1617
									// if (lcb.get(0) instanceof DefinedTermBase) {
1618
									// DefinedTermBase dtb = (DefinedTermBase) lcb.get(0);
1619
									//									if (dtb!=null) {
1620
									//										if (k == 0) {
1621
									dtb.addMedia(media);
1622
									//System.out.println(dtb.getLabel());
1623
									//										} else {
1624
									//											Media me = (Media) media.clone();
1625
									//											dtb.addMedia(me);
1626
									//										}
1627
									//									}
1628
								} else if (lcb.get(k) instanceof Reference) {
1629
									Reference rb = (Reference) lcb.get(k);
1630
									//} else if (lcb.get(0) instanceof Reference) {
1631
									//Reference rb = (Reference) lcb.get(0);
1632
									// rb.setTitleCache(label);
1633
									//									if (rb!=null) {
1634
									//										if (k == 0) {
1635
									rb.addMedia(media);
1636
									//System.out.println(rb.getTitle());
1637
									//										} else {
1638
									//											Media me = (Media) media.clone();
1639
									//											rb.addMedia(me);
1640
									//										}
1641
									//									}
1642
								}/* else if (lcb.get(k) instanceof TaxonNameBase){
1643
									TaxonNameBase tb = (TaxonNameBase) lcb.get(k);
1644
									tb.addMedia(media);
1645
							}*/
1646
							}
1647
						}
1648
					}
1649

    
1650
				} catch (Exception e) {
1651
					//FIXME
1652
					logger.warn("Could not attach MediaObject " + j + "(SDD: " + id + ") to several objects.");
1653
					cdmState.setUnsuccessfull();
1654
				}
1655

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

    
1658
				}
1659
			}
1660
		}
1661
	}
1662

    
1663
	// imports the <DescriptiveConcepts> block ; DescriptiveConcepts are used as nodes in CharacterTrees and Characters as leaves
1664
	// but since Modifiers can be linked to DescriptiveConcepts they are stored as features with a particular Marker
1665
	protected void importDescriptiveConcepts(Element elDataset, Namespace sddNamespace, SDDImportState cdmState){
1666
		/* <DescriptiveConcepts>
1667
		      <DescriptiveConcept id="dc0">
1668
			        <Representation>
1669
			          <Label>Fixed set of modifiers supported in Lucid3</Label>
1670
			        </Representation>
1671
			        <Modifiers>
1672
			          <Modifier id="mod1">
1673
			            <Representation>
1674
			              <Label>rarely</Label>
1675
			            </Representation>
1676
			            <ModifierClass>Frequency</ModifierClass>
1677
			            <ProportionRange lowerestimate="0.0" upperestimate="0.25"/>
1678
			          </Modifier>
1679
		          </Modifiers>
1680
		        </DescriptiveConcept>
1681
	         </DescriptiveConcepts>
1682
		 */
1683
		logger.info("start DescriptiveConcepts ...");
1684
		Element elDescriptiveConcepts = elDataset.getChild("DescriptiveConcepts",sddNamespace);
1685
		if (elDescriptiveConcepts != null) {
1686
			List<Element> listDescriptiveConcepts = elDescriptiveConcepts.getChildren("DescriptiveConcept", sddNamespace);
1687
			int j = 0;
1688

    
1689
			for (Element elDescriptiveConcept : listDescriptiveConcepts){
1690
				try {
1691
				String id = elDescriptiveConcept.getAttributeValue("id");
1692
					Feature feature = Feature.NewInstance();
1693
					feature.setKindOf(Feature.DESCRIPTION());
1694
					if (!id.equals("")) {
1695
					//	 <Representation>
1696
					//       <Label>Body</Label>
1697
					importRepresentation(elDescriptiveConcept, sddNamespace, feature, id, cdmState);
1698
						features.put(id, feature);
1699
						getTermService().save(feature);//XIM
1700
						descriptiveConcepts.add(feature);
1701
						// imports the modifiers
1702
						Element elModifiers = elDescriptiveConcept.getChild("Modifiers", sddNamespace);
1703
					if (elModifiers !=null){
1704
						List<Element> listModifiers = elModifiers.getChildren("Modifier", sddNamespace);
1705
							TermVocabulary<Modifier> termVocabularyState = TermVocabulary.NewInstance(null, null, null, null);
1706
						for (Element elModifier : listModifiers) {
1707
								Modifier modif = Modifier.NewInstance();
1708
								String idmod = elModifier.getAttributeValue("id");
1709
								importRepresentation(elModifier, sddNamespace, modif, idmod, cdmState);
1710
								termVocabularyState.addTerm(modif);
1711
								//termVocabularyStates.add(termVocabularyState);
1712
								getVocabularyService().save(termVocabularyState);//XIM
1713
								modifiers.put(idmod, modif);
1714
						}
1715
							feature.addRecommendedModifierEnumeration(termVocabularyState);
1716
				}
1717

    
1718
					}
1719
				}
1720
				catch (Exception e) {
1721
					logger.warn("Import of DescriptiveConcept " + j + " failed: " + e.getMessage());
1722
				}
1723
				if ((++j % modCount) == 0){ logger.info("DescriptiveConcepts handled: " + j);}
1724

    
1725
			}
1726
		}
1727
	}
1728

    
1729
	// imports the <CharacterTrees> block
1730
	protected void importCharacterTrees(Element elDataset, Namespace sddNamespace, SDDImportState cdmState){
1731
		// <CharacterTrees>
1732
		logger.info("start CharacterTrees ...");
1733
		Element elCharacterTrees = elDataset.getChild("CharacterTrees",sddNamespace);
1734

    
1735
		if (elCharacterTrees != null) {
1736
			List<Element> listCharacterTrees = elCharacterTrees.getChildren("CharacterTree", sddNamespace);
1737
			int j = 0;
1738
			for (Element elCharacterTree : listCharacterTrees){
1739
				try {
1740
					Element elRepresentation = elCharacterTree.getChild("Representation",sddNamespace);
1741
					String label = (String)ImportHelper.getXmlInputValue(elRepresentation,"Label",sddNamespace);
1742
					//Element elDesignedFor = elCharacterTree.getChild("DesignedFor",sddNamespace);//TODO ?
1743

    
1744
					FeatureTree featureTree =  FeatureTree.NewInstance();
1745
					importRepresentation(elCharacterTree, sddNamespace, featureTree, "", cdmState);
1746
					FeatureNode root = featureTree.getRoot();
1747
					List<Element> listeOfNodes = elCharacterTree.getChildren("Nodes", sddNamespace);
1748

    
1749
					//Nodes of CharacterTrees in SDD always refer to DescriptiveConcepts
1750
					for (Element elNodes : listeOfNodes) {
1751
						handleCharacterNodes(sddNamespace, root, elNodes);
1752
					}
1753
					featureTrees.add(featureTree);
1754
					if (workingSet.getDescriptiveSystem() != null){
1755
						//TODO how to handle multiple 
1756
						logger.warn("Multiple feature trees not yet supported");
1757
					}else{
1758
						workingSet.setDescriptiveSystem(featureTree);
1759
					}
1760
				}
1761

    
1762
				catch (Exception e) {
1763
					logger.warn("Import of Character tree " + j + " failed.");
1764
					cdmState.setUnsuccessfull();
1765
				}
1766
				if ((++j % modCount) == 0){ logger.info("CharacterTrees handled: " + j);}
1767

    
1768
			}
1769

    
1770
		}
1771
	}
1772

    
1773
	/**
1774
	 * @param sddNamespace
1775
	 * @param root
1776
	 * @param elNodes
1777
	 */
1778
	private void handleCharacterNodes(Namespace sddNamespace, FeatureNode root, Element elNodes) {
1779
		List<Element> listNodes = elNodes.getChildren("Node", sddNamespace);
1780
		if (listNodes != null) {
1781
			for (Element elNode : listNodes){
1782
				String idN = elNode.getAttributeValue("id");
1783
				FeatureNode fn = null;
1784
				Feature dc = null;
1785
				if (idN!=null) {
1786
					// DescriptiveConcepts are used as nodes in CharacterTrees
1787
					Element elDescriptiveConcept = elNode.getChild("DescriptiveConcept", sddNamespace);
1788
					if (elDescriptiveConcept != null){
1789
						String refDC = elDescriptiveConcept.getAttributeValue("ref");
1790
						dc = features.get(refDC);
1791
						fn = FeatureNode.NewInstance(dc);
1792
					}
1793
					if (fn==null){
1794
						fn = FeatureNode.NewInstance();
1795
					}
1796
					Element elParent = elNode.getChild("Parent", sddNamespace);
1797
					// in SDD links between Nodes are referenced by the <Parent> tag
1798
					if (elParent!=null){
1799
						String refP = elParent.getAttributeValue("ref");
1800
						if (refP!=null) {
1801
							FeatureNode parent = featureNodes.get(refP);
1802
							if (parent==null){
1803
								root.addChild(fn); // if no parent found or the reference is broken, add the node to the root of the tree
1804
							}
1805
							else {
1806
								parent.addChild(fn);
1807
							}
1808
						}
1809
					}
1810
					else {
1811
						root.addChild(fn); // if no parent found or the reference is broken, add the node to the root of the tree
1812
					}
1813
				}
1814
				featureNodes.put(idN, fn);
1815
			}
1816
		}
1817

    
1818
		// Leaves of CharacterTrees in SDD are always CharNodes (referring to Characters)
1819
		List<Element> listCharNodes = elNodes.getChildren("CharNode", sddNamespace);
1820
		if (listCharNodes != null) {
1821
			for (Element elCharNode : listCharNodes){
1822
				Element elParent = elCharNode.getChild("Parent", sddNamespace);
1823
				Element elCharacter = elCharNode.getChild("Character", sddNamespace);
1824
				Element elDependencyRules = elCharNode.getChild("DependencyRules", sddNamespace);
1825
				FeatureNode fn = FeatureNode.NewInstance();
1826
				
1827
				if (elDependencyRules!=null){
1828
					Element elInapplicableIf = elCharNode.getChild("InapplicableIf", sddNamespace);
1829
					if (elInapplicableIf!=null){
1830
						List<Element> listStates = elInapplicableIf.getChildren("State", sddNamespace);
1831
						for (Element stateElement : listStates) {
1832
							String refState = stateElement.getAttributeValue("ref");
1833
							if ((refState!=null)&&(!refState.equals(""))) {
1834
								State state = states.get(refState);
1835
								fn.addInapplicableState(state);
1836
							}
1837
						}
1838
					}
1839
					Element elOnlyapplicableIf = elCharNode.getChild("OnlyApplicableIf", sddNamespace);
1840
					if (elOnlyapplicableIf!=null){
1841
						List<Element> listStates = elInapplicableIf.getChildren("State", sddNamespace);
1842
						for (Element stateElement : listStates) {
1843
							String refState = stateElement.getAttributeValue("ref");
1844
							if ((refState!=null)&&(!refState.equals(""))) {
1845
								State state = states.get(refState);
1846
								fn.addApplicableState(state);
1847
							}
1848
						}
1849
					}
1850
				}
1851
				
1852
				if (elParent!=null){
1853
					String refP = elParent.getAttributeValue("ref");
1854
					if ((refP!=null)&&(!refP.equals(""))) {
1855
					FeatureNode parent = featureNodes.get(refP);
1856
						if (parent==null){
1857
						parent = root; // if no parent found or the reference is broken, add the node to the root of the tree
1858
						}
1859
						parent.addChild(fn);
1860
					}
1861
				}
1862
				String refC = elCharacter.getAttributeValue("ref");
1863
				if ((refC!=null)&&(!refC.equals(""))){
1864
					Feature character = features.get(refC);
1865
					fn.setFeature(character);
1866
					featureNodes.put(refC, fn);
1867
				}
1868
			}		
1869
		}
1870
	}
1871

    
1872
	// imports the <TaxonHierarchies> block
1873
	protected void importTaxonHierarchies(Element elDataset, Namespace sddNamespace, SDDImportState cdmState){
1874

    
1875
		logger.info("start TaxonHierarchies ...");
1876
		Element elTaxonHierarchies = elDataset.getChild("TaxonHierarchies",sddNamespace);
1877

    
1878
		if (elTaxonHierarchies != null) {
1879
			List<Element> listTaxonHierarchies = elTaxonHierarchies.getChildren("TaxonHierarchy", sddNamespace);
1880
			int j = 0;
1881
			for (Element elTaxonHierarchy : listTaxonHierarchies){
1882
				try {
1883
					Element elRepresentation = elTaxonHierarchy.getChild("Representation",sddNamespace);
1884
					String label = (String)ImportHelper.getXmlInputValue(elRepresentation,"Label",sddNamespace);
1885
						Classification classification =  Classification.NewInstance(label);
1886
						importRepresentation(elTaxonHierarchy, sddNamespace, classification, "", cdmState);
1887
					
1888
						Set<TaxonNode> root = classification.getChildNodes();
1889
						Element elNodes = elTaxonHierarchy.getChild("Nodes", sddNamespace); // There can be only one <Nodes> block for TaxonHierarchies
1890
						List<Element> listNodes = elNodes.getChildren("Node", sddNamespace);
1891
						
1892
						for (Element elNode : listNodes){
1893
							String idN = elNode.getAttributeValue("id");
1894
							TaxonNameBase tnb = null;
1895
							if (!idN.equals("")) {
1896
								Element elTaxonName = elNode.getChild("TaxonName", sddNamespace);
1897
								String refTN = elTaxonName.getAttributeValue("ref");
1898
								tnb = taxonNameBases.get(refTN);
1899
								Taxon taxon = (Taxon) tnb.getTaxa().iterator().next() ;
1900
								Element elParent = elNode.getChild("Parent", sddNamespace);
1901
								if (elParent!=null){
1902
									String refP = elParent.getAttributeValue("ref");
1903
									if (!refP.equals("")) {
1904
										TaxonNode parent = taxonNodes.get(refP);
1905
										TaxonNode child = parent.addChildTaxon(taxon, sec, "", Synonym.NewInstance(tnb, sec));
1906
										taxonNodes.put(idN,child);
1907
									}
1908
								}
1909
								else {
1910
									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
1911
									taxonNodes.put(idN,tn);
1912
								}
1913
							}
1914
						}
1915

    
1916
						classifications.add(classification);
1917
					}
1918

    
1919
				catch (Exception e) {
1920
					//FIXME
1921
					logger.warn("Import of Taxon Hierarchy " + j + " failed.");
1922
					cdmState.setUnsuccessfull();
1923
				}
1924

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

    
1927
			}
1928

    
1929
		}
1930
	}
1931
	
1932
	
1933
	// imports the <GeographicAreas> block 
1934
	protected void importGeographicAreas(Element elDataset, Namespace sddNamespace, SDDImportState cdmState) {
1935
		Element elGeographicAreas = elDataset.getChild("GeographicAreas",sddNamespace);
1936
		if (elGeographicAreas != null) {
1937
			List<Element> listGeographicAreas = elGeographicAreas.getChildren("GeographicArea", sddNamespace);
1938
			int j = 0;
1939
						
1940
			for (Element elGeographicArea : listGeographicAreas){
1941

    
1942
				String id = elGeographicArea.getAttributeValue("id");
1943
				NamedArea na = new NamedArea();
1944
				importRepresentation(elGeographicArea, sddNamespace, na, id, cdmState);
1945
				namedAreas.put(id,na);
1946
								}
1947
			if ((++j % modCount) == 0){ logger.info("GeographicAreas handled: " + j);}
1948
							}
1949
							}
1950
}
(1-1/4)