3159b9ba976bf60d02bf4be955214def70606fa3
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / sdd / in / SDDImport.java
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.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.List;
21 import java.util.Map;
22 import java.util.Set;
23
24 import org.apache.commons.lang.StringUtils;
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.IDescriptionService;
33 import eu.etaxonomy.cdm.common.mediaMetaData.ImageMetaData;
34 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
35 import eu.etaxonomy.cdm.io.common.CdmImportBase;
36 import eu.etaxonomy.cdm.io.common.ICdmImport;
37 import eu.etaxonomy.cdm.io.common.IImportConfigurator;
38 import eu.etaxonomy.cdm.io.common.ImportHelper;
39 import eu.etaxonomy.cdm.io.sdd.SDDTransformer;
40 import eu.etaxonomy.cdm.model.agent.Person;
41 import eu.etaxonomy.cdm.model.agent.Team;
42 import eu.etaxonomy.cdm.model.common.Annotation;
43 import eu.etaxonomy.cdm.model.common.AnnotationType;
44 import eu.etaxonomy.cdm.model.common.CdmBase;
45 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
46 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
47 import eu.etaxonomy.cdm.model.common.IdentifiableSource;
48 import eu.etaxonomy.cdm.model.common.Language;
49 import eu.etaxonomy.cdm.model.common.LanguageString;
50 import eu.etaxonomy.cdm.model.common.Marker;
51 import eu.etaxonomy.cdm.model.common.MarkerType;
52 import eu.etaxonomy.cdm.model.common.Representation;
53 import eu.etaxonomy.cdm.model.common.TermBase;
54 import eu.etaxonomy.cdm.model.common.TermVocabulary;
55 import eu.etaxonomy.cdm.model.common.VersionableEntity;
56 import eu.etaxonomy.cdm.model.description.CategoricalData;
57 import eu.etaxonomy.cdm.model.description.Feature;
58 import eu.etaxonomy.cdm.model.description.FeatureNode;
59 import eu.etaxonomy.cdm.model.description.FeatureTree;
60 import eu.etaxonomy.cdm.model.description.MeasurementUnit;
61 import eu.etaxonomy.cdm.model.description.Modifier;
62 import eu.etaxonomy.cdm.model.description.QuantitativeData;
63 import eu.etaxonomy.cdm.model.description.State;
64 import eu.etaxonomy.cdm.model.description.StateData;
65 import eu.etaxonomy.cdm.model.description.StatisticalMeasure;
66 import eu.etaxonomy.cdm.model.description.StatisticalMeasurementValue;
67 import eu.etaxonomy.cdm.model.description.TaxonDescription;
68 import eu.etaxonomy.cdm.model.description.TextData;
69 import eu.etaxonomy.cdm.model.location.NamedArea;
70 import eu.etaxonomy.cdm.model.media.IdentifiableMediaEntity;
71 import eu.etaxonomy.cdm.model.media.ImageFile;
72 import eu.etaxonomy.cdm.model.media.Media;
73 import eu.etaxonomy.cdm.model.media.MediaRepresentation;
74 import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
75 import eu.etaxonomy.cdm.model.media.Rights;
76 import eu.etaxonomy.cdm.model.name.NonViralName;
77 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
78 import eu.etaxonomy.cdm.model.occurrence.Specimen;
79 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
80 import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
81 import eu.etaxonomy.cdm.model.taxon.Synonym;
82 import eu.etaxonomy.cdm.model.taxon.Taxon;
83 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
84 import eu.etaxonomy.cdm.model.taxon.TaxonomicTree;
85
86 /**
87 * @author h.fradin
88 * @created 24.10.2008
89 * @version 1.0
90 */
91 @Component("sddImport")
92 public class SDDImport extends CdmImportBase<SDDImportConfigurator, SDDImportState> implements ICdmImport<SDDImportConfigurator, SDDImportState> {
93 private static final Logger logger = Logger.getLogger(SDDImport.class);
94
95 private static int modCount = 1000;
96
97 private Map<String,Person> authors = new HashMap<String,Person>();
98 private Map<String,String> citations = new HashMap<String,String>();
99 private Map<String,String> defaultUnitPrefixes = new HashMap<String,String>();
100 private Map<String,Person> editors = new HashMap<String,Person>();
101 private Map<String,FeatureNode> featureNodes = new HashMap<String,FeatureNode>();
102 private Map<String,Feature> features = new HashMap<String,Feature>();
103 private Map<String,String> locations = new HashMap<String,String>();
104 private Map<String,List<CdmBase>> mediaObject_ListCdmBase = new HashMap<String,List<CdmBase>>();
105 private Map<String,String> mediaObject_Role = new HashMap<String,String>();
106 private Map<String,ReferenceBase> publications = new HashMap<String,ReferenceBase>();
107 private Map<String,State> states = new HashMap<String,State>();
108 private Map<String,TaxonDescription> taxonDescriptions = new HashMap<String,TaxonDescription>();
109 private Map<String,NonViralName> taxonNameBases = new HashMap<String,NonViralName>();
110 private Map<String,MeasurementUnit> units = new HashMap<String,MeasurementUnit>();
111 private Map<String,TaxonNode> taxonNodes = new HashMap<String,TaxonNode>();
112 private Map<String,NamedArea> namedAreas = new HashMap<String,NamedArea>();
113 private Map<String,Specimen> specimens = new HashMap<String,Specimen>();
114 private Map<String,Modifier> modifiers = new HashMap<String,Modifier>();
115
116 private Set<MarkerType> markerTypes = new HashSet<MarkerType>();
117 private Set<TermVocabulary> vocabularies = new HashSet<TermVocabulary>();
118
119 private Set<Feature> descriptiveConcepts = new HashSet<Feature>();
120 private Set<AnnotationType> annotationTypes = new HashSet<AnnotationType>();
121 private Set<Feature> featureSet = new HashSet<Feature>();
122 private ReferenceBase sec = ReferenceFactory.newDatabase();
123 private ReferenceBase sourceReference = null;
124
125 private Language datasetLanguage = null;
126
127 private Namespace xmlNamespace = Namespace.getNamespace("xml","http://www.w3.org/XML/1998/namespace");
128
129 private String generatorName = "";
130 private String generatorVersion = "";
131
132 private Set<StatisticalMeasure> statisticalMeasures = new HashSet<StatisticalMeasure>();
133 private Set<VersionableEntity> featureData = new HashSet<VersionableEntity>();
134 private Set<FeatureTree> featureTrees = new HashSet<FeatureTree>();
135 private Set<TaxonomicTree> taxonomicTrees = new HashSet<TaxonomicTree>();
136
137 private Rights copyright = null;
138
139 private int taxonNamesCount = 0; //XIM ajout
140
141 public SDDImport(){
142 super();
143 }
144
145 @Override
146 public boolean doCheck(SDDImportState state){
147 boolean result = true;
148 logger.warn("No check implemented for SDD");
149 return result;
150 }
151
152 // @Override
153 // public boolean doInvoke(IImportConfigurator config, Map<String, MapWrapper<? extends CdmBase>> stores){
154 @Override
155 public boolean doInvoke(SDDImportState state){
156 boolean success = true;
157
158 TransactionStatus ts = startTransaction();
159 SDDImportConfigurator sddConfig = state.getConfig();
160
161 logger.info("start Datasets ...");
162 // <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">
163 Element root = sddConfig.getSourceRoot();
164 Namespace sddNamespace = sddConfig.getSddNamespace();
165
166 logger.info("start TechnicalMetadata ...");
167 // <TechnicalMetadata created="2006-04-20T10:00:00">
168 importTechnicalMetadata(root, sddNamespace, sddConfig);
169 List<Element> elDatasets = root.getChildren("Dataset",sddNamespace);
170 int i = 0;
171
172 //for each Dataset
173 logger.info("start Dataset ...");
174 for (Element elDataset : elDatasets){
175 success &= importDataset(elDataset, sddNamespace, state);
176 if ((++i % modCount) == 0){ logger.info("Datasets handled: " + i);}
177 logger.info(i + " Datasets handled");
178 }
179 commitTransaction(ts);
180 return success;
181 }
182
183 /* (non-Javadoc)
184 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
185 */
186 protected boolean isIgnore(SDDImportState state){
187 return false;
188 }
189
190
191 // associates the reference of a media object in SDD with a CdmBase Object
192 protected void associateImageWithCdmBase(String refMO, CdmBase cb){
193 if ((refMO != null) && (cb!=null)) {
194 if (! refMO.equals("")) {
195 if (! mediaObject_ListCdmBase.containsKey(refMO)) {
196 List<CdmBase> lcb = new ArrayList<CdmBase>();
197 lcb.add(cb);
198 mediaObject_ListCdmBase.put(refMO,lcb);
199 } else {
200 List<CdmBase> lcb = mediaObject_ListCdmBase.get(refMO);
201 lcb.add(cb);
202 mediaObject_ListCdmBase.put(refMO,lcb);
203 }
204 }
205 }
206 }
207
208 // imports information about the Dataset
209 protected void importDatasetRepresentation(Element parent, Namespace sddNamespace){
210 logger.info("start Representation ...");
211 /* <Representation>
212 <Label>The Genus Viola</Label>
213 <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>
214 </Representation>
215 */
216 Element elRepresentation = parent.getChild("Representation",sddNamespace);
217 String label = (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace);
218 String detail = (String)ImportHelper.getXmlInputValue(elRepresentation, "Detail",sddNamespace);
219
220 sec.setTitleCache(label, true);
221
222 if (detail != null) {
223 Annotation annotation = Annotation.NewInstance(detail, datasetLanguage);
224 annotation.setAnnotationType(AnnotationType.EDITORIAL());
225 sec.addAnnotation(annotation);
226 }
227
228 List<Element> listMediaObjects = elRepresentation.getChildren("MediaObject",sddNamespace);
229
230 for (Element elMediaObject : listMediaObjects) {
231 String ref = null;
232 String role = null;
233 if (elMediaObject != null) {
234 ref = elMediaObject.getAttributeValue("ref");
235 role = elMediaObject.getAttributeValue("role");
236 }
237 if (ref != null) {
238 if (!ref.equals("")) {
239 this.associateImageWithCdmBase(ref,sourceReference);
240 this.associateImageWithCdmBase(ref,sec);
241 mediaObject_Role.put(ref,role);
242 }
243 }
244 }
245 }
246
247 // imports the representation (label, detail, lang) of a particular SDD element
248 protected void importRepresentation(Element parent, Namespace sddNamespace, VersionableEntity ve, String id, IImportConfigurator config){
249 Element elRepresentation = parent.getChild("Representation",sddNamespace);
250
251 Map<Language,List<String>> langLabDet = new HashMap<Language,List<String>>();
252
253 handleRepresentationLabels(sddNamespace, elRepresentation, langLabDet);
254 handleRepresentationDetails(sddNamespace, elRepresentation, langLabDet);
255
256 if (ve instanceof TermBase) {
257 makeRepresentationForTerms((TermBase)ve, langLabDet);
258 }else if (ve instanceof Media) {
259 makeRepresentationForMedia((Media)ve, langLabDet);
260 }else if (ve instanceof IdentifiableEntity<?>) {
261 IdentifiableEntity<?> ie = (IdentifiableEntity<?>)ve;
262 makeRepresentationForIdentifiableEntity(sddNamespace, ie, elRepresentation, langLabDet);
263 if (ve instanceof IdentifiableMediaEntity<?>){
264 makeRepresentationForIdentifiableMediaEntity(parent, sddNamespace, (IdentifiableMediaEntity<?>)ve);
265 }
266 }
267
268 makeRepresentationMediaObjects(sddNamespace, ve, elRepresentation);
269
270 }
271
272
273 /**
274 * Handles the "Detail" children of representations. Adds the result to the langLabDet.
275 * @param sddNamespace
276 * @param elRepresentation
277 * @param langLabDet
278 */
279 private void handleRepresentationDetails(Namespace sddNamespace,
280 Element elRepresentation, Map<Language, List<String>> langLabDet) {
281 List<Element> listDetails = elRepresentation.getChildren("Detail",sddNamespace);
282 for (Element elDetail : listDetails){
283 Language language = getLanguage(elDetail);
284 String role = elDetail.getAttributeValue("role");
285 String detail = elDetail.getText();
286 List<String> labDet = langLabDet.get(language);
287 labDet.add(detail);
288 labDet.add(role);
289 langLabDet.put(language, labDet);
290 }
291 }
292
293 /**
294 * Handles the "Label" children of representations. Adds the result to the langLabDet.
295 * @param sddNamespace
296 * @param elRepresentation
297 * @param langLabDet
298 */
299 private void handleRepresentationLabels(Namespace sddNamespace,
300 Element elRepresentation, Map<Language, List<String>> langLabDet) {
301 // <Label xml:lang="la">Viola hederacea Labill.</Label>
302 List<Element> listLabels = elRepresentation.getChildren("Label",sddNamespace);
303 for (Element elLabel : listLabels){
304 Language language = getLanguage(elLabel);
305 String label = elLabel.getText();
306 List<String> labDet = new ArrayList<String>(3);
307 labDet.add(label);
308 langLabDet.put(language, labDet);
309 }
310 }
311
312 /**
313 *
314 * @param ve
315 * @param langLabDet
316 */
317 private void makeRepresentationForMedia(Media m, Map<Language, List<String>> langLabDet) {
318 for (Language lang : langLabDet.keySet()){
319 List<String> labDet = langLabDet.get(lang);
320 if (labDet.get(0) != null){
321 m.addTitle(LanguageString.NewInstance(labDet.get(0), lang));
322 }
323 if (labDet.size()>1) {
324 m.addDescription(labDet.get(1), lang);
325 }
326 }
327 }
328
329 /**
330 * Handles representations for terms. Adds one representation per language in langLabDet.
331 *
332 * @param ve
333 * @param langLabDet
334 */
335 private void makeRepresentationForTerms(TermBase tb, Map<Language, List<String>> langLabDet) {
336 for (Language lang : langLabDet.keySet()){
337 List<String> labDet = langLabDet.get(lang);
338 if (labDet.size()>0){
339 if (labDet.size()>1) {
340 tb.addRepresentation(Representation.NewInstance(labDet.get(1), labDet.get(0), labDet.get(0), lang));
341 } else {
342 tb.addRepresentation(Representation.NewInstance(labDet.get(0), labDet.get(0), labDet.get(0), lang));
343 }
344 }
345 }
346 }
347
348
349 /**
350 * Handles the "MediaObject" children of representations.
351 * @param sddNamespace
352 * @param ve
353 * @param elRepresentation
354 */
355 private void makeRepresentationMediaObjects(Namespace sddNamespace,
356 VersionableEntity ve, Element elRepresentation) {
357 List <Element> listMediaObjects = elRepresentation.getChildren("MediaObject", sddNamespace);
358 for (Element elMediaObject : listMediaObjects) {
359 String ref = null;
360 //TODO
361 String role = null;
362 if (elMediaObject != null) {
363 ref = elMediaObject.getAttributeValue("ref");
364 role = elMediaObject.getAttributeValue("role");
365 }
366 if (StringUtils.isNotBlank(ref)) {
367 if (ve instanceof TaxonDescription) {
368 TaxonDescription td = (TaxonDescription) ve;
369 //TODO: ensure that all images are imported
370 if (td.getDescriptionSources().size() > 0) {
371 this.associateImageWithCdmBase(ref,(ReferenceBase) td.getDescriptionSources().toArray()[0]);
372 } else {
373 ReferenceBase descriptionSource = ReferenceFactory.newGeneric();
374 td.addDescriptionSource(descriptionSource);
375 this.associateImageWithCdmBase(ref,descriptionSource);
376 }
377 } else {
378 this.associateImageWithCdmBase(ref,ve);
379 }
380 }
381 }
382 }
383
384 /**
385 * Handles the "Links" element
386 * @param parent
387 * @param sddNamespace
388 * @param ve
389 */
390 private void makeRepresentationForIdentifiableMediaEntity(Element parent,
391 Namespace sddNamespace, IdentifiableMediaEntity ime) {
392 Element elLinks = parent.getChild("Links",sddNamespace);
393
394 if (elLinks != null) {
395
396 // <Link rel="Alternate" href="http://www.diversitycampus.net/people/hagedorn"/>
397 List<Element> listLinks = elLinks.getChildren("Link", sddNamespace);
398 Media link = Media.NewInstance();
399 MediaRepresentation mr = MediaRepresentation.NewInstance();
400 int k = 0;
401 //for each Link
402 for (Element elLink : listLinks){
403
404 try {
405 //TODO
406 String rel = elLink.getAttributeValue("rel");
407 String href = elLink.getAttributeValue("href");
408
409 mr.addRepresentationPart(MediaRepresentationPart.NewInstance(href, null));
410 link.addRepresentation(mr);
411 ime.addMedia(link);
412
413 } catch (Exception e) {
414 //FIXME
415 logger.warn("Import of Link " + k + " failed.");
416 }
417
418 if ((++k % modCount) == 0){ logger.info("Links handled: " + k);}
419
420 }
421 }
422 }
423
424 /**
425 * @param sddNamespace
426 * @param ve
427 * @param elRepresentation
428 * @param langLabDet
429 * @return
430 */
431 private void makeRepresentationForIdentifiableEntity(Namespace sddNamespace, IdentifiableEntity<?> ie,
432 Element elRepresentation, Map<Language, List<String>> langLabDet) {
433 List<String> labDet = null;
434
435 if (ie instanceof TaxonNameBase) {
436 if (langLabDet.keySet().contains(getTermService().getLanguageByIso("la"))) {
437 labDet = langLabDet.get(getTermService().getLanguageByIso("la"));
438 } else if (langLabDet.keySet().contains(datasetLanguage)) {
439 labDet = langLabDet.get(datasetLanguage);
440 logger.info("TaxonName " + (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace) + " is not specified as a latin name.");
441 } else {
442 labDet = langLabDet.get(langLabDet.keySet().iterator().next());
443 logger.info("TaxonName " + (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace) + " is not specified as a latin name.");
444 }
445 } else {
446 labDet = langLabDet.get(langLabDet.keySet().iterator().next());
447 }
448
449 //FIXME labDet is != null only for TaxonNameBase
450 ie.setTitleCache(labDet.get(0), true);
451
452 if (labDet.size()>1) {
453 Annotation annotation = null;
454 if (labDet.get(1) != null) {
455 if (labDet.get(2) != null) {
456 annotation = Annotation.NewInstance(labDet.get(2) + " - " + labDet.get(1), datasetLanguage);
457 } else {
458 annotation = Annotation.NewInstance(labDet.get(1), datasetLanguage);
459 }
460 }
461 ie.addAnnotation(annotation);
462 }
463 return;
464 }
465
466 /**
467 * @param elLabel
468 * @return
469 */
470 private Language getLanguage(Element elLanguage) {
471 String lang = elLanguage.getAttributeValue("lang",xmlNamespace);
472 Language language = null;
473 if (StringUtils.isNotBlank(lang)) {
474 language = getTermService().getLanguageByIso(lang.substring(0, 2));
475 } else {
476 language = datasetLanguage;
477 }
478 return language;
479 }
480
481
482 // imports the representation (label, detail, lang) of a particular SDD element
483 protected void importTechnicalMetadata(Element root, Namespace sddNamespace, SDDImportConfigurator sddConfig){
484 Element elTechnicalMetadata = root.getChild("TechnicalMetadata", sddNamespace);
485 String nameCreated = elTechnicalMetadata.getAttributeValue("created");
486 sourceReference = sddConfig.getSourceReference();
487
488 if (nameCreated != null) {
489 if (!nameCreated.equals("")) {
490 int year = Integer.parseInt(nameCreated.substring(0,4));
491 int monthOfYear = Integer.parseInt(nameCreated.substring(5,7));
492 int dayOfMonth = Integer.parseInt(nameCreated.substring(8,10));
493 int hourOfDay = Integer.parseInt(nameCreated.substring(11,13));
494 int minuteOfHour = Integer.parseInt(nameCreated.substring(14,16));
495 int secondOfMinute = Integer.parseInt(nameCreated.substring(17,19));
496 DateTime created = new DateTime(year,monthOfYear,dayOfMonth,hourOfDay,minuteOfHour,secondOfMinute,0);
497 sourceReference.setCreated(created);
498 sec.setCreated(created);
499 }
500 }
501
502 // <Generator name="n/a, handcrafted instance document" version="n/a"/>
503 Element elGenerator = elTechnicalMetadata.getChild("Generator", sddNamespace);
504 generatorName = elGenerator.getAttributeValue("name");
505 generatorVersion = elGenerator.getAttributeValue("version");
506
507 sec.addAnnotation(Annotation.NewDefaultLanguageInstance(generatorName + " - " + generatorVersion));
508 sourceReference.addAnnotation(Annotation.NewDefaultLanguageInstance(generatorName + " - " + generatorVersion));
509
510 }
511
512 // imports the complete dataset information
513 protected boolean importDataset(Element elDataset, Namespace sddNamespace, SDDImportState state){ // <Dataset xml:lang="en-us">
514 boolean success = true;
515 SDDImportConfigurator sddConfig = state.getConfig();
516
517 importDatasetLanguage(elDataset,sddConfig);
518 importDatasetRepresentation(elDataset, sddNamespace);
519 importRevisionData(elDataset, sddNamespace);
520 importIPRStatements(elDataset, sddNamespace, sddConfig);
521 importTaxonNames(elDataset, sddNamespace, sddConfig);
522
523 importDescriptiveConcepts(elDataset, sddNamespace, sddConfig);
524 //FIXME delete
525 getTermService().getLanguageByIso("la");
526 success &= importCharacters(elDataset, sddNamespace, sddConfig);
527 importCharacterTrees(elDataset, sddNamespace, sddConfig, success);
528
529 //FIXME delete
530 getTermService().getLanguageByIso("la");
531
532
533 MarkerType editorMarkerType = getMarkerType(state, SDDTransformer.uuidMarkerEditor, "editor", "Editor", "edt");
534 MarkerType geographicAreaMarkerType = getMarkerType(state, SDDTransformer.uuidMarkerSDDGeographicArea, "SDDGeographicArea", "SDDGeographicArea", "ga");
535 MarkerType descriptiveConceptMarkerType = getMarkerType(state, SDDTransformer.uuidMarkerDescriptiveConcept, "DescriptiveConcept", "Descriptive Concept", "DC");
536 markerTypes.add(editorMarkerType);
537 markerTypes.add(geographicAreaMarkerType);
538 markerTypes.add(descriptiveConceptMarkerType);
539
540 //FIXME delete
541 getTermService().getLanguageByIso("la");
542
543
544 //saving of all imported data into the CDM db
545 saveVocabularies();
546 saveFeatures();
547 saveModifiers();
548 saveStates();
549 saveMarkerType();
550 saveAreas(geographicAreaMarkerType);
551 saveUnits();
552 saveStatisticalMeasure();
553 saveAnnotationType();
554
555 //FIXME delete
556 getTermService().getLanguageByIso("la");
557
558 success &= importCodedDescriptions(elDataset, sddNamespace, sddConfig);
559 importAgents(elDataset, sddNamespace, sddConfig, success);
560 importPublications(elDataset, sddNamespace, sddConfig, success);
561 importMediaObjects(elDataset, sddNamespace, sddConfig, success);
562 importTaxonHierarchies(elDataset, sddNamespace, sddConfig, success);
563 importGeographicAreas(elDataset, sddNamespace, sddConfig);
564 importSpecimens(elDataset,sddNamespace, sddConfig);
565
566
567
568 if ((authors != null)||(editors != null)) {
569 Team team = Team.NewInstance();
570 if (authors != null) {
571 for (Person author : authors.values()){
572 team.addTeamMember(author);
573 }
574 }
575 if (editors != null) {
576 Marker marker = Marker.NewInstance();
577 marker.setMarkerType(editorMarkerType);
578 for (Person editor : editors.values()){
579 Person edit = editor;
580 edit.addMarker(marker);
581 team.addTeamMember(edit);
582 }
583 }
584 sec.setAuthorTeam(team);
585 sourceReference.setAuthorTeam(team);
586 }
587
588 if (copyright != null) {
589 sourceReference.addRights(copyright);
590 sec.addRights(copyright);
591 }
592
593 // Returns a CdmApplicationController created by the values of this configuration.
594 IDescriptionService descriptionService = getDescriptionService();
595
596 for (TaxonDescription taxonDescription : taxonDescriptions.values()){
597 // Persists a Description
598 descriptionService.save(taxonDescription);
599 }
600
601 for (String ref : taxonDescriptions.keySet()){
602 TaxonDescription td = taxonDescriptions.get(ref);
603 if (citations.containsKey(ref)) {
604 ReferenceBase publication = publications.get(citations.get(ref));
605 if (locations.containsKey(ref)) {
606 Annotation location = Annotation.NewInstance(locations.get(ref), datasetLanguage);
607 AnnotationType annotationType = AnnotationType.NewInstance("", "location", "");
608 annotationTypes.add(annotationType);
609 location.setAnnotationType(annotationType);
610 (publication).addAnnotation(location);
611 }
612 td.addDescriptionSource(publication);
613 }
614 }
615 logger.info("end makeTaxonDescriptions ...");
616
617
618 // for (Iterator<TermVocabulary<Modifier>> k = termVocabularyStates.iterator() ; k.hasNext() ;){
619 // TermVocabulary<Modifier> termVocabulary = k.next();
620 // getVocabularyService().save(termVocabulary); //XIM
621 // }
622
623
624 //sddConfig.setSourceReference(sourceReference);
625
626
627 if (descriptiveConcepts != null) {
628 for (Feature feature : descriptiveConcepts) {
629 Marker marker = Marker.NewInstance();
630 marker.setMarkerType(descriptiveConceptMarkerType);
631 feature.addMarker(marker);
632 }
633 }
634 saveFeatures();
635
636 /*Marker markerd = Marker.NewInstance();
637 markerd.setMarkerType(descriptiveConceptMarker);
638 Feature fiture = Feature.NewInstance("Fitoure","Fitoure","Fitoure");
639 fiture.addMarker(markerd);
640 TermVocabulary<Modifier> termVocabularyState = new TermVocabulary<Modifier>("test","test","test","test");
641 Modifier modif = new Modifier("zoub","zab","zib");
642 termVocabularyState.addTerm(modif);
643 getVocabularyService().save(termVocabularyState);
644 fiture.addRecommendedModifierEnumeration(termVocabularyState);
645 termService.save(modif);
646 termService.save(fiture);*/
647
648 //XIMtermService.save(editorMarkerType);
649
650 //XIMtermService.save(geographicAreaMarkerType);
651
652 // referenceService.saveReference(sourceReference);
653 for (ReferenceBase publication : publications.values()){
654 getReferenceService().save(publication);
655 }
656
657 for (FeatureTree featureTree : featureTrees) {
658 getFeatureTreeService().save(featureTree);
659 }
660 for (TaxonomicTree taxonomicTree : taxonomicTrees) {
661 getTaxonTreeService().save(taxonomicTree);
662 }
663 for (Specimen specimen : specimens.values()) {
664 getOccurrenceService().save(specimen);
665 }
666 logger.info("end of persistence ...");
667
668 return success;
669 }
670
671 /**
672 *
673 */
674 private void saveVocabularies() {
675 for (TermVocabulary vocabulary : vocabularies ){
676 getVocabularyService().save(vocabulary);
677 }
678
679 }
680
681 private void saveAnnotationType() {
682 for (AnnotationType annotationType: annotationTypes){
683 getTermService().save(annotationType);
684 }
685 }
686
687 private void saveStatisticalMeasure() {
688 for (StatisticalMeasure sm : statisticalMeasures){
689 getTermService().save(sm);
690 }
691 }
692
693 private void saveUnits() {
694 if (units != null) {
695 for (MeasurementUnit unit : units.values()){
696 if (unit != null) {
697 getTermService().save(unit);
698 }
699 }
700 }
701 }
702
703 private void saveAreas(MarkerType geographicAreaMarkerType) {
704 for (NamedArea area : namedAreas.values() ){
705 Marker marker = Marker.NewInstance();
706 marker.setMarkerType(geographicAreaMarkerType);
707 area.addMarker(marker);
708 getTermService().save(area);
709 }
710 }
711
712 private void saveStates() {
713 for (State state : states.values() ){
714 getTermService().save(state);
715 }
716 }
717
718 private void saveMarkerType() {
719 for (MarkerType markerType : markerTypes){
720 getTermService().save(markerType);
721 }
722 }
723
724 private void saveModifiers() {
725 for (Modifier modifier : modifiers.values() ){
726 getTermService().save(modifier);
727 }
728 }
729
730 private void saveFeatures() {
731 for (Feature feature : features.values() ){
732 getTermService().save(feature);
733 }
734 }
735
736 // imports the default language of the dataset
737 protected void importDatasetLanguage(Element elDataset, SDDImportConfigurator sddConfig){
738 String nameLang = elDataset.getAttributeValue("lang",xmlNamespace);
739
740 if (!nameLang.equals("")) {
741 String iso = nameLang.substring(0, 2);
742 datasetLanguage = getTermService().getLanguageByIso(iso);
743 } else {
744 datasetLanguage = Language.DEFAULT();
745 }
746 if (datasetLanguage == null) {
747 datasetLanguage = Language.DEFAULT();
748 }
749 }
750
751 // imports the specimens
752 protected void importSpecimens(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig) {
753 logger.info("start Specimens ...");
754 /* <Specimens>
755 <Specimen id="sp1">
756 <Representation>
757 <Label>TJM45337</Label>
758 </Representation>
759 </Specimen>
760 </Specimens>
761 */
762 Element elSpecimens = elDataset.getChild("Specimens",sddNamespace);
763 if (elSpecimens != null){
764 List<Element> listSpecimens = elSpecimens.getChildren("Specimen", sddNamespace);
765 int j = 0;
766 for (Element elSpecimen : listSpecimens) {
767 String id = elSpecimen.getAttributeValue("id");
768 Specimen specimen = null;
769 if (!id.equals("")) {
770 specimen = Specimen.NewInstance();
771 specimens.put(id,specimen);
772 importRepresentation(elSpecimen, sddNamespace, specimen, id, sddConfig);
773 }
774 }
775
776 }
777 }
778
779 // imports the revision data associated with the Dataset (authors, modifications)
780 protected void importRevisionData(Element elDataset, Namespace sddNamespace){
781 // <RevisionData>
782 logger.info("start RevisionData ...");
783 Element elRevisionData = elDataset.getChild("RevisionData",sddNamespace);
784 if (elRevisionData != null){
785 // <Creators>
786 Element elCreators = elRevisionData.getChild("Creators",sddNamespace);
787
788 // <Agent role="aut" ref="a1"/>
789 List<Element> listAgents = elCreators.getChildren("Agent", sddNamespace);
790
791 int j = 0;
792 //for each Agent
793 for (Element elAgent : listAgents){
794
795 String role = elAgent.getAttributeValue("role");
796 String ref = elAgent.getAttributeValue("ref");
797 if (role.equals("aut")) {
798 if(!ref.equals("")) {
799 authors.put(ref, null);
800 }
801 }
802 if (role.equals("edt")) {
803 if(!ref.equals("")) {
804 editors.put(ref, null);
805 }
806 }
807 if ((++j % modCount) == 0){ logger.info("Agents handled: " + j);}
808
809 }
810
811 // <DateModified>2006-04-08T00:00:00</DateModified>
812 String stringDateModified = (String)ImportHelper.getXmlInputValue(elRevisionData, "DateModified",sddNamespace);
813
814 if (stringDateModified != null) {
815 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");
816 Date d = null;
817 try {
818 d = sdf.parse(stringDateModified);
819 } catch(Exception e) {
820 System.err.println("Exception :");
821 e.printStackTrace();
822 }
823
824 DateTime updated = null;
825 if (d != null) {
826 updated = new DateTime(d);
827 sourceReference.setUpdated(updated);
828 sec.setUpdated(updated);
829 }
830 }
831 }
832 }
833
834 // imports ipr statements associated with a dataset
835 protected void importIPRStatements(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig){
836 // <IPRStatements>
837 logger.info("start IPRStatements ...");
838 Element elIPRStatements = elDataset.getChild("IPRStatements",sddNamespace);
839 // <IPRStatement role="Copyright">
840 if (elIPRStatements != null) {
841 List<Element> listIPRStatements = elIPRStatements.getChildren("IPRStatement", sddNamespace);
842 int j = 0;
843 //for each IPRStatement
844
845 for (Element elIPRStatement : listIPRStatements){
846
847 String role = elIPRStatement.getAttributeValue("role");
848 // <Label xml:lang="en-au">(c) 2003-2006 Centre for Occasional Botany.</Label>
849 Element elLabel = elIPRStatement.getChild("Label",sddNamespace);
850 String lang = "";
851 if (elLabel != null) {
852 lang = elLabel.getAttributeValue("lang",xmlNamespace);
853 }
854 String label = (String)ImportHelper.getXmlInputValue(elIPRStatement, "Label",sddNamespace);
855
856 if (role.equals("Copyright")) {
857 Language iprLanguage = null;
858 if (lang != null) {
859 if (!lang.equals("")) {
860 iprLanguage = getTermService().getLanguageByIso(lang.substring(0, 2));
861 } else {
862 iprLanguage = datasetLanguage;
863 }
864 }
865 if (iprLanguage == null) {
866 iprLanguage = datasetLanguage;
867 }
868 copyright = Rights.NewInstance(label, iprLanguage);
869 }
870
871 if (copyright != null) {
872 sourceReference.addRights(copyright);
873 sec.addRights(copyright);
874 }
875
876 if ((++j % modCount) == 0){ logger.info("IPRStatements handled: " + j);}
877
878 }
879 }
880 }
881
882 // imports the taxon names
883 protected void importTaxonNames(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig){
884 // <TaxonNames>
885 logger.info("start TaxonNames ...");
886 Element elTaxonNames = elDataset.getChild("TaxonNames",sddNamespace);
887 // <TaxonName id="t1" uri="urn:lsid:authority:namespace:my-own-id">
888 if (elTaxonNames != null) {
889 List<Element> listTaxonNames = elTaxonNames.getChildren("TaxonName", sddNamespace);
890 int j = 0;
891 //for each TaxonName
892 for (Element elTaxonName : listTaxonNames){
893
894 String id = elTaxonName.getAttributeValue("id");
895 String uri = elTaxonName.getAttributeValue("uri");
896
897 NonViralName tnb = null;
898 if (!id.equals("")) {
899 tnb = NonViralName.NewInstance(null);
900 IdentifiableSource source = null;
901 if (uri != null) {
902 if (!uri.equals("")) {
903 source = IdentifiableSource.NewInstance(id, "TaxonName", ReferenceFactory.newGeneric(), uri);
904 }
905 } else {
906 source = IdentifiableSource.NewInstance(id, "TaxonName");
907 }
908 tnb.addSource(source);
909 taxonNameBases.put(id,tnb);
910 }
911
912 // <Representation>
913 // <Label xml:lang="la">Viola hederacea Labill.</Label>
914 importRepresentation(elTaxonName, sddNamespace, tnb, id, sddConfig);
915
916 if ((++j % modCount) == 0){ logger.info("TaxonNames handled: " + j);}
917
918 }
919 }
920 }
921
922 // imports the characters (categorical, quantitative and text ; sequence characters not supported) which correspond to CDM Features
923 protected boolean importCharacters(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig){
924 boolean success = true;
925 // <Characters>
926 logger.info("start Characters ...");
927 Element elCharacters = elDataset.getChild("Characters", sddNamespace);
928
929 // <CategoricalCharacter id="c1">
930 if (elCharacters != null) {
931 success &= handleCategoricalData(sddNamespace, sddConfig, elCharacters);
932 success &= handleQuantitativeData(sddNamespace, sddConfig, elCharacters);
933 success &= handleTextCharacters(sddNamespace, sddConfig, elCharacters);
934 }
935
936 /*for (Iterator<Feature> f = features.values().iterator() ; f.hasNext() ;){
937 featureSet.add(f.next()); //XIM Why this line ?
938 }*/
939
940 return success;
941
942 }
943
944 /**
945 * @param sddNamespace
946 * @param sddConfig
947 * @param success
948 * @param elCharacters
949 * @return
950 */
951 private boolean handleCategoricalData(Namespace sddNamespace, SDDImportConfigurator sddConfig, Element elCharacters) {
952 boolean success = true;
953 List<Element> elCategoricalCharacters = elCharacters.getChildren("CategoricalCharacter", sddNamespace);
954 int j = 0;
955 for (Element elCategoricalCharacter : elCategoricalCharacters){
956 try {
957
958 String idCC = elCategoricalCharacter.getAttributeValue("id");
959 Feature categoricalCharacter = Feature.NewInstance();
960 categoricalCharacter.setKindOf(Feature.DESCRIPTION());
961 importRepresentation(elCategoricalCharacter, sddNamespace, categoricalCharacter, idCC, sddConfig);
962 categoricalCharacter.setSupportsCategoricalData(true);
963
964 // <States>
965 Element elStates = elCategoricalCharacter.getChild("States",sddNamespace);
966
967 // <StateDefinition id="s1">
968 List<Element> elStateDefinitions = elStates.getChildren("StateDefinition",sddNamespace);
969 TermVocabulary<State> termVocabularyState = new TermVocabulary<State>();
970
971 vocabularies.add(termVocabularyState);
972
973 int k = 0;
974 //for each StateDefinition
975 for (Element elStateDefinition : elStateDefinitions){
976
977 if ((++k % modCount) == 0){ logger.info("StateDefinitions handled: " + (k-1));}
978
979 String idS = elStateDefinition.getAttributeValue("id");
980 State state = states.get(idS);
981 if (state == null){
982 state = State.NewInstance();
983 }else{
984 logger.debug("State duplicate found");
985 }
986 importRepresentation(elStateDefinition, sddNamespace, state, idS, sddConfig);
987
988 //StateData stateData = StateData.NewInstance();
989 //stateData.setState(state);
990 termVocabularyState.addTerm(state);
991 states.put(idS,state);
992 }
993 categoricalCharacter.addSupportedCategoricalEnumeration(termVocabularyState);
994 features.put(idCC, categoricalCharacter);
995
996 } catch (Exception e) {
997 logger.warn("Import of CategoricalCharacter " + j + " failed.");
998 success = false;
999 }
1000
1001 if ((++j % modCount) == 0){ logger.info("CategoricalCharacters handled: " + j);}
1002
1003 }
1004 return success;
1005 }
1006
1007 /**
1008 * @param sddNamespace
1009 * @param sddConfig
1010 * @param elCharacters
1011 */
1012 private boolean handleQuantitativeData(Namespace sddNamespace, SDDImportConfigurator sddConfig, Element elCharacters) {
1013 boolean success = true;
1014 int j;
1015 // <QuantitativeCharacter id="c2">
1016 List<Element> elQuantitativeCharacters = elCharacters.getChildren("QuantitativeCharacter", sddNamespace);
1017 j = 0;
1018 //for each QuantitativeCharacter
1019 for (Element elQuantitativeCharacter : elQuantitativeCharacters){
1020
1021 try {
1022
1023 String idQC = elQuantitativeCharacter.getAttributeValue("id");
1024
1025 // <Representation>
1026 // <Label>Leaf length</Label>
1027 // </Representation>
1028 Feature quantitativeCharacter = Feature.NewInstance();
1029 quantitativeCharacter.setKindOf(Feature.DESCRIPTION());
1030 importRepresentation(elQuantitativeCharacter, sddNamespace, quantitativeCharacter, idQC, sddConfig);
1031
1032 quantitativeCharacter.setSupportsQuantitativeData(true);
1033
1034 // <MeasurementUnit>
1035 // <Label role="Abbrev">m</Label>
1036 // </MeasurementUnit>
1037 Element elMeasurementUnit = elQuantitativeCharacter.getChild("MeasurementUnit",sddNamespace);
1038 String label = "";
1039 String role = "";
1040 if (elMeasurementUnit != null) {
1041 Element elLabel = elMeasurementUnit.getChild("Label",sddNamespace);
1042 role = elLabel.getAttributeValue("role");
1043 label = (String)ImportHelper.getXmlInputValue(elMeasurementUnit, "Label",sddNamespace);
1044 }
1045
1046 MeasurementUnit unit = null;
1047 if (!label.equals("")){
1048 if (role != null) {
1049 if (role.equals("Abbrev")){
1050 unit = MeasurementUnit.NewInstance(label,label,label);
1051 }
1052 } else {
1053 unit = MeasurementUnit.NewInstance(label,label,label);
1054 }
1055 }
1056
1057 if (unit != null) {
1058 units.put(idQC, unit);
1059 }
1060
1061 //<Default>
1062 // <MeasurementUnitPrefix>milli</MeasurementUnitPrefix>
1063 //</Default>
1064 Element elDefault = elQuantitativeCharacter.getChild("Default",sddNamespace);
1065 if (elDefault != null) {
1066 String measurementUnitPrefix = (String)ImportHelper.getXmlInputValue(elDefault, "MeasurementUnitPrefix",sddNamespace);
1067 if (! measurementUnitPrefix.equals("")){
1068 defaultUnitPrefixes.put(idQC, measurementUnitPrefix);
1069 }
1070 }
1071
1072 features.put(idQC, quantitativeCharacter);
1073
1074 } catch (Exception e) {
1075 //FIXME
1076 logger.warn("Import of QuantitativeCharacter " + j + " failed.");
1077 success = false;
1078 }
1079
1080 if ((++j % modCount) == 0){ logger.info("QuantitativeCharacters handled: " + j);}
1081
1082 }
1083 return success;
1084 }
1085
1086 private boolean handleTextCharacters(Namespace sddNamespace, SDDImportConfigurator sddConfig, Element elCharacters) {
1087 boolean success = true;
1088 int j;
1089 // <TextCharacter id="c3">
1090 List<Element> elTextCharacters = elCharacters.getChildren("TextCharacter", sddNamespace);
1091 j = 0;
1092 //for each TextCharacter
1093 for (Element elTextCharacter : elTextCharacters){
1094
1095 try {
1096
1097 String idTC = elTextCharacter.getAttributeValue("id");
1098
1099 // <Representation>
1100 // <Label xml:lang="en">Leaf features not covered by other characters</Label>
1101 // </Representation>
1102 Feature textCharacter = Feature.NewInstance();
1103 textCharacter.setKindOf(Feature.DESCRIPTION());
1104 importRepresentation(elTextCharacter, sddNamespace, textCharacter, idTC, sddConfig);
1105
1106 textCharacter.setSupportsTextData(true);
1107
1108 features.put(idTC, textCharacter);
1109
1110 } catch (Exception e) {
1111 //FIXME
1112 logger.warn("Import of TextCharacter " + j + " failed.");
1113 success = false;
1114 }
1115
1116 if ((++j % modCount) == 0){ logger.info("TextCharacters handled: " + j);}
1117
1118 }
1119 return success;
1120 }
1121
1122 // imports the descriptions of taxa
1123 protected boolean importCodedDescriptions(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig){
1124 boolean success = true;
1125
1126 // <CodedDescriptions>
1127 logger.info("start CodedDescriptions ...");
1128 Element elCodedDescriptions = elDataset.getChild("CodedDescriptions",sddNamespace);
1129
1130 // <CodedDescription id="D101">
1131 if (elCodedDescriptions != null) {
1132 List<Element> listCodedDescriptions = elCodedDescriptions.getChildren("CodedDescription", sddNamespace);
1133 int j = 0;
1134 //for each CodedDescription
1135 for (Element elCodedDescription : listCodedDescriptions){
1136 success &= handleCodedDescription(sddNamespace, sddConfig, elCodedDescription, j);
1137 if ((++j % modCount) == 0){ logger.info("CodedDescriptions handled: " + j);}
1138 }
1139 }
1140 return success;
1141 }
1142
1143 /**
1144 * @param sddNamespace
1145 * @param sddConfig
1146 * @param j
1147 * @param elCodedDescription
1148 * @return
1149 */
1150 private boolean handleCodedDescription(Namespace sddNamespace, SDDImportConfigurator sddConfig, Element elCodedDescription, int j) {
1151 boolean success = true ;
1152 try {
1153
1154 String idCD = elCodedDescription.getAttributeValue("id");
1155
1156 // <Representation>
1157 // <Label>&lt;i&gt;Viola hederacea&lt;/i&gt; Labill. as revised by R. Morris April 8, 2006</Label>
1158 // </Representation>
1159 TaxonDescription taxonDescription = TaxonDescription.NewInstance();
1160 importRepresentation(elCodedDescription, sddNamespace, taxonDescription, idCD, sddConfig);
1161
1162 // <Scope>
1163 // <TaxonName ref="t1"/>
1164 // <Citation ref="p1" location="p. 30"/>
1165 // </Scope>
1166 Element elScope = elCodedDescription.getChild("Scope", sddNamespace);
1167 Taxon taxon;
1168 if (elScope != null) {
1169 taxon = handleCDScope(sddNamespace, sddConfig, idCD, elScope);
1170 } else {//in case no taxon is linked to the description, a new one is created
1171 taxon = handleCDNoScope(sddNamespace, sddConfig, elCodedDescription);
1172 }
1173
1174 // <SummaryData>
1175 Element elSummaryData = elCodedDescription.getChild("SummaryData",sddNamespace);
1176 if (elSummaryData != null) {
1177 handleSummaryCategoricalData(sddNamespace, taxonDescription, elSummaryData);
1178 handleSummaryQuantitativeData(sddNamespace, taxonDescription, elSummaryData);
1179 handleSummaryTextData(sddNamespace, taxonDescription, elSummaryData);
1180 }
1181
1182 if (taxon != null) {
1183 taxon.addDescription(taxonDescription);
1184 }
1185
1186 taxonDescription.setDescriptiveSystem(featureSet);
1187
1188 taxonDescriptions.put(idCD, taxonDescription);//FIXME
1189
1190 } catch (Exception e) {
1191 //FIXME
1192 logger.warn("Import of CodedDescription " + j + " failed.", e);
1193 success = false;
1194 }
1195 return success;
1196 }
1197
1198 /**
1199 * @param sddNamespace
1200 * @param sddConfig
1201 * @param elCodedDescription
1202 * @param taxon
1203 * @return
1204 */
1205 private Taxon handleCDNoScope(Namespace sddNamespace,
1206 SDDImportConfigurator sddConfig, Element elCodedDescription ) {
1207 Taxon taxon = null;
1208 NonViralName nonViralName = NonViralName.NewInstance(null);
1209 String id = new String("" + taxonNamesCount);
1210 IdentifiableSource source = IdentifiableSource.NewInstance(id, "TaxonName");
1211 importRepresentation(elCodedDescription, sddNamespace, nonViralName, id, sddConfig);
1212
1213 if(sddConfig.isDoMatchTaxa()){
1214 taxon = getTaxonService().findBestMatchingTaxon(nonViralName.getTitleCache());
1215 }
1216
1217 if(taxon != null){
1218 nonViralName = HibernateProxyHelper.deproxy(taxon.getName(), NonViralName.class);
1219 // taxonNameBases.put(id ,tnb);
1220 // taxonNamesCount++;
1221 logger.info("using existing Taxon" + taxon.getTitleCache());
1222 } else {
1223 nonViralName.addSource(source);
1224 taxonNameBases.put(id ,nonViralName);
1225 taxonNamesCount++;
1226 logger.info("creating new Taxon from TaxonName" + nonViralName.getTitleCache());
1227 taxon = Taxon.NewInstance(nonViralName, sec);
1228 }
1229 return taxon;
1230 }
1231
1232 /**
1233 * @param sddNamespace
1234 * @param sddConfig
1235 * @param idCD
1236 * @param elScope
1237 * @param taxon
1238 * @return
1239 */
1240 private Taxon handleCDScope(Namespace sddNamespace, SDDImportConfigurator sddConfig,
1241 String idCD, Element elScope) {
1242 Taxon taxon = null;
1243 Element elTaxonName = elScope.getChild("TaxonName", sddNamespace);
1244 String ref = elTaxonName.getAttributeValue("ref");
1245 NonViralName nonViralName = taxonNameBases.get(ref);
1246
1247 if(sddConfig.isDoMatchTaxa()){
1248 taxon = getTaxonService().findBestMatchingTaxon(nonViralName.getTitleCache());
1249 }
1250
1251 if(taxon != null){
1252 logger.info("using existing Taxon" + taxon.getTitleCache());
1253 if(!nonViralName.getUuid().equals(taxon.getName().getUuid())){
1254 logger.warn("TaxonNameBase entity of existing taxon does not match Name in list -> replacing Name in list");
1255 nonViralName = HibernateProxyHelper.deproxy(taxon.getName(), NonViralName.class);
1256 }
1257 } else {
1258 logger.info("creating new Taxon from TaxonName" + nonViralName.getTitleCache());
1259 taxon = Taxon.NewInstance(nonViralName, sec);
1260 }
1261
1262 //citation
1263 Element elCitation = elScope.getChild("Citation",sddNamespace);
1264 if (elCitation != null) {
1265 String refCitation = elCitation.getAttributeValue("ref");
1266 if (! refCitation.equals("")){
1267 citations.put(idCD, refCitation);
1268 }
1269 String location = elCitation.getAttributeValue("location");
1270 if (! location.equals("")){
1271 locations.put(idCD, location);
1272 }
1273 }
1274 return taxon;
1275 }
1276
1277 /**
1278 * @param sddNamespace
1279 * @param taxonDescription
1280 * @param elSummaryData
1281 */
1282 private void handleSummaryTextData(Namespace sddNamespace,
1283 TaxonDescription taxonDescription, Element elSummaryData) {
1284 String ref;
1285 int k;
1286 // <TextChar ref="c3">
1287 List<Element> elTextChars = elSummaryData.getChildren("TextChar", sddNamespace);
1288 k = 0;
1289 //for each TextChar
1290 for (Element elTextChar : elTextChars){
1291 if ((++k % modCount) == 0){ logger.info("TextChar handled: " + (k-1));}
1292 ref = elTextChar.getAttributeValue("ref");
1293 Feature feature = features.get(ref);
1294 TextData textData = TextData.NewInstance();
1295 textData.setFeature(feature);
1296
1297 // <Content>Free form text</Content>
1298 String content = (String)ImportHelper.getXmlInputValue(elTextChar, "Content",sddNamespace);
1299 textData.putText(content, datasetLanguage);
1300 taxonDescription.addElement(textData);
1301 }
1302 }
1303
1304 /**
1305 * @param sddNamespace
1306 * @param taxonDescription
1307 * @param elSummaryData
1308 */
1309 private void handleSummaryQuantitativeData(Namespace sddNamespace,
1310 TaxonDescription taxonDescription, Element elSummaryData) {
1311 String ref;
1312 int k;
1313 // <Quantitative ref="c2">
1314 List<Element> elQuantitatives = elSummaryData.getChildren("Quantitative", sddNamespace);
1315 k = 0;
1316 //for each Quantitative
1317 for (Element elQuantitative : elQuantitatives){
1318 if ((++k % modCount) == 0){ logger.warn("Quantitative handled: " + (k-1));}
1319 ref = elQuantitative.getAttributeValue("ref");
1320 Feature feature = features.get(ref);
1321 QuantitativeData quantitativeData = QuantitativeData.NewInstance();
1322 quantitativeData.setFeature(feature);
1323
1324 MeasurementUnit unit = units.get(ref);
1325 String prefix = defaultUnitPrefixes.get(ref);
1326 if (unit != null) {
1327 String u = unit.getLabel();
1328 if (prefix != null) {
1329 u = prefix + u;
1330 }
1331 unit.setLabel(u);
1332 quantitativeData.setUnit(unit);
1333 }
1334
1335 // <Measure type="Min" value="2.3"/>
1336 List<Element> elMeasures = elQuantitative.getChildren("Measure", sddNamespace);
1337 int l = 0;
1338
1339 //for each State
1340 for (Element elMeasure : elMeasures){
1341 if ((++l % modCount) == 0){ logger.info("States handled: " + (l-1));}
1342 String type = elMeasure.getAttributeValue("type");
1343 String value = elMeasure.getAttributeValue("value");
1344 if (value.contains(",")) {
1345 value = value.replace(',', '.');
1346 }
1347 Float v = Float.parseFloat(value);
1348 //Float v = new Float(0);
1349 StatisticalMeasure t = null;
1350 if (type.equals("Min")) {
1351 t = StatisticalMeasure.MIN();
1352 } else if (type.equals("Mean")) {
1353 t = StatisticalMeasure.AVERAGE();
1354 } else if (type.equals("Max")) {
1355 t = StatisticalMeasure.MAX();
1356 } else if (type.equals("SD")) {
1357 t = StatisticalMeasure.STANDARD_DEVIATION();
1358 } else if (type.equals("N")) {
1359 t = StatisticalMeasure.SAMPLE_SIZE();
1360 } else if (type.equals("UMethLower")) {
1361 t = StatisticalMeasure.TYPICAL_LOWER_BOUNDARY();
1362 } else if (type.equals("UMethUpper")) {
1363 t = StatisticalMeasure.TYPICAL_UPPER_BOUNDARY();
1364 } else if (type.equals("Var")) {
1365 t = StatisticalMeasure.VARIANCE();
1366 } else {
1367 t = StatisticalMeasure.NewInstance(type,type,type);
1368 statisticalMeasures.add(t);
1369 }
1370
1371 StatisticalMeasurementValue statisticalValue = StatisticalMeasurementValue.NewInstance();
1372 statisticalValue.setValue(v);
1373 statisticalValue.setType(t);
1374 quantitativeData.addStatisticalValue(statisticalValue);
1375 featureData.add(statisticalValue);
1376 }
1377 taxonDescription.addElement(quantitativeData);
1378 }
1379 }
1380
1381 /**
1382 * @param sddNamespace
1383 * @param taxonDescription
1384 * @param elSummaryData
1385 */
1386 private void handleSummaryCategoricalData(Namespace sddNamespace,
1387 TaxonDescription taxonDescription, Element elSummaryData) {
1388 String ref;
1389 // <Categorical ref="c4">
1390 List<Element> elCategoricals = elSummaryData.getChildren("Categorical", sddNamespace);
1391 int k = 0;
1392 //for each Categorical
1393 for (Element elCategorical : elCategoricals){
1394 if ((++k % modCount) == 0){ logger.warn("Categorical handled: " + (k-1));}
1395 ref = elCategorical.getAttributeValue("ref");
1396 Feature feature = features.get(ref);
1397 CategoricalData categoricalData = CategoricalData.NewInstance();
1398 categoricalData.setFeature(feature);
1399
1400 // <State ref="s3"/>
1401 List<Element> elStates = elCategorical.getChildren("State", sddNamespace);
1402 int l = 0;
1403
1404 //for each State
1405 for (Element elState : elStates){
1406 if ((++l % modCount) == 0){ logger.info("States handled: " + (l-1));}
1407 ref = elState.getAttributeValue("ref");
1408 State state = states.get(ref);
1409 if (state != null) {
1410 StateData stateData = StateData.NewInstance();
1411 stateData.setState(state);
1412 List<Element> elModifiers = elState.getChildren("Modifier", sddNamespace);
1413 for (Element elModifier : elModifiers){
1414 ref = elModifier.getAttributeValue("ref");
1415 Modifier modifier = modifiers.get(ref);
1416 if (modifier != null) {
1417 stateData.addModifier(modifier);
1418 }
1419 }
1420 categoricalData.addState(stateData);
1421 }
1422 taxonDescription.addElement(categoricalData);
1423 }
1424 }
1425 }
1426
1427 // imports the persons associated with the dataset creation, modification, related publications
1428 protected void importAgents(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig, boolean success){
1429 // <Agents>
1430 logger.info("start Agents ...");
1431 Element elAgents = elDataset.getChild("Agents",sddNamespace);
1432 if (elAgents != null) {
1433 // <Agent id="a1">
1434 List <Element> listAgents = elAgents.getChildren("Agent", sddNamespace);
1435 int j = 0;
1436 //for each Agent
1437 for (Element elAgent : listAgents){
1438
1439 try {
1440
1441 String idA = elAgent.getAttributeValue("id");
1442
1443 // <Representation>
1444 // <Label>Kevin Thiele</Label>
1445 // <Detail role="Description">Ali Baba is also known as r.a.m.</Detail>
1446 // </Representation>
1447 Person person = Person.NewInstance();
1448 importRepresentation(elAgent, sddNamespace, person, idA, sddConfig);
1449 person.addSource(IdentifiableSource.NewInstance(idA, "Agent"));
1450
1451 /*XIM <Links>
1452 Element elLinks = elAgent.getChild("Links",sddNamespace);
1453
1454 if (elLinks != null) {
1455
1456 // <Link rel="Alternate" href="http://www.diversitycampus.net/people/hagedorn"/>
1457 List<Element> listLinks = elLinks.getChildren("Link", sddNamespace);
1458 int k = 0;
1459 //for each Link
1460 for (Element elLink : listLinks){
1461
1462 try {
1463
1464 String rel = elLink.getAttributeValue("rel");
1465 String href = elLink.getAttributeValue("href");
1466
1467 Media link = Media.NewInstance();
1468 MediaRepresentation mr = MediaRepresentation.NewInstance();
1469 mr.addRepresentationPart(MediaRepresentationPart.NewInstance(href, null));
1470 link.addRepresentation(mr);
1471 person.addMedia(link);
1472
1473 } catch (Exception e) {
1474 //FIXME
1475 logger.warn("Import of Link " + k + " failed.");
1476 success = false;
1477 }
1478
1479 if ((++k % modCount) == 0){ logger.info("Links handled: " + k);}
1480
1481 }
1482 }
1483 */
1484 if (authors.containsKey(idA)) {
1485 authors.put(idA,person);
1486 }
1487
1488 if (editors.containsKey(idA)) {
1489 editors.put(idA, person);
1490 }
1491
1492 } catch (Exception e) {
1493 //FIXME
1494 logger.warn("Import of Agent " + j + " failed.");
1495 success = false;
1496 }
1497
1498 if ((++j % modCount) == 0){ logger.info("Agents handled: " + j);}
1499
1500 }
1501 }
1502 }
1503
1504 // imports publications related with the data set
1505 protected void importPublications(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig, boolean success){
1506 /* <Publications>
1507 <Publication id="p112">
1508 <Representation>
1509 <Label>Gee, X. & Haa, Y. (2003). How to be happy in five minutes. Instant Gratifications, Palm Beach.</Label>
1510 </Representation>
1511 <Links>
1512 <Link rel="BasedOn" href="doi:10.1992/32311"/>
1513 <Link rel="Alternate" href="http://some.service.net/providing/bibliographic.data"/>
1514 </Links>
1515 </Publications>
1516 */
1517 logger.info("start Publications ...");
1518 Element elPublications = elDataset.getChild("Publications",sddNamespace);
1519
1520 if (elPublications != null) {
1521 List<Element> listPublications = elPublications.getChildren("Publication", sddNamespace);
1522 int j = 0;
1523 for (Element elPublication : listPublications){
1524
1525 try {
1526
1527 String idP = elPublication.getAttributeValue("id");
1528 ReferenceBase publication = ReferenceFactory.newArticle();
1529 importRepresentation(elPublication, sddNamespace, publication, idP, sddConfig);
1530
1531 publications.put(idP,publication);
1532
1533 } catch (Exception e) {
1534 logger.warn("Import of Publication " + j + " failed.");
1535 success = false;
1536 }
1537
1538 if ((++j % modCount) == 0){ logger.info("Publications handled: " + j);}
1539
1540 }
1541 }
1542 }
1543
1544 // imports media objects such as images //FIXME check mediaobj
1545 protected void importMediaObjects(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig, boolean success){
1546 // <MediaObjects>
1547 logger.info("start MediaObjects ...");
1548 Element elMediaObjects = elDataset.getChild("MediaObjects",sddNamespace);
1549
1550 if (elMediaObjects != null) {
1551 // <MediaObject id="m1">
1552 List<Element> listMediaObjects = elMediaObjects.getChildren("MediaObject", sddNamespace);
1553 int j = 0;
1554 for (Element elMO : listMediaObjects){
1555
1556 String id = "";
1557
1558 try {
1559 String idMO = elMO.getAttributeValue("id");
1560 id = idMO;
1561
1562 // <Representation>
1563 // <Label>Image description, e.g. to be used for alt-attribute in html.</Label>
1564 // </Representation>
1565 Media media = Media.NewInstance();
1566 importRepresentation(elMO, sddNamespace, media, idMO, sddConfig);
1567
1568 // <Type>Image</Type>
1569 // <Source href="http://test.edu/test.jpg"/>
1570 String type = (String)ImportHelper.getXmlInputValue(elMO,"Type",sddNamespace);
1571
1572 if ((type != null) && (type.equals("Image"))) {
1573 Element elSource = elMO.getChild("Source",sddNamespace);
1574 String href = elSource.getAttributeValue("href");
1575
1576 ImageMetaData imageMetaData = ImageMetaData.newInstance();
1577 ImageFile image = null;
1578 if (href.substring(0,7).equals("http://")) {
1579 try{
1580 URL url = new URL(href);
1581
1582 imageMetaData.readMetaData(url.toURI(), 0);
1583 image = ImageFile.NewInstance(url.toString(), null, imageMetaData);
1584 } catch (MalformedURLException e) {
1585 logger.error("Malformed URL", e);
1586 }
1587 } else {
1588 String sns = sddConfig.getSourceNameString();
1589 File f = new File(sns);
1590 File parent = f.getParentFile();
1591 String fi = href;
1592 //String fi = parent.toString() + File.separator + href; //TODO erase file:/
1593 File file = new File(fi);
1594 imageMetaData.readMetaData(file.toURI(), 0);
1595 image = ImageFile.NewInstance(file.toString(), null, imageMetaData);
1596 }
1597 MediaRepresentation representation = MediaRepresentation.NewInstance(imageMetaData.getMimeType(), null);
1598 representation.addRepresentationPart(image);
1599
1600 media.addRepresentation(representation);
1601
1602 ArrayList<CdmBase> lcb = (ArrayList<CdmBase>) mediaObject_ListCdmBase.get(idMO);
1603 if (lcb != null) {
1604 for (int k = 0; k < lcb.size(); k++) {
1605 if (lcb.get(k) instanceof DefinedTermBase) {
1606 DefinedTermBase dtb = (DefinedTermBase) lcb.get(k);
1607 // if (lcb.get(0) instanceof DefinedTermBase) {
1608 // DefinedTermBase dtb = (DefinedTermBase) lcb.get(0);
1609 // if (dtb!=null) {
1610 // if (k == 0) {
1611 dtb.addMedia(media);
1612 //System.out.println(dtb.getLabel());
1613 // } else {
1614 // Media me = (Media) media.clone();
1615 // dtb.addMedia(me);
1616 // }
1617 // }
1618 } else if (lcb.get(k) instanceof ReferenceBase) {
1619 ReferenceBase rb = (ReferenceBase) lcb.get(k);
1620 //} else if (lcb.get(0) instanceof ReferenceBase) {
1621 //ReferenceBase rb = (ReferenceBase) lcb.get(0);
1622 // rb.setTitleCache(label);
1623 // if (rb!=null) {
1624 // if (k == 0) {
1625 rb.addMedia(media);
1626 //System.out.println(rb.getTitle());
1627 // } else {
1628 // Media me = (Media) media.clone();
1629 // rb.addMedia(me);
1630 // }
1631 // }
1632 }/* else if (lcb.get(k) instanceof TaxonNameBase){
1633 TaxonNameBase tb = (TaxonNameBase) lcb.get(k);
1634 tb.addMedia(media);
1635 }*/
1636 }
1637 }
1638 }
1639
1640 } catch (Exception e) {
1641 //FIXME
1642 logger.warn("Could not attach MediaObject " + j + "(SDD: " + id + ") to several objects.");
1643 success = false;
1644 }
1645
1646 if ((++j % modCount) == 0){ logger.info("MediaObjects handled: " + j);
1647
1648 }
1649 }
1650 }
1651 }
1652
1653 // imports the <DescriptiveConcepts> block ; DescriptiveConcepts are used as nodes in CharacterTrees and Characters as leaves
1654 // but since Modifiers can be linked to DescriptiveConcepts they are stored as features with a particular Marker
1655 protected void importDescriptiveConcepts(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig){
1656 /* <DescriptiveConcepts>
1657 <DescriptiveConcept id="dc0">
1658 <Representation>
1659 <Label>Fixed set of modifiers supported in Lucid3</Label>
1660 </Representation>
1661 <Modifiers>
1662 <Modifier id="mod1">
1663 <Representation>
1664 <Label>rarely</Label>
1665 </Representation>
1666 <ModifierClass>Frequency</ModifierClass>
1667 <ProportionRange lowerestimate="0.0" upperestimate="0.25"/>
1668 </Modifier>
1669 </Modifiers>
1670 </DescriptiveConcept>
1671 </DescriptiveConcepts>
1672 */
1673 logger.info("start DescriptiveConcepts ...");
1674 Element elDescriptiveConcepts = elDataset.getChild("DescriptiveConcepts",sddNamespace);
1675 if (elDescriptiveConcepts != null) {
1676 List<Element> listDescriptiveConcepts = elDescriptiveConcepts.getChildren("DescriptiveConcept", sddNamespace);
1677 int j = 0;
1678
1679 for (Element elDescriptiveConcept : listDescriptiveConcepts){
1680 try {
1681 String id = elDescriptiveConcept.getAttributeValue("id");
1682 Feature feature = Feature.NewInstance();
1683 feature.setKindOf(Feature.DESCRIPTION());
1684 if (!id.equals("")) {
1685 // <Representation>
1686 // <Label>Body</Label>
1687 importRepresentation(elDescriptiveConcept, sddNamespace, feature, id, sddConfig);
1688 features.put(id, feature);
1689 getTermService().save(feature);//XIM
1690 descriptiveConcepts.add(feature);
1691 // imports the modifiers
1692 Element elModifiers = elDescriptiveConcept.getChild("Modifiers", sddNamespace);
1693 if (elModifiers !=null){
1694 List<Element> listModifiers = elModifiers.getChildren("Modifier", sddNamespace);
1695 TermVocabulary<Modifier> termVocabularyState = new TermVocabulary<Modifier>();
1696 for (Element elModifier : listModifiers) {
1697 Modifier modif = Modifier.NewInstance();
1698 String idmod = elModifier.getAttributeValue("id");
1699 importRepresentation(elModifier, sddNamespace, modif, idmod, sddConfig);
1700 termVocabularyState.addTerm(modif);
1701 //termVocabularyStates.add(termVocabularyState);
1702 getVocabularyService().save(termVocabularyState);//XIM
1703 modifiers.put(idmod, modif);
1704 }
1705 feature.addRecommendedModifierEnumeration(termVocabularyState);
1706 }
1707
1708 }
1709 }
1710 catch (Exception e) {
1711 logger.warn("Import of DescriptiveConcept " + j + " failed.");
1712 }
1713 if ((++j % modCount) == 0){ logger.info("DescriptiveConcepts handled: " + j);}
1714
1715 }
1716 }
1717 }
1718
1719 // imports the <CharacterTrees> block
1720 protected void importCharacterTrees(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig, boolean success){
1721 // <CharacterTrees>
1722 logger.info("start CharacterTrees ...");
1723 Element elCharacterTrees = elDataset.getChild("CharacterTrees",sddNamespace);
1724
1725 if (elCharacterTrees != null) {
1726 List<Element> listCharacterTrees = elCharacterTrees.getChildren("CharacterTree", sddNamespace);
1727 int j = 0;
1728 for (Element elCharacterTree : listCharacterTrees){
1729 try {
1730 Element elRepresentation = elCharacterTree.getChild("Representation",sddNamespace);
1731 String label = (String)ImportHelper.getXmlInputValue(elRepresentation,"Label",sddNamespace);
1732 //Element elDesignedFor = elCharacterTree.getChild("DesignedFor",sddNamespace);//TODO ?
1733
1734 FeatureTree feattree = FeatureTree.NewInstance();
1735 importRepresentation(elCharacterTree, sddNamespace, feattree, "", sddConfig);
1736 FeatureNode root = feattree.getRoot();
1737 List<Element> listelNodes = elCharacterTree.getChildren("Nodes", sddNamespace);
1738
1739 //Nodes of CharacterTrees in SDD always refer to DescriptiveConcepts
1740 for (Element elNodes : listelNodes) {
1741 handleCharacterNodes(sddNamespace, root, elNodes);
1742 }
1743 featureTrees.add(feattree);
1744 }
1745
1746 catch (Exception e) {
1747 logger.warn("Import of Character tree " + j + " failed.");
1748 success = false;
1749 }
1750 if ((++j % modCount) == 0){ logger.info("CharacterTrees handled: " + j);}
1751
1752 }
1753
1754 }
1755 }
1756
1757 /**
1758 * @param sddNamespace
1759 * @param root
1760 * @param elNodes
1761 */
1762 private void handleCharacterNodes(Namespace sddNamespace, FeatureNode root, Element elNodes) {
1763 List<Element> listNodes = elNodes.getChildren("Node", sddNamespace);
1764 if (listNodes != null) {
1765 for (Element elNode : listNodes){
1766 String idN = elNode.getAttributeValue("id");
1767 FeatureNode fn = null;
1768 Feature dc = null;
1769 if (idN!=null) {
1770 // DescriptiveConcepts are used as nodes in CharacterTrees
1771 Element elDescriptiveConcept = elNode.getChild("DescriptiveConcept", sddNamespace);
1772 if (elDescriptiveConcept != null){
1773 String refDC = elDescriptiveConcept.getAttributeValue("ref");
1774 dc = features.get(refDC);
1775 fn = FeatureNode.NewInstance(dc);
1776 }
1777 if (fn==null){
1778 fn = FeatureNode.NewInstance();
1779 }
1780 Element elParent = elNode.getChild("Parent", sddNamespace);
1781 // in SDD links between Nodes are referenced by the <Parent> tag
1782 if (elParent!=null){
1783 String refP = elParent.getAttributeValue("ref");
1784 if (refP!=null) {
1785 FeatureNode parent = featureNodes.get(refP);
1786 if (parent==null){
1787 root.addChild(fn); // if no parent found or the reference is broken, add the node to the root of the tree
1788 }
1789 else {
1790 parent.addChild(fn);
1791 }
1792 }
1793 }
1794 else {
1795 root.addChild(fn); // if no parent found or the reference is broken, add the node to the root of the tree
1796 }
1797 }
1798 featureNodes.put(idN, fn);
1799 }
1800 }
1801
1802 // Leaves of CharacterTrees in SDD are always CharNodes (referring to Characters)
1803 List<Element> listCharNodes = elNodes.getChildren("CharNode", sddNamespace);
1804 if (listCharNodes != null) {
1805 for (Element elCharNode : listCharNodes){
1806 Element elParent = elCharNode.getChild("Parent", sddNamespace);
1807 Element elCharacter = elCharNode.getChild("Character", sddNamespace);
1808 FeatureNode fn = FeatureNode.NewInstance();
1809 if (elParent!=null){
1810 String refP = elParent.getAttributeValue("ref");
1811 if ((refP!=null)&&(!refP.equals(""))) {
1812 FeatureNode parent = featureNodes.get(refP);
1813 if (parent==null){
1814 parent = root; // if no parent found or the reference is broken, add the node to the root of the tree
1815 }
1816 parent.addChild(fn);
1817 }
1818 }
1819 String refC = elCharacter.getAttributeValue("ref");
1820 if ((refC!=null)&&(!refC.equals(""))){
1821 Feature character = features.get(refC);
1822 fn.setFeature(character);
1823 featureNodes.put(refC, fn);
1824 }
1825 }
1826 }
1827 }
1828
1829 // imports the <TaxonHierarchies> block
1830 protected void importTaxonHierarchies(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig, boolean success){
1831
1832 logger.info("start TaxonHierarchies ...");
1833 Element elTaxonHierarchies = elDataset.getChild("TaxonHierarchies",sddNamespace);
1834
1835 if (elTaxonHierarchies != null) {
1836 List<Element> listTaxonHierarchies = elTaxonHierarchies.getChildren("TaxonHierarchy", sddNamespace);
1837 int j = 0;
1838 for (Element elTaxonHierarchy : listTaxonHierarchies){
1839 try {
1840 Element elRepresentation = elTaxonHierarchy.getChild("Representation",sddNamespace);
1841 String label = (String)ImportHelper.getXmlInputValue(elRepresentation,"Label",sddNamespace);
1842 TaxonomicTree taxonomicTree = TaxonomicTree.NewInstance(label);
1843 importRepresentation(elTaxonHierarchy, sddNamespace, taxonomicTree, "", sddConfig);
1844
1845 Set<TaxonNode> root = taxonomicTree.getChildNodes();
1846 Element elNodes = elTaxonHierarchy.getChild("Nodes", sddNamespace); // There can be only one <Nodes> block for TaxonHierarchies
1847 List<Element> listNodes = elNodes.getChildren("Node", sddNamespace);
1848
1849 for (Element elNode : listNodes){
1850 String idN = elNode.getAttributeValue("id");
1851 TaxonNameBase tnb = null;
1852 if (!idN.equals("")) {
1853 Element elTaxonName = elNode.getChild("TaxonName", sddNamespace);
1854 String refTN = elTaxonName.getAttributeValue("ref");
1855 tnb = taxonNameBases.get(refTN);
1856 Taxon taxon = (Taxon) tnb.getTaxa().iterator().next() ;
1857 Element elParent = elNode.getChild("Parent", sddNamespace);
1858 if (elParent!=null){
1859 String refP = elParent.getAttributeValue("ref");
1860 if (!refP.equals("")) {
1861 TaxonNode parent = taxonNodes.get(refP);
1862 TaxonNode child = parent.addChildTaxon(taxon, sec, "", Synonym.NewInstance(tnb, sec));
1863 taxonNodes.put(idN,child);
1864 }
1865 }
1866 else {
1867 TaxonNode tn = taxonomicTree.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
1868 taxonNodes.put(idN,tn);
1869 }
1870 }
1871 }
1872
1873 taxonomicTrees.add(taxonomicTree);
1874 }
1875
1876 catch (Exception e) {
1877 //FIXME
1878 logger.warn("Import of Taxon Hierarchy " + j + " failed.");
1879 success = false;
1880 }
1881
1882 if ((++j % modCount) == 0){ logger.info("TaxonHierarchies handled: " + j);}
1883
1884 }
1885
1886 }
1887 }
1888
1889
1890 // imports the <GeographicAreas> block
1891 protected void importGeographicAreas(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig) {
1892 Element elGeographicAreas = elDataset.getChild("GeographicAreas",sddNamespace);
1893 if (elGeographicAreas != null) {
1894 List<Element> listGeographicAreas = elGeographicAreas.getChildren("GeographicArea", sddNamespace);
1895 int j = 0;
1896
1897 for (Element elGeographicArea : listGeographicAreas){
1898
1899 String id = elGeographicArea.getAttributeValue("id");
1900 NamedArea na = new NamedArea();
1901 importRepresentation(elGeographicArea, sddNamespace, na, id, sddConfig);
1902 namedAreas.put(id,na);
1903 }
1904 if ((++j % modCount) == 0){ logger.info("GeographicAreas handled: " + j);}
1905 }
1906 }
1907 }