X-Git-Url: https://dev.e-taxonomy.eu/gitweb/cdmlib.git/blobdiff_plain/9653d159233d36e6c1684727cc1684cbba455324..1ffe31066bec4d71a980d15686061bb7a2592486:/cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/sdd/SDDDescriptionIO.java diff --git a/cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/sdd/SDDDescriptionIO.java b/cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/sdd/SDDDescriptionIO.java index 34a8b52531..6fe8a9aff8 100644 --- a/cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/sdd/SDDDescriptionIO.java +++ b/cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/sdd/SDDDescriptionIO.java @@ -1,8 +1,20 @@ +/** + * Copyright (C) 2007 EDIT + * European Distributed Institute of Taxonomy + * http://www.e-taxonomy.eu + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * See LICENSE.TXT at the top of this package for the full license terms. + */ + package eu.etaxonomy.cdm.io.sdd; +import java.io.File; +import java.net.MalformedURLException; +import java.net.URL; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Date; -import java.util.GregorianCalendar; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -14,928 +26,1371 @@ import org.apache.log4j.Logger; import org.jdom.Element; import org.jdom.Namespace; import org.joda.time.DateTime; +import org.springframework.stereotype.Component; import org.springframework.transaction.TransactionStatus; +import eu.etaxonomy.cdm.api.service.IAgentService; import eu.etaxonomy.cdm.api.service.IDescriptionService; import eu.etaxonomy.cdm.api.service.IReferenceService; import eu.etaxonomy.cdm.api.service.ITermService; +import eu.etaxonomy.cdm.common.MediaMetaData.ImageMetaData; +import eu.etaxonomy.cdm.io.common.CdmImportBase; +import eu.etaxonomy.cdm.io.common.CdmIoBase; import eu.etaxonomy.cdm.io.common.ICdmIO; +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.common.MapWrapper; import eu.etaxonomy.cdm.model.agent.Person; +import eu.etaxonomy.cdm.model.agent.Team; import eu.etaxonomy.cdm.model.common.Annotation; +import eu.etaxonomy.cdm.model.common.AnnotationType; import eu.etaxonomy.cdm.model.common.CdmBase; +import eu.etaxonomy.cdm.model.common.DefinedTermBase; +import eu.etaxonomy.cdm.model.common.IdentifiableEntity; import eu.etaxonomy.cdm.model.common.Language; import eu.etaxonomy.cdm.model.common.LanguageString; -import eu.etaxonomy.cdm.model.common.MultilanguageText; import eu.etaxonomy.cdm.model.common.OriginalSource; +import eu.etaxonomy.cdm.model.common.Representation; +import eu.etaxonomy.cdm.model.common.TermBase; import eu.etaxonomy.cdm.model.common.TermVocabulary; import eu.etaxonomy.cdm.model.common.VersionableEntity; import eu.etaxonomy.cdm.model.description.CategoricalData; import eu.etaxonomy.cdm.model.description.Feature; +import eu.etaxonomy.cdm.model.description.FeatureNode; +import eu.etaxonomy.cdm.model.description.FeatureTree; import eu.etaxonomy.cdm.model.description.MeasurementUnit; import eu.etaxonomy.cdm.model.description.QuantitativeData; import eu.etaxonomy.cdm.model.description.State; +import eu.etaxonomy.cdm.model.description.StateData; 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.media.ImageFile; import eu.etaxonomy.cdm.model.media.Media; +import eu.etaxonomy.cdm.model.media.MediaRepresentation; +import eu.etaxonomy.cdm.model.media.MediaRepresentationPart; 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.reference.Article; import eu.etaxonomy.cdm.model.reference.Database; +import eu.etaxonomy.cdm.model.reference.Generic; import eu.etaxonomy.cdm.model.reference.ReferenceBase; import eu.etaxonomy.cdm.model.taxon.Taxon; - -public class SDDDescriptionIO extends SDDIoBase implements ICdmIO { +/** + * @author h.fradin + * @created 24.10.2008 + * @version 1.0 + */ +@Component("sddDescriptionIO") +public class SDDDescriptionIO extends CdmImportBase implements ICdmImport { private static final Logger logger = Logger.getLogger(SDDDescriptionIO.class); private static int modCount = 1000; + private Map authors = new HashMap(); + private Map citations = new HashMap(); + private Map defaultUnitPrefixes = new HashMap(); + private Map editors = new HashMap(); + private Map featureNodes = new HashMap(); + private Map features = new HashMap(); + private Map locations = new HashMap(); + private Map> mediaObject_ListCdmBase = new HashMap>(); + private Map mediaObject_Role = new HashMap(); + private Map nodes = new HashMap(); + private Map publications = new HashMap(); + private Map stateDatas = new HashMap(); + private Map taxonDescriptions = new HashMap(); + private Map taxonNameBases = new HashMap(); + private Map units = new HashMap(); + + private Set annotationTypes = new HashSet(); + private Set featureSet = new HashSet(); + + private ReferenceBase sec = Database.NewInstance(); + private ReferenceBase sourceReference = null; + + private Language datasetLanguage = 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 Rights copyright = null; + public SDDDescriptionIO(){ super(); } @Override - public boolean doCheck(IImportConfigurator config){ + public boolean doCheck(SDDImportState state){ boolean result = true; logger.warn("No check implemented for SDD"); return result; } - @Override - public boolean doInvoke(IImportConfigurator config, Map> stores){ +// @Override +// public boolean doInvoke(IImportConfigurator config, Map> stores){ + @Override + public boolean doInvoke(SDDImportState state){ - String value; - logger.info("start Datasets ..."); - SDDImportConfigurator sddConfig = (SDDImportConfigurator)config; + TransactionStatus ts = startTransaction(); + SDDImportConfigurator sddConfig = state.getConfig(); + logger.info("start Datasets ..."); // Element root = sddConfig.getSourceRoot(); - boolean success =true; - + boolean success = true; Namespace sddNamespace = sddConfig.getSddNamespace(); - Namespace xmlNamespace = Namespace.getNamespace("xml","http://www.w3.org/XML/1998/namespace"); - + logger.info("start TechnicalMetadata ..."); // - Element elTechnicalMetadata = root.getChild("TechnicalMetadata", sddNamespace); - String nameCreated = elTechnicalMetadata.getAttributeValue("created"); - ReferenceBase sourceReference = config.getSourceReference(); - ReferenceBase sec = Database.NewInstance(); - if (nameCreated != null) { - if (!nameCreated.equals("")) { - int year = Integer.parseInt(nameCreated.substring(0,4)); - int monthOfYear = Integer.parseInt(nameCreated.substring(5,7)); - int dayOfMonth = Integer.parseInt(nameCreated.substring(8,10)); - int hourOfDay = Integer.parseInt(nameCreated.substring(11,13)); - int minuteOfHour = Integer.parseInt(nameCreated.substring(14,16)); - int secondOfMinute = Integer.parseInt(nameCreated.substring(17,19)); - DateTime created = new DateTime(year,monthOfYear,dayOfMonth,hourOfDay,minuteOfHour,secondOfMinute,0); - sourceReference.setCreated(created); - sec.setCreated(created); - } - } - - // - Element elGenerator = elTechnicalMetadata.getChild("Generator", sddNamespace); - String generatorName = elGenerator.getAttributeValue("name"); - String generatorVersion = elGenerator.getAttributeValue("version"); - + importTechnicalMetadata(root, sddNamespace, sddConfig); List elDatasets = root.getChildren("Dataset",sddNamespace); - - Map taxonDescriptions = new HashMap(); - Map states = new HashMap(); - Map units = new HashMap(); - Map defaultUnitPrefixes = new HashMap(); - Map features = new HashMap(); - Map publications = new HashMap(); - Map mediaObjects = new HashMap(); - - Set statisticalMeasures = new HashSet(); - Set featureData = new HashSet(); - int i = 0; + //for each Dataset logger.info("start Dataset ..."); for (Element elDataset : elDatasets){ + importDataset(elDataset, sddNamespace, success, sddConfig); + if ((++i % modCount) == 0){ logger.info("Datasets handled: " + i);} + logger.info(i + " Datasets handled"); + } + commitTransaction(ts); + return success; + } - // - String nameLang = elDataset.getAttributeValue("lang",xmlNamespace); - Language datasetLanguage = null; - if (!nameLang.equals("")) { -// config.getCdmAppController().getTermService().getLanguageByIso(nameLang); - datasetLanguage = Language.ENGLISH(); - } else { - datasetLanguage = Language.ENGLISH(); - } - if (datasetLanguage == null) { - datasetLanguage = Language.ENGLISH(); - } + /* (non-Javadoc) + * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator) + */ + protected boolean isIgnore(SDDImportState state){ + return false; + } - /* - - 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". - - */ - logger.info("start Representation ..."); - Element elRepresentation = elDataset.getChild("Representation",sddNamespace); - String label = (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace); - String detail = (String)ImportHelper.getXmlInputValue(elRepresentation, "Detail",sddNamespace); - sourceReference.setTitleCache(generatorName + " - " + generatorVersion + " - " + label); - sec.setTitleCache(generatorName + " - " + generatorVersion + " - " + label); - + // associates the reference of a media object in SDD with a CdmBase Object + protected void associateImageWithCdmBase(String refMO, CdmBase cb){ + if ((refMO != null) && (cb!=null)) { + if (!refMO.equals("")) { + if (!mediaObject_ListCdmBase.containsKey(refMO)) { + List lcb = new ArrayList(); + lcb.add(cb); + mediaObject_ListCdmBase.put(refMO,lcb); + } else { + List lcb = mediaObject_ListCdmBase.get(refMO); + lcb.add(cb); + mediaObject_ListCdmBase.put(refMO,lcb); + } + } + } + } + + // imports information about the Dataset + protected void importDatasetRepresentation(Element parent, Namespace sddNamespace){ + logger.info("start Representation ..."); + /* + + 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". + + */ + Element elRepresentation = parent.getChild("Representation",sddNamespace); + String label = (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace); + String detail = (String)ImportHelper.getXmlInputValue(elRepresentation, "Detail",sddNamespace); + + sec.setTitleCache(label); + + if (detail != null) { Annotation annotation = Annotation.NewInstance(detail, datasetLanguage); + sec.addAnnotation(annotation); + } - // - logger.info("start RevisionData ..."); - Element elRevisionData = elDataset.getChild("RevisionData",sddNamespace); + List listMediaObjects = elRepresentation.getChildren("MediaObject",sddNamespace); - // - Element elCreators = elRevisionData.getChild("Creators",sddNamespace); + for (Element elMediaObject : listMediaObjects) { + String ref = null; + String role = null; + if (elMediaObject != null) { + ref = elMediaObject.getAttributeValue("ref"); + role = elMediaObject.getAttributeValue("role"); + } + if (ref != null) { + if (!ref.equals("")) { + this.associateImageWithCdmBase(ref,sourceReference); + this.associateImageWithCdmBase(ref,sec); + mediaObject_Role.put(ref,role); + } + } + } + } - // - List listAgents = elCreators.getChildren("Agent", sddNamespace); + // imports the representation (label, detail, lang) of a particular SDD element + protected void importRepresentation(Element parent, Namespace sddNamespace, VersionableEntity ve, String id, IImportConfigurator config){ + Element elRepresentation = parent.getChild("Representation",sddNamespace); + // + 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; + } + String label = elLabel.getText(); + List labDet = new ArrayList(3); + labDet.add(label); + langLabDet.put(language, labDet); + } - int j = 0; - //for each Agent - Map authors = new HashMap(); - Map editors = new HashMap(); - for (Element elAgent : listAgents){ + for (Element elDetail : listDetails){ + String lang = elDetail.getAttributeValue("lang",xmlNamespace); + 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); + } - String role = elAgent.getAttributeValue("role"); - String ref = elAgent.getAttributeValue("ref"); - if (role.equals("aut")) { - if(!ref.equals("")) { - authors.put(ref, null); - } + 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."); } - if (role.equals("edt")) { - if(!ref.equals("")) { - editors.put(ref, null); + } else { + labDet = langLabDet.get(langLabDet.keySet().iterator().next()); + } + + ie.setTitleCache(labDet.get(0)); + + 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); + } + + ve = ie; - if ((++j % modCount) == 0){ logger.info("Agents handled: " + j);} + } else if (ve instanceof TermBase) { + TermBase tb = (TermBase) ve; + for (Iterator l = langLabDet.keySet().iterator() ; l.hasNext() ;){ + Language lang = l.next(); + List labDet = langLabDet.get(lang); + if (labDet.size()>0){ + if (labDet.size()>1) { + tb.addRepresentation(Representation.NewInstance(labDet.get(1), labDet.get(0), labDet.get(0), lang)); + } else { + tb.addRepresentation(Representation.NewInstance(labDet.get(0), labDet.get(0), labDet.get(0), lang)); + } + } + ve = tb; } - // 2006-04-08T00:00:00 - String stringDateModified = (String)ImportHelper.getXmlInputValue(elRevisionData, "DateModified",sddNamespace); + } else if (ve instanceof Media) { + Media m = (Media) ve; - if (stringDateModified != null) { - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss"); - Date d = null; - try { - d = sdf.parse(stringDateModified); - } catch(Exception e) { - System.err.println("Exception :"); - e.printStackTrace(); + for (Iterator 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)); } - - GregorianCalendar updated = null; - if (d != null) { - updated = new java.util.GregorianCalendar(); - updated.setTime(d); - sourceReference.setUpdated(updated); - sec.setUpdated(updated); + if (labDet.size()>1) { + m.addDescription(labDet.get(1), lang); } + ve = m; } - // - logger.info("start IPRStatements ..."); - Element elIPRStatements = elDataset.getChild("IPRStatements",sddNamespace); - // - Rights copyright = null; - if (elIPRStatements != null) { - List listIPRStatements = elIPRStatements.getChildren("IPRStatement", sddNamespace); - j = 0; - //for each IPRStatement - - for (Element elIPRStatement : listIPRStatements){ - - String role = elIPRStatement.getAttributeValue("role"); - // - Element elLabel = elIPRStatement.getChild("Label",sddNamespace); - String lang = ""; - if (elLabel != null) { - lang = elLabel.getAttributeValue("lang",xmlNamespace); - } - label = (String)ImportHelper.getXmlInputValue(elIPRStatement, "Label",sddNamespace); - - if (role.equals("Copyright")) { - Language iprLanguage = null; - if (lang != null) { - if (!lang.equals("")) { - //iprLanguage = termService.getLanguageByIso(nameLang); - iprLanguage = datasetLanguage; + } + + 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"); + } + 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 { - iprLanguage = datasetLanguage; + ReferenceBase descriptionSource = Generic.NewInstance(); + td.addDescriptionSource(descriptionSource); + this.associateImageWithCdmBase(ref,descriptionSource); } + } else { + this.associateImageWithCdmBase(ref,ve); } - if (iprLanguage == null) { - iprLanguage = datasetLanguage; - } - copyright = Rights.NewInstance(label, iprLanguage); } - if (copyright != null) { - sourceReference.addRights(copyright); - sec.addRights(copyright); - } + } + } + } - if ((++j % modCount) == 0){ logger.info("IPRStatements handled: " + j);} + } - } + // imports the representation (label, detail, lang) of a particular SDD element + protected void importTechnicalMetadata(Element root, Namespace sddNamespace, SDDImportConfigurator sddConfig){ + Element elTechnicalMetadata = root.getChild("TechnicalMetadata", sddNamespace); + String nameCreated = elTechnicalMetadata.getAttributeValue("created"); + sourceReference = sddConfig.getSourceReference(); + + if (nameCreated != null) { + if (!nameCreated.equals("")) { + int year = Integer.parseInt(nameCreated.substring(0,4)); + int monthOfYear = Integer.parseInt(nameCreated.substring(5,7)); + int dayOfMonth = Integer.parseInt(nameCreated.substring(8,10)); + int hourOfDay = Integer.parseInt(nameCreated.substring(11,13)); + int minuteOfHour = Integer.parseInt(nameCreated.substring(14,16)); + int secondOfMinute = Integer.parseInt(nameCreated.substring(17,19)); + DateTime created = new DateTime(year,monthOfYear,dayOfMonth,hourOfDay,minuteOfHour,secondOfMinute,0); + sourceReference.setCreated(created); + sec.setCreated(created); } + } - // - logger.info("start TaxonNames ..."); - Element elTaxonNames = elDataset.getChild("TaxonNames",sddNamespace); - // - Map taxonNameBases = new HashMap(); - if (elTaxonNames != null) { - List listTaxonNames = elTaxonNames.getChildren("TaxonName", sddNamespace); - j = 0; - //for each TaxonName - for (Element elTaxonName : listTaxonNames){ + // + Element elGenerator = elTechnicalMetadata.getChild("Generator", sddNamespace); + generatorName = elGenerator.getAttributeValue("name"); + generatorVersion = elGenerator.getAttributeValue("version"); - String id = elTaxonName.getAttributeValue("id"); - String uri = elTaxonName.getAttributeValue("uri"); - // - elRepresentation = elTaxonName.getChild("Representation",sddNamespace); - // - Element elLabel = elRepresentation.getChild("Label",sddNamespace); - String lang = elLabel.getAttributeValue("lang",xmlNamespace); - label = (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace); - NonViralName tnb = NonViralName.NewInstance(null); - if ((lang != null) && (!lang.equals("la"))) { - logger.info("TaxonName " + j + " is not specified as a latin name."); - } - tnb.setTitleCache(label); - OriginalSource source = OriginalSource.NewInstance(id, "TaxonName"); - tnb.addSource(source); - if (!id.equals("")) { - taxonNameBases.put(id,tnb); - } + sec.addAnnotation(Annotation.NewDefaultLanguageInstance(generatorName + " - " + generatorVersion)); + sourceReference.addAnnotation(Annotation.NewDefaultLanguageInstance(generatorName + " - " + generatorVersion)); - if ((++j % modCount) == 0){ logger.info("TaxonNames handled: " + j);} + } - } + // imports the complete dataset information + protected void importDataset(Element elDataset, Namespace sddNamespace, boolean success, SDDImportConfigurator sddConfig){ // + + 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); + importCharacterTrees(elDataset, sddNamespace, sddConfig, success); + importCodedDescriptions(elDataset, sddNamespace, sddConfig, success); + importAgents(elDataset, sddNamespace, sddConfig, success); + importPublications(elDataset, sddNamespace, sddConfig, success); + importMediaObjects(elDataset, sddNamespace, sddConfig, success); + + if (authors != null) { + Team team = Team.NewInstance(); + for (Iterator author = authors.values().iterator() ; author.hasNext() ;){ + team.addTeamMember(author.next()); } + sec.setAuthorTeam(team); + sourceReference.setAuthorTeam(team); + } - // - logger.info("start Characters ..."); - Element elCharacters = elDataset.getChild("Characters", sddNamespace); + if (editors != null) { + Person ed = Person.NewInstance(); + for (Iterator editor = editors.values().iterator() ; editor.hasNext() ;){ + ed = editor.next(); + } + // TODO updatedBy refactored to use a user account, so setting a person is no longer applicable +// sec.setUpdatedBy(ed); +// sourceReference.setUpdatedBy(ed); + } - // - if (elCharacters != null) { - List elCategoricalCharacters = elCharacters.getChildren("CategoricalCharacter", sddNamespace); - j = 0; - //for each CategoricalCharacter - for (Element elCategoricalCharacter : elCategoricalCharacters){ + if (copyright != null) { + sourceReference.addRights(copyright); + sec.addRights(copyright); + } - try { + for (Iterator refCD = taxonDescriptions.keySet().iterator() ; refCD.hasNext() ;){ + String ref = refCD.next(); + TaxonDescription td = taxonDescriptions.get(ref); + td.addDescriptionSource(sec); + if (citations.containsKey(ref)) { + Article publication = (Article) 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); + publication.addAnnotation(location); + } + td.addDescriptionSource(publication); + } + } + logger.info("end makeTaxonDescriptions ..."); - String idCC = elCategoricalCharacter.getAttributeValue("id"); + sddConfig.setSourceReference(sourceReference); - // - // - // - elRepresentation = elCategoricalCharacter.getChild("Representation",sddNamespace); - label = (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace); + //saving of all imported data into the CDM db + ITermService termService = getTermService(); + for (Iterator k = stateDatas.values().iterator() ; k.hasNext() ;){ + StateData sd = k.next(); + termService.saveTerm(sd.getState()); + } + for (Iterator k = features.values().iterator() ; k.hasNext() ;){ + Feature feature = k.next(); + termService.saveTerm(feature); + } + if (units != null) { + for (Iterator k = units.values().iterator() ; k.hasNext() ;){ + MeasurementUnit unit = k.next(); + if (unit != null) { + termService.saveTerm(unit); + } + } + } + for (Iterator k = statisticalMeasures.iterator() ; k.hasNext() ;) { + StatisticalMeasure sm = k.next(); + termService.saveTerm(sm); + } + + for (Iterator at = annotationTypes.iterator() ; at.hasNext() ;) { + AnnotationType annotationType = at.next(); + termService.saveTerm(annotationType); + } - Feature categoricalCharacter = null; - if (label != null){ - categoricalCharacter = Feature.NewInstance(label, label, label); - } - categoricalCharacter.setSupportsQuantitativeData(false); - categoricalCharacter.setSupportsTextData(true); - - // - Element elStates = elCategoricalCharacter.getChild("States",sddNamespace); - - // - 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));} - - String idSD = elStateDefinition.getAttributeValue("id"); - // - // - // - elRepresentation = elStateDefinition.getChild("Representation",sddNamespace); - label = (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace); - State state = new State(label,label,label); - states.put(idSD, state); - termVocabularyState.addTerm(state); - } + IReferenceService referenceService = getReferenceService(); + // referenceService.saveReference(sourceReference); + for (Iterator k = publications.values().iterator() ; k.hasNext() ;){ + Article publication = (Article) k.next(); + referenceService.saveReference(publication); + } - categoricalCharacter.addSupportedCategoricalEnumeration(termVocabularyState); - features.put(idCC, categoricalCharacter); + IAgentService agentService = getAgentService(); + for (Iterator p = authors.values().iterator() ; p.hasNext() ;) { + Person person = p.next(); + agentService.saveAgent(person); + } + + for (Iterator p = editors.values().iterator() ; p.hasNext() ;) { + Person person = p.next(); + agentService.saveAgent(person); + } + + // Returns a CdmApplicationController created by the values of this configuration. + IDescriptionService descriptionService = getDescriptionService(); + + for (Iterator k = taxonDescriptions.values().iterator() ; k.hasNext() ;){ + TaxonDescription taxonDescription = k.next(); + // Persists a Description + descriptionService.saveDescription(taxonDescription); + } - } catch (Exception e) { - //FIXME - logger.warn("Import of CategoricalCharacter " + j + " failed."); - success = false; - } +// descriptionService.saveFeatureNodeAll(featureNodes.values()); + + for (Iterator k = featureTrees.iterator() ; k.hasNext() ;) { + FeatureTree tree = k.next(); + descriptionService.saveFeatureTree(tree); + } - if ((++j % modCount) == 0){ logger.info("CategoricalCharacters handled: " + j);} +} - } +// imports the default language of the dataset +protected void importDatasetLanguage(Element elDataset, SDDImportConfigurator sddConfig){ + String nameLang = elDataset.getAttributeValue("lang",xmlNamespace); - // - List elQuantitativeCharacters = elCharacters.getChildren("QuantitativeCharacter", sddNamespace); - j = 0; - //for each QuantitativeCharacter - for (Element elQuantitativeCharacter : elQuantitativeCharacters){ + if (!nameLang.equals("")) { + String iso = nameLang.substring(0, 2); + datasetLanguage = getTermService().getLanguageByIso(iso); + } else { + datasetLanguage = Language.ENGLISH(); + } + if (datasetLanguage == null) { + datasetLanguage = Language.ENGLISH(); + } +} - try { +// imports the revision data associated with the Dataset (authors, modifications) +protected void importRevisionData(Element elDataset, Namespace sddNamespace){ + // + logger.info("start RevisionData ..."); + Element elRevisionData = elDataset.getChild("RevisionData",sddNamespace); - String idQC = elQuantitativeCharacter.getAttributeValue("id"); + // + Element elCreators = elRevisionData.getChild("Creators",sddNamespace); - // - // - // - elRepresentation = elQuantitativeCharacter.getChild("Representation",sddNamespace); - label = (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace); + // + List listAgents = elCreators.getChildren("Agent", sddNamespace); - Feature quantitativeCharacter = Feature.NewInstance(); + int j = 0; + //for each Agent + for (Element elAgent : listAgents){ - if (!label.equals("")){ - quantitativeCharacter = Feature.NewInstance(label, label, label); - } - quantitativeCharacter.setSupportsQuantitativeData(true); - quantitativeCharacter.setSupportsTextData(false); - - // - // - // - Element elMeasurementUnit = elQuantitativeCharacter.getChild("MeasurementUnit",sddNamespace); - label = ""; - String role = ""; - if (elMeasurementUnit != null) { - Element elLabel = elMeasurementUnit.getChild("Label",sddNamespace); - role = elLabel.getAttributeValue("role"); - label = (String)ImportHelper.getXmlInputValue(elMeasurementUnit, "Label",sddNamespace); - } + String role = elAgent.getAttributeValue("role"); + String ref = elAgent.getAttributeValue("ref"); + if (role.equals("aut")) { + if(!ref.equals("")) { + authors.put(ref, null); + } + } + if (role.equals("edt")) { + if(!ref.equals("")) { + editors.put(ref, null); + } + } - 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); - } - } + if ((++j % modCount) == 0){ logger.info("Agents handled: " + j);} - if (unit != null) { - units.put(idQC, unit); - } + } - // - // 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); - } - } + // 2006-04-08T00:00:00 + String stringDateModified = (String)ImportHelper.getXmlInputValue(elRevisionData, "DateModified",sddNamespace); + + if (stringDateModified != null) { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss"); + Date d = null; + try { + d = sdf.parse(stringDateModified); + } catch(Exception e) { + System.err.println("Exception :"); + e.printStackTrace(); + } - features.put(idQC, quantitativeCharacter); + DateTime updated = null; + if (d != null) { + updated = new DateTime(d); + sourceReference.setUpdated(updated); + sec.setUpdated(updated); + } + } +} - } catch (Exception e) { - //FIXME - logger.warn("Import of QuantitativeCharacter " + j + " failed."); - success = false; +// imports ipr statements associated with a dataset +protected void importIPRStatements(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig){ + // + logger.info("start IPRStatements ..."); + Element elIPRStatements = elDataset.getChild("IPRStatements",sddNamespace); + // + if (elIPRStatements != null) { + List listIPRStatements = elIPRStatements.getChildren("IPRStatement", sddNamespace); + int j = 0; + //for each IPRStatement + + for (Element elIPRStatement : listIPRStatements){ + + String role = elIPRStatement.getAttributeValue("role"); + // + Element elLabel = elIPRStatement.getChild("Label",sddNamespace); + String lang = ""; + if (elLabel != null) { + lang = elLabel.getAttributeValue("lang",xmlNamespace); + } + String label = (String)ImportHelper.getXmlInputValue(elIPRStatement, "Label",sddNamespace); + + if (role.equals("Copyright")) { + Language iprLanguage = null; + if (lang != null) { + if (!lang.equals("")) { + iprLanguage = getTermService().getLanguageByIso(lang.substring(0, 2)); + //iprLanguage = datasetLanguage; + } else { + iprLanguage = datasetLanguage; } + } + if (iprLanguage == null) { + iprLanguage = datasetLanguage; + } + copyright = Rights.NewInstance(label, iprLanguage); + } + + if (copyright != null) { + sourceReference.addRights(copyright); + sec.addRights(copyright); + } - if ((++j % modCount) == 0){ logger.info("QuantitativeCharacters handled: " + j);} + if ((++j % modCount) == 0){ logger.info("IPRStatements handled: " + j);} + } + } +} + +// imports the taxon names +protected void importTaxonNames(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig){ + // + logger.info("start TaxonNames ..."); + Element elTaxonNames = elDataset.getChild("TaxonNames",sddNamespace); + // + if (elTaxonNames != null) { + List listTaxonNames = elTaxonNames.getChildren("TaxonName", sddNamespace); + int j = 0; + //for each TaxonName + for (Element elTaxonName : listTaxonNames){ + + String id = elTaxonName.getAttributeValue("id"); + String uri = elTaxonName.getAttributeValue("uri"); + + NonViralName tnb = null; + if (!id.equals("")) { + tnb = NonViralName.NewInstance(null); + OriginalSource source = null; + if (uri != null) { + if (!uri.equals("")) { + source = OriginalSource.NewInstance(id, "TaxonName", Generic.NewInstance(), uri); + } + } else { + source = OriginalSource.NewInstance(id, "TaxonName"); } + tnb.addSource(source); + taxonNameBases.put(id,tnb); + } - // - List elTextCharacters = elCharacters.getChildren("TextCharacter", sddNamespace); - j = 0; - //for each TextCharacter - for (Element elTextCharacter : elTextCharacters){ + // + // + importRepresentation(elTaxonName, sddNamespace, tnb, id, sddConfig); - try { + if ((++j % modCount) == 0){ logger.info("TaxonNames handled: " + j);} - String idTC = elTextCharacter.getAttributeValue("id"); - - // - // - // - elRepresentation = elTextCharacter.getChild("Representation",sddNamespace); - Element elLabel = elRepresentation.getChild("Label",sddNamespace); - nameLang = elLabel.getAttributeValue("lang",xmlNamespace); - Language language = null; - if (nameLang != null) { - if (!nameLang.equals("")) { - // language = termService.getLanguageByIso(nameLang); - language = datasetLanguage; - } else { - language = datasetLanguage; - } - } else { - language = datasetLanguage; - } + } + } +} - label = (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace); +// imports the representation (label, detail, lang) of a particular SDD element +protected void importCharacters(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig, boolean success){ + // + logger.info("start Characters ..."); + Element elCharacters = elDataset.getChild("Characters", sddNamespace); - Feature textCharacter = Feature.NewInstance(); + // + if (elCharacters != null) { + List elCategoricalCharacters = elCharacters.getChildren("CategoricalCharacter", sddNamespace); + int j = 0; + //for each CategoricalCharacter + for (Element elCategoricalCharacter : elCategoricalCharacters){ - if (label != null) { - if (!label.equals("")){ - textCharacter = Feature.NewInstance(label, label, label); - textCharacter.setLabel(label, language); - } - } + try { - textCharacter.setSupportsQuantitativeData(false); - textCharacter.setSupportsTextData(true); + String idCC = elCategoricalCharacter.getAttributeValue("id"); - features.put(idTC, textCharacter); + // + // + // - } catch (Exception e) { - //FIXME - logger.warn("Import of TextCharacter " + j + " failed."); - success = false; - } + Feature categoricalCharacter = Feature.NewInstance(); + importRepresentation(elCategoricalCharacter, sddNamespace, categoricalCharacter, idCC, sddConfig); - if ((++j % modCount) == 0){ logger.info("TextCharacters handled: " + j);} + categoricalCharacter.setSupportsCategoricalData(true); - } + // + Element elStates = elCategoricalCharacter.getChild("States",sddNamespace); - } + // + List elStateDefinitions = elStates.getChildren("StateDefinition",sddNamespace); + TermVocabulary termVocabularyState = new TermVocabulary(); - // - logger.info("start CodedDescriptions ..."); - Element elCodedDescriptions = elDataset.getChild("CodedDescriptions",sddNamespace); + int k = 0; + //for each StateDefinition + for (Element elStateDefinition : elStateDefinitions){ - // + if ((++k % modCount) == 0){ logger.info("StateDefinitions handled: " + (k-1));} - Map citations = new HashMap(); - Map locations = new HashMap(); + String idSD = elStateDefinition.getAttributeValue("id"); + // + // + // + // + State state = State.NewInstance(); + importRepresentation(elStateDefinition, sddNamespace, state, idSD, sddConfig); + + StateData stateData = StateData.NewInstance(); + stateData.setState(state); + termVocabularyState.addTerm(state); + stateDatas.put(idSD,stateData); + } - if (elCodedDescriptions != null) { - List listCodedDescriptions = elCodedDescriptions.getChildren("CodedDescription", sddNamespace); - j = 0; - //for each CodedDescription + categoricalCharacter.addSupportedCategoricalEnumeration(termVocabularyState); + features.put(idCC, categoricalCharacter); - for (Element elCodedDescription : listCodedDescriptions){ + } catch (Exception e) { + //FIXME + logger.warn("Import of CategoricalCharacter " + j + " failed."); + success = false; + } - try { + if ((++j % modCount) == 0){ logger.info("CategoricalCharacters handled: " + j);} - String idCD = elCodedDescription.getAttributeValue("id"); - - // - // - // - elRepresentation = elCodedDescription.getChild("Representation",sddNamespace); - Element elLabel = elRepresentation.getChild("Label",sddNamespace); - label = (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace); - - TaxonDescription taxonDescription = TaxonDescription.NewInstance(); - taxonDescription.setTitleCache(label); - OriginalSource source = OriginalSource.NewInstance(idCD, "CodedDescription"); - taxonDescription.addSource(source); - - // - // - // - // - 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); - taxon = Taxon.NewInstance(taxonNameBase, sec); - } + } - String refCitation = ""; - String location = ""; + // + List elQuantitativeCharacters = elCharacters.getChildren("QuantitativeCharacter", sddNamespace); + j = 0; + //for each QuantitativeCharacter + for (Element elQuantitativeCharacter : elQuantitativeCharacters){ + + try { + + String idQC = elQuantitativeCharacter.getAttributeValue("id"); + + // + // + // + Feature quantitativeCharacter = Feature.NewInstance(); + 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); + } - if (elScope != null) { - Element elCitation = elScope.getChild("Citation",sddNamespace); - if (elCitation != null) { - refCitation = elCitation.getAttributeValue("ref"); - location = elCitation.getAttributeValue("location"); - } + 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); + } + } - // - Element elSummaryData = elCodedDescription.getChild("SummaryData",sddNamespace); - - if (elSummaryData != null) { - - // - List elCategoricals = elSummaryData.getChildren("Categorical", sddNamespace); - int k = 0; - //for each Categorical - for (Element elCategorical : elCategoricals){ - if ((++k % modCount) == 0){ logger.info("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); - categoricalData.addState(state); - } - taxonDescription.addElement(categoricalData); - } + if (unit != null) { + units.put(idQC, unit); + } - // - List elQuantitatives = elSummaryData.getChildren("Quantitative", sddNamespace); - k = 0; - //for each Quantitative - for (Element elQuantitative : elQuantitatives){ - if ((++k % modCount) == 0){ logger.info("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); - } - - // - 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"); - value = elMeasure.getAttributeValue("value"); - float v = Float.parseFloat(value); - 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")) { - // Create a new StatisticalMeasure for standard deviation - t = StatisticalMeasure.STANDARD_DEVIATION(); - } else if (type.equals("N")) { - t = StatisticalMeasure.SAMPLE_SIZE(); - } else { - t = StatisticalMeasure.NewInstance(type,type,type); - statisticalMeasures.add(t); - } - - StatisticalMeasurementValue statisticalValue = StatisticalMeasurementValue.NewInstance(); - statisticalValue.setValue(v); - statisticalValue.setType(t); - quantitativeData.addStatisticalValue(statisticalValue); - featureData.add(statisticalValue); - } - taxonDescription.addElement(quantitativeData); - } + // + // 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); + } + } - // - 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); - } + features.put(idQC, quantitativeCharacter); - } + } catch (Exception e) { + //FIXME + logger.warn("Import of QuantitativeCharacter " + j + " failed."); + success = false; + } - if (taxon != null) { - taxon.addDescription(taxonDescription); - } + if ((++j % modCount) == 0){ logger.info("QuantitativeCharacters handled: " + j);} - if (!refCitation.equals("")){ - citations.put(idCD,refCitation); - } + } - if (!location.equals("")){ - locations.put(idCD, location); - } + // + List elTextCharacters = elCharacters.getChildren("TextCharacter", sddNamespace); + j = 0; + //for each TextCharacter + for (Element elTextCharacter : elTextCharacters){ - taxonDescriptions.put(idCD, taxonDescription); + try { - } catch (Exception e) { - //FIXME - logger.warn("Import of CodedDescription " + j + " failed."); - success = false; - } + String idTC = elTextCharacter.getAttributeValue("id"); - if ((++j % modCount) == 0){ logger.info("CodedDescriptions handled: " + j);} + // + // + // + Feature textCharacter = Feature.NewInstance(); + importRepresentation(elTextCharacter, sddNamespace, textCharacter, idTC, sddConfig); - } + textCharacter.setSupportsTextData(true); + features.put(idTC, textCharacter); + + } catch (Exception e) { + //FIXME + logger.warn("Import of TextCharacter " + j + " failed."); + success = false; } - // - logger.info("start Agents ..."); - Element elAgents = elDataset.getChild("Agents",sddNamespace); + if ((++j % modCount) == 0){ logger.info("TextCharacters handled: " + j);} + + } + + } + + for (Iterator f = features.values().iterator() ; f.hasNext() ;){ + featureSet.add(f.next()); + } + +} - // - listAgents = elAgents.getChildren("Agent", sddNamespace); - j = 0; - //for each Agent - for (Element elAgent : listAgents){ +// imports the descriptions of taxa (specimens TODO) +protected void importCodedDescriptions(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig, boolean success){ + // + logger.info("start CodedDescriptions ..."); + Element elCodedDescriptions = elDataset.getChild("CodedDescriptions",sddNamespace); - try { + // - String idA = elAgent.getAttributeValue("id"); + if (elCodedDescriptions != null) { + List listCodedDescriptions = elCodedDescriptions.getChildren("CodedDescription", sddNamespace); + int j = 0; + //for each CodedDescription - // - // - // Ali Baba is also known as r.a.m. - // - elRepresentation = elAgent.getChild("Representation",sddNamespace); - label = (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace); - Element elDetail = elRepresentation.getChild("Detail",sddNamespace); + for (Element elCodedDescription : listCodedDescriptions){ - Person person = Person.NewTitledInstance(label); - OriginalSource source = OriginalSource.NewInstance(idA, "Agent"); - person.addSource(source); + try { - if (elDetail != null) { - String role = elDetail.getAttributeValue("role"); - detail = (String)ImportHelper.getXmlInputValue(elRepresentation, "Detail",sddNamespace); - annotation = Annotation.NewInstance(role + " - " + detail, datasetLanguage); - person.addAnnotation(annotation); - } + String idCD = elCodedDescription.getAttributeValue("id"); - // - Element elLinks = elAgent.getChild("Links",sddNamespace); + // + // + // + TaxonDescription taxonDescription = TaxonDescription.NewInstance(); + importRepresentation(elCodedDescription, sddNamespace, taxonDescription, idCD, sddConfig); - if (elLinks != null) { + // + // + // + // + Element elScope = elCodedDescription.getChild("Scope",sddNamespace); + String ref = ""; + Taxon taxon = null; + if (elScope != null) { + Element elTaxonName = elScope.getChild("TaxonName",sddNamespace); + ref = elTaxonName.getAttributeValue("ref"); - // - List listLinks = elLinks.getChildren("Link", sddNamespace); - int k = 0; - //for each Link - for (Element elLink : listLinks){ + NonViralName taxonNameBase = taxonNameBases.get(ref); + taxon = Taxon.NewInstance(taxonNameBase, sec); + } - try { + String refCitation = ""; + String location = ""; - String rel = elLink.getAttributeValue("rel"); - String href = elLink.getAttributeValue("href"); + if (elScope != null) { + Element elCitation = elScope.getChild("Citation",sddNamespace); + if (elCitation != null) { + refCitation = elCitation.getAttributeValue("ref"); + location = elCitation.getAttributeValue("location"); + } + } - if (k==1) { - source = OriginalSource.NewInstance(rel, href); - person.addSource(source); - } + // + Element elSummaryData = elCodedDescription.getChild("SummaryData",sddNamespace); + + if (elSummaryData != null) { + + // + List elCategoricals = elSummaryData.getChildren("Categorical", sddNamespace); + int k = 0; + //for each Categorical + for (Element elCategorical : elCategoricals){ + if ((++k % modCount) == 0){ logger.info("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"); + StateData stateData = stateDatas.get(ref); + categoricalData.addState(stateData); + } + taxonDescription.addElement(categoricalData); + } - } catch (Exception e) { - //FIXME - logger.warn("Import of Link " + k + " failed."); - success = false; + // + List elQuantitatives = elSummaryData.getChildren("Quantitative", sddNamespace); + k = 0; + //for each Quantitative + for (Element elQuantitative : elQuantitatives){ + if ((++k % modCount) == 0){ logger.info("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 ((++k % modCount) == 0){ logger.info("Links handled: " + k);} + // + 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"); + float v = Float.parseFloat(value); + 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")) { + // Create a new StatisticalMeasure for standard deviation + t = StatisticalMeasure.STANDARD_DEVIATION(); + } else if (type.equals("N")) { + t = StatisticalMeasure.SAMPLE_SIZE(); + } else { + t = StatisticalMeasure.NewInstance(type,type,type); + statisticalMeasures.add(t); + } + StatisticalMeasurementValue statisticalValue = StatisticalMeasurementValue.NewInstance(); + statisticalValue.setValue(v); + statisticalValue.setType(t); + quantitativeData.addStatisticalValue(statisticalValue); + featureData.add(statisticalValue); } - } - if (authors.containsKey(idA)) { - authors.put(idA,person); + taxonDescription.addElement(quantitativeData); } - if (editors.containsKey(idA)) { - editors.put(idA, person); + // + 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); } - } catch (Exception e) { - //FIXME - logger.warn("Import of Agent " + j + " failed."); - success = false; } - if ((++j % modCount) == 0){ logger.info("Agents handled: " + j);} + if (taxon != null) { + taxon.addDescription(taxonDescription); + } + + if (!refCitation.equals("")){ + citations.put(idCD,refCitation); + } + if (!location.equals("")){ + locations.put(idCD, location); + } + + taxonDescription.setDescriptiveSystem(featureSet); + + taxonDescriptions.put(idCD, taxonDescription); + + } catch (Exception e) { + //FIXME + logger.warn("Import of CodedDescription " + j + " failed."); + success = false; } - // - logger.info("start Publications ..."); - Element elPublications = elDataset.getChild("Publications",sddNamespace); + if ((++j % modCount) == 0){ logger.info("CodedDescriptions handled: " + j);} - if (elPublications != null) { - // - List listPublications = elPublications.getChildren("Publication", sddNamespace); - j = 0; - //for each Publication - for (Element elPublication : listPublications){ + } - try { + } +} + +// imports the persons associated with the dataset creation, modification, related publications +protected void importAgents(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig, boolean success){ + // + logger.info("start Agents ..."); + Element elAgents = elDataset.getChild("Agents",sddNamespace); + + // + List listAgents = elAgents.getChildren("Agent", sddNamespace); + int j = 0; + //for each Agent + for (Element elAgent : listAgents){ + + try { + + String idA = elAgent.getAttributeValue("id"); + + // + // + // Ali Baba is also known as r.a.m. + // + Person person = Person.NewInstance(); + importRepresentation(elAgent, sddNamespace, person, idA, sddConfig); + person.addSource(OriginalSource.NewInstance(idA, "Agent")); - String idP = elPublication.getAttributeValue("id"); + // + Element elLinks = elAgent.getChild("Links",sddNamespace); - // - // - // - elRepresentation = elPublication.getChild("Representation",sddNamespace); - label = (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace); - Article publication = Article.NewInstance(); - publication.setTitle(label); - OriginalSource source = OriginalSource.NewInstance(idP, "Publication"); - publication.addSource(source); + if (elLinks != null) { - publications.put(idP,publication); + // + List listLinks = elLinks.getChildren("Link", sddNamespace); + int k = 0; + //for each Link + for (Element elLink : listLinks){ + + try { + + String rel = elLink.getAttributeValue("rel"); + String href = elLink.getAttributeValue("href"); + + Media link = Media.NewInstance(); + MediaRepresentation mr = MediaRepresentation.NewInstance(); + mr.addRepresentationPart(MediaRepresentationPart.NewInstance(href, null)); + link.addRepresentation(mr); + person.addMedia(link); } catch (Exception e) { //FIXME - logger.warn("Import of Publication " + j + " failed."); + logger.warn("Import of Link " + k + " failed."); success = false; } - if ((++j % modCount) == 0){ logger.info("Publications handled: " + j);} + if ((++k % modCount) == 0){ logger.info("Links handled: " + k);} } } + if (authors.containsKey(idA)) { + authors.put(idA,person); + } - // - logger.info("start MediaObjects ..."); - Element elMediaObjects = elDataset.getChild("MediaObjects",sddNamespace); + if (editors.containsKey(idA)) { + editors.put(idA, person); + } - if (elMediaObjects != null) { - // - List listMediaObjects = elMediaObjects.getChildren("elMediaObject", sddNamespace); - j = 0; - //for each Publication - for (Element elMediaObject : listMediaObjects){ + } catch (Exception e) { + //FIXME + logger.warn("Import of Agent " + j + " failed."); + success = false; + } - try { + if ((++j % modCount) == 0){ logger.info("Agents handled: " + j);} - String idMO = elMediaObject.getAttributeValue("id"); + } +} - // - // - // - elRepresentation = elMediaObject.getChild("Representation",sddNamespace); - label = (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace); - Media mediaObject = Media.NewInstance(); - MultilanguageText m = MultilanguageText.NewInstance(LanguageString.NewInstance(label, datasetLanguage)); - mediaObject.setTitle(m); - OriginalSource source = OriginalSource.NewInstance(idMO, "MediaObject"); - // mediaObject.addSource(source); +// imports publications related with the data set +protected void importPublications(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig, boolean success){ + // + logger.info("start Publications ..."); + Element elPublications = elDataset.getChild("Publications",sddNamespace); - mediaObjects.put(idMO,mediaObject); + if (elPublications != null) { + // + List listPublications = elPublications.getChildren("Publication", sddNamespace); + int j = 0; + //for each Publication + for (Element elPublication : listPublications){ - } catch (Exception e) { - //FIXME - logger.warn("Import of MediaObject " + j + " failed."); - success = false; - } + try { - if ((++j % modCount) == 0){ logger.info("Publications handled: " + j);} + String idP = elPublication.getAttributeValue("id"); - } + // + // + // + Article publication = Article.NewInstance(); + importRepresentation(elPublication, sddNamespace, publication, idP, sddConfig); + + publications.put(idP,publication); + + } catch (Exception e) { + //FIXME + logger.warn("Import of Publication " + j + " failed."); + success = false; } - /* - // when several authors are supported - if (authors.size()>1) { - for (Iterator author = authors.values().iterator() ; author.hasNext() ;){ - sourceReference.setCreatedBy(author.next()); + if ((++j % modCount) == 0){ logger.info("Publications handled: " + j);} + + } + } +} + +// imports media objects such as images +protected void importMediaObjects(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig, boolean success){ + // + logger.info("start MediaObjects ..."); + Element elMediaObjects = elDataset.getChild("MediaObjects",sddNamespace); + + if (elMediaObjects != null) { + // + List listMediaObjects = elMediaObjects.getChildren("MediaObject", sddNamespace); + int j = 0; + //for each Publication + for (Element elMO : listMediaObjects){ + + String id = ""; + + try { + + String idMO = elMO.getAttributeValue("id"); + id = idMO; + + // + // + // + Media media = Media.NewInstance(); + importRepresentation(elMO, sddNamespace, media, idMO, sddConfig); + + // Image + // + String type = (String)ImportHelper.getXmlInputValue(elMO,"Type",sddNamespace); + + if ((type != null) && (type.equals("Image"))) { + Element elSource = elMO.getChild("Source",sddNamespace); + String href = elSource.getAttributeValue("href"); + + ImageMetaData imageMetaData = new ImageMetaData(); + ImageFile image = null; + + if (href.substring(0,7).equals("http://")) { + try{ + URL url = new URL(href); + imageMetaData.readFrom(url); + image = ImageFile.NewInstance(url.toString(), null, imageMetaData); + } catch (MalformedURLException e) { + logger.error("Malformed URL", e); + } + } else { + String sns = sddConfig.getSourceNameString(); + File f = new File(sns); + File parent = f.getParentFile(); + String fi = parent.toString() + File.separator + href; + File file = new File(fi); + imageMetaData.readFrom(file); + image = ImageFile.NewInstance(file.toString(), null, imageMetaData); } - } else { - Team team = Team.NewInstance(); - for (Iterator author = authors.values().iterator() ; author.hasNext() ;){ - team.addTeamMember(author.next()); + + MediaRepresentation representation = MediaRepresentation.NewInstance(imageMetaData.getMimeType(), null); + representation.addRepresentationPart(image); + + media.addRepresentation(representation); + + ArrayList lcb = (ArrayList) mediaObject_ListCdmBase.get(idMO); + if (lcb != null) { + for (int k = 0; k < lcb.size(); k++) { + if (lcb.get(k) instanceof DefinedTermBase) { + DefinedTermBase dtb = (DefinedTermBase) lcb.get(k); + // if (lcb.get(0) instanceof DefinedTermBase) { + // DefinedTermBase dtb = (DefinedTermBase) lcb.get(0); + // if (dtb!=null) { + // if (k == 0) { + dtb.addMedia(media); + // } else { + // Media me = (Media) media.clone(); + // dtb.addMedia(me); + // } + // } + } else if (lcb.get(k) instanceof ReferenceBase) { + ReferenceBase rb = (ReferenceBase) lcb.get(k); + //} else if (lcb.get(0) instanceof ReferenceBase) { + //ReferenceBase rb = (ReferenceBase) lcb.get(0); + // rb.setTitleCache(label); + // if (rb!=null) { + // if (k == 0) { + rb.addMedia(media); + // } else { + // Media me = (Media) media.clone(); + // rb.addMedia(me); + // } + // } + } + } } - sourceReference.setCreatedBy(team); } - */ - Iterator author = authors.values().iterator(); - if (author.hasNext()){ - Person a = author.next(); - sourceReference.setCreatedBy(a); - sec.setCreatedBy(a); + } catch (Exception e) { + //FIXME + logger.warn("Could not attached MediaObject " + j + "(SDD: " + id + ") to several objects."); + success = false; } - Iterator editor = editors.values().iterator(); - if (editor.hasNext()){ - Person e = editor.next(); - sourceReference.setUpdatedBy(e); - sec.setUpdatedBy(e); - } + if ((++j % modCount) == 0){ logger.info("MediaObjects handled: " + j); - if (copyright != null) { - sourceReference.addRights(copyright); - sec.addRights(copyright); } + } + } +} - for (Iterator refCD = taxonDescriptions.keySet().iterator() ; refCD.hasNext() ;){ - String ref = refCD.next(); - TaxonDescription td = taxonDescriptions.get(ref); - if (citations.containsKey(ref)) { - Article publication = (Article) publications.get(citations.get(ref)); - if (locations.containsKey(ref)) { - publication.addAnnotation(Annotation.NewInstance(locations.get(ref), datasetLanguage)); - } - td.addDescriptionSource(publication); - } +// imports the block +protected void importDescriptiveConcepts(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig){ + // + logger.info("start DescriptiveConcepts ..."); + Element elDescriptiveConcepts = elDataset.getChild("DescriptiveConcepts",sddNamespace); + // + if (elDescriptiveConcepts != null) { + List listDescriptiveConcepts = elDescriptiveConcepts.getChildren("DescriptiveConcept", sddNamespace); + int j = 0; + //for each DescriptiveConcept + int g = 1; + for (Element elDescriptiveConcept : listDescriptiveConcepts){ + + String id = elDescriptiveConcept.getAttributeValue("id"); + String uri = elDescriptiveConcept.getAttributeValue("uri"); + + FeatureNode fn = null; + + if (!id.equals("")) { + fn = FeatureNode.NewInstance(); + Feature feature = Feature.NewInstance(); + // + // + importRepresentation(elDescriptiveConcept, sddNamespace, feature, id, sddConfig); + features.put("g" + g, feature); + g++; + fn.setFeature(feature); + + // TODO if an OriginalSource can be attached to a FeatureNode or a Feature + // OriginalSource source = null; + // if (uri != null) { + // if (!uri.equals("")) { + // source = OriginalSource.NewInstance(id, "DescriptiveConcept", Generic.NewInstance(), uri); + // } + // } else { + // source = OriginalSource.NewInstance(id, "DescriptiveConcept"); + // } + // fn.addSource(source); + + featureNodes.put(id,fn); } - if ((++i % modCount) == 0){ logger.info("Datasets handled: " + i);} - - config.setSourceReference(sourceReference); + if ((++j % modCount) == 0){ logger.info("DescriptiveConcepts handled: " + j);} } - logger.info(i + " Datasets handled"); - - TransactionStatus ts = config.getCdmAppController().startTransaction(); - - ITermService termService = config.getCdmAppController().getTermService(); - for (Iterator k = states.values().iterator() ; k.hasNext() ;){ - State state = k.next(); - termService.saveTerm(state); - } - for (Iterator k = features.values().iterator() ; k.hasNext() ;){ - Feature feature = k.next(); - termService.saveTerm(feature); - } - if (units != null) { - for (Iterator k = units.values().iterator() ; k.hasNext() ;){ - MeasurementUnit unit = k.next(); - if (unit != null) { - termService.saveTerm(unit); - } - } - } - for (Iterator k = statisticalMeasures.iterator() ; k.hasNext() ;) { - StatisticalMeasure sm = k.next(); - termService.saveTerm(sm); - } + } +} - IReferenceService referenceService = config.getCdmAppController().getReferenceService(); +// imports the descriptions of taxa (specimens TODO) +protected void importCharacterTrees(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig, boolean success){ + // + logger.info("start CharacterTrees ..."); + Element elCharacterTrees = elDataset.getChild("CharacterTrees",sddNamespace); - for (Iterator k = publications.values().iterator() ; k.hasNext() ;){ - Article publication = (Article) k.next(); - referenceService.saveReference(publication); - } + // - // Returns a CdmApplicationController created by the values of this configuration. - IDescriptionService descriptionService = config.getCdmAppController().getDescriptionService(); + if (elCharacterTrees != null) { + List listCharacterTrees = elCharacterTrees.getChildren("CharacterTree", sddNamespace); + int j = 0; + //for each CharacterTree - for (Iterator k = taxonDescriptions.values().iterator() ; k.hasNext() ;){ - TaxonDescription taxonDescription = k.next(); - // Persists a Description - descriptionService.saveDescription(taxonDescription); - } + for (Element elCharacterTree : listCharacterTrees){ - config.getCdmAppController().commitTransaction(ts); + try { + Element elRepresentation = elCharacterTree.getChild("Representation",sddNamespace); + String label = (String)ImportHelper.getXmlInputValue(elRepresentation,"Label",sddNamespace); + Element elDesignedFor = elCharacterTree.getChild("DesignedFor",sddNamespace); + List listRoles = elDesignedFor.getChildren("Role",sddNamespace); + boolean isgroups = false; - // makeNameSpecificData(nameMap); - logger.info("end makeTaxonDescriptions ..."); - return success; + for (Element elRole : listRoles){ + if (elRole.getText().equals("Filtering")) { + isgroups = true; + } + } + + if ((label.contains("group")) || (isgroups)) { + + FeatureTree groups = FeatureTree.NewInstance(); + importRepresentation(elCharacterTree, sddNamespace, groups, "", sddConfig); + FeatureNode root = groups.getRoot(); + + Element elNodes = elCharacterTree.getChild("Nodes", sddNamespace); + List listNodes = elNodes.getChildren("Node", sddNamespace); + for (Element elNode : listNodes){ + String idN = elNode.getAttributeValue("id"); + FeatureNode fn = null; + if (!idN.equals("")) { + Element elDescriptiveConcept = elNode.getChild("DescriptiveConcept", sddNamespace); + String refDC = elDescriptiveConcept.getAttributeValue("ref"); + fn = featureNodes.get(refDC); + root.addChild(fn); + } + nodes.put(idN, fn); + } + + List listCharNodes = elNodes.getChildren("CharNode", sddNamespace); + for (Element elCharNode : listCharNodes){ + Element elParent = elCharNode.getChild("Parent", sddNamespace); + String refP = elParent.getAttributeValue("ref"); + Element elCharacter = elCharNode.getChild("Character", sddNamespace); + String refC = elCharacter.getAttributeValue("ref"); + FeatureNode fn = FeatureNode.NewInstance(); + if (!refP.equals("")) { + FeatureNode parent = nodes.get(refP); + parent.addChild(fn); + Feature character = features.get(refC); + fn.setFeature(character); + // if method setParent() in FeatureNode becomes visible + // fn.setParent(parent); + } + nodes.put(refC, fn); + } + featureTrees.add(groups); + } - } - /* (non-Javadoc) - * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator) - */ - protected boolean isIgnore(IImportConfigurator config){ - return false; - } + } catch (Exception e) { + //FIXME + logger.warn("Import of Character tree " + j + " failed."); + success = false; + } + if ((++j % modCount) == 0){ logger.info("CharacterTrees handled: " + j);} + + } + + } +} }