Project

General

Profile

Download (48.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;
11

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

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

    
32
import eu.etaxonomy.cdm.api.service.IAgentService;
33
import eu.etaxonomy.cdm.api.service.IDescriptionService;
34
import eu.etaxonomy.cdm.api.service.IReferenceService;
35
import eu.etaxonomy.cdm.api.service.ITermService;
36
import eu.etaxonomy.cdm.common.MediaMetaData.ImageMetaData;
37
import eu.etaxonomy.cdm.io.common.ICdmIO;
38
import eu.etaxonomy.cdm.io.common.IImportConfigurator;
39
import eu.etaxonomy.cdm.io.common.ImportHelper;
40
import eu.etaxonomy.cdm.io.common.MapWrapper;
41
import eu.etaxonomy.cdm.model.agent.Person;
42
import eu.etaxonomy.cdm.model.agent.Team;
43
import eu.etaxonomy.cdm.model.common.Annotation;
44
import eu.etaxonomy.cdm.model.common.AnnotationType;
45
import eu.etaxonomy.cdm.model.common.CdmBase;
46
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
47
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
48
import eu.etaxonomy.cdm.model.common.Language;
49
import eu.etaxonomy.cdm.model.common.LanguageString;
50
import eu.etaxonomy.cdm.model.common.OriginalSource;
51
import eu.etaxonomy.cdm.model.common.Representation;
52
import eu.etaxonomy.cdm.model.common.TermBase;
53
import eu.etaxonomy.cdm.model.common.TermVocabulary;
54
import eu.etaxonomy.cdm.model.common.VersionableEntity;
55
import eu.etaxonomy.cdm.model.description.CategoricalData;
56
import eu.etaxonomy.cdm.model.description.Feature;
57
import eu.etaxonomy.cdm.model.description.FeatureNode;
58
import eu.etaxonomy.cdm.model.description.FeatureTree;
59
import eu.etaxonomy.cdm.model.description.MeasurementUnit;
60
import eu.etaxonomy.cdm.model.description.QuantitativeData;
61
import eu.etaxonomy.cdm.model.description.State;
62
import eu.etaxonomy.cdm.model.description.StateData;
63
import eu.etaxonomy.cdm.model.description.StatisticalMeasure;
64
import eu.etaxonomy.cdm.model.description.StatisticalMeasurementValue;
65
import eu.etaxonomy.cdm.model.description.TaxonDescription;
66
import eu.etaxonomy.cdm.model.description.TextData;
67
import eu.etaxonomy.cdm.model.media.ImageFile;
68
import eu.etaxonomy.cdm.model.media.Media;
69
import eu.etaxonomy.cdm.model.media.MediaRepresentation;
70
import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
71
import eu.etaxonomy.cdm.model.media.Rights;
72
import eu.etaxonomy.cdm.model.name.NonViralName;
73
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
74
import eu.etaxonomy.cdm.model.reference.Article;
75
import eu.etaxonomy.cdm.model.reference.Database;
76
import eu.etaxonomy.cdm.model.reference.Generic;
77
import eu.etaxonomy.cdm.model.reference.ReferenceBase;
78
import eu.etaxonomy.cdm.model.taxon.Taxon;
79

    
80
/**
81
 * @author h.fradin
82
 * @created 24.10.2008
83
 * @version 1.0
84
 */
85
@Component("sddDescriptionIO")
86
public class SDDDescriptionIO extends SDDIoBase implements ICdmIO<IImportConfigurator> {
87
	private static final Logger logger = Logger.getLogger(SDDDescriptionIO.class);
88

    
89
	private static int modCount = 1000;
90

    
91
	private Map<String,Person> authors = new HashMap<String,Person>();
92
	private Map<String,String> citations = new HashMap<String,String>();
93
	private Map<String,String> defaultUnitPrefixes = new HashMap<String,String>();
94
	private Map<String,Person> editors = new HashMap<String,Person>();
95
	private Map<String,FeatureNode> featureNodes = new HashMap<String,FeatureNode>();
96
	private Map<String,Feature> features = new HashMap<String,Feature>();
97
	private Map<String,String> locations = new HashMap<String,String>();
98
	private Map<String,List<CdmBase>> mediaObject_ListCdmBase = new HashMap<String,List<CdmBase>>();
99
	private Map<String,String> mediaObject_Role = new HashMap<String,String>();
100
	private Map<String,FeatureNode> nodes = new HashMap<String,FeatureNode>();
101
	private Map<String,ReferenceBase> publications = new HashMap<String,ReferenceBase>();
102
	private Map<String,StateData> stateDatas = new HashMap<String,StateData>();
103
	private Map<String,TaxonDescription> taxonDescriptions = new HashMap<String,TaxonDescription>();
104
	private Map<String,NonViralName> taxonNameBases = new HashMap<String,NonViralName>();
105
	private Map<String,MeasurementUnit> units = new HashMap<String,MeasurementUnit>();
106
	
107
	private Set<AnnotationType> annotationTypes = new HashSet<AnnotationType>();
108
	private Set<Feature> featureSet = new HashSet<Feature>();
109

    
110
	private ReferenceBase sec = Database.NewInstance();
111
	private ReferenceBase sourceReference = null;
112

    
113
	private Language datasetLanguage = null;
114

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

    
117
	private String generatorName = "";
118
	private String generatorVersion = "";
119

    
120
	private Set<StatisticalMeasure> statisticalMeasures = new HashSet<StatisticalMeasure>();
121
	private Set<VersionableEntity> featureData = new HashSet<VersionableEntity>();
122
	private Set<FeatureTree> featureTrees = new HashSet<FeatureTree>();
123

    
124
	private Rights copyright = null;
125

    
126
	public SDDDescriptionIO(){
127
		super();
128
	}
129

    
130
	@Override
131
	public boolean doCheck(IImportConfigurator config){
132
		boolean result = true;
133
		logger.warn("No check implemented for SDD");
134
		return result;
135
	}
136

    
137
	@Override
138
	public boolean doInvoke(IImportConfigurator config, Map<String, MapWrapper<? extends CdmBase>> stores){
139

    
140
		TransactionStatus ts = startTransaction();
141
		SDDImportConfigurator sddConfig = (SDDImportConfigurator)config;
142

    
143
		logger.info("start Datasets ...");
144
		// <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">
145
		Element root = sddConfig.getSourceRoot();
146
		boolean success = true;
147
		Namespace sddNamespace = sddConfig.getSddNamespace();
148

    
149
		logger.info("start TechnicalMetadata ...");
150
		// <TechnicalMetadata created="2006-04-20T10:00:00">
151
		importTechnicalMetadata(root, sddNamespace, sddConfig);
152
		List<Element> elDatasets = root.getChildren("Dataset",sddNamespace);
153
		int i = 0;
154

    
155
		//for each Dataset
156
		logger.info("start Dataset ...");
157
		for (Element elDataset : elDatasets){
158
			importDataset(elDataset, sddNamespace, success, sddConfig);			
159
			if ((++i % modCount) == 0){ logger.info("Datasets handled: " + i);}
160
			logger.info(i + " Datasets handled");
161
		}
162
		commitTransaction(ts);
163
		return success;
164
	}
165

    
166
	/* (non-Javadoc)
167
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
168
	 */
169
	protected boolean isIgnore(IImportConfigurator config){
170
		return false;
171
	}
172

    
173

    
174
	// associates the reference of a media object in SDD with a CdmBase Object
175
	protected void associateImageWithCdmBase(String refMO, CdmBase cb){
176
		if ((refMO != null) && (cb!=null)) {
177
			if (!refMO.equals("")) {
178
				if (!mediaObject_ListCdmBase.containsKey(refMO)) {
179
					List<CdmBase> lcb = new ArrayList<CdmBase>();
180
					lcb.add(cb);
181
					mediaObject_ListCdmBase.put(refMO,lcb);
182
				} else {
183
					List<CdmBase> lcb = mediaObject_ListCdmBase.get(refMO);
184
					lcb.add(cb);
185
					mediaObject_ListCdmBase.put(refMO,lcb);
186
				}
187
			}
188
		}
189
	}
190

    
191
	// imports information about the Dataset
192
	protected void importDatasetRepresentation(Element parent, Namespace sddNamespace){
193
		logger.info("start Representation ...");
194
		/* <Representation>
195
			<Label>The Genus Viola</Label>
196
			<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>
197
	       </Representation>
198
		 */
199
		Element elRepresentation = parent.getChild("Representation",sddNamespace);
200
		String label = (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace);
201
		String detail = (String)ImportHelper.getXmlInputValue(elRepresentation, "Detail",sddNamespace);
202

    
203
		sec.setTitleCache(label);
204

    
205
		if (detail != null) {
206
			Annotation annotation = Annotation.NewInstance(detail, datasetLanguage);
207
			sec.addAnnotation(annotation);
208
		}
209

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

    
212
		for (Element elMediaObject : listMediaObjects) {
213
			String ref = null;
214
			String role = null;
215
			if (elMediaObject != null) {
216
				ref = elMediaObject.getAttributeValue("ref");
217
				role = elMediaObject.getAttributeValue("role");
218
			}
219
			if (ref != null) {
220
				if (!ref.equals("")) {
221
					this.associateImageWithCdmBase(ref,sourceReference);
222
					this.associateImageWithCdmBase(ref,sec);
223
					mediaObject_Role.put(ref,role);
224
				}
225
			}
226
		}
227
	}
228

    
229
	// imports the representation (label, detail, lang) of a particular SDD element
230
	protected void importRepresentation(Element parent, Namespace sddNamespace, VersionableEntity ve, String id, IImportConfigurator config){
231
		Element elRepresentation = parent.getChild("Representation",sddNamespace);
232
		// <Label xml:lang="la">Viola hederacea Labill.</Label>
233
		List<Element> listLabels = elRepresentation.getChildren("Label",sddNamespace);
234
		List<Element> listDetails = elRepresentation.getChildren("Detail",sddNamespace);
235
		Map<Language,List<String>> langLabDet = new HashMap<Language,List<String>>();
236

    
237
		for (Element elLabel : listLabels){
238
			String lang = elLabel.getAttributeValue("lang",xmlNamespace);
239
			Language language = null;
240
			if (lang != null) {
241
				if (!lang.equals("")) {
242
					language = getTermService().getLanguageByIso(lang.substring(0, 2));
243
				} else {
244
					language = datasetLanguage;
245
				}
246
			} else {
247
				language = datasetLanguage;
248
			}
249
			String label = elLabel.getText();
250
			List<String> labDet = new ArrayList<String>(3);
251
			labDet.add(label);
252
			langLabDet.put(language, labDet);
253
		}
254

    
255
		for (Element elDetail : listDetails){
256
			String lang = elDetail.getAttributeValue("lang",xmlNamespace);
257
			String role = elDetail.getAttributeValue("role");
258
			Language language = null;
259
			if (lang != null) {
260
				if (!lang.equals("")) {
261
					language = getTermService().getLanguageByIso(lang.substring(0, 2));
262
				} else {
263
					language = datasetLanguage;
264
				}
265
			} else {
266
				language = datasetLanguage;
267
			}
268
			String detail = elDetail.getText();
269
			List<String> labDet = langLabDet.get(language);
270
			labDet.add(detail);
271
			labDet.add(role);
272
			langLabDet.put(language, labDet);
273
		}
274

    
275
		if (ve instanceof IdentifiableEntity) {
276
			IdentifiableEntity ie = (IdentifiableEntity) ve;
277
			List<String> labDet = null;
278
			
279
			if (ve instanceof TaxonNameBase) {
280
				if (langLabDet.keySet().contains(getTermService().getLanguageByIso("la"))) {
281
					labDet = langLabDet.get(getTermService().getLanguageByIso("la"));
282
				} else if (langLabDet.keySet().contains(datasetLanguage)) {
283
					labDet = langLabDet.get(datasetLanguage);
284
					logger.info("TaxonName " + (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace) + " is not specified as a latin name.");
285
				} else {
286
					labDet = langLabDet.get(langLabDet.keySet().iterator().next());
287
					logger.info("TaxonName " + (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace) + " is not specified as a latin name.");
288
				}
289
			} else {
290
				labDet = langLabDet.get(langLabDet.keySet().iterator().next());
291
			}
292
			
293
			ie.setTitleCache(labDet.get(0));
294

    
295
			if (labDet.size()>1) {
296
				Annotation annotation = null;
297
				if (labDet.get(1) != null) {
298
					if (labDet.get(2) != null) {
299
						annotation = Annotation.NewInstance(labDet.get(2) + " - " + labDet.get(1), datasetLanguage);
300
					} else {
301
						annotation = Annotation.NewInstance(labDet.get(1), datasetLanguage);
302
					}
303
				}
304
				ie.addAnnotation(annotation);
305
			}
306

    
307
			ve = ie;
308

    
309
		} else if (ve instanceof TermBase) {
310
			TermBase tb = (TermBase) ve;
311

    
312
			for (Iterator<Language> l = langLabDet.keySet().iterator() ; l.hasNext() ;){
313
				Language lang = l.next();
314
				List<String> labDet = langLabDet.get(lang);
315
				if (labDet.size()>0){
316
					if (labDet.size()>1) {
317
						tb.addRepresentation(Representation.NewInstance(labDet.get(1), labDet.get(0), labDet.get(0), lang));
318
					} else {
319
						tb.addRepresentation(Representation.NewInstance(labDet.get(0), labDet.get(0), labDet.get(0), lang));
320
					}
321
				}
322
				ve = tb;
323
			}
324

    
325
		} else if (ve instanceof Media) {
326
			Media m = (Media) ve;
327

    
328
			for (Iterator<Language> l = langLabDet.keySet().iterator() ; l.hasNext() ;){
329
				Language lang = l.next();
330
				List<String> labDet = langLabDet.get(lang);
331
				if (labDet.get(0) != null){
332
					m.addTitle(LanguageString.NewInstance(labDet.get(0), lang));
333
				}
334
				if (labDet.size()>1) {
335
					m.addDescription(labDet.get(1), lang);
336
				}
337
				ve = m;
338
			}
339

    
340
		}
341

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

    
344
		for (Element elMediaObject : listMediaObjects) {
345
			String ref = null;
346
			String role = null;
347
			if (elMediaObject != null) {
348
				ref = elMediaObject.getAttributeValue("ref");
349
				role = elMediaObject.getAttributeValue("role");
350
			}
351
			if (ref != null) {
352
				if (!ref.equals("")) {
353
					if (ref != null) {
354
						if (ve instanceof TaxonDescription) {
355
							TaxonDescription td = (TaxonDescription) ve;
356
							//TODO: ensure that all images are imported
357
							if (td.getDescriptionSources().toArray().length > 0) {
358
								this.associateImageWithCdmBase(ref,(ReferenceBase) td.getDescriptionSources().toArray()[0]);
359
							} else {
360
								ReferenceBase descriptionSource = Generic.NewInstance();
361
								td.addDescriptionSource(descriptionSource);
362
								this.associateImageWithCdmBase(ref,descriptionSource);
363
							}
364
						} else {
365
							this.associateImageWithCdmBase(ref,ve);
366
						}
367
					}
368

    
369
				}
370
			}
371
		}
372

    
373
	}
374

    
375
	// imports the representation (label, detail, lang) of a particular SDD element
376
	protected void importTechnicalMetadata(Element root, Namespace sddNamespace, SDDImportConfigurator sddConfig){
377
		Element elTechnicalMetadata = root.getChild("TechnicalMetadata", sddNamespace);
378
		String nameCreated = elTechnicalMetadata.getAttributeValue("created");
379
		sourceReference = sddConfig.getSourceReference();
380

    
381
		if (nameCreated != null) {
382
			if (!nameCreated.equals("")) {
383
				int year = Integer.parseInt(nameCreated.substring(0,4));
384
				int monthOfYear = Integer.parseInt(nameCreated.substring(5,7));
385
				int dayOfMonth = Integer.parseInt(nameCreated.substring(8,10));
386
				int hourOfDay = Integer.parseInt(nameCreated.substring(11,13));
387
				int minuteOfHour = Integer.parseInt(nameCreated.substring(14,16));
388
				int secondOfMinute = Integer.parseInt(nameCreated.substring(17,19));
389
				DateTime created = new DateTime(year,monthOfYear,dayOfMonth,hourOfDay,minuteOfHour,secondOfMinute,0);
390
				sourceReference.setCreated(created);
391
				sec.setCreated(created);
392
			}
393
		}
394

    
395
		// <Generator name="n/a, handcrafted instance document" version="n/a"/>
396
		Element elGenerator = elTechnicalMetadata.getChild("Generator", sddNamespace);
397
		generatorName = elGenerator.getAttributeValue("name");
398
		generatorVersion = elGenerator.getAttributeValue("version");
399

    
400
		sec.addAnnotation(Annotation.NewDefaultLanguageInstance(generatorName + " - " + generatorVersion));
401
		sourceReference.addAnnotation(Annotation.NewDefaultLanguageInstance(generatorName + " - " + generatorVersion));
402

    
403
	}
404

    
405
	// imports the complete dataset information
406
	protected void importDataset(Element elDataset, Namespace sddNamespace, boolean success, SDDImportConfigurator sddConfig){			// <Dataset xml:lang="en-us">
407

    
408
		importDatasetLanguage(elDataset,sddConfig);
409
		importDatasetRepresentation(elDataset, sddNamespace);
410
		importRevisionData(elDataset, sddNamespace);
411
		importIPRStatements(elDataset, sddNamespace, sddConfig);
412
		importTaxonNames(elDataset, sddNamespace, sddConfig);
413
		importDescriptiveConcepts(elDataset, sddNamespace, sddConfig);
414
		importCharacters(elDataset, sddNamespace, sddConfig, success);
415
		importCharacterTrees(elDataset, sddNamespace, sddConfig, success);
416
		importCodedDescriptions(elDataset, sddNamespace, sddConfig, success);
417
		importAgents(elDataset, sddNamespace, sddConfig, success);
418
		importPublications(elDataset, sddNamespace, sddConfig, success);
419
		importMediaObjects(elDataset, sddNamespace, sddConfig, success);
420

    
421
		if (authors != null) {
422
			Team team = Team.NewInstance();
423
			for (Iterator<Person> author = authors.values().iterator() ; author.hasNext() ;){
424
				team.addTeamMember(author.next());
425
			}
426
			sec.setAuthorTeam(team);
427
			sourceReference.setAuthorTeam(team);
428
		}
429

    
430
		if (editors != null) {
431
			Person ed = Person.NewInstance();
432
			for (Iterator<Person> editor = editors.values().iterator() ; editor.hasNext() ;){
433
				ed = editor.next();
434
			}
435
			// TODO updatedBy refactored to use a user account, so setting a person is no longer applicable
436
//			sec.setUpdatedBy(ed);
437
//			sourceReference.setUpdatedBy(ed);
438
		}
439

    
440
		if (copyright != null) {
441
			sourceReference.addRights(copyright);
442
			sec.addRights(copyright);
443
		}
444

    
445
		for (Iterator<String> refCD = taxonDescriptions.keySet().iterator() ; refCD.hasNext() ;){
446
			String ref = refCD.next();
447
			TaxonDescription td = taxonDescriptions.get(ref);
448
			td.addDescriptionSource(sec);
449
			if (citations.containsKey(ref)) {
450
				Article publication = (Article) publications.get(citations.get(ref));
451
				if (locations.containsKey(ref)) {
452
					Annotation location = Annotation.NewInstance(locations.get(ref), datasetLanguage);
453
					AnnotationType annotationType = AnnotationType.NewInstance("", "location", "");
454
					annotationTypes.add(annotationType);
455
					location.setAnnotationType(annotationType);
456
					publication.addAnnotation(location);
457
				}
458
				td.addDescriptionSource(publication);
459
			}
460
		}
461
		logger.info("end makeTaxonDescriptions ...");
462

    
463
		sddConfig.setSourceReference(sourceReference);
464

    
465
		//saving of all imported data into the CDM db
466
		ITermService termService = getTermService();
467
	for (Iterator<StateData> k = stateDatas.values().iterator() ; k.hasNext() ;){
468
		StateData sd = k.next();
469
		termService.saveTerm(sd.getState()); 
470
	}
471
	for (Iterator<Feature> k = features.values().iterator() ; k.hasNext() ;){
472
		Feature feature = k.next();
473
		termService.saveTerm(feature); 
474
	}
475
	if (units != null) {
476
		for (Iterator<MeasurementUnit> k = units.values().iterator() ; k.hasNext() ;){
477
			MeasurementUnit unit = k.next();
478
			if (unit != null) {
479
				termService.saveTerm(unit); 
480
			}
481
		}
482
	}
483
	for (Iterator<StatisticalMeasure> k = statisticalMeasures.iterator() ; k.hasNext() ;) {
484
		StatisticalMeasure sm = k.next();
485
		termService.saveTerm(sm); 
486
	}
487
	
488
	for (Iterator<AnnotationType> at = annotationTypes.iterator() ; at.hasNext() ;) {
489
		AnnotationType annotationType = at.next();
490
		termService.saveTerm(annotationType); 
491
	}
492

    
493
	IReferenceService referenceService = getReferenceService();
494
	// referenceService.saveReference(sourceReference); 
495
	for (Iterator<ReferenceBase> k = publications.values().iterator() ; k.hasNext() ;){
496
		Article publication = (Article) k.next();
497
		referenceService.saveReference(publication); 
498
	}
499

    
500
	IAgentService agentService = getAgentService();
501
	for (Iterator<Person> p = authors.values().iterator() ; p.hasNext() ;) {
502
		Person person = p.next();
503
		agentService.saveAgent(person);
504
	}
505
	
506
	for (Iterator<Person> p = editors.values().iterator() ; p.hasNext() ;) {
507
		Person person = p.next();
508
		agentService.saveAgent(person);
509
	}
510
	
511
	// Returns a CdmApplicationController created by the values of this configuration.
512
	IDescriptionService descriptionService = getDescriptionService();
513
	
514
	for (Iterator<TaxonDescription> k = taxonDescriptions.values().iterator() ; k.hasNext() ;){
515
		TaxonDescription taxonDescription = k.next();
516
		// Persists a Description
517
		descriptionService.saveDescription(taxonDescription); 
518
	}
519

    
520
//	descriptionService.saveFeatureNodeAll(featureNodes.values());
521
	
522
	for (Iterator<FeatureTree> k = featureTrees.iterator() ; k.hasNext() ;) {
523
		FeatureTree tree = k.next();
524
		descriptionService.saveFeatureTree(tree);
525
	}
526

    
527
}
528

    
529
// imports the default language of the dataset
530
protected void importDatasetLanguage(Element elDataset, SDDImportConfigurator sddConfig){
531
	String nameLang = elDataset.getAttributeValue("lang",xmlNamespace);
532

    
533
	if (!nameLang.equals("")) {
534
		String iso = nameLang.substring(0, 2);
535
		datasetLanguage = getTermService().getLanguageByIso(iso);
536
	} else {
537
		datasetLanguage = Language.ENGLISH();
538
	}
539
	if (datasetLanguage == null) {
540
		datasetLanguage = Language.ENGLISH();
541
	}
542
}
543

    
544
// imports the revision data associated with the Dataset (authors, modifications)
545
protected void importRevisionData(Element elDataset, Namespace sddNamespace){
546
	// <RevisionData>
547
	logger.info("start RevisionData ...");
548
	Element elRevisionData = elDataset.getChild("RevisionData",sddNamespace);
549

    
550
	// <Creators>
551
	Element elCreators = elRevisionData.getChild("Creators",sddNamespace);
552

    
553
	// <Agent role="aut" ref="a1"/>
554
	List<Element> listAgents = elCreators.getChildren("Agent", sddNamespace);
555

    
556
	int j = 0;
557
	//for each Agent
558
	for (Element elAgent : listAgents){
559

    
560
		String role = elAgent.getAttributeValue("role");
561
		String ref = elAgent.getAttributeValue("ref");
562
		if (role.equals("aut")) {
563
			if(!ref.equals("")) {
564
				authors.put(ref, null);
565
			}
566
		}
567
		if (role.equals("edt")) {
568
			if(!ref.equals("")) {
569
				editors.put(ref, null);
570
			}
571
		}
572

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

    
575
	}
576

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

    
580
	if (stringDateModified != null) {
581
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");
582
		Date d = null;
583
		try {
584
			d = sdf.parse(stringDateModified);
585
		} catch(Exception e) {
586
			System.err.println("Exception :");
587
			e.printStackTrace();
588
		}
589

    
590
		DateTime updated = null;
591
		if (d != null) {
592
			updated = new DateTime(d);
593
			sourceReference.setUpdated(updated);
594
			sec.setUpdated(updated);
595
		}
596
	}
597
}
598

    
599
// imports ipr statements associated with a dataset
600
protected void importIPRStatements(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig){
601
	// <IPRStatements>
602
	logger.info("start IPRStatements ...");
603
	Element elIPRStatements = elDataset.getChild("IPRStatements",sddNamespace);
604
	// <IPRStatement role="Copyright">
605
	if (elIPRStatements != null) {
606
		List<Element> listIPRStatements = elIPRStatements.getChildren("IPRStatement", sddNamespace);
607
		int j = 0;
608
		//for each IPRStatement
609

    
610
		for (Element elIPRStatement : listIPRStatements){
611

    
612
			String role = elIPRStatement.getAttributeValue("role");
613
			// <Label xml:lang="en-au">(c) 2003-2006 Centre for Occasional Botany.</Label>
614
			Element elLabel = elIPRStatement.getChild("Label",sddNamespace);
615
			String lang = "";
616
			if (elLabel != null) {
617
				lang = elLabel.getAttributeValue("lang",xmlNamespace);
618
			}
619
			String label = (String)ImportHelper.getXmlInputValue(elIPRStatement, "Label",sddNamespace);
620

    
621
			if (role.equals("Copyright")) {
622
				Language iprLanguage = null;
623
				if (lang != null) {
624
					if (!lang.equals("")) {
625
						iprLanguage = getTermService().getLanguageByIso(lang.substring(0, 2));
626
						//iprLanguage = datasetLanguage;
627
					} else {
628
						iprLanguage = datasetLanguage;
629
					}
630
				}
631
				if (iprLanguage == null) {
632
					iprLanguage = datasetLanguage;
633
				}
634
				copyright = Rights.NewInstance(label, iprLanguage);
635
			}
636

    
637
			if (copyright != null) {
638
				sourceReference.addRights(copyright);
639
				sec.addRights(copyright);
640
			}
641

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

    
644
		}
645
	}
646
}
647

    
648
// imports the taxon names
649
protected void importTaxonNames(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig){
650
	// <TaxonNames>
651
	logger.info("start TaxonNames ...");
652
	Element elTaxonNames = elDataset.getChild("TaxonNames",sddNamespace);
653
	// <TaxonName id="t1" uri="urn:lsid:authority:namespace:my-own-id">
654
	if (elTaxonNames != null) {
655
		List<Element> listTaxonNames = elTaxonNames.getChildren("TaxonName", sddNamespace);
656
		int j = 0;
657
		//for each TaxonName
658
		for (Element elTaxonName : listTaxonNames){
659

    
660
			String id = elTaxonName.getAttributeValue("id");
661
			String uri = elTaxonName.getAttributeValue("uri");
662

    
663
			NonViralName tnb = null;
664
			if (!id.equals("")) {
665
				tnb = NonViralName.NewInstance(null);
666
				OriginalSource source = null;
667
				if (uri != null) {
668
					if (!uri.equals("")) {
669
						source = OriginalSource.NewInstance(id, "TaxonName", Generic.NewInstance(), uri);
670
					}
671
				} else {
672
					source = OriginalSource.NewInstance(id, "TaxonName");
673
				}
674
				tnb.addSource(source);
675
				taxonNameBases.put(id,tnb);
676
			}
677

    
678
			// <Representation>
679
			// <Label xml:lang="la">Viola hederacea Labill.</Label>
680
			importRepresentation(elTaxonName, sddNamespace, tnb, id, sddConfig);
681

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

    
684
		}
685
	}
686
}
687

    
688
// imports the representation (label, detail, lang) of a particular SDD element
689
protected void importCharacters(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig, boolean success){
690
	// <Characters>
691
	logger.info("start Characters ...");
692
	Element elCharacters = elDataset.getChild("Characters", sddNamespace);
693

    
694
	// <CategoricalCharacter id="c1">
695
	if (elCharacters != null) {
696
		List<Element> elCategoricalCharacters = elCharacters.getChildren("CategoricalCharacter", sddNamespace);
697
		int j = 0;
698
		//for each CategoricalCharacter
699
		for (Element elCategoricalCharacter : elCategoricalCharacters){
700

    
701
			try {
702

    
703
				String idCC = elCategoricalCharacter.getAttributeValue("id");
704

    
705
				// <Representation>
706
				//  <Label> Leaf complexity</Label>
707
				// </Representation>
708

    
709
				Feature categoricalCharacter = Feature.NewInstance();
710
				importRepresentation(elCategoricalCharacter, sddNamespace, categoricalCharacter, idCC, sddConfig);
711

    
712
				categoricalCharacter.setSupportsCategoricalData(true);
713

    
714
				// <States>
715
				Element elStates = elCategoricalCharacter.getChild("States",sddNamespace);
716

    
717
				// <StateDefinition id="s1">
718
				List<Element> elStateDefinitions = elStates.getChildren("StateDefinition",sddNamespace);
719
				TermVocabulary<State> termVocabularyState = new TermVocabulary<State>();
720

    
721
				int k = 0;
722
				//for each StateDefinition
723
				for (Element elStateDefinition : elStateDefinitions){
724

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

    
727
					String idSD = elStateDefinition.getAttributeValue("id");
728
					// <Representation>
729
					//  <Label>Simple</Label>
730
					//  <MediaObject ref="ib" role="Primary"/>
731
					// </Representation>
732
					State state = State.NewInstance();
733
					importRepresentation(elStateDefinition, sddNamespace, state, idSD, sddConfig);
734

    
735
					StateData stateData = StateData.NewInstance();
736
					stateData.setState(state);
737
					termVocabularyState.addTerm(state);
738
					stateDatas.put(idSD,stateData);
739
				}
740

    
741
				categoricalCharacter.addSupportedCategoricalEnumeration(termVocabularyState);
742
				features.put(idCC, categoricalCharacter);
743

    
744
			} catch (Exception e) {
745
				//FIXME
746
				logger.warn("Import of CategoricalCharacter " + j + " failed.");
747
				success = false; 
748
			}
749

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

    
752
		}
753

    
754
		// <QuantitativeCharacter id="c2">
755
		List<Element> elQuantitativeCharacters = elCharacters.getChildren("QuantitativeCharacter", sddNamespace);
756
		j = 0;
757
		//for each QuantitativeCharacter
758
		for (Element elQuantitativeCharacter : elQuantitativeCharacters){
759

    
760
			try {
761

    
762
				String idQC = elQuantitativeCharacter.getAttributeValue("id");
763

    
764
				// <Representation>
765
				//  <Label>Leaf length</Label>
766
				// </Representation>
767
				Feature quantitativeCharacter = Feature.NewInstance();
768
				importRepresentation(elQuantitativeCharacter, sddNamespace, quantitativeCharacter, idQC, sddConfig);
769

    
770
				quantitativeCharacter.setSupportsQuantitativeData(true);
771

    
772
				// <MeasurementUnit>
773
				//  <Label role="Abbrev">m</Label>
774
				// </MeasurementUnit>
775
				Element elMeasurementUnit = elQuantitativeCharacter.getChild("MeasurementUnit",sddNamespace);
776
				String label = "";
777
				String role = "";
778
				if (elMeasurementUnit != null) {
779
					Element elLabel = elMeasurementUnit.getChild("Label",sddNamespace);
780
					role = elLabel.getAttributeValue("role");
781
					label = (String)ImportHelper.getXmlInputValue(elMeasurementUnit, "Label",sddNamespace);
782
				}
783

    
784
				MeasurementUnit unit = null;
785
				if (!label.equals("")){
786
					if (role != null) {
787
						if (role.equals("Abbrev")){
788
							unit = MeasurementUnit.NewInstance(label,label,label);
789
						}
790
					} else {
791
						unit = MeasurementUnit.NewInstance(label,label,label);
792
					}
793
				}
794

    
795
				if (unit != null) {
796
					units.put(idQC, unit);
797
				}
798

    
799
				//<Default>
800
				//  <MeasurementUnitPrefix>milli</MeasurementUnitPrefix>
801
				//</Default>
802
				Element elDefault = elQuantitativeCharacter.getChild("Default",sddNamespace);
803
				if (elDefault != null) {
804
					String measurementUnitPrefix = (String)ImportHelper.getXmlInputValue(elDefault, "MeasurementUnitPrefix",sddNamespace);
805
					if (!measurementUnitPrefix.equals("")){
806
						defaultUnitPrefixes.put(idQC, measurementUnitPrefix);
807
					}
808
				}
809

    
810
				features.put(idQC, quantitativeCharacter);
811

    
812
			} catch (Exception e) {
813
				//FIXME
814
				logger.warn("Import of QuantitativeCharacter " + j + " failed.");
815
				success = false; 
816
			}
817

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

    
820
		}
821

    
822
		// <TextCharacter id="c3">
823
		List<Element> elTextCharacters = elCharacters.getChildren("TextCharacter", sddNamespace);
824
		j = 0;
825
		//for each TextCharacter
826
		for (Element elTextCharacter : elTextCharacters){
827

    
828
			try {
829

    
830
				String idTC = elTextCharacter.getAttributeValue("id");
831

    
832
				// <Representation>
833
				//  <Label xml:lang="en">Leaf features not covered by other characters</Label>
834
				// </Representation>
835
				Feature textCharacter = Feature.NewInstance();
836
				importRepresentation(elTextCharacter, sddNamespace, textCharacter, idTC, sddConfig);
837

    
838
				textCharacter.setSupportsTextData(true);
839

    
840
				features.put(idTC, textCharacter);
841

    
842
			} catch (Exception e) {
843
				//FIXME
844
				logger.warn("Import of TextCharacter " + j + " failed.");
845
				success = false; 
846
			}
847

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

    
850
		}
851

    
852
	}
853
	
854
	for (Iterator<Feature> f = features.values().iterator() ; f.hasNext() ;){
855
		featureSet.add(f.next());
856
	}
857
	
858
}
859

    
860
// imports the descriptions of taxa (specimens TODO)
861
protected void importCodedDescriptions(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig, boolean success){
862
	// <CodedDescriptions>
863
	logger.info("start CodedDescriptions ...");
864
	Element elCodedDescriptions = elDataset.getChild("CodedDescriptions",sddNamespace);
865

    
866
	// <CodedDescription id="D101">
867

    
868
	if (elCodedDescriptions != null) {
869
		List<Element> listCodedDescriptions = elCodedDescriptions.getChildren("CodedDescription", sddNamespace);
870
		int j = 0;
871
		//for each CodedDescription
872

    
873
		for (Element elCodedDescription : listCodedDescriptions){
874

    
875
			try {
876

    
877
				String idCD = elCodedDescription.getAttributeValue("id");
878

    
879
				// <Representation>
880
				//  <Label>&lt;i&gt;Viola hederacea&lt;/i&gt; Labill. as revised by R. Morris April 8, 2006</Label>
881
				// </Representation>
882
				TaxonDescription taxonDescription = TaxonDescription.NewInstance();
883
				importRepresentation(elCodedDescription, sddNamespace, taxonDescription, idCD, sddConfig);
884

    
885
				// <Scope>
886
				//  <TaxonName ref="t1"/>
887
				//  <Citation ref="p1" location="p. 30"/>
888
				// </Scope>
889
				Element elScope = elCodedDescription.getChild("Scope",sddNamespace);
890
				String ref = "";
891
				Taxon taxon = null;
892
				if (elScope != null) {
893
					Element elTaxonName = elScope.getChild("TaxonName",sddNamespace);
894
					ref = elTaxonName.getAttributeValue("ref");
895

    
896
					NonViralName taxonNameBase = taxonNameBases.get(ref);
897
					taxon = Taxon.NewInstance(taxonNameBase, sec);
898
				}
899

    
900
				String refCitation = "";
901
				String location = "";
902

    
903
				if (elScope != null) {
904
					Element elCitation = elScope.getChild("Citation",sddNamespace);
905
					if (elCitation != null) {
906
						refCitation = elCitation.getAttributeValue("ref");
907
						location = elCitation.getAttributeValue("location");
908
					}
909
				}
910

    
911
				// <SummaryData>
912
				Element elSummaryData = elCodedDescription.getChild("SummaryData",sddNamespace);
913

    
914
				if (elSummaryData != null) {
915

    
916
					// <Categorical ref="c4">
917
					List<Element> elCategoricals = elSummaryData.getChildren("Categorical", sddNamespace);
918
					int k = 0;
919
					//for each Categorical
920
					for (Element elCategorical : elCategoricals){
921
						if ((++k % modCount) == 0){ logger.info("Categorical handled: " + (k-1));}
922
						ref = elCategorical.getAttributeValue("ref");
923
						Feature feature = features.get(ref);
924
						CategoricalData categoricalData = CategoricalData.NewInstance();
925
						categoricalData.setFeature(feature);
926

    
927
						// <State ref="s3"/>
928
						List<Element> elStates = elCategorical.getChildren("State", sddNamespace);
929
						int l = 0;
930
						//for each State
931
						for (Element elState : elStates){
932
							if ((++l % modCount) == 0){ logger.info("States handled: " + (l-1));}
933
							ref = elState.getAttributeValue("ref");
934
							StateData stateData = stateDatas.get(ref);
935
							categoricalData.addState(stateData);
936
						}
937
						taxonDescription.addElement(categoricalData);
938
					}
939

    
940
					// <Quantitative ref="c2">
941
					List<Element> elQuantitatives = elSummaryData.getChildren("Quantitative", sddNamespace);
942
					k = 0;
943
					//for each Quantitative
944
					for (Element elQuantitative : elQuantitatives){
945
						if ((++k % modCount) == 0){ logger.info("Quantitative handled: " + (k-1));}
946
						ref = elQuantitative.getAttributeValue("ref");
947
						Feature feature = features.get(ref);
948
						QuantitativeData quantitativeData = QuantitativeData.NewInstance();
949
						quantitativeData.setFeature(feature);
950

    
951
						MeasurementUnit unit = units.get(ref);
952
						String prefix = defaultUnitPrefixes.get(ref);
953
						if (unit != null) {
954
							String u = unit.getLabel();
955
							if (prefix != null) {
956
								u = prefix + u;
957
							}
958
							unit.setLabel(u);
959
							quantitativeData.setUnit(unit);
960
						}
961

    
962
						// <Measure type="Min" value="2.3"/>
963
						List<Element> elMeasures = elQuantitative.getChildren("Measure", sddNamespace);
964
						int l = 0;
965
						//for each State
966
						for (Element elMeasure : elMeasures){
967
							if ((++l % modCount) == 0){ logger.info("States handled: " + (l-1));}
968
							String type = elMeasure.getAttributeValue("type");
969
							String value = elMeasure.getAttributeValue("value");
970
							float v = Float.parseFloat(value);
971
							StatisticalMeasure t = null;
972
							if (type.equals("Min")) {
973
								t = StatisticalMeasure.MIN();
974
							} else if (type.equals("Mean")) {
975
								t = StatisticalMeasure.AVERAGE();
976
							} else if (type.equals("Max")) {
977
								t = StatisticalMeasure.MAX();
978
							} else if (type.equals("SD")) {
979
								// Create a new StatisticalMeasure for standard deviation
980
								t = StatisticalMeasure.STANDARD_DEVIATION();
981
							} else if (type.equals("N")) {
982
								t = StatisticalMeasure.SAMPLE_SIZE();
983
							} else {
984
								t = StatisticalMeasure.NewInstance(type,type,type);
985
								statisticalMeasures.add(t);
986
							}
987

    
988
							StatisticalMeasurementValue statisticalValue = StatisticalMeasurementValue.NewInstance();
989
							statisticalValue.setValue(v);
990
							statisticalValue.setType(t);
991
							quantitativeData.addStatisticalValue(statisticalValue);
992
							featureData.add(statisticalValue);
993
						}
994
						taxonDescription.addElement(quantitativeData);
995
					}
996

    
997
					// <TextChar ref="c3">
998
					List<Element> elTextChars = elSummaryData.getChildren("TextChar", sddNamespace);
999
					k = 0;
1000
					//for each TextChar
1001
					for (Element elTextChar : elTextChars){
1002
						if ((++k % modCount) == 0){ logger.info("TextChar handled: " + (k-1));}
1003
						ref = elTextChar.getAttributeValue("ref");
1004
						Feature feature = features.get(ref);
1005
						TextData textData = TextData.NewInstance();
1006
						textData.setFeature(feature);
1007

    
1008
						// <Content>Free form text</Content>
1009
						String content = (String)ImportHelper.getXmlInputValue(elTextChar, "Content",sddNamespace);
1010
						textData.putText(content, datasetLanguage);
1011
						taxonDescription.addElement(textData);
1012
					}
1013

    
1014
				}
1015

    
1016
				if (taxon != null) {
1017
					taxon.addDescription(taxonDescription);
1018
				}
1019

    
1020
				if (!refCitation.equals("")){
1021
					citations.put(idCD,refCitation);
1022
				}
1023

    
1024
				if (!location.equals("")){
1025
					locations.put(idCD, location);
1026
				}
1027
				
1028
				taxonDescription.setDescriptiveSystem(featureSet);
1029
				
1030
				taxonDescriptions.put(idCD, taxonDescription);
1031

    
1032
			} catch (Exception e) {
1033
				//FIXME
1034
				logger.warn("Import of CodedDescription " + j + " failed.");
1035
				success = false; 
1036
			}
1037

    
1038
			if ((++j % modCount) == 0){ logger.info("CodedDescriptions handled: " + j);}
1039

    
1040
		}
1041

    
1042
	}
1043
}
1044

    
1045
// imports the persons associated with the dataset creation, modification, related publications
1046
protected void importAgents(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig, boolean success){
1047
	// <Agents>
1048
	logger.info("start Agents ...");
1049
	Element elAgents = elDataset.getChild("Agents",sddNamespace);
1050

    
1051
	// <Agent id="a1">
1052
	List <Element> listAgents = elAgents.getChildren("Agent", sddNamespace);
1053
	int j = 0;
1054
	//for each Agent
1055
	for (Element elAgent : listAgents){
1056

    
1057
		try {
1058

    
1059
			String idA = elAgent.getAttributeValue("id");
1060

    
1061
			//  <Representation>
1062
			//   <Label>Kevin Thiele</Label>
1063
			//   <Detail role="Description">Ali Baba is also known as r.a.m.</Detail>
1064
			//  </Representation>
1065
			Person person = Person.NewInstance();
1066
			importRepresentation(elAgent, sddNamespace, person, idA, sddConfig);
1067
			person.addSource(OriginalSource.NewInstance(idA, "Agent"));
1068

    
1069
			// <Links>
1070
			Element elLinks = elAgent.getChild("Links",sddNamespace);
1071

    
1072
			if (elLinks != null) {
1073

    
1074
				//  <Link rel="Alternate" href="http://www.diversitycampus.net/people/hagedorn"/>
1075
				List<Element> listLinks = elLinks.getChildren("Link", sddNamespace);
1076
				int k = 0;
1077
				//for each Link
1078
				for (Element elLink : listLinks){
1079

    
1080
					try {
1081

    
1082
						String rel = elLink.getAttributeValue("rel");
1083
						String href = elLink.getAttributeValue("href");
1084

    
1085
						Media link = Media.NewInstance();
1086
						MediaRepresentation mr = MediaRepresentation.NewInstance();
1087
						mr.addRepresentationPart(MediaRepresentationPart.NewInstance(href, null));
1088
						link.addRepresentation(mr);
1089
						person.addMedia(link);
1090

    
1091
					} catch (Exception e) {
1092
						//FIXME
1093
						logger.warn("Import of Link " + k + " failed.");
1094
						success = false; 
1095
					}
1096

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

    
1099
				}
1100
			}
1101
			if (authors.containsKey(idA)) {
1102
				authors.put(idA,person);
1103
			}
1104

    
1105
			if (editors.containsKey(idA)) {
1106
				editors.put(idA, person);
1107
			}
1108

    
1109
		} catch (Exception e) {
1110
			//FIXME
1111
			logger.warn("Import of Agent " + j + " failed.");
1112
			success = false; 
1113
		}
1114

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

    
1117
	}
1118
}
1119

    
1120
// imports publications related with the data set
1121
protected void importPublications(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig, boolean success){
1122
	// <Publications>
1123
	logger.info("start Publications ...");
1124
	Element elPublications = elDataset.getChild("Publications",sddNamespace);
1125

    
1126
	if (elPublications != null) {
1127
		// <Publication id="p1">
1128
		List<Element> listPublications = elPublications.getChildren("Publication", sddNamespace);
1129
		int j = 0;
1130
		//for each Publication
1131
		for (Element elPublication : listPublications){
1132

    
1133
			try {
1134

    
1135
				String idP = elPublication.getAttributeValue("id");
1136

    
1137
				//  <Representation>
1138
				//   <Label>Sample Citation</Label>
1139
				//  </Representation>
1140
				Article publication = Article.NewInstance();
1141
				importRepresentation(elPublication, sddNamespace, publication, idP, sddConfig);
1142

    
1143
				publications.put(idP,publication);
1144

    
1145
			} catch (Exception e) {
1146
				//FIXME
1147
				logger.warn("Import of Publication " + j + " failed.");
1148
				success = false; 
1149
			}
1150

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

    
1153
		}
1154
	}
1155
}
1156

    
1157
// imports media objects such as images
1158
protected void importMediaObjects(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig, boolean success){
1159
	// <MediaObjects>
1160
	logger.info("start MediaObjects ...");
1161
	Element elMediaObjects = elDataset.getChild("MediaObjects",sddNamespace);
1162

    
1163
	if (elMediaObjects != null) {
1164
		// <MediaObject id="m1">
1165
		List<Element> listMediaObjects = elMediaObjects.getChildren("MediaObject", sddNamespace);
1166
		int j = 0;
1167
		//for each Publication
1168
		for (Element elMO : listMediaObjects){
1169

    
1170
			String id = "";
1171

    
1172
			try {
1173

    
1174
				String idMO = elMO.getAttributeValue("id");
1175
				id = idMO;
1176

    
1177
				//  <Representation>
1178
				//   <Label>Image description, e.g. to be used for alt-attribute in html.</Label>
1179
				//  </Representation>
1180
				Media media = Media.NewInstance();
1181
				importRepresentation(elMO, sddNamespace, media, idMO, sddConfig);
1182

    
1183
				// <Type>Image</Type>
1184
				// <Source href="http://test.edu/test.jpg"/>
1185
				String type = (String)ImportHelper.getXmlInputValue(elMO,"Type",sddNamespace);
1186

    
1187
				if ((type != null) && (type.equals("Image"))) {
1188
					Element elSource = elMO.getChild("Source",sddNamespace);
1189
					String href = elSource.getAttributeValue("href");
1190

    
1191
					ImageMetaData imageMetaData = new ImageMetaData();
1192
					ImageFile image = null;
1193

    
1194
					if (href.substring(0,7).equals("http://")) {
1195
						try{
1196
							URL url = new URL(href);
1197
							imageMetaData.readFrom(url);
1198
							image = ImageFile.NewInstance(url.toString(), null, imageMetaData);
1199
						} catch (MalformedURLException e) {
1200
							logger.error("Malformed URL", e);
1201
						}
1202
					} else {
1203
						String sns = sddConfig.getSourceNameString();
1204
						File f = new File(sns);
1205
						File parent = f.getParentFile();
1206
						String fi = parent.toString() + File.separator + href;
1207
						File file = new File(fi);
1208
						imageMetaData.readFrom(file);
1209
						image = ImageFile.NewInstance(file.toString(), null, imageMetaData);
1210
					}
1211

    
1212
					MediaRepresentation representation = MediaRepresentation.NewInstance(imageMetaData.getMimeType(), null);
1213
					representation.addRepresentationPart(image);
1214

    
1215
					media.addRepresentation(representation);
1216

    
1217
					ArrayList<CdmBase> lcb = (ArrayList<CdmBase>) mediaObject_ListCdmBase.get(idMO);
1218
					if (lcb != null) {
1219
						for (int k = 0; k < lcb.size(); k++) {
1220
							if (lcb.get(k) instanceof DefinedTermBase) {
1221
								DefinedTermBase dtb = (DefinedTermBase) lcb.get(k);
1222
								// if (lcb.get(0) instanceof DefinedTermBase) {
1223
								// DefinedTermBase dtb = (DefinedTermBase) lcb.get(0);
1224
								//									if (dtb!=null) {
1225
								//										if (k == 0) {
1226
								dtb.addMedia(media);
1227
								//										} else {
1228
								//											Media me = (Media) media.clone();
1229
								//											dtb.addMedia(me);
1230
								//										}
1231
								//									}
1232
							} else if (lcb.get(k) instanceof ReferenceBase) {
1233
								ReferenceBase rb = (ReferenceBase) lcb.get(k);
1234
								//} else if (lcb.get(0) instanceof ReferenceBase) {
1235
								//ReferenceBase rb = (ReferenceBase) lcb.get(0);
1236
								// rb.setTitleCache(label);
1237
								//									if (rb!=null) {
1238
								//										if (k == 0) {
1239
								rb.addMedia(media);
1240
								//										} else {
1241
								//											Media me = (Media) media.clone();
1242
								//											rb.addMedia(me);
1243
								//										}
1244
								//									}
1245
							}
1246
						}
1247
					}
1248
				}
1249

    
1250
			} catch (Exception e) {
1251
				//FIXME
1252
				logger.warn("Could not attached MediaObject " + j + "(SDD: " + id + ") to several objects.");
1253
				success = false; 
1254
			}
1255

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

    
1258
			}
1259
		}
1260
	}
1261
}
1262

    
1263
// imports the <DescriptiveConcepts> block
1264
protected void importDescriptiveConcepts(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig){
1265
	// <DescriptiveConcepts>
1266
	logger.info("start DescriptiveConcepts ...");
1267
	Element elDescriptiveConcepts = elDataset.getChild("DescriptiveConcepts",sddNamespace);
1268
	// <DescriptiveConcept id="b">
1269
	if (elDescriptiveConcepts != null) {
1270
		List<Element> listDescriptiveConcepts = elDescriptiveConcepts.getChildren("DescriptiveConcept", sddNamespace);
1271
		int j = 0;
1272
		//for each DescriptiveConcept
1273
		int g = 1;
1274
		for (Element elDescriptiveConcept : listDescriptiveConcepts){
1275

    
1276
			String id = elDescriptiveConcept.getAttributeValue("id");
1277
			String uri = elDescriptiveConcept.getAttributeValue("uri");
1278

    
1279
			FeatureNode fn = null;
1280
			
1281
			if (!id.equals("")) {
1282
				fn = FeatureNode.NewInstance();
1283
				Feature feature = Feature.NewInstance();
1284
				//	 <Representation>
1285
				//       <Label>Body</Label>
1286
				importRepresentation(elDescriptiveConcept, sddNamespace, feature, id, sddConfig);
1287
				features.put("g" + g, feature);
1288
				g++;
1289
				fn.setFeature(feature);
1290

    
1291
				//	TODO if an OriginalSource can be attached to a FeatureNode or a Feature		
1292
				//					OriginalSource source = null;
1293
				//					if (uri != null) {
1294
				//						if (!uri.equals("")) {
1295
				//							source = OriginalSource.NewInstance(id, "DescriptiveConcept", Generic.NewInstance(), uri);
1296
				//						}
1297
				//					} else {
1298
				//						source = OriginalSource.NewInstance(id, "DescriptiveConcept");
1299
				//					}
1300
				//					fn.addSource(source);
1301

    
1302
				featureNodes.put(id,fn);
1303
			}
1304

    
1305
			if ((++j % modCount) == 0){ logger.info("DescriptiveConcepts handled: " + j);}
1306

    
1307
		}
1308
	}
1309
}
1310

    
1311
// imports the descriptions of taxa (specimens TODO)
1312
protected void importCharacterTrees(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig, boolean success){
1313
	// <CharacterTrees>
1314
	logger.info("start CharacterTrees ...");
1315
	Element elCharacterTrees = elDataset.getChild("CharacterTrees",sddNamespace);
1316

    
1317
	// <CharacterTree>
1318

    
1319
	if (elCharacterTrees != null) {
1320
		List<Element> listCharacterTrees = elCharacterTrees.getChildren("CharacterTree", sddNamespace);
1321
		int j = 0;
1322
		//for each CharacterTree
1323

    
1324
		for (Element elCharacterTree : listCharacterTrees){
1325

    
1326
			try {
1327
				Element elRepresentation = elCharacterTree.getChild("Representation",sddNamespace);
1328
				String label = (String)ImportHelper.getXmlInputValue(elRepresentation,"Label",sddNamespace);
1329
				Element elDesignedFor = elCharacterTree.getChild("DesignedFor",sddNamespace);
1330
				List<Element> listRoles = elDesignedFor.getChildren("Role",sddNamespace);
1331
				boolean isgroups = false;
1332

    
1333
				for (Element elRole : listRoles){
1334
					if (elRole.getText().equals("Filtering")) {
1335
						isgroups = true;
1336
					}
1337
				}
1338
				
1339
				if ((label.contains("group")) || (isgroups)) {
1340
					
1341
					FeatureTree groups =  FeatureTree.NewInstance();
1342
					importRepresentation(elCharacterTree, sddNamespace, groups, "", sddConfig);
1343
					FeatureNode root = groups.getRoot();
1344
					
1345
					Element elNodes = elCharacterTree.getChild("Nodes", sddNamespace);
1346
					List<Element> listNodes = elNodes.getChildren("Node", sddNamespace);
1347
					for (Element elNode : listNodes){
1348
						String idN = elNode.getAttributeValue("id");
1349
						FeatureNode fn = null;
1350
						if (!idN.equals("")) {
1351
							Element elDescriptiveConcept = elNode.getChild("DescriptiveConcept", sddNamespace);
1352
							String refDC = elDescriptiveConcept.getAttributeValue("ref");
1353
							fn = featureNodes.get(refDC);
1354
							root.addChild(fn);
1355
						}
1356
						nodes.put(idN, fn);
1357
					}
1358
					
1359
					List<Element> listCharNodes = elNodes.getChildren("CharNode", sddNamespace);
1360
					for (Element elCharNode : listCharNodes){
1361
						Element elParent = elCharNode.getChild("Parent", sddNamespace);
1362
						String refP = elParent.getAttributeValue("ref");
1363
						Element elCharacter = elCharNode.getChild("Character", sddNamespace);
1364
						String refC = elCharacter.getAttributeValue("ref");
1365
						FeatureNode fn = FeatureNode.NewInstance();
1366
						if (!refP.equals("")) {
1367
							FeatureNode parent = nodes.get(refP);
1368
							parent.addChild(fn);
1369
							Feature character = features.get(refC);
1370
							fn.setFeature(character);
1371
							// if method setParent() in FeatureNode becomes visible
1372
							// fn.setParent(parent);
1373
						}
1374
						nodes.put(refC, fn);
1375
					}
1376
					featureTrees.add(groups);
1377
				}
1378

    
1379
			} catch (Exception e) {
1380
				//FIXME
1381
				logger.warn("Import of Character tree " + j + " failed.");
1382
				success = false; 
1383
			}
1384

    
1385
			if ((++j % modCount) == 0){ logger.info("CharacterTrees handled: " + j);}
1386

    
1387
		}
1388

    
1389
	}
1390
}
1391
}
(3-3/7)