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
.stereotype
.Component
;
22 import org
.springframework
.transaction
.TransactionStatus
;
24 import eu
.etaxonomy
.cdm
.api
.service
.IDescriptionService
;
25 import eu
.etaxonomy
.cdm
.api
.service
.IReferenceService
;
26 import eu
.etaxonomy
.cdm
.api
.service
.ITermService
;
27 import eu
.etaxonomy
.cdm
.common
.MediaMetaData
.ImageMetaData
;
28 import eu
.etaxonomy
.cdm
.io
.common
.ICdmIO
;
29 import eu
.etaxonomy
.cdm
.io
.common
.IImportConfigurator
;
30 import eu
.etaxonomy
.cdm
.io
.common
.ImportHelper
;
31 import eu
.etaxonomy
.cdm
.io
.common
.MapWrapper
;
32 import eu
.etaxonomy
.cdm
.model
.agent
.Person
;
33 import eu
.etaxonomy
.cdm
.model
.agent
.Team
;
34 import eu
.etaxonomy
.cdm
.model
.common
.Annotation
;
35 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
36 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTermBase
;
37 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableEntity
;
38 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
39 import eu
.etaxonomy
.cdm
.model
.common
.LanguageString
;
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
.MediaRepresentationPart
;
59 import eu
.etaxonomy
.cdm
.model
.media
.Rights
;
60 import eu
.etaxonomy
.cdm
.model
.name
.NonViralName
;
61 import eu
.etaxonomy
.cdm
.model
.reference
.Article
;
62 import eu
.etaxonomy
.cdm
.model
.reference
.Database
;
63 import eu
.etaxonomy
.cdm
.model
.reference
.Generic
;
64 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceBase
;
65 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
68 public class SDDDescriptionIO
extends SDDIoBase
implements ICdmIO
<IImportConfigurator
> {
69 private static final Logger logger
= Logger
.getLogger(SDDDescriptionIO
.class);
71 private static int modCount
= 1000;
73 private Map
<String
,List
<CdmBase
>> mediaObject_ListCdmBase
= new HashMap
<String
,List
<CdmBase
>>();
74 private Map
<String
,String
> mediaObject_Role
= new HashMap
<String
,String
>();
75 private Map
<String
,TaxonDescription
> taxonDescriptions
= new HashMap
<String
,TaxonDescription
>();
76 private Map
<String
,StateData
> stateDatas
= new HashMap
<String
,StateData
>();
77 private Map
<String
,MeasurementUnit
> units
= new HashMap
<String
,MeasurementUnit
>();
78 private Map
<String
,String
> defaultUnitPrefixes
= new HashMap
<String
,String
>();
79 private Map
<String
,Feature
> features
= new HashMap
<String
,Feature
>();
80 private Map
<String
,ReferenceBase
> publications
= new HashMap
<String
,ReferenceBase
>();
81 private Map
<String
,Person
> authors
= new HashMap
<String
,Person
>();
82 private Map
<String
,Person
> editors
= new HashMap
<String
,Person
>();
83 private Map
<String
,NonViralName
> taxonNameBases
= new HashMap
<String
,NonViralName
>();
84 private Map
<String
,String
> citations
= new HashMap
<String
,String
>();
85 private Map
<String
,String
> locations
= new HashMap
<String
,String
>();
87 private ReferenceBase sec
= Database
.NewInstance();
88 private ReferenceBase sourceReference
= null;
90 private Language datasetLanguage
= null;
92 private Namespace xmlNamespace
= Namespace
.getNamespace("xml","http://www.w3.org/XML/1998/namespace");
94 private String generatorName
= "";
95 private String generatorVersion
= "";
97 private Set
<StatisticalMeasure
> statisticalMeasures
= new HashSet
<StatisticalMeasure
>();
98 private Set
<VersionableEntity
> featureData
= new HashSet
<VersionableEntity
>();
100 private Rights copyright
= null;
102 public SDDDescriptionIO(){
107 public boolean doCheck(IImportConfigurator config
){
108 boolean result
= true;
109 logger
.warn("No check implemented for SDD");
114 public boolean doInvoke(IImportConfigurator config
, Map
<String
, MapWrapper
<?
extends CdmBase
>> stores
){
116 TransactionStatus ts
= startTransaction();
117 SDDImportConfigurator sddConfig
= (SDDImportConfigurator
)config
;
119 logger
.info("start Datasets ...");
120 // <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">
121 Element root
= sddConfig
.getSourceRoot();
122 boolean success
= true;
123 Namespace sddNamespace
= sddConfig
.getSddNamespace();
125 logger
.info("start TechnicalMetadata ...");
126 // <TechnicalMetadata created="2006-04-20T10:00:00">
127 importTechnicalMetadata(root
, sddNamespace
, sddConfig
);
128 List
<Element
> elDatasets
= root
.getChildren("Dataset",sddNamespace
);
132 logger
.info("start Dataset ...");
133 for (Element elDataset
: elDatasets
){
134 importDataset(elDataset
, sddNamespace
, success
, sddConfig
);
135 if ((++i
% modCount
) == 0){ logger
.info("Datasets handled: " + i
);}
136 logger
.info(i
+ " Datasets handled");
138 commitTransaction(ts
);
143 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
145 protected boolean isIgnore(IImportConfigurator config
){
150 // associates the reference of a media object in SDD with a CdmBase Object
151 protected void associateImageWithCdmBase(String refMO
, CdmBase cb
){
152 if ((refMO
!= null) && (cb
!=null)) {
153 if (!refMO
.equals("")) {
154 if (!mediaObject_ListCdmBase
.containsKey(refMO
)) {
155 List
<CdmBase
> lcb
= new ArrayList
<CdmBase
>();
157 mediaObject_ListCdmBase
.put(refMO
,lcb
);
159 List
<CdmBase
> lcb
= mediaObject_ListCdmBase
.get(refMO
);
161 mediaObject_ListCdmBase
.put(refMO
,lcb
);
167 // imports information about the Dataset
168 protected void importDatasetRepresentation(Element parent
, Namespace sddNamespace
){
169 logger
.info("start Representation ...");
171 <Label>The Genus Viola</Label>
172 <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>
175 Element elRepresentation
= parent
.getChild("Representation",sddNamespace
);
176 String label
= (String
)ImportHelper
.getXmlInputValue(elRepresentation
, "Label",sddNamespace
);
177 String detail
= (String
)ImportHelper
.getXmlInputValue(elRepresentation
, "Detail",sddNamespace
);
179 sec
.setTitleCache(label
);
181 if (detail
!= null) {
182 Annotation annotation
= Annotation
.NewInstance(detail
, datasetLanguage
);
183 sec
.addAnnotation(annotation
);
186 List
<Element
> listMediaObjects
= elRepresentation
.getChildren("MediaObject",sddNamespace
);
188 for (Element elMediaObject
: listMediaObjects
) {
191 if (elMediaObject
!= null) {
192 ref
= elMediaObject
.getAttributeValue("ref");
193 role
= elMediaObject
.getAttributeValue("role");
196 if (!ref
.equals("")) {
197 this.associateImageWithCdmBase(ref
,sourceReference
);
198 this.associateImageWithCdmBase(ref
,sec
);
199 mediaObject_Role
.put(ref
,role
);
205 // imports the representation (label, detail, lang) of a particular SDD element
206 protected void importRepresentation(Element parent
, Namespace sddNamespace
, VersionableEntity ve
, String id
, IImportConfigurator config
){
207 Element elRepresentation
= parent
.getChild("Representation",sddNamespace
);
208 // <Label xml:lang="la">Viola hederacea Labill.</Label>
209 Element elLabel
= elRepresentation
.getChild("Label",sddNamespace
);
210 Element elDetail
= elRepresentation
.getChild("Detail",sddNamespace
);
211 String lang
= elLabel
.getAttributeValue("lang",xmlNamespace
);
212 Language language
= null;
214 if (!lang
.equals("")) {
215 language
= getTermService().getLanguageByIso(lang
.substring(0, 2));
217 language
= datasetLanguage
;
220 language
= datasetLanguage
;
222 String label
= (String
)ImportHelper
.getXmlInputValue(elRepresentation
, "Label",sddNamespace
);
223 String detail
= (String
)ImportHelper
.getXmlInputValue(elRepresentation
, "Detail",sddNamespace
);
225 if ((lang
!= null) && (!lang
.equals("la")) && (ve
instanceof NonViralName
)) {
226 logger
.info("TaxonName " + label
+ " is not specified as a latin name.");
229 if (ve
instanceof IdentifiableEntity
) {
230 IdentifiableEntity ie
= (IdentifiableEntity
) ve
;
231 ie
.setTitleCache(label
);
233 if (elDetail
!= null) {
234 String role
= elDetail
.getAttributeValue("role");
235 Annotation annotation
= null;
236 if (detail
!= null) {
238 annotation
= Annotation
.NewInstance(role
+ " - " + detail
, language
);
240 annotation
= Annotation
.NewInstance(detail
, language
);
243 ie
.addAnnotation(annotation
);
248 } else if (ve
instanceof TermBase
) {
249 TermBase tb
= (TermBase
) ve
;
252 if (detail
!= null) {
253 tb
.addRepresentation(Representation
.NewInstance(detail
, label
, label
, language
));
255 tb
.addRepresentation(Representation
.NewInstance(label
, label
, label
, language
));
261 List
<Element
> listMediaObjects
= elRepresentation
.getChildren("MediaObject",sddNamespace
);
263 for (Element elMediaObject
: listMediaObjects
) {
266 if (elMediaObject
!= null) {
267 ref
= elMediaObject
.getAttributeValue("ref");
268 role
= elMediaObject
.getAttributeValue("role");
271 if (!ref
.equals("")) {
273 if (ve
instanceof TaxonDescription
) {
274 TaxonDescription td
= (TaxonDescription
) ve
;
275 //TODO: ensure that all images are imported
276 if (td
.getDescriptionSources().toArray().length
> 0) {
277 this.associateImageWithCdmBase(ref
,(ReferenceBase
) td
.getDescriptionSources().toArray()[0]);
279 ReferenceBase descriptionSource
= Generic
.NewInstance();
280 td
.addDescriptionSource(descriptionSource
);
281 this.associateImageWithCdmBase(ref
,descriptionSource
);
284 this.associateImageWithCdmBase(ref
,ve
);
294 // imports the representation (label, detail, lang) of a particular SDD element
295 protected void importTechnicalMetadata(Element root
, Namespace sddNamespace
, SDDImportConfigurator sddConfig
){
296 Element elTechnicalMetadata
= root
.getChild("TechnicalMetadata", sddNamespace
);
297 String nameCreated
= elTechnicalMetadata
.getAttributeValue("created");
298 sourceReference
= sddConfig
.getSourceReference();
300 if (nameCreated
!= null) {
301 if (!nameCreated
.equals("")) {
302 int year
= Integer
.parseInt(nameCreated
.substring(0,4));
303 int monthOfYear
= Integer
.parseInt(nameCreated
.substring(5,7));
304 int dayOfMonth
= Integer
.parseInt(nameCreated
.substring(8,10));
305 int hourOfDay
= Integer
.parseInt(nameCreated
.substring(11,13));
306 int minuteOfHour
= Integer
.parseInt(nameCreated
.substring(14,16));
307 int secondOfMinute
= Integer
.parseInt(nameCreated
.substring(17,19));
308 DateTime created
= new DateTime(year
,monthOfYear
,dayOfMonth
,hourOfDay
,minuteOfHour
,secondOfMinute
,0);
309 sourceReference
.setCreated(created
);
310 sec
.setCreated(created
);
314 // <Generator name="n/a, handcrafted instance document" version="n/a"/>
315 Element elGenerator
= elTechnicalMetadata
.getChild("Generator", sddNamespace
);
316 generatorName
= elGenerator
.getAttributeValue("name");
317 generatorVersion
= elGenerator
.getAttributeValue("version");
319 sec
.addAnnotation(Annotation
.NewDefaultLanguageInstance(generatorName
+ " - " + generatorVersion
));
320 sourceReference
.addAnnotation(Annotation
.NewDefaultLanguageInstance(generatorName
+ " - " + generatorVersion
));
324 // imports the complete dataset information
325 protected void importDataset(Element elDataset
, Namespace sddNamespace
, boolean success
, SDDImportConfigurator sddConfig
){ // <Dataset xml:lang="en-us">
327 importDatasetLanguage(elDataset
,sddConfig
);
328 importDatasetRepresentation(elDataset
, sddNamespace
);
329 importRevisionData(elDataset
, sddNamespace
);
330 importIPRStatements(elDataset
, sddNamespace
, sddConfig
);
331 importTaxonNames(elDataset
, sddNamespace
, sddConfig
);
332 importCharacters(elDataset
, sddNamespace
, sddConfig
, success
);
333 importCodedDescriptions(elDataset
, sddNamespace
, sddConfig
, success
);
334 importAgents(elDataset
, sddNamespace
, sddConfig
, success
);
335 importPublications(elDataset
, sddNamespace
, sddConfig
, success
);
336 importMediaObjects(elDataset
, sddNamespace
, sddConfig
, success
);
338 if (authors
!= null) {
339 Team team
= Team
.NewInstance();
340 for (Iterator
<Person
> author
= authors
.values().iterator() ; author
.hasNext() ;){
341 team
.addTeamMember(author
.next());
343 sec
.setAuthorTeam(team
);
344 sourceReference
.setAuthorTeam(team
);
347 if (editors
!= null) {
348 Person ed
= Person
.NewInstance();
349 for (Iterator
<Person
> editor
= editors
.values().iterator() ; editor
.hasNext() ;){
352 sec
.setUpdatedBy(ed
);
353 sourceReference
.setUpdatedBy(ed
);
356 if (copyright
!= null) {
357 sourceReference
.addRights(copyright
);
358 sec
.addRights(copyright
);
361 for (Iterator
<String
> refCD
= taxonDescriptions
.keySet().iterator() ; refCD
.hasNext() ;){
362 String ref
= refCD
.next();
363 TaxonDescription td
= taxonDescriptions
.get(ref
);
364 td
.addDescriptionSource(sec
);
365 if (citations
.containsKey(ref
)) {
366 Article publication
= (Article
) publications
.get(citations
.get(ref
));
367 if (locations
.containsKey(ref
)) {
368 publication
.addAnnotation(Annotation
.NewInstance(locations
.get(ref
), datasetLanguage
));
370 td
.addDescriptionSource(publication
);
373 logger
.info("end makeTaxonDescriptions ...");
375 sddConfig
.setSourceReference(sourceReference
);
377 //saving of all imported data into the CDM db
378 ITermService termService
= getTermService();
379 for (Iterator
<StateData
> k
= stateDatas
.values().iterator() ; k
.hasNext() ;){
380 StateData sd
= k
.next();
381 termService
.saveTerm(sd
.getState());
383 for (Iterator
<Feature
> k
= features
.values().iterator() ; k
.hasNext() ;){
384 Feature feature
= k
.next();
385 termService
.saveTerm(feature
);
388 for (Iterator
<MeasurementUnit
> k
= units
.values().iterator() ; k
.hasNext() ;){
389 MeasurementUnit unit
= k
.next();
391 termService
.saveTerm(unit
);
395 for (Iterator
<StatisticalMeasure
> k
= statisticalMeasures
.iterator() ; k
.hasNext() ;) {
396 StatisticalMeasure sm
= k
.next();
397 termService
.saveTerm(sm
);
400 IReferenceService referenceService
= getReferenceService();
401 // referenceService.saveReference(sourceReference);
402 for (Iterator
<ReferenceBase
> k
= publications
.values().iterator() ; k
.hasNext() ;){
403 Article publication
= (Article
) k
.next();
404 referenceService
.saveReference(publication
);
407 // Returns a CdmApplicationController created by the values of this configuration.
408 IDescriptionService descriptionService
= getDescriptionService();
410 for (Iterator
<TaxonDescription
> k
= taxonDescriptions
.values().iterator() ; k
.hasNext() ;){
411 TaxonDescription taxonDescription
= k
.next();
412 // Persists a Description
413 descriptionService
.saveDescription(taxonDescription
);
417 // imports the default language of the dataset
418 protected void importDatasetLanguage(Element elDataset
, SDDImportConfigurator sddConfig
){
419 String nameLang
= elDataset
.getAttributeValue("lang",xmlNamespace
);
421 if (!nameLang
.equals("")) {
422 String iso
= nameLang
.substring(0, 2);
423 datasetLanguage
= getTermService().getLanguageByIso(iso
);
425 datasetLanguage
= Language
.ENGLISH();
427 if (datasetLanguage
== null) {
428 datasetLanguage
= Language
.ENGLISH();
432 // imports the revision data associated with the Dataset (authors, modifications)
433 protected void importRevisionData(Element elDataset
, Namespace sddNamespace
){
435 logger
.info("start RevisionData ...");
436 Element elRevisionData
= elDataset
.getChild("RevisionData",sddNamespace
);
439 Element elCreators
= elRevisionData
.getChild("Creators",sddNamespace
);
441 // <Agent role="aut" ref="a1"/>
442 List
<Element
> listAgents
= elCreators
.getChildren("Agent", sddNamespace
);
446 for (Element elAgent
: listAgents
){
448 String role
= elAgent
.getAttributeValue("role");
449 String ref
= elAgent
.getAttributeValue("ref");
450 if (role
.equals("aut")) {
451 if(!ref
.equals("")) {
452 authors
.put(ref
, null);
455 if (role
.equals("edt")) {
456 if(!ref
.equals("")) {
457 editors
.put(ref
, null);
461 if ((++j
% modCount
) == 0){ logger
.info("Agents handled: " + j
);}
465 // <DateModified>2006-04-08T00:00:00</DateModified>
466 String stringDateModified
= (String
)ImportHelper
.getXmlInputValue(elRevisionData
, "DateModified",sddNamespace
);
468 if (stringDateModified
!= null) {
469 SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");
472 d
= sdf
.parse(stringDateModified
);
473 } catch(Exception e
) {
474 System
.err
.println("Exception :");
478 GregorianCalendar updated
= null;
480 updated
= new java
.util
.GregorianCalendar();
482 sourceReference
.setUpdated(updated
);
483 sec
.setUpdated(updated
);
488 // imports ipr statements associated with a dataset
489 protected void importIPRStatements(Element elDataset
, Namespace sddNamespace
, SDDImportConfigurator sddConfig
){
491 logger
.info("start IPRStatements ...");
492 Element elIPRStatements
= elDataset
.getChild("IPRStatements",sddNamespace
);
493 // <IPRStatement role="Copyright">
494 if (elIPRStatements
!= null) {
495 List
<Element
> listIPRStatements
= elIPRStatements
.getChildren("IPRStatement", sddNamespace
);
497 //for each IPRStatement
499 for (Element elIPRStatement
: listIPRStatements
){
501 String role
= elIPRStatement
.getAttributeValue("role");
502 // <Label xml:lang="en-au">(c) 2003-2006 Centre for Occasional Botany.</Label>
503 Element elLabel
= elIPRStatement
.getChild("Label",sddNamespace
);
505 if (elLabel
!= null) {
506 lang
= elLabel
.getAttributeValue("lang",xmlNamespace
);
508 String label
= (String
)ImportHelper
.getXmlInputValue(elIPRStatement
, "Label",sddNamespace
);
510 if (role
.equals("Copyright")) {
511 Language iprLanguage
= null;
513 if (!lang
.equals("")) {
514 iprLanguage
= getTermService().getLanguageByIso(lang
.substring(0, 2));
515 //iprLanguage = datasetLanguage;
517 iprLanguage
= datasetLanguage
;
520 if (iprLanguage
== null) {
521 iprLanguage
= datasetLanguage
;
523 copyright
= Rights
.NewInstance(label
, iprLanguage
);
526 if (copyright
!= null) {
527 sourceReference
.addRights(copyright
);
528 sec
.addRights(copyright
);
531 if ((++j
% modCount
) == 0){ logger
.info("IPRStatements handled: " + j
);}
537 // imports the taxon names
538 protected void importTaxonNames(Element elDataset
, Namespace sddNamespace
, SDDImportConfigurator sddConfig
){
540 logger
.info("start TaxonNames ...");
541 Element elTaxonNames
= elDataset
.getChild("TaxonNames",sddNamespace
);
542 // <TaxonName id="t1" uri="urn:lsid:authority:namespace:my-own-id">
543 if (elTaxonNames
!= null) {
544 List
<Element
> listTaxonNames
= elTaxonNames
.getChildren("TaxonName", sddNamespace
);
547 for (Element elTaxonName
: listTaxonNames
){
549 String id
= elTaxonName
.getAttributeValue("id");
550 String uri
= elTaxonName
.getAttributeValue("uri");
552 NonViralName tnb
= null;
553 if (!id
.equals("")) {
554 tnb
= NonViralName
.NewInstance(null);
555 OriginalSource source
= null;
557 if (!uri
.equals("")) {
558 source
= OriginalSource
.NewInstance(id
, "TaxonName", Generic
.NewInstance(), uri
);
561 source
= OriginalSource
.NewInstance(id
, "TaxonName");
563 tnb
.addSource(source
);
564 taxonNameBases
.put(id
,tnb
);
568 // <Label xml:lang="la">Viola hederacea Labill.</Label>
569 importRepresentation(elTaxonName
, sddNamespace
, tnb
, id
, sddConfig
);
571 if ((++j
% modCount
) == 0){ logger
.info("TaxonNames handled: " + j
);}
577 // imports the representation (label, detail, lang) of a particular SDD element
578 protected void importCharacters(Element elDataset
, Namespace sddNamespace
, SDDImportConfigurator sddConfig
, boolean success
){
580 logger
.info("start Characters ...");
581 Element elCharacters
= elDataset
.getChild("Characters", sddNamespace
);
583 // <CategoricalCharacter id="c1">
584 if (elCharacters
!= null) {
585 List
<Element
> elCategoricalCharacters
= elCharacters
.getChildren("CategoricalCharacter", sddNamespace
);
587 //for each CategoricalCharacter
588 for (Element elCategoricalCharacter
: elCategoricalCharacters
){
592 String idCC
= elCategoricalCharacter
.getAttributeValue("id");
595 // <Label> Leaf complexity</Label>
598 Feature categoricalCharacter
= Feature
.NewInstance();
599 importRepresentation(elCategoricalCharacter
, sddNamespace
, categoricalCharacter
, idCC
, sddConfig
);
601 categoricalCharacter
.setSupportsQuantitativeData(false);
602 categoricalCharacter
.setSupportsTextData(true);
605 Element elStates
= elCategoricalCharacter
.getChild("States",sddNamespace
);
607 // <StateDefinition id="s1">
608 List
<Element
> elStateDefinitions
= elStates
.getChildren("StateDefinition",sddNamespace
);
609 TermVocabulary
<State
> termVocabularyState
= new TermVocabulary
<State
>();
612 //for each StateDefinition
613 for (Element elStateDefinition
: elStateDefinitions
){
615 if ((++k
% modCount
) == 0){ logger
.info("StateDefinitions handled: " + (k
-1));}
617 String idSD
= elStateDefinition
.getAttributeValue("id");
619 // <Label>Simple</Label>
620 // <MediaObject ref="ib" role="Primary"/>
622 State state
= State
.NewInstance();
623 importRepresentation(elStateDefinition
, sddNamespace
, state
, idSD
, sddConfig
);
625 StateData stateData
= StateData
.NewInstance();
626 stateData
.setState(state
);
627 termVocabularyState
.addTerm(state
);
628 stateDatas
.put(idSD
,stateData
);
631 categoricalCharacter
.addSupportedCategoricalEnumeration(termVocabularyState
);
632 features
.put(idCC
, categoricalCharacter
);
634 } catch (Exception e
) {
636 logger
.warn("Import of CategoricalCharacter " + j
+ " failed.");
640 if ((++j
% modCount
) == 0){ logger
.info("CategoricalCharacters handled: " + j
);}
644 // <QuantitativeCharacter id="c2">
645 List
<Element
> elQuantitativeCharacters
= elCharacters
.getChildren("QuantitativeCharacter", sddNamespace
);
647 //for each QuantitativeCharacter
648 for (Element elQuantitativeCharacter
: elQuantitativeCharacters
){
652 String idQC
= elQuantitativeCharacter
.getAttributeValue("id");
655 // <Label>Leaf length</Label>
657 Feature quantitativeCharacter
= Feature
.NewInstance();
658 importRepresentation(elQuantitativeCharacter
, sddNamespace
, quantitativeCharacter
, idQC
, sddConfig
);
660 quantitativeCharacter
.setSupportsQuantitativeData(true);
661 quantitativeCharacter
.setSupportsTextData(false);
664 // <Label role="Abbrev">m</Label>
665 // </MeasurementUnit>
666 Element elMeasurementUnit
= elQuantitativeCharacter
.getChild("MeasurementUnit",sddNamespace
);
669 if (elMeasurementUnit
!= null) {
670 Element elLabel
= elMeasurementUnit
.getChild("Label",sddNamespace
);
671 role
= elLabel
.getAttributeValue("role");
672 label
= (String
)ImportHelper
.getXmlInputValue(elMeasurementUnit
, "Label",sddNamespace
);
675 MeasurementUnit unit
= null;
676 if (!label
.equals("")){
678 if (role
.equals("Abbrev")){
679 unit
= MeasurementUnit
.NewInstance(label
,label
,label
);
682 unit
= MeasurementUnit
.NewInstance(label
,label
,label
);
687 units
.put(idQC
, unit
);
691 // <MeasurementUnitPrefix>milli</MeasurementUnitPrefix>
693 Element elDefault
= elQuantitativeCharacter
.getChild("Default",sddNamespace
);
694 if (elDefault
!= null) {
695 String measurementUnitPrefix
= (String
)ImportHelper
.getXmlInputValue(elDefault
, "MeasurementUnitPrefix",sddNamespace
);
696 if (!measurementUnitPrefix
.equals("")){
697 defaultUnitPrefixes
.put(idQC
, measurementUnitPrefix
);
701 features
.put(idQC
, quantitativeCharacter
);
703 } catch (Exception e
) {
705 logger
.warn("Import of QuantitativeCharacter " + j
+ " failed.");
709 if ((++j
% modCount
) == 0){ logger
.info("QuantitativeCharacters handled: " + j
);}
713 // <TextCharacter id="c3">
714 List
<Element
> elTextCharacters
= elCharacters
.getChildren("TextCharacter", sddNamespace
);
716 //for each TextCharacter
717 for (Element elTextCharacter
: elTextCharacters
){
721 String idTC
= elTextCharacter
.getAttributeValue("id");
724 // <Label xml:lang="en">Leaf features not covered by other characters</Label>
726 Feature textCharacter
= Feature
.NewInstance();
727 importRepresentation(elTextCharacter
, sddNamespace
, textCharacter
, idTC
, sddConfig
);
729 textCharacter
.setSupportsQuantitativeData(false);
730 textCharacter
.setSupportsTextData(true);
732 features
.put(idTC
, textCharacter
);
734 } catch (Exception e
) {
736 logger
.warn("Import of TextCharacter " + j
+ " failed.");
740 if ((++j
% modCount
) == 0){ logger
.info("TextCharacters handled: " + j
);}
747 // imports the descriptions of taxa (specimens TODO)
748 protected void importCodedDescriptions(Element elDataset
, Namespace sddNamespace
, SDDImportConfigurator sddConfig
, boolean success
){
749 // <CodedDescriptions>
750 logger
.info("start CodedDescriptions ...");
751 Element elCodedDescriptions
= elDataset
.getChild("CodedDescriptions",sddNamespace
);
753 // <CodedDescription id="D101">
755 if (elCodedDescriptions
!= null) {
756 List
<Element
> listCodedDescriptions
= elCodedDescriptions
.getChildren("CodedDescription", sddNamespace
);
758 //for each CodedDescription
760 for (Element elCodedDescription
: listCodedDescriptions
){
764 String idCD
= elCodedDescription
.getAttributeValue("id");
767 // <Label><i>Viola hederacea</i> Labill. as revised by R. Morris April 8, 2006</Label>
769 TaxonDescription taxonDescription
= TaxonDescription
.NewInstance();
770 importRepresentation(elCodedDescription
, sddNamespace
, taxonDescription
, idCD
, sddConfig
);
773 // <TaxonName ref="t1"/>
774 // <Citation ref="p1" location="p. 30"/>
776 Element elScope
= elCodedDescription
.getChild("Scope",sddNamespace
);
779 if (elScope
!= null) {
780 Element elTaxonName
= elScope
.getChild("TaxonName",sddNamespace
);
781 ref
= elTaxonName
.getAttributeValue("ref");
783 NonViralName taxonNameBase
= taxonNameBases
.get(ref
);
784 taxon
= Taxon
.NewInstance(taxonNameBase
, sec
);
787 String refCitation
= "";
788 String location
= "";
790 if (elScope
!= null) {
791 Element elCitation
= elScope
.getChild("Citation",sddNamespace
);
792 if (elCitation
!= null) {
793 refCitation
= elCitation
.getAttributeValue("ref");
794 location
= elCitation
.getAttributeValue("location");
799 Element elSummaryData
= elCodedDescription
.getChild("SummaryData",sddNamespace
);
801 if (elSummaryData
!= null) {
803 // <Categorical ref="c4">
804 List
<Element
> elCategoricals
= elSummaryData
.getChildren("Categorical", sddNamespace
);
806 //for each Categorical
807 for (Element elCategorical
: elCategoricals
){
808 if ((++k
% modCount
) == 0){ logger
.info("Categorical handled: " + (k
-1));}
809 ref
= elCategorical
.getAttributeValue("ref");
810 Feature feature
= features
.get(ref
);
811 CategoricalData categoricalData
= CategoricalData
.NewInstance();
812 categoricalData
.setFeature(feature
);
815 List
<Element
> elStates
= elCategorical
.getChildren("State", sddNamespace
);
818 for (Element elState
: elStates
){
819 if ((++l
% modCount
) == 0){ logger
.info("States handled: " + (l
-1));}
820 ref
= elState
.getAttributeValue("ref");
821 StateData stateData
= stateDatas
.get(ref
);
822 categoricalData
.addState(stateData
);
824 taxonDescription
.addElement(categoricalData
);
827 // <Quantitative ref="c2">
828 List
<Element
> elQuantitatives
= elSummaryData
.getChildren("Quantitative", sddNamespace
);
830 //for each Quantitative
831 for (Element elQuantitative
: elQuantitatives
){
832 if ((++k
% modCount
) == 0){ logger
.info("Quantitative handled: " + (k
-1));}
833 ref
= elQuantitative
.getAttributeValue("ref");
834 Feature feature
= features
.get(ref
);
835 QuantitativeData quantitativeData
= QuantitativeData
.NewInstance();
836 quantitativeData
.setFeature(feature
);
838 MeasurementUnit unit
= units
.get(ref
);
839 String prefix
= defaultUnitPrefixes
.get(ref
);
841 String u
= unit
.getLabel();
842 if (prefix
!= null) {
846 quantitativeData
.setUnit(unit
);
849 // <Measure type="Min" value="2.3"/>
850 List
<Element
> elMeasures
= elQuantitative
.getChildren("Measure", sddNamespace
);
853 for (Element elMeasure
: elMeasures
){
854 if ((++l
% modCount
) == 0){ logger
.info("States handled: " + (l
-1));}
855 String type
= elMeasure
.getAttributeValue("type");
856 String value
= elMeasure
.getAttributeValue("value");
857 float v
= Float
.parseFloat(value
);
858 StatisticalMeasure t
= null;
859 if (type
.equals("Min")) {
860 t
= StatisticalMeasure
.MIN();
861 } else if (type
.equals("Mean")) {
862 t
= StatisticalMeasure
.AVERAGE();
863 } else if (type
.equals("Max")) {
864 t
= StatisticalMeasure
.MAX();
865 } else if (type
.equals("SD")) {
866 // Create a new StatisticalMeasure for standard deviation
867 t
= StatisticalMeasure
.STANDARD_DEVIATION();
868 } else if (type
.equals("N")) {
869 t
= StatisticalMeasure
.SAMPLE_SIZE();
871 t
= StatisticalMeasure
.NewInstance(type
,type
,type
);
872 statisticalMeasures
.add(t
);
875 StatisticalMeasurementValue statisticalValue
= StatisticalMeasurementValue
.NewInstance();
876 statisticalValue
.setValue(v
);
877 statisticalValue
.setType(t
);
878 quantitativeData
.addStatisticalValue(statisticalValue
);
879 featureData
.add(statisticalValue
);
881 taxonDescription
.addElement(quantitativeData
);
884 // <TextChar ref="c3">
885 List
<Element
> elTextChars
= elSummaryData
.getChildren("TextChar", sddNamespace
);
888 for (Element elTextChar
: elTextChars
){
889 if ((++k
% modCount
) == 0){ logger
.info("TextChar handled: " + (k
-1));}
890 ref
= elTextChar
.getAttributeValue("ref");
891 Feature feature
= features
.get(ref
);
892 TextData textData
= TextData
.NewInstance();
893 textData
.setFeature(feature
);
895 // <Content>Free form text</Content>
896 String content
= (String
)ImportHelper
.getXmlInputValue(elTextChar
, "Content",sddNamespace
);
897 textData
.putText(content
, datasetLanguage
);
898 taxonDescription
.addElement(textData
);
904 taxon
.addDescription(taxonDescription
);
907 if (!refCitation
.equals("")){
908 citations
.put(idCD
,refCitation
);
911 if (!location
.equals("")){
912 locations
.put(idCD
, location
);
915 taxonDescriptions
.put(idCD
, taxonDescription
);
917 } catch (Exception e
) {
919 logger
.warn("Import of CodedDescription " + j
+ " failed.");
923 if ((++j
% modCount
) == 0){ logger
.info("CodedDescriptions handled: " + j
);}
930 // imports the persons associated with the dataset creation, modification, related publications
931 protected void importAgents(Element elDataset
, Namespace sddNamespace
, SDDImportConfigurator sddConfig
, boolean success
){
933 logger
.info("start Agents ...");
934 Element elAgents
= elDataset
.getChild("Agents",sddNamespace
);
937 List
<Element
> listAgents
= elAgents
.getChildren("Agent", sddNamespace
);
940 for (Element elAgent
: listAgents
){
944 String idA
= elAgent
.getAttributeValue("id");
947 // <Label>Kevin Thiele</Label>
948 // <Detail role="Description">Ali Baba is also known as r.a.m.</Detail>
950 Person person
= Person
.NewInstance();
951 importRepresentation(elAgent
, sddNamespace
, person
, idA
, sddConfig
);
952 person
.addSource(OriginalSource
.NewInstance(idA
, "Agent"));
955 Element elLinks
= elAgent
.getChild("Links",sddNamespace
);
957 if (elLinks
!= null) {
959 // <Link rel="Alternate" href="http://www.diversitycampus.net/people/hagedorn"/>
960 List
<Element
> listLinks
= elLinks
.getChildren("Link", sddNamespace
);
963 for (Element elLink
: listLinks
){
967 String rel
= elLink
.getAttributeValue("rel");
968 String href
= elLink
.getAttributeValue("href");
970 Media link
= Media
.NewInstance();
971 MediaRepresentation mr
= MediaRepresentation
.NewInstance();
972 mr
.addRepresentationPart(MediaRepresentationPart
.NewInstance(href
, null));
973 link
.addRepresentation(mr
);
974 person
.addMedia(link
);
976 } catch (Exception e
) {
978 logger
.warn("Import of Link " + k
+ " failed.");
982 if ((++k
% modCount
) == 0){ logger
.info("Links handled: " + k
);}
986 if (authors
.containsKey(idA
)) {
987 authors
.put(idA
,person
);
990 if (editors
.containsKey(idA
)) {
991 editors
.put(idA
, person
);
994 } catch (Exception e
) {
996 logger
.warn("Import of Agent " + j
+ " failed.");
1000 if ((++j
% modCount
) == 0){ logger
.info("Agents handled: " + j
);}
1005 // imports publications related with the data set
1006 protected void importPublications(Element elDataset
, Namespace sddNamespace
, SDDImportConfigurator sddConfig
, boolean success
){
1008 logger
.info("start Publications ...");
1009 Element elPublications
= elDataset
.getChild("Publications",sddNamespace
);
1011 if (elPublications
!= null) {
1012 // <Publication id="p1">
1013 List
<Element
> listPublications
= elPublications
.getChildren("Publication", sddNamespace
);
1015 //for each Publication
1016 for (Element elPublication
: listPublications
){
1020 String idP
= elPublication
.getAttributeValue("id");
1023 // <Label>Sample Citation</Label>
1024 // </Representation>
1025 Article publication
= Article
.NewInstance();
1026 importRepresentation(elPublication
, sddNamespace
, publication
, idP
, sddConfig
);
1028 publications
.put(idP
,publication
);
1030 } catch (Exception e
) {
1032 logger
.warn("Import of Publication " + j
+ " failed.");
1036 if ((++j
% modCount
) == 0){ logger
.info("Publications handled: " + j
);}
1042 // imports media objects such as images
1043 protected void importMediaObjects(Element elDataset
, Namespace sddNamespace
, SDDImportConfigurator sddConfig
, boolean success
){
1045 logger
.info("start MediaObjects ...");
1046 Element elMediaObjects
= elDataset
.getChild("MediaObjects",sddNamespace
);
1048 if (elMediaObjects
!= null) {
1049 // <MediaObject id="m1">
1050 List
<Element
> listMediaObjects
= elMediaObjects
.getChildren("MediaObject", sddNamespace
);
1052 //for each Publication
1053 for (Element elMO
: listMediaObjects
){
1057 String idMO
= elMO
.getAttributeValue("id");
1060 // <Label>Image description, e.g. to be used for alt-attribute in html.</Label>
1061 // </Representation>
1062 Media media
= Media
.NewInstance();
1063 importRepresentation(elMO
, sddNamespace
, media
, idMO
, sddConfig
);
1065 // <Type>Image</Type>
1066 // <Source href="http://test.edu/test.jpg"/>
1067 String type
= (String
)ImportHelper
.getXmlInputValue(elMO
,"Type",sddNamespace
);
1069 if ((type
!= null) && (type
.equals("Image"))) {
1070 Element elSource
= elMO
.getChild("Source",sddNamespace
);
1071 String href
= elSource
.getAttributeValue("href");
1073 ImageMetaData imageMetaData
= new ImageMetaData();
1074 ImageFile image
= null;
1076 if (href
.substring(0,7).equals("http://")) {
1078 URL url
= new URL(href
);
1079 imageMetaData
.readFrom(url
);
1080 image
= ImageFile
.NewInstance(url
.toString(), null, imageMetaData
);
1081 } catch (MalformedURLException e
) {
1082 logger
.error("Malformed URL", e
);
1085 String sns
= sddConfig
.getSourceNameString();
1086 File f
= new File(sns
);
1087 File parent
= f
.getParentFile();
1088 String fi
= parent
.toString() + File
.separator
+ href
;
1089 File file
= new File(fi
);
1090 imageMetaData
.readFrom(file
);
1091 image
= ImageFile
.NewInstance(file
.toString(), null, imageMetaData
);
1094 MediaRepresentation representation
= MediaRepresentation
.NewInstance(imageMetaData
.getMimeType(), null);
1095 representation
.addRepresentationPart(image
);
1097 media
.addRepresentation(representation
);
1099 ArrayList
<CdmBase
> lcb
= (ArrayList
<CdmBase
>) mediaObject_ListCdmBase
.get(idMO
);
1101 for (int k
= 0; k
< lcb
.size(); k
++) {
1102 if (lcb
.get(k
) instanceof DefinedTermBase
) {
1103 DefinedTermBase dtb
= (DefinedTermBase
) lcb
.get(k
);
1104 // if (lcb.get(0) instanceof DefinedTermBase) {
1105 // DefinedTermBase dtb = (DefinedTermBase) lcb.get(0);
1108 dtb
.addMedia(media
);
1110 Media me
= (Media
) media
.clone();
1114 } else if (lcb
.get(k
) instanceof ReferenceBase
) {
1115 ReferenceBase rb
= (ReferenceBase
) lcb
.get(k
);
1116 //} else if (lcb.get(0) instanceof ReferenceBase) {
1117 //ReferenceBase rb = (ReferenceBase) lcb.get(0);
1118 // rb.setTitleCache(label);
1123 Media me
= (Media
) media
.clone();
1132 } catch (Exception e
) {
1134 logger
.warn("Import of MediaObject " + j
+ " failed.");
1138 if ((++j
% modCount
) == 0){ logger
.info("MediaObjects handled: " + j
);}