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