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