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