Basic import of MediaObjects
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / sdd / SDDDescriptionIO.java
1 package eu.etaxonomy.cdm.io.sdd;
2
3 import java.text.SimpleDateFormat;
4 import java.util.Date;
5 import java.util.GregorianCalendar;
6 import java.util.HashMap;
7 import java.util.HashSet;
8 import java.util.Iterator;
9 import java.util.List;
10 import java.util.Map;
11 import java.util.Set;
12
13 import org.apache.log4j.Logger;
14 import org.jdom.Element;
15 import org.jdom.Namespace;
16 import org.joda.time.DateTime;
17 import org.springframework.transaction.TransactionStatus;
18
19 import eu.etaxonomy.cdm.api.service.IDescriptionService;
20 import eu.etaxonomy.cdm.api.service.IReferenceService;
21 import eu.etaxonomy.cdm.api.service.ITermService;
22 import eu.etaxonomy.cdm.io.common.ICdmIO;
23 import eu.etaxonomy.cdm.io.common.IImportConfigurator;
24 import eu.etaxonomy.cdm.io.common.ImportHelper;
25 import eu.etaxonomy.cdm.io.common.MapWrapper;
26 import eu.etaxonomy.cdm.model.agent.Person;
27 import eu.etaxonomy.cdm.model.common.Annotation;
28 import eu.etaxonomy.cdm.model.common.CdmBase;
29 import eu.etaxonomy.cdm.model.common.Language;
30 import eu.etaxonomy.cdm.model.common.LanguageString;
31 import eu.etaxonomy.cdm.model.common.MultilanguageText;
32 import eu.etaxonomy.cdm.model.common.OriginalSource;
33 import eu.etaxonomy.cdm.model.common.TermVocabulary;
34 import eu.etaxonomy.cdm.model.common.VersionableEntity;
35 import eu.etaxonomy.cdm.model.description.CategoricalData;
36 import eu.etaxonomy.cdm.model.description.Feature;
37 import eu.etaxonomy.cdm.model.description.MeasurementUnit;
38 import eu.etaxonomy.cdm.model.description.QuantitativeData;
39 import eu.etaxonomy.cdm.model.description.State;
40 import eu.etaxonomy.cdm.model.description.StatisticalMeasure;
41 import eu.etaxonomy.cdm.model.description.StatisticalMeasurementValue;
42 import eu.etaxonomy.cdm.model.description.TaxonDescription;
43 import eu.etaxonomy.cdm.model.description.TextData;
44 import eu.etaxonomy.cdm.model.media.Media;
45 import eu.etaxonomy.cdm.model.media.Rights;
46 import eu.etaxonomy.cdm.model.name.NonViralName;
47 import eu.etaxonomy.cdm.model.reference.Article;
48 import eu.etaxonomy.cdm.model.reference.Database;
49 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
50 import eu.etaxonomy.cdm.model.taxon.Taxon;
51
52
53 public class SDDDescriptionIO extends SDDIoBase implements ICdmIO {
54 private static final Logger logger = Logger.getLogger(SDDDescriptionIO.class);
55
56 private static int modCount = 1000;
57
58 public SDDDescriptionIO(){
59 super();
60 }
61
62 @Override
63 public boolean doCheck(IImportConfigurator config){
64 boolean result = true;
65 logger.warn("No check implemented for SDD");
66 return result;
67 }
68
69 @Override
70 public boolean doInvoke(IImportConfigurator config, Map<String, MapWrapper<? extends CdmBase>> stores){
71
72 String value;
73 logger.info("start Datasets ...");
74 SDDImportConfigurator sddConfig = (SDDImportConfigurator)config;
75
76 // <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">
77 Element root = sddConfig.getSourceRoot();
78 boolean success =true;
79
80 Namespace sddNamespace = sddConfig.getSddNamespace();
81 Namespace xmlNamespace = Namespace.getNamespace("xml","http://www.w3.org/XML/1998/namespace");
82
83 logger.info("start TechnicalMetadata ...");
84 // <TechnicalMetadata created="2006-04-20T10:00:00">
85 Element elTechnicalMetadata = root.getChild("TechnicalMetadata", sddNamespace);
86 String nameCreated = elTechnicalMetadata.getAttributeValue("created");
87 ReferenceBase sourceReference = config.getSourceReference();
88 ReferenceBase sec = Database.NewInstance();
89 if (nameCreated != null) {
90 if (!nameCreated.equals("")) {
91 int year = Integer.parseInt(nameCreated.substring(0,4));
92 int monthOfYear = Integer.parseInt(nameCreated.substring(5,7));
93 int dayOfMonth = Integer.parseInt(nameCreated.substring(8,10));
94 int hourOfDay = Integer.parseInt(nameCreated.substring(11,13));
95 int minuteOfHour = Integer.parseInt(nameCreated.substring(14,16));
96 int secondOfMinute = Integer.parseInt(nameCreated.substring(17,19));
97 DateTime created = new DateTime(year,monthOfYear,dayOfMonth,hourOfDay,minuteOfHour,secondOfMinute,0);
98 sourceReference.setCreated(created);
99 sec.setCreated(created);
100 }
101 }
102
103 // <Generator name="n/a, handcrafted instance document" version="n/a"/>
104 Element elGenerator = elTechnicalMetadata.getChild("Generator", sddNamespace);
105 String generatorName = elGenerator.getAttributeValue("name");
106 String generatorVersion = elGenerator.getAttributeValue("version");
107
108 List<Element> elDatasets = root.getChildren("Dataset",sddNamespace);
109
110 Map<String,TaxonDescription> taxonDescriptions = new HashMap<String,TaxonDescription>();
111 Map<String,State> states = new HashMap<String,State>();
112 Map<String,MeasurementUnit> units = new HashMap<String,MeasurementUnit>();
113 Map<String,String> defaultUnitPrefixes = new HashMap<String,String>();
114 Map<String,Feature> features = new HashMap<String,Feature>();
115 Map<String,ReferenceBase> publications = new HashMap<String,ReferenceBase>();
116 Map<String,Media> mediaObjects = new HashMap<String,Media>();
117
118 Set<StatisticalMeasure> statisticalMeasures = new HashSet<StatisticalMeasure>();
119 Set<VersionableEntity> featureData = new HashSet<VersionableEntity>();
120
121 int i = 0;
122 //for each Dataset
123 logger.info("start Dataset ...");
124 for (Element elDataset : elDatasets){
125
126 // <Dataset xml:lang="en-us">
127 String nameLang = elDataset.getAttributeValue("lang",xmlNamespace);
128 Language datasetLanguage = null;
129 if (!nameLang.equals("")) {
130 // config.getCdmAppController().getTermService().getLanguageByIso(nameLang);
131 datasetLanguage = Language.ENGLISH();
132 } else {
133 datasetLanguage = Language.ENGLISH();
134 }
135 if (datasetLanguage == null) {
136 datasetLanguage = Language.ENGLISH();
137 }
138
139 /* <Representation>
140 <Label>The Genus Viola</Label>
141 <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>
142 </Representation>
143 */
144 logger.info("start Representation ...");
145 Element elRepresentation = elDataset.getChild("Representation",sddNamespace);
146 String label = (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace);
147 String detail = (String)ImportHelper.getXmlInputValue(elRepresentation, "Detail",sddNamespace);
148
149 sourceReference.setTitleCache(generatorName + " - " + generatorVersion + " - " + label);
150 sec.setTitleCache(generatorName + " - " + generatorVersion + " - " + label);
151
152 Annotation annotation = Annotation.NewInstance(detail, datasetLanguage);
153
154 // <RevisionData>
155 logger.info("start RevisionData ...");
156 Element elRevisionData = elDataset.getChild("RevisionData",sddNamespace);
157
158 // <Creators>
159 Element elCreators = elRevisionData.getChild("Creators",sddNamespace);
160
161 // <Agent role="aut" ref="a1"/>
162 List<Element> listAgents = elCreators.getChildren("Agent", sddNamespace);
163
164 int j = 0;
165 //for each Agent
166 Map<String,Person> authors = new HashMap<String,Person>();
167 Map<String,Person> editors = new HashMap<String,Person>();
168 for (Element elAgent : listAgents){
169
170 String role = elAgent.getAttributeValue("role");
171 String ref = elAgent.getAttributeValue("ref");
172 if (role.equals("aut")) {
173 if(!ref.equals("")) {
174 authors.put(ref, null);
175 }
176 }
177 if (role.equals("edt")) {
178 if(!ref.equals("")) {
179 editors.put(ref, null);
180 }
181 }
182
183 if ((++j % modCount) == 0){ logger.info("Agents handled: " + j);}
184
185 }
186
187 // <DateModified>2006-04-08T00:00:00</DateModified>
188 String stringDateModified = (String)ImportHelper.getXmlInputValue(elRevisionData, "DateModified",sddNamespace);
189
190 if (stringDateModified != null) {
191 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");
192 Date d = null;
193 try {
194 d = sdf.parse(stringDateModified);
195 } catch(Exception e) {
196 System.err.println("Exception :");
197 e.printStackTrace();
198 }
199
200 GregorianCalendar updated = null;
201 if (d != null) {
202 updated = new java.util.GregorianCalendar();
203 updated.setTime(d);
204 sourceReference.setUpdated(updated);
205 sec.setUpdated(updated);
206 }
207 }
208
209 // <IPRStatements>
210 logger.info("start IPRStatements ...");
211 Element elIPRStatements = elDataset.getChild("IPRStatements",sddNamespace);
212 // <IPRStatement role="Copyright">
213 Rights copyright = null;
214 if (elIPRStatements != null) {
215 List<Element> listIPRStatements = elIPRStatements.getChildren("IPRStatement", sddNamespace);
216 j = 0;
217 //for each IPRStatement
218
219 for (Element elIPRStatement : listIPRStatements){
220
221 String role = elIPRStatement.getAttributeValue("role");
222 // <Label xml:lang="en-au">(c) 2003-2006 Centre for Occasional Botany.</Label>
223 Element elLabel = elIPRStatement.getChild("Label",sddNamespace);
224 String lang = "";
225 if (elLabel != null) {
226 lang = elLabel.getAttributeValue("lang",xmlNamespace);
227 }
228 label = (String)ImportHelper.getXmlInputValue(elIPRStatement, "Label",sddNamespace);
229
230 if (role.equals("Copyright")) {
231 Language iprLanguage = null;
232 if (lang != null) {
233 if (!lang.equals("")) {
234 //iprLanguage = termService.getLanguageByIso(nameLang);
235 iprLanguage = datasetLanguage;
236 } else {
237 iprLanguage = datasetLanguage;
238 }
239 }
240 if (iprLanguage == null) {
241 iprLanguage = datasetLanguage;
242 }
243 copyright = Rights.NewInstance(label, iprLanguage);
244 }
245
246 if (copyright != null) {
247 sourceReference.addRights(copyright);
248 sec.addRights(copyright);
249 }
250
251 if ((++j % modCount) == 0){ logger.info("IPRStatements handled: " + j);}
252
253 }
254 }
255
256 // <TaxonNames>
257 logger.info("start TaxonNames ...");
258 Element elTaxonNames = elDataset.getChild("TaxonNames",sddNamespace);
259 // <TaxonName id="t1" uri="urn:lsid:authority:namespace:my-own-id">
260 Map<String,NonViralName> taxonNameBases = new HashMap<String,NonViralName>();
261 if (elTaxonNames != null) {
262 List<Element> listTaxonNames = elTaxonNames.getChildren("TaxonName", sddNamespace);
263 j = 0;
264 //for each TaxonName
265 for (Element elTaxonName : listTaxonNames){
266
267 String id = elTaxonName.getAttributeValue("id");
268 String uri = elTaxonName.getAttributeValue("uri");
269 // <Representation>
270 elRepresentation = elTaxonName.getChild("Representation",sddNamespace);
271 // <Label xml:lang="la">Viola hederacea Labill.</Label>
272 Element elLabel = elRepresentation.getChild("Label",sddNamespace);
273 String lang = elLabel.getAttributeValue("lang",xmlNamespace);
274 label = (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace);
275 NonViralName tnb = NonViralName.NewInstance(null);
276 if ((lang != null) && (!lang.equals("la"))) {
277 logger.info("TaxonName " + j + " is not specified as a latin name.");
278 }
279 tnb.setTitleCache(label);
280 OriginalSource source = OriginalSource.NewInstance(id, "TaxonName");
281 tnb.addSource(source);
282 if (!id.equals("")) {
283 taxonNameBases.put(id,tnb);
284 }
285
286 if ((++j % modCount) == 0){ logger.info("TaxonNames handled: " + j);}
287
288 }
289 }
290
291 // <Characters>
292 logger.info("start Characters ...");
293 Element elCharacters = elDataset.getChild("Characters", sddNamespace);
294
295 // <CategoricalCharacter id="c1">
296 if (elCharacters != null) {
297 List<Element> elCategoricalCharacters = elCharacters.getChildren("CategoricalCharacter", sddNamespace);
298 j = 0;
299 //for each CategoricalCharacter
300 for (Element elCategoricalCharacter : elCategoricalCharacters){
301
302 try {
303
304 String idCC = elCategoricalCharacter.getAttributeValue("id");
305
306 // <Representation>
307 // <Label> Leaf complexity</Label>
308 // </Representation>
309 elRepresentation = elCategoricalCharacter.getChild("Representation",sddNamespace);
310 label = (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace);
311
312 Feature categoricalCharacter = null;
313 if (label != null){
314 categoricalCharacter = Feature.NewInstance(label, label, label);
315 }
316 categoricalCharacter.setSupportsQuantitativeData(false);
317 categoricalCharacter.setSupportsTextData(true);
318
319 // <States>
320 Element elStates = elCategoricalCharacter.getChild("States",sddNamespace);
321
322 // <StateDefinition id="s1">
323 List<Element> elStateDefinitions = elStates.getChildren("StateDefinition",sddNamespace);
324 TermVocabulary<State> termVocabularyState = new TermVocabulary<State>();
325
326 int k = 0;
327 //for each StateDefinition
328 for (Element elStateDefinition : elStateDefinitions){
329
330 if ((++k % modCount) == 0){ logger.info("StateDefinitions handled: " + (k-1));}
331
332 String idSD = elStateDefinition.getAttributeValue("id");
333 // <Representation>
334 // <Label>Simple</Label>
335 // </Representation>
336 elRepresentation = elStateDefinition.getChild("Representation",sddNamespace);
337 label = (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace);
338 State state = new State(label,label,label);
339 states.put(idSD, state);
340 termVocabularyState.addTerm(state);
341 }
342
343 categoricalCharacter.addSupportedCategoricalEnumeration(termVocabularyState);
344 features.put(idCC, categoricalCharacter);
345
346 } catch (Exception e) {
347 //FIXME
348 logger.warn("Import of CategoricalCharacter " + j + " failed.");
349 success = false;
350 }
351
352 if ((++j % modCount) == 0){ logger.info("CategoricalCharacters handled: " + j);}
353
354 }
355
356 // <QuantitativeCharacter id="c2">
357 List<Element> elQuantitativeCharacters = elCharacters.getChildren("QuantitativeCharacter", sddNamespace);
358 j = 0;
359 //for each QuantitativeCharacter
360 for (Element elQuantitativeCharacter : elQuantitativeCharacters){
361
362 try {
363
364 String idQC = elQuantitativeCharacter.getAttributeValue("id");
365
366 // <Representation>
367 // <Label>Leaf length</Label>
368 // </Representation>
369 elRepresentation = elQuantitativeCharacter.getChild("Representation",sddNamespace);
370 label = (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace);
371
372 Feature quantitativeCharacter = Feature.NewInstance();
373
374 if (!label.equals("")){
375 quantitativeCharacter = Feature.NewInstance(label, label, label);
376 }
377 quantitativeCharacter.setSupportsQuantitativeData(true);
378 quantitativeCharacter.setSupportsTextData(false);
379
380 // <MeasurementUnit>
381 // <Label role="Abbrev">m</Label>
382 // </MeasurementUnit>
383 Element elMeasurementUnit = elQuantitativeCharacter.getChild("MeasurementUnit",sddNamespace);
384 label = "";
385 String role = "";
386 if (elMeasurementUnit != null) {
387 Element elLabel = elMeasurementUnit.getChild("Label",sddNamespace);
388 role = elLabel.getAttributeValue("role");
389 label = (String)ImportHelper.getXmlInputValue(elMeasurementUnit, "Label",sddNamespace);
390 }
391
392 MeasurementUnit unit = null;
393 if (!label.equals("")){
394 if (role != null) {
395 if (role.equals("Abbrev")){
396 unit = MeasurementUnit.NewInstance(label,label,label);
397 }
398 } else {
399 unit = MeasurementUnit.NewInstance(label,label,label);
400 }
401 }
402
403 if (unit != null) {
404 units.put(idQC, unit);
405 }
406
407 //<Default>
408 // <MeasurementUnitPrefix>milli</MeasurementUnitPrefix>
409 //</Default>
410 Element elDefault = elQuantitativeCharacter.getChild("Default",sddNamespace);
411 if (elDefault != null) {
412 String measurementUnitPrefix = (String)ImportHelper.getXmlInputValue(elDefault, "MeasurementUnitPrefix",sddNamespace);
413 if (!measurementUnitPrefix.equals("")){
414 defaultUnitPrefixes.put(idQC, measurementUnitPrefix);
415 }
416 }
417
418 features.put(idQC, quantitativeCharacter);
419
420 } catch (Exception e) {
421 //FIXME
422 logger.warn("Import of QuantitativeCharacter " + j + " failed.");
423 success = false;
424 }
425
426 if ((++j % modCount) == 0){ logger.info("QuantitativeCharacters handled: " + j);}
427
428 }
429
430 // <TextCharacter id="c3">
431 List<Element> elTextCharacters = elCharacters.getChildren("TextCharacter", sddNamespace);
432 j = 0;
433 //for each TextCharacter
434 for (Element elTextCharacter : elTextCharacters){
435
436 try {
437
438 String idTC = elTextCharacter.getAttributeValue("id");
439
440 // <Representation>
441 // <Label xml:lang="en">Leaf features not covered by other characters</Label>
442 // </Representation>
443 elRepresentation = elTextCharacter.getChild("Representation",sddNamespace);
444 Element elLabel = elRepresentation.getChild("Label",sddNamespace);
445 nameLang = elLabel.getAttributeValue("lang",xmlNamespace);
446 Language language = null;
447 if (nameLang != null) {
448 if (!nameLang.equals("")) {
449 // language = termService.getLanguageByIso(nameLang);
450 language = datasetLanguage;
451 } else {
452 language = datasetLanguage;
453 }
454 } else {
455 language = datasetLanguage;
456 }
457
458 label = (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace);
459
460 Feature textCharacter = Feature.NewInstance();
461
462 if (label != null) {
463 if (!label.equals("")){
464 textCharacter = Feature.NewInstance(label, label, label);
465 textCharacter.setLabel(label, language);
466 }
467 }
468
469 textCharacter.setSupportsQuantitativeData(false);
470 textCharacter.setSupportsTextData(true);
471
472 features.put(idTC, textCharacter);
473
474 } catch (Exception e) {
475 //FIXME
476 logger.warn("Import of TextCharacter " + j + " failed.");
477 success = false;
478 }
479
480 if ((++j % modCount) == 0){ logger.info("TextCharacters handled: " + j);}
481
482 }
483
484 }
485
486 // <CodedDescriptions>
487 logger.info("start CodedDescriptions ...");
488 Element elCodedDescriptions = elDataset.getChild("CodedDescriptions",sddNamespace);
489
490 // <CodedDescription id="D101">
491
492 Map<String,String> citations = new HashMap<String,String>();
493 Map<String,String> locations = new HashMap<String,String>();
494
495 if (elCodedDescriptions != null) {
496 List<Element> listCodedDescriptions = elCodedDescriptions.getChildren("CodedDescription", sddNamespace);
497 j = 0;
498 //for each CodedDescription
499
500 for (Element elCodedDescription : listCodedDescriptions){
501
502 try {
503
504 String idCD = elCodedDescription.getAttributeValue("id");
505
506 // <Representation>
507 // <Label>&lt;i&gt;Viola hederacea&lt;/i&gt; Labill. as revised by R. Morris April 8, 2006</Label>
508 // </Representation>
509 elRepresentation = elCodedDescription.getChild("Representation",sddNamespace);
510 Element elLabel = elRepresentation.getChild("Label",sddNamespace);
511 label = (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace);
512
513 TaxonDescription taxonDescription = TaxonDescription.NewInstance();
514 taxonDescription.setTitleCache(label);
515 OriginalSource source = OriginalSource.NewInstance(idCD, "CodedDescription");
516 taxonDescription.addSource(source);
517
518 // <Scope>
519 // <TaxonName ref="t1"/>
520 // <Citation ref="p1" location="p. 30"/>
521 // </Scope>
522 Element elScope = elCodedDescription.getChild("Scope",sddNamespace);
523 String ref = "";
524 Taxon taxon = null;
525 if (elScope != null) {
526 Element elTaxonName = elScope.getChild("TaxonName",sddNamespace);
527 ref = elTaxonName.getAttributeValue("ref");
528
529 NonViralName taxonNameBase = taxonNameBases.get(ref);
530 taxon = Taxon.NewInstance(taxonNameBase, sec);
531 }
532
533 String refCitation = "";
534 String location = "";
535
536 if (elScope != null) {
537 Element elCitation = elScope.getChild("Citation",sddNamespace);
538 if (elCitation != null) {
539 refCitation = elCitation.getAttributeValue("ref");
540 location = elCitation.getAttributeValue("location");
541 }
542 }
543
544 // <SummaryData>
545 Element elSummaryData = elCodedDescription.getChild("SummaryData",sddNamespace);
546
547 if (elSummaryData != null) {
548
549 // <Categorical ref="c4">
550 List<Element> elCategoricals = elSummaryData.getChildren("Categorical", sddNamespace);
551 int k = 0;
552 //for each Categorical
553 for (Element elCategorical : elCategoricals){
554 if ((++k % modCount) == 0){ logger.info("Categorical handled: " + (k-1));}
555 ref = elCategorical.getAttributeValue("ref");
556 Feature feature = features.get(ref);
557 CategoricalData categoricalData = CategoricalData.NewInstance();
558 categoricalData.setFeature(feature);
559
560 // <State ref="s3"/>
561 List<Element> elStates = elCategorical.getChildren("State", sddNamespace);
562 int l = 0;
563 //for each State
564 for (Element elState : elStates){
565 if ((++l % modCount) == 0){ logger.info("States handled: " + (l-1));}
566 ref = elState.getAttributeValue("ref");
567 State state = states.get(ref);
568 categoricalData.addState(state);
569 }
570 taxonDescription.addElement(categoricalData);
571 }
572
573 // <Quantitative ref="c2">
574 List<Element> elQuantitatives = elSummaryData.getChildren("Quantitative", sddNamespace);
575 k = 0;
576 //for each Quantitative
577 for (Element elQuantitative : elQuantitatives){
578 if ((++k % modCount) == 0){ logger.info("Quantitative handled: " + (k-1));}
579 ref = elQuantitative.getAttributeValue("ref");
580 Feature feature = features.get(ref);
581 QuantitativeData quantitativeData = QuantitativeData.NewInstance();
582 quantitativeData.setFeature(feature);
583
584 MeasurementUnit unit = units.get(ref);
585 String prefix = defaultUnitPrefixes.get(ref);
586 if (unit != null) {
587 String u = unit.getLabel();
588 if (prefix != null) {
589 u = prefix + u;
590 }
591 unit.setLabel(u);
592 quantitativeData.setUnit(unit);
593 }
594
595 // <Measure type="Min" value="2.3"/>
596 List<Element> elMeasures = elQuantitative.getChildren("Measure", sddNamespace);
597 int l = 0;
598 //for each State
599 for (Element elMeasure : elMeasures){
600 if ((++l % modCount) == 0){ logger.info("States handled: " + (l-1));}
601 String type = elMeasure.getAttributeValue("type");
602 value = elMeasure.getAttributeValue("value");
603 float v = Float.parseFloat(value);
604 StatisticalMeasure t = null;
605 if (type.equals("Min")) {
606 t = StatisticalMeasure.MIN();
607 } else if (type.equals("Mean")) {
608 t = StatisticalMeasure.AVERAGE();
609 } else if (type.equals("Max")) {
610 t = StatisticalMeasure.MAX();
611 } else if (type.equals("SD")) {
612 // Create a new StatisticalMeasure for standard deviation
613 t = StatisticalMeasure.STANDARD_DEVIATION();
614 } else if (type.equals("N")) {
615 t = StatisticalMeasure.SAMPLE_SIZE();
616 } else {
617 t = StatisticalMeasure.NewInstance(type,type,type);
618 statisticalMeasures.add(t);
619 }
620
621 StatisticalMeasurementValue statisticalValue = StatisticalMeasurementValue.NewInstance();
622 statisticalValue.setValue(v);
623 statisticalValue.setType(t);
624 quantitativeData.addStatisticalValue(statisticalValue);
625 featureData.add(statisticalValue);
626 }
627 taxonDescription.addElement(quantitativeData);
628 }
629
630 // <TextChar ref="c3">
631 List<Element> elTextChars = elSummaryData.getChildren("TextChar", sddNamespace);
632 k = 0;
633 //for each TextChar
634 for (Element elTextChar : elTextChars){
635 if ((++k % modCount) == 0){ logger.info("TextChar handled: " + (k-1));}
636 ref = elTextChar.getAttributeValue("ref");
637 Feature feature = features.get(ref);
638 TextData textData = TextData.NewInstance();
639 textData.setFeature(feature);
640
641 // <Content>Free form text</Content>
642 String content = (String)ImportHelper.getXmlInputValue(elTextChar, "Content",sddNamespace);
643 textData.putText(content, datasetLanguage);
644 taxonDescription.addElement(textData);
645 }
646
647 }
648
649 if (taxon != null) {
650 taxon.addDescription(taxonDescription);
651 }
652
653 if (!refCitation.equals("")){
654 citations.put(idCD,refCitation);
655 }
656
657 if (!location.equals("")){
658 locations.put(idCD, location);
659 }
660
661 taxonDescriptions.put(idCD, taxonDescription);
662
663 } catch (Exception e) {
664 //FIXME
665 logger.warn("Import of CodedDescription " + j + " failed.");
666 success = false;
667 }
668
669 if ((++j % modCount) == 0){ logger.info("CodedDescriptions handled: " + j);}
670
671 }
672
673 }
674
675 // <Agents>
676 logger.info("start Agents ...");
677 Element elAgents = elDataset.getChild("Agents",sddNamespace);
678
679 // <Agent id="a1">
680 listAgents = elAgents.getChildren("Agent", sddNamespace);
681 j = 0;
682 //for each Agent
683 for (Element elAgent : listAgents){
684
685 try {
686
687 String idA = elAgent.getAttributeValue("id");
688
689 // <Representation>
690 // <Label>Kevin Thiele</Label>
691 // <Detail role="Description">Ali Baba is also known as r.a.m.</Detail>
692 // </Representation>
693 elRepresentation = elAgent.getChild("Representation",sddNamespace);
694 label = (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace);
695 Element elDetail = elRepresentation.getChild("Detail",sddNamespace);
696
697 Person person = Person.NewTitledInstance(label);
698 OriginalSource source = OriginalSource.NewInstance(idA, "Agent");
699 person.addSource(source);
700
701 if (elDetail != null) {
702 String role = elDetail.getAttributeValue("role");
703 detail = (String)ImportHelper.getXmlInputValue(elRepresentation, "Detail",sddNamespace);
704 annotation = Annotation.NewInstance(role + " - " + detail, datasetLanguage);
705 person.addAnnotation(annotation);
706 }
707
708 // <Links>
709 Element elLinks = elAgent.getChild("Links",sddNamespace);
710
711 if (elLinks != null) {
712
713 // <Link rel="Alternate" href="http://www.diversitycampus.net/people/hagedorn"/>
714 List<Element> listLinks = elLinks.getChildren("Link", sddNamespace);
715 int k = 0;
716 //for each Link
717 for (Element elLink : listLinks){
718
719 try {
720
721 String rel = elLink.getAttributeValue("rel");
722 String href = elLink.getAttributeValue("href");
723
724 if (k==1) {
725 source = OriginalSource.NewInstance(rel, href);
726 person.addSource(source);
727 }
728
729 } catch (Exception e) {
730 //FIXME
731 logger.warn("Import of Link " + k + " failed.");
732 success = false;
733 }
734
735 if ((++k % modCount) == 0){ logger.info("Links handled: " + k);}
736
737 }
738 }
739 if (authors.containsKey(idA)) {
740 authors.put(idA,person);
741 }
742
743 if (editors.containsKey(idA)) {
744 editors.put(idA, person);
745 }
746
747 } catch (Exception e) {
748 //FIXME
749 logger.warn("Import of Agent " + j + " failed.");
750 success = false;
751 }
752
753 if ((++j % modCount) == 0){ logger.info("Agents handled: " + j);}
754
755 }
756
757 // <Publications>
758 logger.info("start Publications ...");
759 Element elPublications = elDataset.getChild("Publications",sddNamespace);
760
761 if (elPublications != null) {
762 // <Publication id="p1">
763 List<Element> listPublications = elPublications.getChildren("Publication", sddNamespace);
764 j = 0;
765 //for each Publication
766 for (Element elPublication : listPublications){
767
768 try {
769
770 String idP = elPublication.getAttributeValue("id");
771
772 // <Representation>
773 // <Label>Sample Citation</Label>
774 // </Representation>
775 elRepresentation = elPublication.getChild("Representation",sddNamespace);
776 label = (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace);
777 Article publication = Article.NewInstance();
778 publication.setTitle(label);
779 OriginalSource source = OriginalSource.NewInstance(idP, "Publication");
780 publication.addSource(source);
781
782 publications.put(idP,publication);
783
784 } catch (Exception e) {
785 //FIXME
786 logger.warn("Import of Publication " + j + " failed.");
787 success = false;
788 }
789
790 if ((++j % modCount) == 0){ logger.info("Publications handled: " + j);}
791
792 }
793 }
794
795 // <MediaObjects>
796 logger.info("start MediaObjects ...");
797 Element elMediaObjects = elDataset.getChild("MediaObjects",sddNamespace);
798
799 if (elMediaObjects != null) {
800 // <MediaObject id="m1">
801 List<Element> listMediaObjects = elMediaObjects.getChildren("elMediaObject", sddNamespace);
802 j = 0;
803 //for each Publication
804 for (Element elMediaObject : listMediaObjects){
805
806 try {
807
808 String idMO = elMediaObject.getAttributeValue("id");
809
810 // <Representation>
811 // <Label>Image description, e.g. to be used for alt-attribute in html.</Label>
812 // </Representation>
813 elRepresentation = elMediaObject.getChild("Representation",sddNamespace);
814 label = (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace);
815 Media mediaObject = Media.NewInstance();
816 MultilanguageText m = MultilanguageText.NewInstance(LanguageString.NewInstance(label, datasetLanguage));
817 mediaObject.setTitle(m);
818 OriginalSource source = OriginalSource.NewInstance(idMO, "MediaObject");
819 // mediaObject.addSource(source);
820
821 mediaObjects.put(idMO,mediaObject);
822
823 } catch (Exception e) {
824 //FIXME
825 logger.warn("Import of MediaObject " + j + " failed.");
826 success = false;
827 }
828
829 if ((++j % modCount) == 0){ logger.info("Publications handled: " + j);}
830
831 }
832 }
833
834 /*
835 // when several authors are supported
836 if (authors.size()>1) {
837 for (Iterator<Person> author = authors.values().iterator() ; author.hasNext() ;){
838 sourceReference.setCreatedBy(author.next());
839 }
840 } else {
841 Team team = Team.NewInstance();
842 for (Iterator<Person> author = authors.values().iterator() ; author.hasNext() ;){
843 team.addTeamMember(author.next());
844 }
845 sourceReference.setCreatedBy(team);
846 }
847 */
848
849 Iterator<Person> author = authors.values().iterator();
850 if (author.hasNext()){
851 Person a = author.next();
852 sourceReference.setCreatedBy(a);
853 sec.setCreatedBy(a);
854 }
855
856 Iterator<Person> editor = editors.values().iterator();
857 if (editor.hasNext()){
858 Person e = editor.next();
859 sourceReference.setUpdatedBy(e);
860 sec.setUpdatedBy(e);
861 }
862
863 if (copyright != null) {
864 sourceReference.addRights(copyright);
865 sec.addRights(copyright);
866 }
867
868 for (Iterator<String> refCD = taxonDescriptions.keySet().iterator() ; refCD.hasNext() ;){
869 String ref = refCD.next();
870 TaxonDescription td = taxonDescriptions.get(ref);
871 if (citations.containsKey(ref)) {
872 Article publication = (Article) publications.get(citations.get(ref));
873 if (locations.containsKey(ref)) {
874 publication.addAnnotation(Annotation.NewInstance(locations.get(ref), datasetLanguage));
875 }
876 td.addDescriptionSource(publication);
877 }
878 }
879
880 if ((++i % modCount) == 0){ logger.info("Datasets handled: " + i);}
881
882 config.setSourceReference(sourceReference);
883
884 }
885 logger.info(i + " Datasets handled");
886
887 TransactionStatus ts = config.getCdmAppController().startTransaction();
888
889 ITermService termService = config.getCdmAppController().getTermService();
890 for (Iterator<State> k = states.values().iterator() ; k.hasNext() ;){
891 State state = k.next();
892 termService.saveTerm(state);
893 }
894 for (Iterator<Feature> k = features.values().iterator() ; k.hasNext() ;){
895 Feature feature = k.next();
896 termService.saveTerm(feature);
897 }
898 if (units != null) {
899 for (Iterator<MeasurementUnit> k = units.values().iterator() ; k.hasNext() ;){
900 MeasurementUnit unit = k.next();
901 if (unit != null) {
902 termService.saveTerm(unit);
903 }
904 }
905 }
906 for (Iterator<StatisticalMeasure> k = statisticalMeasures.iterator() ; k.hasNext() ;) {
907 StatisticalMeasure sm = k.next();
908 termService.saveTerm(sm);
909 }
910
911 IReferenceService referenceService = config.getCdmAppController().getReferenceService();
912
913 for (Iterator<ReferenceBase> k = publications.values().iterator() ; k.hasNext() ;){
914 Article publication = (Article) k.next();
915 referenceService.saveReference(publication);
916 }
917
918 // Returns a CdmApplicationController created by the values of this configuration.
919 IDescriptionService descriptionService = config.getCdmAppController().getDescriptionService();
920
921 for (Iterator<TaxonDescription> k = taxonDescriptions.values().iterator() ; k.hasNext() ;){
922 TaxonDescription taxonDescription = k.next();
923 // Persists a Description
924 descriptionService.saveDescription(taxonDescription);
925 }
926
927 config.getCdmAppController().commitTransaction(ts);
928
929 // makeNameSpecificData(nameMap);
930 logger.info("end makeTaxonDescriptions ...");
931 return success;
932
933 }
934 /* (non-Javadoc)
935 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
936 */
937 protected boolean isIgnore(IImportConfigurator config){
938 return false;
939 }
940
941 }