1 package eu
.etaxonomy
.cdm
.io
.sdd
;
4 import java
.net
.MalformedURLException
;
6 import java
.text
.SimpleDateFormat
;
7 import java
.util
.ArrayList
;
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
;
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
;
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
;
67 public class SDDDescriptionIO
extends SDDIoBase
implements ICdmIO
{
68 private static final Logger logger
= Logger
.getLogger(SDDDescriptionIO
.class);
70 private static int modCount
= 1000;
72 private Map
<String
,List
<CdmBase
>> mediaObject_ListCdmBase
= new HashMap
<String
,List
<CdmBase
>>();
73 private Map
<String
,String
> mediaObject_Role
= new HashMap
<String
,String
>();
75 private ReferenceBase sec
= Database
.NewInstance();
77 private Language datasetLanguage
= null;
79 private Namespace xmlNamespace
= Namespace
.getNamespace("xml","http://www.w3.org/XML/1998/namespace");
81 public SDDDescriptionIO(){
86 public boolean doCheck(IImportConfigurator config
){
87 boolean result
= true;
88 logger
.warn("No check implemented for SDD");
93 public boolean doInvoke(IImportConfigurator config
, Map
<String
, MapWrapper
<?
extends CdmBase
>> stores
){
96 logger
.info("start Datasets ...");
97 SDDImportConfigurator sddConfig
= (SDDImportConfigurator
)config
;
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;
103 Namespace sddNamespace
= sddConfig
.getSddNamespace();
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();
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
);
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");
130 List
<Element
> elDatasets
= root
.getChildren("Dataset",sddNamespace
);
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
>();
139 Set
<StatisticalMeasure
> statisticalMeasures
= new HashSet
<StatisticalMeasure
>();
140 Set
<VersionableEntity
> featureData
= new HashSet
<VersionableEntity
>();
142 TransactionStatus ts
= config
.getCdmAppController().startTransaction();
146 logger
.info("start Dataset ...");
147 for (Element elDataset
: elDatasets
){
149 // <Dataset xml:lang="en-us">
150 String nameLang
= elDataset
.getAttributeValue("lang",xmlNamespace
);
152 if (!nameLang
.equals("")) {
153 String iso
= nameLang
.substring(0, 2);
154 datasetLanguage
= config
.getCdmAppController().getTermService().getLanguageByIso(iso
);
155 //datasetLanguage = Language.ENGLISH();
157 datasetLanguage
= Language
.ENGLISH();
159 if (datasetLanguage
== null) {
160 datasetLanguage
= Language
.ENGLISH();
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>
169 importDatasetRepresentation(elDataset
, sddNamespace
, sourceReference
, generatorName
, generatorVersion
);
172 logger
.info("start RevisionData ...");
173 Element elRevisionData
= elDataset
.getChild("RevisionData",sddNamespace
);
176 Element elCreators
= elRevisionData
.getChild("Creators",sddNamespace
);
178 // <Agent role="aut" ref="a1"/>
179 List
<Element
> listAgents
= elCreators
.getChildren("Agent", sddNamespace
);
183 Map
<String
,Person
> authors
= new HashMap
<String
,Person
>();
184 Map
<String
,Person
> editors
= new HashMap
<String
,Person
>();
185 for (Element elAgent
: listAgents
){
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);
194 if (role
.equals("edt")) {
195 if(!ref
.equals("")) {
196 editors
.put(ref
, null);
200 if ((++j
% modCount
) == 0){ logger
.info("Agents handled: " + j
);}
204 // <DateModified>2006-04-08T00:00:00</DateModified>
205 String stringDateModified
= (String
)ImportHelper
.getXmlInputValue(elRevisionData
, "DateModified",sddNamespace
);
207 if (stringDateModified
!= null) {
208 SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");
211 d
= sdf
.parse(stringDateModified
);
212 } catch(Exception e
) {
213 System
.err
.println("Exception :");
217 GregorianCalendar updated
= null;
219 updated
= new java
.util
.GregorianCalendar();
221 sourceReference
.setUpdated(updated
);
222 sec
.setUpdated(updated
);
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
);
234 //for each IPRStatement
236 for (Element elIPRStatement
: listIPRStatements
){
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
);
242 if (elLabel
!= null) {
243 lang
= elLabel
.getAttributeValue("lang",xmlNamespace
);
245 String label
= (String
)ImportHelper
.getXmlInputValue(elIPRStatement
, "Label",sddNamespace
);
247 if (role
.equals("Copyright")) {
248 Language iprLanguage
= null;
250 if (!lang
.equals("")) {
251 iprLanguage
= config
.getCdmAppController().getTermService().getLanguageByIso(lang
.substring(0, 2));
252 //iprLanguage = datasetLanguage;
254 iprLanguage
= datasetLanguage
;
257 if (iprLanguage
== null) {
258 iprLanguage
= datasetLanguage
;
260 copyright
= Rights
.NewInstance(label
, iprLanguage
);
263 if (copyright
!= null) {
264 sourceReference
.addRights(copyright
);
265 sec
.addRights(copyright
);
268 if ((++j
% modCount
) == 0){ logger
.info("IPRStatements handled: " + j
);}
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
);
282 for (Element elTaxonName
: listTaxonNames
){
284 String id
= elTaxonName
.getAttributeValue("id");
285 String uri
= elTaxonName
.getAttributeValue("uri");
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
);
296 // <Label xml:lang="la">Viola hederacea Labill.</Label>
297 importRepresentation(elTaxonName
, sddNamespace
, tnb
, id
, config
);
299 if ((++j
% modCount
) == 0){ logger
.info("TaxonNames handled: " + j
);}
305 logger
.info("start Characters ...");
306 Element elCharacters
= elDataset
.getChild("Characters", sddNamespace
);
308 // <CategoricalCharacter id="c1">
309 if (elCharacters
!= null) {
310 List
<Element
> elCategoricalCharacters
= elCharacters
.getChildren("CategoricalCharacter", sddNamespace
);
312 //for each CategoricalCharacter
313 for (Element elCategoricalCharacter
: elCategoricalCharacters
){
317 String idCC
= elCategoricalCharacter
.getAttributeValue("id");
320 // <Label> Leaf complexity</Label>
323 Feature categoricalCharacter
= Feature
.NewInstance();
324 importRepresentation(elCategoricalCharacter
, sddNamespace
, categoricalCharacter
, idCC
, config
);
326 categoricalCharacter
.setSupportsQuantitativeData(false);
327 categoricalCharacter
.setSupportsTextData(true);
330 Element elStates
= elCategoricalCharacter
.getChild("States",sddNamespace
);
332 // <StateDefinition id="s1">
333 List
<Element
> elStateDefinitions
= elStates
.getChildren("StateDefinition",sddNamespace
);
334 TermVocabulary
<State
> termVocabularyState
= new TermVocabulary
<State
>();
337 //for each StateDefinition
338 for (Element elStateDefinition
: elStateDefinitions
){
340 if ((++k
% modCount
) == 0){ logger
.info("StateDefinitions handled: " + (k
-1));}
342 String idSD
= elStateDefinition
.getAttributeValue("id");
344 // <Label>Simple</Label>
345 // <MediaObject ref="ib" role="Primary"/>
347 State state
= State
.NewInstance();
348 importRepresentation(elStateDefinition
, sddNamespace
, state
, idSD
, config
);
350 StateData stateData
= StateData
.NewInstance();
351 stateData
.setState(state
);
352 termVocabularyState
.addTerm(state
);
353 stateDatas
.put(idSD
,stateData
);
356 categoricalCharacter
.addSupportedCategoricalEnumeration(termVocabularyState
);
357 features
.put(idCC
, categoricalCharacter
);
359 } catch (Exception e
) {
361 logger
.warn("Import of CategoricalCharacter " + j
+ " failed.");
365 if ((++j
% modCount
) == 0){ logger
.info("CategoricalCharacters handled: " + j
);}
369 // <QuantitativeCharacter id="c2">
370 List
<Element
> elQuantitativeCharacters
= elCharacters
.getChildren("QuantitativeCharacter", sddNamespace
);
372 //for each QuantitativeCharacter
373 for (Element elQuantitativeCharacter
: elQuantitativeCharacters
){
377 String idQC
= elQuantitativeCharacter
.getAttributeValue("id");
380 // <Label>Leaf length</Label>
382 Feature quantitativeCharacter
= Feature
.NewInstance();
383 importRepresentation(elQuantitativeCharacter
, sddNamespace
, quantitativeCharacter
, idQC
, config
);
385 quantitativeCharacter
.setSupportsQuantitativeData(true);
386 quantitativeCharacter
.setSupportsTextData(false);
389 // <Label role="Abbrev">m</Label>
390 // </MeasurementUnit>
391 Element elMeasurementUnit
= elQuantitativeCharacter
.getChild("MeasurementUnit",sddNamespace
);
394 if (elMeasurementUnit
!= null) {
395 Element elLabel
= elMeasurementUnit
.getChild("Label",sddNamespace
);
396 role
= elLabel
.getAttributeValue("role");
397 label
= (String
)ImportHelper
.getXmlInputValue(elMeasurementUnit
, "Label",sddNamespace
);
400 MeasurementUnit unit
= null;
401 if (!label
.equals("")){
403 if (role
.equals("Abbrev")){
404 unit
= MeasurementUnit
.NewInstance(label
,label
,label
);
407 unit
= MeasurementUnit
.NewInstance(label
,label
,label
);
412 units
.put(idQC
, unit
);
416 // <MeasurementUnitPrefix>milli</MeasurementUnitPrefix>
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
);
426 features
.put(idQC
, quantitativeCharacter
);
428 } catch (Exception e
) {
430 logger
.warn("Import of QuantitativeCharacter " + j
+ " failed.");
434 if ((++j
% modCount
) == 0){ logger
.info("QuantitativeCharacters handled: " + j
);}
438 // <TextCharacter id="c3">
439 List
<Element
> elTextCharacters
= elCharacters
.getChildren("TextCharacter", sddNamespace
);
441 //for each TextCharacter
442 for (Element elTextCharacter
: elTextCharacters
){
446 String idTC
= elTextCharacter
.getAttributeValue("id");
449 // <Label xml:lang="en">Leaf features not covered by other characters</Label>
451 Feature textCharacter
= Feature
.NewInstance();
452 importRepresentation(elTextCharacter
, sddNamespace
, textCharacter
, idTC
, config
);
454 textCharacter
.setSupportsQuantitativeData(false);
455 textCharacter
.setSupportsTextData(true);
457 features
.put(idTC
, textCharacter
);
459 } catch (Exception e
) {
461 logger
.warn("Import of TextCharacter " + j
+ " failed.");
465 if ((++j
% modCount
) == 0){ logger
.info("TextCharacters handled: " + j
);}
471 // <CodedDescriptions>
472 logger
.info("start CodedDescriptions ...");
473 Element elCodedDescriptions
= elDataset
.getChild("CodedDescriptions",sddNamespace
);
475 // <CodedDescription id="D101">
477 Map
<String
,String
> citations
= new HashMap
<String
,String
>();
478 Map
<String
,String
> locations
= new HashMap
<String
,String
>();
480 if (elCodedDescriptions
!= null) {
481 List
<Element
> listCodedDescriptions
= elCodedDescriptions
.getChildren("CodedDescription", sddNamespace
);
483 //for each CodedDescription
485 for (Element elCodedDescription
: listCodedDescriptions
){
489 String idCD
= elCodedDescription
.getAttributeValue("id");
492 // <Label><i>Viola hederacea</i> Labill. as revised by R. Morris April 8, 2006</Label>
494 TaxonDescription taxonDescription
= TaxonDescription
.NewInstance();
495 importRepresentation(elCodedDescription
, sddNamespace
, taxonDescription
, idCD
, sddConfig
);
498 // <TaxonName ref="t1"/>
499 // <Citation ref="p1" location="p. 30"/>
501 Element elScope
= elCodedDescription
.getChild("Scope",sddNamespace
);
504 if (elScope
!= null) {
505 Element elTaxonName
= elScope
.getChild("TaxonName",sddNamespace
);
506 ref
= elTaxonName
.getAttributeValue("ref");
508 NonViralName taxonNameBase
= taxonNameBases
.get(ref
);
509 taxon
= Taxon
.NewInstance(taxonNameBase
, sec
);
512 String refCitation
= "";
513 String location
= "";
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");
524 Element elSummaryData
= elCodedDescription
.getChild("SummaryData",sddNamespace
);
526 if (elSummaryData
!= null) {
528 // <Categorical ref="c4">
529 List
<Element
> elCategoricals
= elSummaryData
.getChildren("Categorical", sddNamespace
);
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
);
540 List
<Element
> elStates
= elCategorical
.getChildren("State", sddNamespace
);
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
);
549 taxonDescription
.addElement(categoricalData
);
552 // <Quantitative ref="c2">
553 List
<Element
> elQuantitatives
= elSummaryData
.getChildren("Quantitative", sddNamespace
);
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
);
563 MeasurementUnit unit
= units
.get(ref
);
564 String prefix
= defaultUnitPrefixes
.get(ref
);
566 String u
= unit
.getLabel();
567 if (prefix
!= null) {
571 quantitativeData
.setUnit(unit
);
574 // <Measure type="Min" value="2.3"/>
575 List
<Element
> elMeasures
= elQuantitative
.getChildren("Measure", sddNamespace
);
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();
596 t
= StatisticalMeasure
.NewInstance(type
,type
,type
);
597 statisticalMeasures
.add(t
);
600 StatisticalMeasurementValue statisticalValue
= StatisticalMeasurementValue
.NewInstance();
601 statisticalValue
.setValue(v
);
602 statisticalValue
.setType(t
);
603 quantitativeData
.addStatisticalValue(statisticalValue
);
604 featureData
.add(statisticalValue
);
606 taxonDescription
.addElement(quantitativeData
);
609 // <TextChar ref="c3">
610 List
<Element
> elTextChars
= elSummaryData
.getChildren("TextChar", sddNamespace
);
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
);
620 // <Content>Free form text</Content>
621 String content
= (String
)ImportHelper
.getXmlInputValue(elTextChar
, "Content",sddNamespace
);
622 textData
.putText(content
, datasetLanguage
);
623 taxonDescription
.addElement(textData
);
629 taxon
.addDescription(taxonDescription
);
632 if (!refCitation
.equals("")){
633 citations
.put(idCD
,refCitation
);
636 if (!location
.equals("")){
637 locations
.put(idCD
, location
);
640 taxonDescriptions
.put(idCD
, taxonDescription
);
642 } catch (Exception e
) {
644 logger
.warn("Import of CodedDescription " + j
+ " failed.");
648 if ((++j
% modCount
) == 0){ logger
.info("CodedDescriptions handled: " + j
);}
655 logger
.info("start Agents ...");
656 Element elAgents
= elDataset
.getChild("Agents",sddNamespace
);
659 listAgents
= elAgents
.getChildren("Agent", sddNamespace
);
662 for (Element elAgent
: listAgents
){
666 String idA
= elAgent
.getAttributeValue("id");
669 // <Label>Kevin Thiele</Label>
670 // <Detail role="Description">Ali Baba is also known as r.a.m.</Detail>
672 Element elRepresentation
= elAgent
.getChild("Representation",sddNamespace
);
673 String label
= (String
)ImportHelper
.getXmlInputValue(elRepresentation
, "Label",sddNamespace
);
674 Element elDetail
= elRepresentation
.getChild("Detail",sddNamespace
);
676 Person person
= Person
.NewTitledInstance(label
);
677 OriginalSource source
= OriginalSource
.NewInstance(idA
, "Agent");
678 person
.addSource(source
);
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
);
688 Element elLinks
= elAgent
.getChild("Links",sddNamespace
);
690 if (elLinks
!= null) {
692 // <Link rel="Alternate" href="http://www.diversitycampus.net/people/hagedorn"/>
693 List
<Element
> listLinks
= elLinks
.getChildren("Link", sddNamespace
);
696 for (Element elLink
: listLinks
){
700 String rel
= elLink
.getAttributeValue("rel");
701 String href
= elLink
.getAttributeValue("href");
704 source
= OriginalSource
.NewInstance(rel
, href
);
705 person
.addSource(source
);
708 } catch (Exception e
) {
710 logger
.warn("Import of Link " + k
+ " failed.");
714 if ((++k
% modCount
) == 0){ logger
.info("Links handled: " + k
);}
718 if (authors
.containsKey(idA
)) {
719 authors
.put(idA
,person
);
722 if (editors
.containsKey(idA
)) {
723 editors
.put(idA
, person
);
726 List
<Element
> listMediaObjects
= elRepresentation
.getChildren("MediaObject",sddNamespace
);
728 for (Element elMediaObject
: listMediaObjects
) {
731 if (elMediaObject
!= null) {
732 ref
= elMediaObject
.getAttributeValue("ref");
733 role
= elMediaObject
.getAttributeValue("role");
736 if (!ref
.equals("")) {
737 this.associateImageWithCdmBase(ref
,person
);
742 } catch (Exception e
) {
744 logger
.warn("Import of Agent " + j
+ " failed.");
748 if ((++j
% modCount
) == 0){ logger
.info("Agents handled: " + j
);}
753 logger
.info("start Publications ...");
754 Element elPublications
= elDataset
.getChild("Publications",sddNamespace
);
756 if (elPublications
!= null) {
757 // <Publication id="p1">
758 List
<Element
> listPublications
= elPublications
.getChildren("Publication", sddNamespace
);
760 //for each Publication
761 for (Element elPublication
: listPublications
){
765 String idP
= elPublication
.getAttributeValue("id");
768 // <Label>Sample Citation</Label>
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
);
780 publications
.put(idP
,publication
);
782 List
<Element
> listMediaObjects
= elRepresentation
.getChildren("MediaObject",sddNamespace
);
784 for (Element elMediaObject
: listMediaObjects
) {
787 if (elMediaObject
!= null) {
788 ref
= elMediaObject
.getAttributeValue("ref");
789 role
= elMediaObject
.getAttributeValue("role");
792 if (!ref
.equals("")) {
793 this.associateImageWithCdmBase(ref
,publication
);
798 } catch (Exception e
) {
800 logger
.warn("Import of Publication " + j
+ " failed.");
804 if ((++j
% modCount
) == 0){ logger
.info("Publications handled: " + j
);}
810 logger
.info("start MediaObjects ...");
811 Element elMediaObjects
= elDataset
.getChild("MediaObjects",sddNamespace
);
813 if (elMediaObjects
!= null) {
814 // <MediaObject id="m1">
815 List
<Element
> listMediaObjects
= elMediaObjects
.getChildren("MediaObject", sddNamespace
);
817 //for each Publication
818 for (Element elMO
: listMediaObjects
){
822 String idMO
= elMO
.getAttributeValue("id");
825 // <Label>Image description, e.g. to be used for alt-attribute in html.</Label>
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
);
833 // mediaObjects.get(idMO).setTitle(m);
834 OriginalSource originalSource
= OriginalSource
.NewInstance(idMO
, "MediaObject");
835 // NO MEDIA SOURCE AVAILABLE
836 // media.addSource();
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");
844 ImageMetaData imageMetaData
= new ImageMetaData();
845 ImageFile image
= null;
847 if (href
.substring(0,7).equals("http://")) {
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
);
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
);
865 MediaRepresentation representation
= MediaRepresentation
.NewInstance(imageMetaData
.getMimeType(), null);
866 representation
.addRepresentationPart(image
);
868 Media media
= Media
.NewInstance();
869 media
.addRepresentation(representation
);
870 media
.addAnnotation(annotation
);
871 // media.setTitle(m);
873 ArrayList
<CdmBase
> lcb
= (ArrayList
<CdmBase
>) mediaObject_ListCdmBase
.get(idMO
);
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);
884 Media me
= (Media
) media
.clone();
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);
897 Media me
= (Media
) media
.clone();
904 } catch (Exception e
) {
906 logger
.warn("Import of MediaObject " + j
+ " failed.");
910 if ((++j
% modCount
) == 0){ logger
.info("MediaObjects handled: " + j
);}
915 if (authors
!= null) {
916 Team team
= Team
.NewInstance();
917 for (Iterator
<Person
> author
= authors
.values().iterator() ; author
.hasNext() ;){
918 team
.addTeamMember(author
.next());
920 sourceReference
.setAuthorTeam(team
);
923 if (editors
!= null) {
924 Person ed
= Person
.NewInstance();
925 for (Iterator
<Person
> editor
= editors
.values().iterator() ; editor
.hasNext() ;){
928 sourceReference
.setUpdatedBy(ed
);
931 if (copyright
!= null) {
932 sourceReference
.addRights(copyright
);
933 sec
.addRights(copyright
);
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
));
944 td
.addDescriptionSource(publication
);
948 if ((++i
% modCount
) == 0){ logger
.info("Datasets handled: " + i
);}
950 config
.setSourceReference(sourceReference
);
953 logger
.info(i
+ " Datasets handled");
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());
960 for (Iterator
<Feature
> k
= features
.values().iterator() ; k
.hasNext() ;){
961 Feature feature
= k
.next();
962 termService
.saveTerm(feature
);
965 for (Iterator
<MeasurementUnit
> k
= units
.values().iterator() ; k
.hasNext() ;){
966 MeasurementUnit unit
= k
.next();
968 termService
.saveTerm(unit
);
972 for (Iterator
<StatisticalMeasure
> k
= statisticalMeasures
.iterator() ; k
.hasNext() ;) {
973 StatisticalMeasure sm
= k
.next();
974 termService
.saveTerm(sm
);
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
);
984 // Returns a CdmApplicationController created by the values of this configuration.
985 IDescriptionService descriptionService
= config
.getCdmAppController().getDescriptionService();
987 for (Iterator
<TaxonDescription
> k
= taxonDescriptions
.values().iterator() ; k
.hasNext() ;){
988 TaxonDescription taxonDescription
= k
.next();
989 // Persists a Description
990 descriptionService
.saveDescription(taxonDescription
);
993 config
.getCdmAppController().commitTransaction(ts
);
995 // makeNameSpecificData(nameMap);
996 logger
.info("end makeTaxonDescriptions ...");
1001 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
1003 protected boolean isIgnore(IImportConfigurator config
){
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
>();
1015 mediaObject_ListCdmBase
.put(refMO
,lcb
);
1017 List
<CdmBase
> lcb
= mediaObject_ListCdmBase
.get(refMO
);
1019 mediaObject_ListCdmBase
.put(refMO
,lcb
);
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
);
1032 sourceReference
.setTitleCache(generatorName
+ " - " + generatorVersion
+ " - " + label
);
1033 sec
.setTitleCache(generatorName
+ " - " + generatorVersion
+ " - " + label
);
1035 if (detail
!= null) {
1036 Annotation annotation
= Annotation
.NewInstance(detail
, datasetLanguage
);
1037 sec
.addAnnotation(annotation
);
1040 List
<Element
> listMediaObjects
= elRepresentation
.getChildren("MediaObject",sddNamespace
);
1042 for (Element elMediaObject
: listMediaObjects
) {
1045 if (elMediaObject
!= null) {
1046 ref
= elMediaObject
.getAttributeValue("ref");
1047 role
= elMediaObject
.getAttributeValue("role");
1050 if (!ref
.equals("")) {
1051 this.associateImageWithCdmBase(ref
, sourceReference
);
1052 this.associateImageWithCdmBase(ref
,sec
);
1053 mediaObject_Role
.put(ref
,role
);
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;
1067 if (!lang
.equals("")) {
1068 language
= config
.getCdmAppController().getTermService().getLanguageByIso(lang
.substring(0, 2));
1070 language
= datasetLanguage
;
1073 language
= datasetLanguage
;
1075 String label
= (String
)ImportHelper
.getXmlInputValue(elRepresentation
, "Label",sddNamespace
);
1076 String detail
= (String
)ImportHelper
.getXmlInputValue(elRepresentation
, "Detail",sddNamespace
);
1078 if ((lang
!= null) && (!lang
.equals("la")) && (ve
instanceof NonViralName
)) {
1079 logger
.info("TaxonName " + label
+ " is not specified as a latin name.");
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
);
1090 } else if (ve
instanceof TermBase
) {
1091 TermBase tb
= (TermBase
) ve
;
1093 if (detail
!= null) {
1094 tb
.addRepresentation(Representation
.NewInstance(detail
, label
, label
, language
));
1096 tb
.addRepresentation(Representation
.NewInstance(label
, label
, label
, language
));
1102 List
<Element
> listMediaObjects
= elRepresentation
.getChildren("MediaObject",sddNamespace
);
1104 for (Element elMediaObject
: listMediaObjects
) {
1107 if (elMediaObject
!= null) {
1108 ref
= elMediaObject
.getAttributeValue("ref");
1109 role
= elMediaObject
.getAttributeValue("role");
1112 if (!ref
.equals("")) {
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]);
1120 ReferenceBase descriptionSource
= Generic
.NewInstance();
1121 td
.addDescriptionSource(descriptionSource
);
1122 this.associateImageWithCdmBase(ref
,descriptionSource
);
1125 this.associateImageWithCdmBase(ref
,ve
);