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