X-Git-Url: https://dev.e-taxonomy.eu/gitweb/cdmlib.git/blobdiff_plain/49f54e3f74b692dbb91b1d21d51142870cd68180..2a1795298ee03b33ec07613db01bc6389f3d7628:/cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/sdd/in/SDDImport.java diff --git a/cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/sdd/in/SDDImport.java b/cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/sdd/in/SDDImport.java index 9c9bde0e8e..c6fe7d36aa 100644 --- a/cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/sdd/in/SDDImport.java +++ b/cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/sdd/in/SDDImport.java @@ -11,17 +11,18 @@ package eu.etaxonomy.cdm.io.sdd.in; import java.io.File; import java.net.MalformedURLException; +import java.net.URI; import java.net.URL; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.HashSet; -import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; +import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import org.jdom.Element; import org.jdom.Namespace; @@ -30,15 +31,14 @@ import org.springframework.stereotype.Component; import org.springframework.transaction.TransactionStatus; import eu.etaxonomy.cdm.api.service.IDescriptionService; -import eu.etaxonomy.cdm.api.service.IReferenceService; -import eu.etaxonomy.cdm.api.service.ITaxonService; -import eu.etaxonomy.cdm.api.service.ITermService; +import eu.etaxonomy.cdm.common.IProgressMonitor; import eu.etaxonomy.cdm.common.mediaMetaData.ImageMetaData; import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper; import eu.etaxonomy.cdm.io.common.CdmImportBase; import eu.etaxonomy.cdm.io.common.ICdmImport; import eu.etaxonomy.cdm.io.common.IImportConfigurator; import eu.etaxonomy.cdm.io.common.ImportHelper; +import eu.etaxonomy.cdm.io.sdd.SDDTransformer; import eu.etaxonomy.cdm.model.agent.Person; import eu.etaxonomy.cdm.model.agent.Team; import eu.etaxonomy.cdm.model.common.Annotation; @@ -68,6 +68,7 @@ import eu.etaxonomy.cdm.model.description.StatisticalMeasure; import eu.etaxonomy.cdm.model.description.StatisticalMeasurementValue; import eu.etaxonomy.cdm.model.description.TaxonDescription; import eu.etaxonomy.cdm.model.description.TextData; +import eu.etaxonomy.cdm.model.description.WorkingSet; import eu.etaxonomy.cdm.model.location.NamedArea; import eu.etaxonomy.cdm.model.media.IdentifiableMediaEntity; import eu.etaxonomy.cdm.model.media.ImageFile; @@ -78,20 +79,19 @@ import eu.etaxonomy.cdm.model.media.Rights; import eu.etaxonomy.cdm.model.name.NonViralName; import eu.etaxonomy.cdm.model.name.TaxonNameBase; import eu.etaxonomy.cdm.model.occurrence.Specimen; -import eu.etaxonomy.cdm.model.reference.IArticle; -import eu.etaxonomy.cdm.model.reference.ReferenceBase; +import eu.etaxonomy.cdm.model.reference.Reference; import eu.etaxonomy.cdm.model.reference.ReferenceFactory; +import eu.etaxonomy.cdm.model.taxon.Classification; import eu.etaxonomy.cdm.model.taxon.Synonym; import eu.etaxonomy.cdm.model.taxon.Taxon; import eu.etaxonomy.cdm.model.taxon.TaxonNode; -import eu.etaxonomy.cdm.model.taxon.TaxonomicTree; /** * @author h.fradin * @created 24.10.2008 * @version 1.0 */ -@Component("sddDescriptionIO") +@Component("sddImport") public class SDDImport extends CdmImportBase implements ICdmImport { private static final Logger logger = Logger.getLogger(SDDImport.class); @@ -106,8 +106,7 @@ public class SDDImport extends CdmImportBase locations = new HashMap(); private Map> mediaObject_ListCdmBase = new HashMap>(); private Map mediaObject_Role = new HashMap(); - private Map publications = new HashMap(); - private Map stateDatas = new HashMap(); + private Map publications = new HashMap(); private Map states = new HashMap(); private Map taxonDescriptions = new HashMap(); private Map taxonNameBases = new HashMap(); @@ -118,29 +117,32 @@ public class SDDImport extends CdmImportBase modifiers = new HashMap(); private Set markerTypes = new HashSet(); + private Set vocabularies = new HashSet(); private Set descriptiveConcepts = new HashSet(); - private Set> termVocabularyStates = new HashSet>(); private Set annotationTypes = new HashSet(); - private Set featureSet = new HashSet(); - private ReferenceBase sec = ReferenceFactory.newDatabase(); - private ReferenceBase sourceReference = null; +// private Set featureSet = new HashSet(); + private Set sources = new HashSet(); + private Reference sec = ReferenceFactory.newDatabase(); + private Reference sourceReference = null; private Language datasetLanguage = null; + private WorkingSet workingSet = null; private Namespace xmlNamespace = Namespace.getNamespace("xml","http://www.w3.org/XML/1998/namespace"); private String generatorName = ""; private String generatorVersion = ""; + private Set statisticalMeasures = new HashSet(); private Set featureData = new HashSet(); private Set featureTrees = new HashSet(); - private Set taxonomicTrees = new HashSet(); + private Set classifications = new HashSet(); private Rights copyright = null; - private int taxonNamesCount = 0; //XIM ajout + private int taxonNamesCount = 0; public SDDImport(){ super(); @@ -161,8 +163,10 @@ public class SDDImport extends CdmImportBase Element root = sddConfig.getSourceRoot(); Namespace sddNamespace = sddConfig.getSddNamespace(); @@ -175,12 +179,16 @@ public class SDDImport extends CdmImportBase lcb = new ArrayList(); lcb.add(cb); mediaObject_ListCdmBase.put(refMO,lcb); @@ -217,17 +225,27 @@ public class SDDImport extends CdmImportBaseThis 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". */ + + + Element elRepresentation = parent.getChild("Representation",sddNamespace); String label = (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace); String detail = (String)ImportHelper.getXmlInputValue(elRepresentation, "Detail",sddNamespace); + + //new + Representation representation = Representation.NewInstance(detail, label, null, datasetLanguage); + workingSet.addRepresentation(representation); + - sec.setTitleCache(label, true); - - if (detail != null) { - Annotation annotation = Annotation.NewInstance(detail, datasetLanguage); - annotation.setAnnotationType(AnnotationType.EDITORIAL()); - sec.addAnnotation(annotation); - } + //old +// sec.setTitleCache(label, true); +// +// if (detail != null) { +// Annotation annotation = Annotation.NewInstance(detail, datasetLanguage); +// annotation.setAnnotationType(AnnotationType.EDITORIAL()); +// sec.addAnnotation(annotation); +// } + List listMediaObjects = elRepresentation.getChildren("MediaObject",sddNamespace); @@ -251,54 +269,93 @@ public class SDDImport extends CdmImportBaseViola hederacea Labill. - List listLabels = elRepresentation.getChildren("Label",sddNamespace); - List listDetails = elRepresentation.getChildren("Detail",sddNamespace); + Map> langLabDet = new HashMap>(); - for (Element elLabel : listLabels){ - String lang = elLabel.getAttributeValue("lang",xmlNamespace); - Language language = null; - if (lang != null) { - if (!lang.equals("")) { - language = getTermService().getLanguageByIso(lang.substring(0, 2)); - } else { - language = datasetLanguage; - } - } else { - language = datasetLanguage; + handleRepresentationLabels(sddNamespace, elRepresentation, langLabDet); + handleRepresentationDetails(sddNamespace, elRepresentation, langLabDet); + + if (ve instanceof TermBase) { + makeRepresentationForTerms((TermBase)ve, langLabDet); + }else if (ve instanceof Media) { + makeRepresentationForMedia((Media)ve, langLabDet); + }else if (ve instanceof IdentifiableEntity) { + IdentifiableEntity ie = (IdentifiableEntity)ve; + makeRepresentationForIdentifiableEntity(sddNamespace, ie, elRepresentation, langLabDet); + if (ve instanceof IdentifiableMediaEntity){ + makeRepresentationForIdentifiableMediaEntity(parent, sddNamespace, (IdentifiableMediaEntity)ve); } - String label = elLabel.getText(); - List labDet = new ArrayList(3); - labDet.add(label); - langLabDet.put(language, labDet); } + makeRepresentationMediaObjects(sddNamespace, ve, elRepresentation);//FIXME + + } + + + /** + * Handles the "Detail" children of representations. Adds the result to the langLabDet. + * @param sddNamespace + * @param elRepresentation + * @param langLabDet + */ + private void handleRepresentationDetails(Namespace sddNamespace, + Element elRepresentation, Map> langLabDet) { + List listDetails = elRepresentation.getChildren("Detail",sddNamespace); for (Element elDetail : listDetails){ - String lang = elDetail.getAttributeValue("lang",xmlNamespace); + Language language = getLanguage(elDetail); String role = elDetail.getAttributeValue("role"); - Language language = null; - if (lang != null) { - if (!lang.equals("")) { - language = getTermService().getLanguageByIso(lang.substring(0, 2)); - } else { - language = datasetLanguage; - } - } else { - language = datasetLanguage; - } String detail = elDetail.getText(); List labDet = langLabDet.get(language); labDet.add(detail); labDet.add(role); langLabDet.put(language, labDet); } + } - if (ve instanceof TermBase) { - TermBase tb = (TermBase) ve; + /** + * Handles the "Label" children of representations. Adds the result to the langLabDet. + * @param sddNamespace + * @param elRepresentation + * @param langLabDet + */ + private void handleRepresentationLabels(Namespace sddNamespace, + Element elRepresentation, Map> langLabDet) { + // + List listLabels = elRepresentation.getChildren("Label",sddNamespace); + for (Element elLabel : listLabels){ + Language language = getLanguage(elLabel); + String label = elLabel.getText(); + List labDet = new ArrayList(3); + labDet.add(label); + langLabDet.put(language, labDet); + } + } + + /** + * + * @param ve + * @param langLabDet + */ + private void makeRepresentationForMedia(Media m, Map> langLabDet) { + for (Language lang : langLabDet.keySet()){ + List labDet = langLabDet.get(lang); + if (labDet.get(0) != null){ + m.putTitle(LanguageString.NewInstance(labDet.get(0), lang)); + } + if (labDet.size()>1) { + m.addDescription(labDet.get(1), lang); + } + } + } - for (Iterator l = langLabDet.keySet().iterator() ; l.hasNext() ;){ - Language lang = l.next(); + /** + * Handles representations for terms. Adds one representation per language in langLabDet. + * + * @param ve + * @param langLabDet + */ + private void makeRepresentationForTerms(TermBase tb, Map> langLabDet) { + for (Language lang : langLabDet.keySet()){ List labDet = langLabDet.get(lang); if (labDet.size()>0){ if (labDet.size()>1) { @@ -307,125 +364,139 @@ public class SDDImport extends CdmImportBase l = langLabDet.keySet().iterator() ; l.hasNext() ;){ - Language lang = l.next(); - List labDet = langLabDet.get(lang); - if (labDet.get(0) != null){ - m.addTitle(LanguageString.NewInstance(labDet.get(0), lang)); - } - if (labDet.size()>1) { - m.addDescription(labDet.get(1), lang); - } - ve = m; - } - - } - if (ve instanceof IdentifiableEntity) { - IdentifiableEntity ie = (IdentifiableEntity) ve; - List labDet = null; - - if (ve instanceof TaxonNameBase) { - if (langLabDet.keySet().contains(getTermService().getLanguageByIso("la"))) { - labDet = langLabDet.get(getTermService().getLanguageByIso("la")); - } else if (langLabDet.keySet().contains(datasetLanguage)) { - labDet = langLabDet.get(datasetLanguage); - logger.info("TaxonName " + (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace) + " is not specified as a latin name."); - } else { - labDet = langLabDet.get(langLabDet.keySet().iterator().next()); - logger.info("TaxonName " + (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace) + " is not specified as a latin name."); - } - } else { - labDet = langLabDet.get(langLabDet.keySet().iterator().next()); + /** + * Handles the "MediaObject" children of representations. + * @param sddNamespace + * @param ve + * @param elRepresentation + */ + private void makeRepresentationMediaObjects(Namespace sddNamespace, VersionableEntity ve, Element elRepresentation) { + List listMediaObjects = elRepresentation.getChildren("MediaObject", sddNamespace); + for (Element elMediaObject : listMediaObjects) { + String ref = null; + //TODO + String role = null; + if (elMediaObject != null) { + ref = elMediaObject.getAttributeValue("ref"); + role = elMediaObject.getAttributeValue("role"); } - - ie.setTitleCache(labDet.get(0), true); - - if (labDet.size()>1) { - Annotation annotation = null; - if (labDet.get(1) != null) { - if (labDet.get(2) != null) { - annotation = Annotation.NewInstance(labDet.get(2) + " - " + labDet.get(1), datasetLanguage); + if (StringUtils.isNotBlank(ref)) { + if (ve instanceof TaxonDescription) { + TaxonDescription td = (TaxonDescription) ve; + if (td.getDescriptionSources().size() > 0) { + this.associateImageWithCdmBase(ref,(Reference) td.getDescriptionSources().toArray()[0]); } else { - annotation = Annotation.NewInstance(labDet.get(1), datasetLanguage); + Reference descriptionSource = ReferenceFactory.newGeneric(); + sources.add(descriptionSource); + td.addDescriptionSource(descriptionSource); + this.associateImageWithCdmBase(ref,descriptionSource); } + } else { + this.associateImageWithCdmBase(ref,ve); } - ie.addAnnotation(annotation); } - - ve = ie; - } - - if (ve instanceof IdentifiableMediaEntity){ - IdentifiableMediaEntity ime = (IdentifiableMediaEntity) ve; - Element elLinks = parent.getChild("Links",sddNamespace); - - if (elLinks != null) { + } - // - List listLinks = elLinks.getChildren("Link", sddNamespace); - Media link = Media.NewInstance(); - MediaRepresentation mr = MediaRepresentation.NewInstance(); - int k = 0; - //for each Link - for (Element elLink : listLinks){ + /** + * Handles the "Links" element + * @param parent + * @param sddNamespace + * @param ve + */ + private void makeRepresentationForIdentifiableMediaEntity(Element parent, + Namespace sddNamespace, IdentifiableMediaEntity ime) { + Element elLinks = parent.getChild("Links",sddNamespace); - try { + if (elLinks != null) { - String rel = elLink.getAttributeValue("rel"); - String href = elLink.getAttributeValue("href"); + // + List listLinks = elLinks.getChildren("Link", sddNamespace); + Media link = Media.NewInstance(); + MediaRepresentation mr = MediaRepresentation.NewInstance(); + int k = 0; + //for each Link + for (Element elLink : listLinks){ - mr.addRepresentationPart(MediaRepresentationPart.NewInstance(href, null)); - link.addRepresentation(mr); - ime.addMedia(link); + try { + //TODO + String rel = elLink.getAttributeValue("rel"); + String href = elLink.getAttributeValue("href"); + URI uri = new URI(href); + mr.addRepresentationPart(MediaRepresentationPart.NewInstance(uri, null)); + link.addRepresentation(mr); + ime.addMedia(link); - } catch (Exception e) { - //FIXME - logger.warn("Import of Link " + k + " failed."); - } + } catch (Exception e) { + //FIXME + logger.warn("Import of Link " + k + " failed."); + } - if ((++k % modCount) == 0){ logger.info("Links handled: " + k);} + if ((++k % modCount) == 0){ logger.info("Links handled: " + k);} - } } } + } - List listMediaObjects = elRepresentation.getChildren("MediaObject",sddNamespace); - for (Element elMediaObject : listMediaObjects) { - String ref = null; - String role = null; - if (elMediaObject != null) { - ref = elMediaObject.getAttributeValue("ref"); - role = elMediaObject.getAttributeValue("role"); + /** + * @param sddNamespace + * @param ve + * @param elRepresentation + * @param langLabDet + * @return + */ + private void makeRepresentationForIdentifiableEntity(Namespace sddNamespace, IdentifiableEntity ie, + Element elRepresentation, Map> langLabDet) { + List labDet = null; + + if (ie instanceof TaxonNameBase) { + if (langLabDet.keySet().contains(getTermService().getLanguageByIso("la"))) { + labDet = langLabDet.get(getTermService().getLanguageByIso("la")); + } else if (langLabDet.keySet().contains(datasetLanguage)) { + labDet = langLabDet.get(datasetLanguage); + logger.info("TaxonName " + (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace) + " is not specified as a latin name."); + } else { + labDet = langLabDet.get(langLabDet.keySet().iterator().next()); + logger.info("TaxonName " + (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace) + " is not specified as a latin name."); } - if (ref != null) { - if (!ref.equals("")) { - if (ref != null) { - if (ve instanceof TaxonDescription) { - TaxonDescription td = (TaxonDescription) ve; - //TODO: ensure that all images are imported - if (td.getDescriptionSources().toArray().length > 0) { - this.associateImageWithCdmBase(ref,(ReferenceBase) td.getDescriptionSources().toArray()[0]); - } else { - ReferenceBase descriptionSource = ReferenceFactory.newGeneric(); - td.addDescriptionSource(descriptionSource); - this.associateImageWithCdmBase(ref,descriptionSource); - } - } else { - this.associateImageWithCdmBase(ref,ve); - } - } + } else { + labDet = langLabDet.get(langLabDet.keySet().iterator().next()); + } + //FIXME labDet is != null only for TaxonNameBase + ie.setTitleCache(labDet.get(0), true); + + if (labDet.size()>1) { + Annotation annotation = null; + if (labDet.get(1) != null) { + if (labDet.get(2) != null) { + annotation = Annotation.NewInstance(labDet.get(2) + " - " + labDet.get(1), datasetLanguage); + } else { + annotation = Annotation.NewInstance(labDet.get(1), datasetLanguage); } } + ie.addAnnotation(annotation); } + return; + } + /** + * @param elLabel + * @return + */ + private Language getLanguage(Element elLanguage) { + String lang = elLanguage.getAttributeValue("lang",xmlNamespace); + Language language = null; + if (StringUtils.isNotBlank(lang)) { + language = getTermService().getLanguageByIso(lang.substring(0, 2)); + } else { + language = datasetLanguage; + } + return language; } @@ -460,26 +531,30 @@ public class SDDImport extends CdmImportBase + protected boolean importDataset(Element elDataset, Namespace sddNamespace, SDDImportState state){ // + boolean success = true; + SDDImportConfigurator sddConfig = state.getConfig(); + workingSet = WorkingSet.NewInstance(); importDatasetLanguage(elDataset,sddConfig); importDatasetRepresentation(elDataset, sddNamespace); importRevisionData(elDataset, sddNamespace); importIPRStatements(elDataset, sddNamespace, sddConfig); importTaxonNames(elDataset, sddNamespace, sddConfig); + importDescriptiveConcepts(elDataset, sddNamespace, sddConfig); - importCharacters(elDataset, sddNamespace, sddConfig, success); + success &= importCharacters(elDataset, sddNamespace, sddConfig); importCharacterTrees(elDataset, sddNamespace, sddConfig, success); - //FIXME (a.mueller) - MarkerType editorMarkerType = MarkerType.NewInstance("Editor", "editor", "edt") ; - MarkerType geographicAreaMarkerType = MarkerType.NewInstance("", "SDDGeographicArea", "ga"); - MarkerType descriptiveConceptMarkerType = MarkerType.NewInstance("Descriptive Concept", "DescriptiveConcept", "DC"); + MarkerType editorMarkerType = getMarkerType(state, SDDTransformer.uuidMarkerEditor, "editor", "Editor", "edt"); + MarkerType geographicAreaMarkerType = getMarkerType(state, SDDTransformer.uuidMarkerSDDGeographicArea, "SDDGeographicArea", "SDDGeographicArea", "ga"); + MarkerType descriptiveConceptMarkerType = getMarkerType(state, SDDTransformer.uuidMarkerDescriptiveConcept, "DescriptiveConcept", "Descriptive Concept", "DC"); markerTypes.add(editorMarkerType); markerTypes.add(geographicAreaMarkerType); markerTypes.add(descriptiveConceptMarkerType); - + //saving of all imported data into the CDM db + saveVocabularies(); saveFeatures(); saveModifiers(); saveStates(); @@ -488,33 +563,32 @@ public class SDDImport extends CdmImportBase author = authors.values().iterator() ; author.hasNext() ;){ - team.addTeamMember(author.next()); - } + for (Person author : authors.values()){ + team.addTeamMember(author); + } } if (editors != null) { Marker marker = Marker.NewInstance(); marker.setMarkerType(editorMarkerType); - for (Iterator editor = editors.values().iterator() ; editor.hasNext() ;){ - Person edit = editor.next(); + for (Person editor : editors.values()){ + Person edit = editor; edit.addMarker(marker); team.addTeamMember(edit); } - } + } sec.setAuthorTeam(team); sourceReference.setAuthorTeam(team); } @@ -526,110 +600,85 @@ public class SDDImport extends CdmImportBase k = taxonDescriptions.values().iterator() ; k.hasNext() ;){ - TaxonDescription taxonDescription = k.next(); + + for (TaxonDescription taxonDescription : taxonDescriptions.values()){ // Persists a Description descriptionService.save(taxonDescription); } - - - for (Iterator refCD = taxonDescriptions.keySet().iterator() ; refCD.hasNext() ;){ - String ref = refCD.next(); + for (String ref : taxonDescriptions.keySet()){ TaxonDescription td = taxonDescriptions.get(ref); if (citations.containsKey(ref)) { - IArticle publication = (IArticle) publications.get(citations.get(ref)); + Reference publication = publications.get(citations.get(ref)); if (locations.containsKey(ref)) { Annotation location = Annotation.NewInstance(locations.get(ref), datasetLanguage); AnnotationType annotationType = AnnotationType.NewInstance("", "location", ""); annotationTypes.add(annotationType); location.setAnnotationType(annotationType); - ((ReferenceBase)publication).addAnnotation(location); + (publication).addAnnotation(location); } - td.addDescriptionSource((ReferenceBase)publication); + td.addDescriptionSource(publication); } } logger.info("end makeTaxonDescriptions ..."); - - -// for (Iterator> k = termVocabularyStates.iterator() ; k.hasNext() ;){ -// TermVocabulary termVocabulary = k.next(); -// getVocabularyService().save(termVocabulary); //XIM -// } - - - //sddConfig.setSourceReference(sourceReference); - if (descriptiveConcepts != null) { - for (Iterator feat = descriptiveConcepts.iterator() ; feat.hasNext() ;) { + for (Feature feature : descriptiveConcepts) { Marker marker = Marker.NewInstance(); marker.setMarkerType(descriptiveConceptMarkerType); - Feature feature = feat.next(); feature.addMarker(marker); } } saveFeatures(); - /*Marker markerd = Marker.NewInstance(); - markerd.setMarkerType(descriptiveConceptMarker); - Feature fiture = Feature.NewInstance("Fitoure","Fitoure","Fitoure"); - fiture.addMarker(markerd); - TermVocabulary termVocabularyState = new TermVocabulary("test","test","test","test"); - Modifier modif = new Modifier("zoub","zab","zib"); - termVocabularyState.addTerm(modif); - getVocabularyService().save(termVocabularyState); - fiture.addRecommendedModifierEnumeration(termVocabularyState); - termService.save(modif); - termService.save(fiture);*/ - - //XIMtermService.save(editorMarkerType); + for (Reference publication : publications.values()){ + getReferenceService().save(publication); + } - //XIMtermService.save(geographicAreaMarkerType); - - IReferenceService referenceService = getReferenceService(); - // referenceService.saveReference(sourceReference); - for (Iterator k = publications.values().iterator() ; k.hasNext() ;){ - ReferenceBase publication = (ReferenceBase) k.next(); - referenceService.save(publication); + for (Reference source : sources){ + getReferenceService().save(source); } - for (Iterator k = featureTrees.iterator() ; k.hasNext() ;) { - FeatureTree tree = k.next(); - getFeatureTreeService().save(tree); + for (FeatureTree featureTree : featureTrees) { + getFeatureTreeService().save(featureTree); } - for (Iterator k = taxonomicTrees.iterator() ; k.hasNext() ;) { - TaxonomicTree tree = k.next(); - getTaxonTreeService().save(tree); + getWorkingSetService().save(workingSet); + for (Classification classification : classifications) { + getClassificationService().save(classification); } - for (Iterator k = specimens.values().iterator() ; k.hasNext() ;) { - Specimen specimen = k.next(); + for (Specimen specimen : specimens.values()) { getOccurrenceService().save(specimen); } logger.info("end of persistence ..."); + return success; + } + + /** + * + */ + private void saveVocabularies() { + for (TermVocabulary vocabulary : vocabularies ){ + getVocabularyService().save(vocabulary); + } } private void saveAnnotationType() { - for (Iterator at = annotationTypes.iterator() ; at.hasNext() ;) { - AnnotationType annotationType = at.next(); + for (AnnotationType annotationType: annotationTypes){ getTermService().save(annotationType); } } private void saveStatisticalMeasure() { - for (Iterator k = statisticalMeasures.iterator() ; k.hasNext() ;) { - StatisticalMeasure sm = k.next(); + for (StatisticalMeasure sm : statisticalMeasures){ getTermService().save(sm); } } private void saveUnits() { if (units != null) { - for (Iterator k = units.values().iterator() ; k.hasNext() ;){ - MeasurementUnit unit = k.next(); + for (MeasurementUnit unit : units.values()){ if (unit != null) { getTermService().save(unit); } @@ -638,44 +687,35 @@ public class SDDImport extends CdmImportBase k = namedAreas.values().iterator() ; k.hasNext() ;) { + for (NamedArea area : namedAreas.values() ){ Marker marker = Marker.NewInstance(); marker.setMarkerType(geographicAreaMarkerType); - NamedArea area = k.next(); area.addMarker(marker); - //getTermService().save(area); getTermService().save(area); } } private void saveStates() { - for (Iterator k = states.values().iterator() ; k.hasNext() ;){ - State state = k.next(); + for (State state : states.values() ){ getTermService().save(state); } } private void saveMarkerType() { - for(Iterator k = markerTypes.iterator() ; k.hasNext() ;){ - MarkerType markerType = k.next(); + for (MarkerType markerType : markerTypes){ getTermService().save(markerType); } } private void saveModifiers() { - - for (Iterator k = modifiers.values().iterator() ; k.hasNext() ;){ - Modifier modifier = k.next(); + for (Modifier modifier : modifiers.values() ){ getTermService().save(modifier); } } private void saveFeatures() { - ITermService termService = getTermService(); - - for (Iterator k = features.values().iterator() ; k.hasNext() ;){ - Feature feature = k.next(); - termService.save(feature); + for (Feature feature : features.values() ){ + getTermService().save(feature); } } @@ -683,7 +723,7 @@ public class SDDImport extends CdmImportBase logger.info("start Characters ..."); Element elCharacters = elDataset.getChild("Characters", sddNamespace); // if (elCharacters != null) { - List elCategoricalCharacters = elCharacters.getChildren("CategoricalCharacter", sddNamespace); - int j = 0; - for (Element elCategoricalCharacter : elCategoricalCharacters){ - try { - - String idCC = elCategoricalCharacter.getAttributeValue("id"); - Feature categoricalCharacter = Feature.NewInstance(); - categoricalCharacter.setKindOf(Feature.DESCRIPTION()); - importRepresentation(elCategoricalCharacter, sddNamespace, categoricalCharacter, idCC, sddConfig); - categoricalCharacter.setSupportsCategoricalData(true); + success &= handleCategoricalData(sddNamespace, sddConfig, elCharacters); + success &= handleQuantitativeData(sddNamespace, sddConfig, elCharacters); + success &= handleTextCharacters(sddNamespace, sddConfig, elCharacters); + } - // - Element elStates = elCategoricalCharacter.getChild("States",sddNamespace); + /*for (Iterator f = features.values().iterator() ; f.hasNext() ;){ + featureSet.add(f.next()); //XIM Why this line ? + }*/ + + return success; - // - List elStateDefinitions = elStates.getChildren("StateDefinition",sddNamespace); - TermVocabulary termVocabularyState = new TermVocabulary(); - int k = 0; - //for each StateDefinition - for (Element elStateDefinition : elStateDefinitions){ + } - if ((++k % modCount) == 0){ logger.info("StateDefinitions handled: " + (k-1));} + /** + * @param sddNamespace + * @param sddConfig + * @param success + * @param elCharacters + * @return + */ + private boolean handleCategoricalData(Namespace sddNamespace, SDDImportConfigurator sddConfig, Element elCharacters) { + boolean success = true; + List elCategoricalCharacters = elCharacters.getChildren("CategoricalCharacter", sddNamespace); + int j = 0; + for (Element elCategoricalCharacter : elCategoricalCharacters){ + try { + + String idCC = elCategoricalCharacter.getAttributeValue("id"); + Feature categoricalCharacter = Feature.NewInstance(); + categoricalCharacter.setKindOf(Feature.DESCRIPTION()); + importRepresentation(elCategoricalCharacter, sddNamespace, categoricalCharacter, idCC, sddConfig); + categoricalCharacter.setSupportsCategoricalData(true); + + // + Element elStates = elCategoricalCharacter.getChild("States",sddNamespace); + + // + List elStateDefinitions = elStates.getChildren("StateDefinition",sddNamespace); + TermVocabulary termVocabularyState = TermVocabulary.NewInstance(null, null, null, null); + + vocabularies.add(termVocabularyState); + + int k = 0; + //for each StateDefinition + for (Element elStateDefinition : elStateDefinitions){ - String idS = elStateDefinition.getAttributeValue("id"); - State state = State.NewInstance(); - importRepresentation(elStateDefinition, sddNamespace, state, idS, sddConfig); + if ((++k % modCount) == 0){ logger.info("StateDefinitions handled: " + (k-1));} - //StateData stateData = StateData.NewInstance(); - //stateData.setState(state); - termVocabularyState.addTerm(state); - states.put(idS,state); + String idS = elStateDefinition.getAttributeValue("id"); + State state = states.get(idS); + if (state == null){ + state = State.NewInstance(); + }else{ + logger.debug("State duplicate found"); } - categoricalCharacter.addSupportedCategoricalEnumeration(termVocabularyState); - features.put(idCC, categoricalCharacter); + importRepresentation(elStateDefinition, sddNamespace, state, idS, sddConfig); - } catch (Exception e) { - logger.warn("Import of CategoricalCharacter " + j + " failed."); - success = false; + termVocabularyState.addTerm(state); + states.put(idS,state); } + categoricalCharacter.addSupportedCategoricalEnumeration(termVocabularyState); + features.put(idCC, categoricalCharacter); - if ((++j % modCount) == 0){ logger.info("CategoricalCharacters handled: " + j);} - + } catch (Exception e) { + logger.warn("Import of CategoricalCharacter " + j + " failed."); + success = false; } - // - List elQuantitativeCharacters = elCharacters.getChildren("QuantitativeCharacter", sddNamespace); - j = 0; - //for each QuantitativeCharacter - for (Element elQuantitativeCharacter : elQuantitativeCharacters){ + if ((++j % modCount) == 0){ logger.info("CategoricalCharacters handled: " + j);} - try { + } + return success; + } - String idQC = elQuantitativeCharacter.getAttributeValue("id"); - - // - // - // - Feature quantitativeCharacter = Feature.NewInstance(); - quantitativeCharacter.setKindOf(Feature.DESCRIPTION()); - importRepresentation(elQuantitativeCharacter, sddNamespace, quantitativeCharacter, idQC, sddConfig); - - quantitativeCharacter.setSupportsQuantitativeData(true); - - // - // - // - Element elMeasurementUnit = elQuantitativeCharacter.getChild("MeasurementUnit",sddNamespace); - String label = ""; - String role = ""; - if (elMeasurementUnit != null) { - Element elLabel = elMeasurementUnit.getChild("Label",sddNamespace); - role = elLabel.getAttributeValue("role"); - label = (String)ImportHelper.getXmlInputValue(elMeasurementUnit, "Label",sddNamespace); - } + /** + * @param sddNamespace + * @param sddConfig + * @param elCharacters + */ + private boolean handleQuantitativeData(Namespace sddNamespace, SDDImportConfigurator sddConfig, Element elCharacters) { + boolean success = true; + int j; + // + List elQuantitativeCharacters = elCharacters.getChildren("QuantitativeCharacter", sddNamespace); + j = 0; + //for each QuantitativeCharacter + for (Element elQuantitativeCharacter : elQuantitativeCharacters){ - MeasurementUnit unit = null; - if (!label.equals("")){ - if (role != null) { - if (role.equals("Abbrev")){ - unit = MeasurementUnit.NewInstance(label,label,label); - } - } else { - unit = MeasurementUnit.NewInstance(label,label,label); - } - } + try { - if (unit != null) { - units.put(idQC, unit); - } + String idQC = elQuantitativeCharacter.getAttributeValue("id"); - // - // milli - // - Element elDefault = elQuantitativeCharacter.getChild("Default",sddNamespace); - if (elDefault != null) { - String measurementUnitPrefix = (String)ImportHelper.getXmlInputValue(elDefault, "MeasurementUnitPrefix",sddNamespace); - if (!measurementUnitPrefix.equals("")){ - defaultUnitPrefixes.put(idQC, measurementUnitPrefix); + // + // + // + Feature quantitativeCharacter = Feature.NewInstance(); + quantitativeCharacter.setKindOf(Feature.DESCRIPTION()); + importRepresentation(elQuantitativeCharacter, sddNamespace, quantitativeCharacter, idQC, sddConfig); + + quantitativeCharacter.setSupportsQuantitativeData(true); + + // + // + // + Element elMeasurementUnit = elQuantitativeCharacter.getChild("MeasurementUnit",sddNamespace); + String label = ""; + String role = ""; + if (elMeasurementUnit != null) { + Element elLabel = elMeasurementUnit.getChild("Label",sddNamespace); + role = elLabel.getAttributeValue("role"); + label = (String)ImportHelper.getXmlInputValue(elMeasurementUnit, "Label",sddNamespace); + } + + MeasurementUnit unit = null; + if (!label.equals("")){ + if (role != null) { + if (role.equals("Abbrev")){ + unit = MeasurementUnit.NewInstance(label,label,label); } + } else { + unit = MeasurementUnit.NewInstance(label,label,label); } + } - features.put(idQC, quantitativeCharacter); + if (unit != null) { + units.put(idQC, unit); + } - } catch (Exception e) { - //FIXME - logger.warn("Import of QuantitativeCharacter " + j + " failed."); - success = false; + // + // milli + // + Element elDefault = elQuantitativeCharacter.getChild("Default",sddNamespace); + if (elDefault != null) { + String measurementUnitPrefix = (String)ImportHelper.getXmlInputValue(elDefault, "MeasurementUnitPrefix",sddNamespace); + if (! measurementUnitPrefix.equals("")){ + defaultUnitPrefixes.put(idQC, measurementUnitPrefix); + } } - if ((++j % modCount) == 0){ logger.info("QuantitativeCharacters handled: " + j);} + features.put(idQC, quantitativeCharacter); + } catch (Exception e) { + //FIXME + logger.warn("Import of QuantitativeCharacter " + j + " failed."); + success = false; } - // - List elTextCharacters = elCharacters.getChildren("TextCharacter", sddNamespace); - j = 0; - //for each TextCharacter - for (Element elTextCharacter : elTextCharacters){ + if ((++j % modCount) == 0){ logger.info("QuantitativeCharacters handled: " + j);} - try { + } + return success; + } - String idTC = elTextCharacter.getAttributeValue("id"); + private boolean handleTextCharacters(Namespace sddNamespace, SDDImportConfigurator sddConfig, Element elCharacters) { + boolean success = true; + int j; + // + List elTextCharacters = elCharacters.getChildren("TextCharacter", sddNamespace); + j = 0; + //for each TextCharacter + for (Element elTextCharacter : elTextCharacters){ - // - // - // - Feature textCharacter = Feature.NewInstance(); - textCharacter.setKindOf(Feature.DESCRIPTION()); - importRepresentation(elTextCharacter, sddNamespace, textCharacter, idTC, sddConfig); + try { - textCharacter.setSupportsTextData(true); + String idTC = elTextCharacter.getAttributeValue("id"); - features.put(idTC, textCharacter); + // + // + // + Feature textCharacter = Feature.NewInstance(); + textCharacter.setKindOf(Feature.DESCRIPTION()); + importRepresentation(elTextCharacter, sddNamespace, textCharacter, idTC, sddConfig); - } catch (Exception e) { - //FIXME - logger.warn("Import of TextCharacter " + j + " failed."); - success = false; - } + textCharacter.setSupportsTextData(true); - if ((++j % modCount) == 0){ logger.info("TextCharacters handled: " + j);} + features.put(idTC, textCharacter); + } catch (Exception e) { + //FIXME + logger.warn("Import of TextCharacter " + j + " failed."); + success = false; } - } - - /*for (Iterator f = features.values().iterator() ; f.hasNext() ;){ - featureSet.add(f.next()); //XIM Why this line ? - }*/ + if ((++j % modCount) == 0){ logger.info("TextCharacters handled: " + j);} + } + return success; } // imports the descriptions of taxa - protected void importCodedDescriptions(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig, boolean success){ + protected boolean importCodedDescriptions(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig){ + boolean success = true; + // logger.info("start CodedDescriptions ..."); Element elCodedDescriptions = elDataset.getChild("CodedDescriptions",sddNamespace); - // - - ITaxonService taxonService = getTaxonService(); - + // if (elCodedDescriptions != null) { List listCodedDescriptions = elCodedDescriptions.getChildren("CodedDescription", sddNamespace); int j = 0; //for each CodedDescription - for (Element elCodedDescription : listCodedDescriptions){ + success &= handleCodedDescription(sddNamespace, sddConfig, elCodedDescription, j); + if ((++j % modCount) == 0){ logger.info("CodedDescriptions handled: " + j);} + } + } + return success; + } - try { - - String idCD = elCodedDescription.getAttributeValue("id"); - - // - // - // - TaxonDescription taxonDescription = TaxonDescription.NewInstance(); - importRepresentation(elCodedDescription, sddNamespace, taxonDescription, idCD, sddConfig); - - // - // - // - // - Element elScope = elCodedDescription.getChild("Scope",sddNamespace); - String ref = ""; - Taxon taxon = null; - if (elScope != null) { - Element elTaxonName = elScope.getChild("TaxonName",sddNamespace); - ref = elTaxonName.getAttributeValue("ref"); - NonViralName taxonNameBase = taxonNameBases.get(ref); - - if(sddConfig.isDoMatchTaxa()){ - taxon = getTaxonService().findBestMatchingTaxon(taxonNameBase.getTitleCache()); - } - - if(taxon != null){ - logger.info("using existing Taxon" + taxon.getTitleCache()); - if(!taxonNameBase.getUuid().equals(taxon.getName().getUuid())){ - logger.warn("TaxonNameBase entity of existing taxon does not match Name in list -> replacing Name in list"); - taxonNameBase = HibernateProxyHelper.deproxy(taxon.getName(), NonViralName.class); - } - } else { - logger.info("creating new Taxon from TaxonName" + taxonNameBase.getTitleCache()); - taxon = Taxon.NewInstance(taxonNameBase, sec); - } - } - - else {//in case no taxon is linked to the description, a new one is created - NonViralName tnb = NonViralName.NewInstance(null); - String id = new String(""+taxonNamesCount); - IdentifiableSource source = IdentifiableSource.NewInstance(id, "TaxonName"); - importRepresentation(elCodedDescription, sddNamespace, tnb, id, sddConfig); - - if(sddConfig.isDoMatchTaxa()){ - taxon = getTaxonService().findBestMatchingTaxon(tnb.getTitleCache()); - } - - if(taxon != null){ - tnb = HibernateProxyHelper.deproxy(taxon.getName(), NonViralName.class); -// taxonNameBases.put(id ,tnb); -// taxonNamesCount++; - logger.info("using existing Taxon" + taxon.getTitleCache()); - } else { - tnb.addSource(source); - taxonNameBases.put(id ,tnb); - taxonNamesCount++; - logger.info("creating new Taxon from TaxonName" + tnb.getTitleCache()); - taxon = Taxon.NewInstance(tnb, sec); - } - } + /** + * @param sddNamespace + * @param sddConfig + * @param j + * @param elCodedDescription + * @return + */ + private boolean handleCodedDescription(Namespace sddNamespace, SDDImportConfigurator sddConfig, Element elCodedDescription, int j) { + boolean success = true ; + try { + + String idCD = elCodedDescription.getAttributeValue("id"); + + // + // + // + TaxonDescription taxonDescription = TaxonDescription.NewInstance(); + if (!generatorName.isEmpty()){ + Annotation annotation = Annotation.NewInstance(generatorName, AnnotationType.TECHNICAL(),Language.DEFAULT()); + taxonDescription.addAnnotation(annotation); + } + importRepresentation(elCodedDescription, sddNamespace, taxonDescription, idCD, sddConfig); + + // + // + // + // + Element elScope = elCodedDescription.getChild("Scope", sddNamespace); + Taxon taxon; + if (elScope != null) { + taxon = handleCDScope(sddNamespace, sddConfig, idCD, elScope); + } else {//in case no taxon is linked to the description, a new one is created + taxon = handleCDNoScope(sddNamespace, sddConfig, elCodedDescription); + } - String refCitation = ""; - String location = ""; + // + Element elSummaryData = elCodedDescription.getChild("SummaryData",sddNamespace); + if (elSummaryData != null) { + handleSummaryCategoricalData(sddNamespace, taxonDescription, elSummaryData); + handleSummaryQuantitativeData(sddNamespace, taxonDescription, elSummaryData); + handleSummaryTextData(sddNamespace, taxonDescription, elSummaryData); + } - if (elScope != null) { - Element elCitation = elScope.getChild("Citation",sddNamespace); - if (elCitation != null) { - refCitation = elCitation.getAttributeValue("ref"); - location = elCitation.getAttributeValue("location"); - } - } + if (taxon != null) { + taxon.addDescription(taxonDescription); + } +// + workingSet.addDescription(taxonDescription); + +//OLD taxonDescription.setDescriptiveSystem(featureSet); - // - Element elSummaryData = elCodedDescription.getChild("SummaryData",sddNamespace); - if (elSummaryData != null) { + taxonDescriptions.put(idCD, taxonDescription);//FIXME - // - List elCategoricals = elSummaryData.getChildren("Categorical", sddNamespace); - int k = 0; - //for each Categorical - for (Element elCategorical : elCategoricals){ - if ((++k % modCount) == 0){ logger.warn("Categorical handled: " + (k-1));} - ref = elCategorical.getAttributeValue("ref"); - Feature feature = features.get(ref); - CategoricalData categoricalData = CategoricalData.NewInstance(); - categoricalData.setFeature(feature); - - // - List elStates = elCategorical.getChildren("State", sddNamespace); - int l = 0; - - //for each State - for (Element elState : elStates){ - if ((++l % modCount) == 0){ logger.info("States handled: " + (l-1));} - ref = elState.getAttributeValue("ref"); - State state = states.get(ref); - if (state!=null) { - StateData stateData = StateData.NewInstance(); - stateData.setState(state); - List elModifiers = elState.getChildren("Modifier", sddNamespace); - for (Element elModifier : elModifiers){ - ref = elModifier.getAttributeValue("ref"); - Modifier modifier = modifiers.get(ref); - if (modifier!=null) { - stateData.addModifier(modifier); - } - } - categoricalData.addState(stateData); - } - taxonDescription.addElement(categoricalData); - } - } - // - List elQuantitatives = elSummaryData.getChildren("Quantitative", sddNamespace); - k = 0; - //for each Quantitative - for (Element elQuantitative : elQuantitatives){ - if ((++k % modCount) == 0){ logger.warn("Quantitative handled: " + (k-1));} - ref = elQuantitative.getAttributeValue("ref"); - Feature feature = features.get(ref); - QuantitativeData quantitativeData = QuantitativeData.NewInstance(); - quantitativeData.setFeature(feature); - - MeasurementUnit unit = units.get(ref); - String prefix = defaultUnitPrefixes.get(ref); - if (unit != null) { - String u = unit.getLabel(); - if (prefix != null) { - u = prefix + u; - } - unit.setLabel(u); - quantitativeData.setUnit(unit); - } + } catch (Exception e) { + //FIXME + logger.warn("Import of CodedDescription " + j + " failed.", e); + success = false; + } + return success; + } - // - List elMeasures = elQuantitative.getChildren("Measure", sddNamespace); - int l = 0; - - //for each State - for (Element elMeasure : elMeasures){ - if ((++l % modCount) == 0){ logger.info("States handled: " + (l-1));} - String type = elMeasure.getAttributeValue("type"); - String value = elMeasure.getAttributeValue("value"); - if (value.contains(",")) { - value = value.replace(',', '.'); - } - Float v = Float.parseFloat(value); - //Float v = new Float(0); - StatisticalMeasure t = null; - if (type.equals("Min")) { - t = StatisticalMeasure.MIN(); - } else if (type.equals("Mean")) { - t = StatisticalMeasure.AVERAGE(); - } else if (type.equals("Max")) { - t = StatisticalMeasure.MAX(); - } else if (type.equals("SD")) { - t = StatisticalMeasure.STANDARD_DEVIATION(); - } else if (type.equals("N")) { - t = StatisticalMeasure.SAMPLE_SIZE(); - } else if (type.equals("UMethLower")) { - t = StatisticalMeasure.TYPICAL_LOWER_BOUNDARY(); - } else if (type.equals("UMethUpper")) { - t = StatisticalMeasure.TYPICAL_UPPER_BOUNDARY(); - } else if (type.equals("Var")) { - t = StatisticalMeasure.VARIANCE(); - } else { - t = StatisticalMeasure.NewInstance(type,type,type); - statisticalMeasures.add(t); - } + /** + * @param sddNamespace + * @param sddConfig + * @param elCodedDescription + * @param taxon + * @return + */ + private Taxon handleCDNoScope(Namespace sddNamespace, + SDDImportConfigurator sddConfig, Element elCodedDescription ) { + Taxon taxon = null; + NonViralName nonViralName = NonViralName.NewInstance(null); + String id = new String("" + taxonNamesCount); + IdentifiableSource source = IdentifiableSource.NewInstance(id, "TaxonName"); + importRepresentation(elCodedDescription, sddNamespace, nonViralName, id, sddConfig); + + if(sddConfig.isDoMatchTaxa()){ + taxon = getTaxonService().findBestMatchingTaxon(nonViralName.getTitleCache()); + } + + if(taxon != null){ + nonViralName = HibernateProxyHelper.deproxy(taxon.getName(), NonViralName.class); +// taxonNameBases.put(id ,tnb); +// taxonNamesCount++; + logger.info("using existing Taxon " + taxon.getTitleCache()); + } else { + nonViralName.addSource(source); + taxonNameBases.put(id ,nonViralName); + taxonNamesCount++; + logger.info("creating new Taxon from TaxonName " + nonViralName.getTitleCache()); + taxon = Taxon.NewInstance(nonViralName, sec); + } + return taxon; + } - StatisticalMeasurementValue statisticalValue = StatisticalMeasurementValue.NewInstance(); - statisticalValue.setValue(v); - statisticalValue.setType(t); - quantitativeData.addStatisticalValue(statisticalValue); - featureData.add(statisticalValue); - } - taxonDescription.addElement(quantitativeData); - } + /** + * @param sddNamespace + * @param sddConfig + * @param idCD + * @param elScope + * @param taxon + * @return + */ + private Taxon handleCDScope(Namespace sddNamespace, SDDImportConfigurator sddConfig, + String idCD, Element elScope) { + Taxon taxon = null; + Element elTaxonName = elScope.getChild("TaxonName", sddNamespace); + String ref = elTaxonName.getAttributeValue("ref"); + NonViralName nonViralName = taxonNameBases.get(ref); + + if(sddConfig.isDoMatchTaxa()){ + taxon = getTaxonService().findBestMatchingTaxon(nonViralName.getTitleCache()); + } + + if(taxon != null){ + logger.info("using existing Taxon" + taxon.getTitleCache()); + if(!nonViralName.getUuid().equals(taxon.getName().getUuid())){ + logger.warn("TaxonNameBase entity of existing taxon does not match Name in list -> replacing Name in list"); + nonViralName = HibernateProxyHelper.deproxy(taxon.getName(), NonViralName.class); + } + } else { + logger.info("creating new Taxon from TaxonName '" + nonViralName.getTitleCache()+"'"); + taxon = Taxon.NewInstance(nonViralName, sec); + } + + //citation + Element elCitation = elScope.getChild("Citation",sddNamespace); + if (elCitation != null) { + String refCitation = elCitation.getAttributeValue("ref"); + if (! refCitation.equals("")){ + citations.put(idCD, refCitation); + } + String location = elCitation.getAttributeValue("location"); + if (! location.equals("")){ + locations.put(idCD, location); + } + } + return taxon; + } - // - List elTextChars = elSummaryData.getChildren("TextChar", sddNamespace); - k = 0; - //for each TextChar - for (Element elTextChar : elTextChars){ - if ((++k % modCount) == 0){ logger.info("TextChar handled: " + (k-1));} - ref = elTextChar.getAttributeValue("ref"); - Feature feature = features.get(ref); - TextData textData = TextData.NewInstance(); - textData.setFeature(feature); - - // Free form text - String content = (String)ImportHelper.getXmlInputValue(elTextChar, "Content",sddNamespace); - textData.putText(content, datasetLanguage); - taxonDescription.addElement(textData); - } + /** + * @param sddNamespace + * @param taxonDescription + * @param elSummaryData + */ + private void handleSummaryTextData(Namespace sddNamespace, + TaxonDescription taxonDescription, Element elSummaryData) { + String ref; + int k; + // + List elTextChars = elSummaryData.getChildren("TextChar", sddNamespace); + k = 0; + //for each TextChar + for (Element elTextChar : elTextChars){ + if ((++k % modCount) == 0){ logger.info("TextChar handled: " + (k-1));} + ref = elTextChar.getAttributeValue("ref"); + Feature feature = features.get(ref); + TextData textData = TextData.NewInstance(); + textData.setFeature(feature); + + // Free form text + String content = (String)ImportHelper.getXmlInputValue(elTextChar, "Content",sddNamespace); + textData.putText(datasetLanguage, content); + taxonDescription.addElement(textData); + } + } - } + /** + * @param sddNamespace + * @param taxonDescription + * @param elSummaryData + */ + private void handleSummaryQuantitativeData(Namespace sddNamespace, + TaxonDescription taxonDescription, Element elSummaryData) { + String ref; + int k; + // + List elQuantitatives = elSummaryData.getChildren("Quantitative", sddNamespace); + k = 0; + //for each Quantitative + for (Element elQuantitative : elQuantitatives){ + if ((++k % modCount) == 0){ logger.warn("Quantitative handled: " + (k-1));} + ref = elQuantitative.getAttributeValue("ref"); + Feature feature = features.get(ref); + QuantitativeData quantitativeData = QuantitativeData.NewInstance(); + quantitativeData.setFeature(feature); + + MeasurementUnit unit = units.get(ref); + String prefix = defaultUnitPrefixes.get(ref); + if (unit != null) { + String u = unit.getLabel(); + if (prefix != null) { + u = prefix + u; + } + unit.setLabel(u); + quantitativeData.setUnit(unit); + } - if (taxon != null) { - taxon.addDescription(taxonDescription); - } + // + List elMeasures = elQuantitative.getChildren("Measure", sddNamespace); + int l = 0; + + //for each State + for (Element elMeasure : elMeasures){ + if ((++l % modCount) == 0){ logger.info("States handled: " + (l-1));} + String type = elMeasure.getAttributeValue("type"); + String value = elMeasure.getAttributeValue("value"); + if (value.contains(",")) { + value = value.replace(',', '.'); + } + Float v = Float.parseFloat(value); + //Float v = new Float(0); + StatisticalMeasure t = null; + if (type.equals("Min")) { + t = StatisticalMeasure.MIN(); + } else if (type.equals("Mean")) { + t = StatisticalMeasure.AVERAGE(); + } else if (type.equals("Max")) { + t = StatisticalMeasure.MAX(); + } else if (type.equals("SD")) { + t = StatisticalMeasure.STANDARD_DEVIATION(); + } else if (type.equals("N")) { + t = StatisticalMeasure.SAMPLE_SIZE(); + } else if (type.equals("UMethLower")) { + t = StatisticalMeasure.TYPICAL_LOWER_BOUNDARY(); + } else if (type.equals("UMethUpper")) { + t = StatisticalMeasure.TYPICAL_UPPER_BOUNDARY(); + } else if (type.equals("Var")) { + t = StatisticalMeasure.VARIANCE(); + } else { + t = StatisticalMeasure.NewInstance(type,type,type); + statisticalMeasures.add(t); + } - if (!refCitation.equals("")){ - citations.put(idCD,refCitation); - } + StatisticalMeasurementValue statisticalValue = StatisticalMeasurementValue.NewInstance(); + statisticalValue.setValue(v); + statisticalValue.setType(t); + quantitativeData.addStatisticalValue(statisticalValue); + featureData.add(statisticalValue); + } + taxonDescription.addElement(quantitativeData); + } + } - if (!location.equals("")){ - locations.put(idCD, location); + /** + * @param sddNamespace + * @param taxonDescription + * @param elSummaryData + */ + private void handleSummaryCategoricalData(Namespace sddNamespace, + TaxonDescription taxonDescription, Element elSummaryData) { + String ref; + // + List elCategoricals = elSummaryData.getChildren("Categorical", sddNamespace); + int k = 0; + //for each Categorical + for (Element elCategorical : elCategoricals){ + if ((++k % modCount) == 0){ logger.warn("Categorical handled: " + (k-1));} + ref = elCategorical.getAttributeValue("ref"); + Feature feature = features.get(ref); + CategoricalData categoricalData = CategoricalData.NewInstance(); + categoricalData.setFeature(feature); + + // + List elStates = elCategorical.getChildren("State", sddNamespace); + int l = 0; + + //for each State + for (Element elState : elStates){ + if ((++l % modCount) == 0){ logger.info("States handled: " + (l-1));} + ref = elState.getAttributeValue("ref"); + State state = states.get(ref); + if (state != null) { + StateData stateData = StateData.NewInstance(); + stateData.setState(state); + List elModifiers = elState.getChildren("Modifier", sddNamespace); + for (Element elModifier : elModifiers){ + ref = elModifier.getAttributeValue("ref"); + Modifier modifier = modifiers.get(ref); + if (modifier != null) { + stateData.addModifier(modifier); + } } - - taxonDescription.setDescriptiveSystem(featureSet); - - taxonDescriptions.put(idCD, taxonDescription);//FIXME - - } catch (Exception e) { - //FIXME - logger.warn("Import of CodedDescription " + j + " failed.", e); - success = false; + categoricalData.addState(stateData); } - if ((++j % modCount) == 0){ logger.info("CodedDescriptions handled: " + j);} - + taxonDescription.addElement(categoricalData); } - } } @@ -1370,7 +1515,7 @@ public class SDDImport extends CdmImportBase listModifiers = elModifiers.getChildren("Modifier", sddNamespace); - TermVocabulary termVocabularyState = new TermVocabulary(); + TermVocabulary termVocabularyState = TermVocabulary.NewInstance(null, null, null, null); for (Element elModifier : listModifiers) { Modifier modif = Modifier.NewInstance(); String idmod = elModifier.getAttributeValue("id"); @@ -1553,7 +1699,7 @@ public class SDDImport extends CdmImportBase listelNodes = elCharacterTree.getChildren("Nodes", sddNamespace); + FeatureTree featureTree = FeatureTree.NewInstance(); + importRepresentation(elCharacterTree, sddNamespace, featureTree, "", sddConfig); + FeatureNode root = featureTree.getRoot(); + List listeOfNodes = elCharacterTree.getChildren("Nodes", sddNamespace); //Nodes of CharacterTrees in SDD always refer to DescriptiveConcepts - for (Element elNodes : listelNodes) { - List listNodes = elNodes.getChildren("Node", sddNamespace); - if (listNodes != null) { - for (Element elNode : listNodes){ - String idN = elNode.getAttributeValue("id"); - FeatureNode fn = null; - Feature dc = null; - if (idN!=null) { - // DescriptiveConcepts are used as nodes in CharacterTrees - Element elDescriptiveConcept = elNode.getChild("DescriptiveConcept", sddNamespace); - if (elDescriptiveConcept != null){ - String refDC = elDescriptiveConcept.getAttributeValue("ref"); - dc = features.get(refDC); - fn = FeatureNode.NewInstance(dc); - } - if (fn==null){ - fn = FeatureNode.NewInstance(); - } - Element elParent = elNode.getChild("Parent", sddNamespace); - // in SDD links between Nodes are referenced by the tag - if (elParent!=null){ - String refP = elParent.getAttributeValue("ref"); - if (refP!=null) { - FeatureNode parent = featureNodes.get(refP); - if (parent==null){ - root.addChild(fn); // if no parent found or the reference is broken, add the node to the root of the tree - } - else { - parent.addChild(fn); - } - } - } - else { - root.addChild(fn); // if no parent found or the reference is broken, add the node to the root of the tree - } - } - featureNodes.put(idN, fn); - } - } - - // Leaves of CharacterTrees in SDD are always CharNodes (referring to Characters) - List listCharNodes = elNodes.getChildren("CharNode", sddNamespace); - if (listCharNodes != null) { - for (Element elCharNode : listCharNodes){ - Element elParent = elCharNode.getChild("Parent", sddNamespace); - Element elCharacter = elCharNode.getChild("Character", sddNamespace); - FeatureNode fn = FeatureNode.NewInstance(); - if (elParent!=null){ - String refP = elParent.getAttributeValue("ref"); - if ((refP!=null)&&(!refP.equals(""))) { - FeatureNode parent = featureNodes.get(refP); - if (parent==null){ - parent = root; // if no parent found or the reference is broken, add the node to the root of the tree - } - parent.addChild(fn); - } - } - String refC = elCharacter.getAttributeValue("ref"); - if ((refC!=null)&&(!refC.equals(""))){ - Feature character = features.get(refC); - fn.setFeature(character); - featureNodes.put(refC, fn); - } - } - } - } - featureTrees.add(feattree); + for (Element elNodes : listeOfNodes) { + handleCharacterNodes(sddNamespace, root, elNodes); } + featureTrees.add(featureTree); + if (workingSet.getDescriptiveSystem() != null){ + //TODO how to handle multiple + logger.warn("Multiple feature trees not yet supported"); + }else{ + workingSet.setDescriptiveSystem(featureTree); + } + } catch (Exception e) { logger.warn("Import of Character tree " + j + " failed."); @@ -1662,6 +1751,105 @@ public class SDDImport extends CdmImportBase listNodes = elNodes.getChildren("Node", sddNamespace); + if (listNodes != null) { + for (Element elNode : listNodes){ + String idN = elNode.getAttributeValue("id"); + FeatureNode fn = null; + Feature dc = null; + if (idN!=null) { + // DescriptiveConcepts are used as nodes in CharacterTrees + Element elDescriptiveConcept = elNode.getChild("DescriptiveConcept", sddNamespace); + if (elDescriptiveConcept != null){ + String refDC = elDescriptiveConcept.getAttributeValue("ref"); + dc = features.get(refDC); + fn = FeatureNode.NewInstance(dc); + } + if (fn==null){ + fn = FeatureNode.NewInstance(); + } + Element elParent = elNode.getChild("Parent", sddNamespace); + // in SDD links between Nodes are referenced by the tag + if (elParent!=null){ + String refP = elParent.getAttributeValue("ref"); + if (refP!=null) { + FeatureNode parent = featureNodes.get(refP); + if (parent==null){ + root.addChild(fn); // if no parent found or the reference is broken, add the node to the root of the tree + } + else { + parent.addChild(fn); + } + } + } + else { + root.addChild(fn); // if no parent found or the reference is broken, add the node to the root of the tree + } + } + featureNodes.put(idN, fn); + } + } + + // Leaves of CharacterTrees in SDD are always CharNodes (referring to Characters) + List listCharNodes = elNodes.getChildren("CharNode", sddNamespace); + if (listCharNodes != null) { + for (Element elCharNode : listCharNodes){ + Element elParent = elCharNode.getChild("Parent", sddNamespace); + Element elCharacter = elCharNode.getChild("Character", sddNamespace); + Element elDependencyRules = elCharNode.getChild("DependencyRules", sddNamespace); + FeatureNode fn = FeatureNode.NewInstance(); + + if (elDependencyRules!=null){ + Element elInapplicableIf = elCharNode.getChild("InapplicableIf", sddNamespace); + if (elInapplicableIf!=null){ + List listStates = elInapplicableIf.getChildren("State", sddNamespace); + for (Element stateElement : listStates) { + String refState = stateElement.getAttributeValue("ref"); + if ((refState!=null)&&(!refState.equals(""))) { + State state = states.get(refState); + fn.addInapplicableState(state); + } + } + } + Element elOnlyapplicableIf = elCharNode.getChild("OnlyApplicableIf", sddNamespace); + if (elOnlyapplicableIf!=null){ + List listStates = elInapplicableIf.getChildren("State", sddNamespace); + for (Element stateElement : listStates) { + String refState = stateElement.getAttributeValue("ref"); + if ((refState!=null)&&(!refState.equals(""))) { + State state = states.get(refState); + fn.addApplicableState(state); + } + } + } + } + + if (elParent!=null){ + String refP = elParent.getAttributeValue("ref"); + if ((refP!=null)&&(!refP.equals(""))) { + FeatureNode parent = featureNodes.get(refP); + if (parent==null){ + parent = root; // if no parent found or the reference is broken, add the node to the root of the tree + } + parent.addChild(fn); + } + } + String refC = elCharacter.getAttributeValue("ref"); + if ((refC!=null)&&(!refC.equals(""))){ + Feature character = features.get(refC); + fn.setFeature(character); + featureNodes.put(refC, fn); + } + } + } + } + // imports the block protected void importTaxonHierarchies(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig, boolean success){ @@ -1675,10 +1863,10 @@ public class SDDImport extends CdmImportBase root = taxonomicTree.getChildNodes(); + Set root = classification.getChildNodes(); Element elNodes = elTaxonHierarchy.getChild("Nodes", sddNamespace); // There can be only one block for TaxonHierarchies List listNodes = elNodes.getChildren("Node", sddNamespace); @@ -1700,13 +1888,13 @@ public class SDDImport extends CdmImportBase