X-Git-Url: https://dev.e-taxonomy.eu/gitweb/cdmlib.git/blobdiff_plain/cdbae74d7c34cc66ea1dcfd84c4981cd15095b68..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 d99e455058..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,460 +26,897 @@ 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.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 = 10; + 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"); - 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); + importTechnicalMetadata(root, sddNamespace, sddConfig); + List elDatasets = root.getChildren("Dataset",sddNamespace); + int i = 0; - GregorianCalendar updated = null; + //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; + } - // - Element elGenerator = elTechnicalMetadata.getChild("Generator", sddNamespace); - String generatorName = elGenerator.getAttributeValue("name"); - String generatorVersion = elGenerator.getAttributeValue("version"); + /* (non-Javadoc) + * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator) + */ + protected boolean isIgnore(SDDImportState state){ + return false; + } - 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(); + // 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); + } + } + } + } - Set statisticalMeasures = new HashSet(); - Set featureData = new HashSet(); + // 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); + } - NonViralName taxonName = null; - Taxon taxon = Taxon.NewInstance(taxonName, null); - Rights copyright = null; + List listMediaObjects = elRepresentation.getChildren("MediaObject",sddNamespace); - int i = 0; - //for each Dataset - for (Element elDataset : elDatasets){ + 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); + } + } + } + } + + // 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); + } - if ((++i % modCount) == 0){ logger.info("Datasets handled: " + (i-1));} + 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 nameLang = elDataset.getAttributeValue("lang",xmlNamespace); - Language datasetLanguage = Language.NewInstance(); - if (!nameLang.equals("")) { - if(nameLang.equals("en-us")) { - datasetLanguage = Language.ENGLISH(); + 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(nameLang.equals("en-au")) { - datasetLanguage = Language.ENGLISH(); + } 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); } - /* - - 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 = elDataset.getChild("Representation",sddNamespace); - String label = (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace); - String detail = (String)ImportHelper.getXmlInputValue(elRepresentation, "Detail",sddNamespace); + ve = ie; - OriginalSource originalSource = OriginalSource.NewInstance(" ", generatorName + " - " + generatorVersion + " - " + label); -// originalSource.setCitation(citation) - Annotation annotation = Annotation.NewInstance(detail, datasetLanguage); + } else if (ve instanceof TermBase) { + TermBase tb = (TermBase) ve; - // - Element elRevisionData = elDataset.getChild("RevisionData",sddNamespace); - - // - Element elCreators = elRevisionData.getChild("Creators",sddNamespace); - - // - List listAgents = elCreators.getChildren("Agent", sddNamespace); - - int j = 0; - //for each Agent - Map authors = new HashMap(); - Map editors = new HashMap(); - for (Element elAgent : listAgents){ - if ((++j % modCount) == 0){ logger.info("Agents handled: " + (j-1));} - String role = elAgent.getAttributeValue("role"); - String ref = elAgent.getAttributeValue("ref"); - if (role.equals("aut")) { - if(!ref.equals("")) { - authors.put(ref, null); + 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)); } } - if (role.equals("edt")) { - if(!ref.equals("")) { - editors.put(ref, null); - } + ve = tb; + } + + } else if (ve instanceof Media) { + Media m = (Media) ve; + + 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)); + } + if (labDet.size()>1) { + m.addDescription(labDet.get(1), lang); } + ve = m; } - // 2006-04-08T00:00:00 - String stringDateModified = (String)ImportHelper.getXmlInputValue(elRevisionData, "DateModified",sddNamespace); + } - 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(); - } - - updated = new java.util.GregorianCalendar(); - updated.setTime(d); - - // - Element elIPRStatements = elDataset.getChild("IPRStatements",sddNamespace); - // - List listIPRStatements = elIPRStatements.getChildren("IPRStatement", sddNamespace); - j = 0; - //for each IPRStatement - for (Element elIPRStatement : listIPRStatements){ - if ((++j % modCount) == 0){ logger.info("IPRStatements handled: " + (j-1));} - String role = elIPRStatement.getAttributeValue("role"); - // - Element elLabel = elIPRStatement.getChild("Label",sddNamespace); - String lang = elLabel.getAttributeValue("lang",xmlNamespace); - label = (String)ImportHelper.getXmlInputValue(elIPRStatement, "Label",sddNamespace); - if (role.equals("Copyright")) { - Language iprLanguage = Language.NewInstance(); - if (!lang.equals("")) { - if(lang.equals("en-us")) { - iprLanguage = Language.ENGLISH(); - } - if(lang.equals("en-au")) { - iprLanguage = Language.ENGLISH(); + 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 { + ReferenceBase descriptionSource = Generic.NewInstance(); + td.addDescriptionSource(descriptionSource); + this.associateImageWithCdmBase(ref,descriptionSource); + } + } else { + this.associateImageWithCdmBase(ref,ve); } } - copyright = Rights.NewInstance(label, iprLanguage); + } } + } - // - Element elTaxonNames = elDataset.getChild("TaxonNames",sddNamespace); - // - List listTaxonNames = elTaxonNames.getChildren("TaxonName", sddNamespace); - j = 0; - Map taxonNameBases = new HashMap(); - //for each TaxonName - for (Element elTaxonName : listTaxonNames){ - if ((++j % modCount) == 0){ logger.info("TaxonNames handled: " + (j-1));} - 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); - tnb.setCreated(created); - tnb.setUpdated(updated); - if ((lang.equals("la")) || (lang.equals(""))) { - tnb.setTitleCache(label); - } - if (!id.equals("")) { - taxonNameBases.put(id,tnb); + } + + // 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); + } + } + + // + Element elGenerator = elTechnicalMetadata.getChild("Generator", sddNamespace); + generatorName = elGenerator.getAttributeValue("name"); + generatorVersion = elGenerator.getAttributeValue("version"); + + sec.addAnnotation(Annotation.NewDefaultLanguageInstance(generatorName + " - " + generatorVersion)); + sourceReference.addAnnotation(Annotation.NewDefaultLanguageInstance(generatorName + " - " + generatorVersion)); + + } + + // 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); + } + + 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 (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); + 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 ..."); + + sddConfig.setSourceReference(sourceReference); + //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); + } - // - Element elCharacters = elDataset.getChild("Characters", sddNamespace); + IReferenceService referenceService = getReferenceService(); + // referenceService.saveReference(sourceReference); + for (Iterator k = publications.values().iterator() ; k.hasNext() ;){ + Article publication = (Article) k.next(); + referenceService.saveReference(publication); + } - // - List elCategoricalCharacters = elCharacters.getChildren("CategoricalCharacter", sddNamespace); - j = 0; - //for each CategoricalCharacter - for (Element elCategoricalCharacter : elCategoricalCharacters){ + 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); + } - if ((++j % modCount) == 0){ logger.info("CategoricalCharacters handled: " + (j-1));} +// descriptionService.saveFeatureNodeAll(featureNodes.values()); + + for (Iterator k = featureTrees.iterator() ; k.hasNext() ;) { + FeatureTree tree = k.next(); + descriptionService.saveFeatureTree(tree); + } - try { +} - String idCC = elCategoricalCharacter.getAttributeValue("id"); +// imports the default language of the dataset +protected void importDatasetLanguage(Element elDataset, SDDImportConfigurator sddConfig){ + String nameLang = elDataset.getAttributeValue("lang",xmlNamespace); - // - // - // - elRepresentation = elCategoricalCharacter.getChild("Representation",sddNamespace); - label = (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace); + if (!nameLang.equals("")) { + String iso = nameLang.substring(0, 2); + datasetLanguage = getTermService().getLanguageByIso(iso); + } else { + datasetLanguage = Language.ENGLISH(); + } + if (datasetLanguage == null) { + datasetLanguage = Language.ENGLISH(); + } +} - Feature categoricalCharacter = null; - if (label != null){ - categoricalCharacter = Feature.NewInstance(label, label, label); - } - categoricalCharacter.setSupportsQuantitativeData(false); - categoricalCharacter.setSupportsTextData(true); +// 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); - // - Element elStates = elCategoricalCharacter.getChild("States",sddNamespace); + // + Element elCreators = elRevisionData.getChild("Creators",sddNamespace); - // - List elStateDefinitions = elStates.getChildren("StateDefinition",sddNamespace); - TermVocabulary termVocabularyState = new TermVocabulary(); + // + List listAgents = elCreators.getChildren("Agent", sddNamespace); - 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); - } + int j = 0; + //for each Agent + for (Element elAgent : listAgents){ + + 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); + } + } + + if ((++j % modCount) == 0){ logger.info("Agents handled: " + j);} - categoricalCharacter.addSupportedCategoricalEnumeration(termVocabularyState); - features.put(idCC, categoricalCharacter); + } + + // 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(); + } + + DateTime updated = null; + if (d != null) { + updated = new DateTime(d); + sourceReference.setUpdated(updated); + sec.setUpdated(updated); + } + } +} - } catch (Exception e) { - //FIXME - logger.warn("Import of CategoricalCharacter " + 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); } - // - List elQuantitativeCharacters = elCharacters.getChildren("QuantitativeCharacter", sddNamespace); - j = 0; - //for each QuantitativeCharacter - for (Element elQuantitativeCharacter : elQuantitativeCharacters){ + 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); + } + + // + // + importRepresentation(elTaxonName, sddNamespace, tnb, id, sddConfig); + + if ((++j % modCount) == 0){ logger.info("TaxonNames handled: " + j);} + + } + } +} + +// 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); - if ((++j % modCount) == 0){ logger.info("QuantitativeCharacters handled: " + (j-1));} + // + if (elCharacters != null) { + List elCategoricalCharacters = elCharacters.getChildren("CategoricalCharacter", sddNamespace); + int j = 0; + //for each CategoricalCharacter + for (Element elCategoricalCharacter : elCategoricalCharacters){ - try { + try { + + String idCC = elCategoricalCharacter.getAttributeValue("id"); + + // + // + // + + Feature categoricalCharacter = Feature.NewInstance(); + importRepresentation(elCategoricalCharacter, sddNamespace, categoricalCharacter, idCC, sddConfig); - String idQC = elQuantitativeCharacter.getAttributeValue("id"); + categoricalCharacter.setSupportsCategoricalData(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 = elQuantitativeCharacter.getChild("Representation",sddNamespace); - label = (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace); + State state = State.NewInstance(); + importRepresentation(elStateDefinition, sddNamespace, state, idSD, sddConfig); + + StateData stateData = StateData.NewInstance(); + stateData.setState(state); + termVocabularyState.addTerm(state); + stateDatas.put(idSD,stateData); + } - Feature quantitativeCharacter = Feature.NewInstance(); + categoricalCharacter.addSupportedCategoricalEnumeration(termVocabularyState); + features.put(idCC, categoricalCharacter); - if (!label.equals("")){ - quantitativeCharacter = Feature.NewInstance(label, label, label); - } - quantitativeCharacter.setSupportsQuantitativeData(true); - quantitativeCharacter.setSupportsTextData(false); + } catch (Exception e) { + //FIXME + logger.warn("Import of CategoricalCharacter " + j + " failed."); + success = false; + } + + if ((++j % modCount) == 0){ logger.info("CategoricalCharacters handled: " + j);} + + } + + // + List elQuantitativeCharacters = elCharacters.getChildren("QuantitativeCharacter", sddNamespace); + j = 0; + //for each QuantitativeCharacter + for (Element elQuantitativeCharacter : elQuantitativeCharacters){ + + try { + + String idQC = elQuantitativeCharacter.getAttributeValue("id"); - // - // - // - Element elMeasurementUnit = elQuantitativeCharacter.getChild("MeasurementUnit",sddNamespace); + // + // + // + 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); - String role = elLabel.getAttributeValue("role"); + role = elLabel.getAttributeValue("role"); label = (String)ImportHelper.getXmlInputValue(elMeasurementUnit, "Label",sddNamespace); + } - MeasurementUnit unit = null; - if (!label.equals("")){ + 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); } + } else { + unit = MeasurementUnit.NewInstance(label,label,label); } + } + if (unit != null) { units.put(idQC, unit); + } - // - // milli - // - Element elDefault = elQuantitativeCharacter.getChild("Default",sddNamespace); + // + // 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); } - - features.put(idQC, quantitativeCharacter); - - } catch (Exception e) { - //FIXME - logger.warn("Import of QuantitativeCharacter " + j + " failed."); - success = false; } + 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);} - if ((++j % modCount) == 0){ logger.info("TextCharacters handled: " + (j-1));} + } - try { + // + List elTextCharacters = elCharacters.getChildren("TextCharacter", sddNamespace); + j = 0; + //for each TextCharacter + for (Element elTextCharacter : elTextCharacters){ - String idTC = elTextCharacter.getAttributeValue("id"); + try { - // - // - // - elRepresentation = elTextCharacter.getChild("Representation",sddNamespace); - Element elLabel = elRepresentation.getChild("Label",sddNamespace); - nameLang = elLabel.getAttributeValue("lang",xmlNamespace); - Language language = Language.NewInstance(); - if (!nameLang.equals("")) { - if(nameLang.equals("en")) { - language = Language.ENGLISH(); - } - } - label = (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace); + String idTC = elTextCharacter.getAttributeValue("id"); - Feature textCharacter = Feature.NewInstance(); + // + // + // + Feature textCharacter = Feature.NewInstance(); + importRepresentation(elTextCharacter, sddNamespace, textCharacter, idTC, sddConfig); - if (!idTC.equals("")){ - textCharacter = Feature.NewInstance(label, label, label); - } - textCharacter.setSupportsQuantitativeData(false); - textCharacter.setSupportsTextData(true); - textCharacter.setLabel(label, language); - - Map textCharacters = new HashMap(); - Map textCharactersLang = new HashMap(); - if ((!idTC.equals("")) && (!label.equals(""))){ - textCharacters.put(idTC, label); - } - if (!idTC.equals("")){ - textCharactersLang.put(idTC, language); - } + textCharacter.setSupportsTextData(true); - features.put(idTC, textCharacter); - - } catch (Exception e) { - //FIXME - logger.warn("Import of TextCharacter " + j + " failed."); - success = false; - } + features.put(idTC, textCharacter); + } catch (Exception e) { + //FIXME + logger.warn("Import of TextCharacter " + j + " failed."); + success = false; } - // - Element elCodedDescriptions = elDataset.getChild("CodedDescriptions",sddNamespace); + if ((++j % modCount) == 0){ logger.info("TextCharacters handled: " + j);} + + } + + } + + for (Iterator f = features.values().iterator() ; f.hasNext() ;){ + featureSet.add(f.next()); + } + +} - // - List listCodedDescriptions = elCodedDescriptions.getChildren("CodedDescription", sddNamespace); - j = 0; - //for each CodedDescription - Map citations = new HashMap(); - Map locations = new HashMap(); - for (Element elCodedDescription : listCodedDescriptions){ +// 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); - if ((++j % modCount) == 0){ logger.info("CodedDescriptions handled: " + (j-1));} + // - try { + if (elCodedDescriptions != null) { + List listCodedDescriptions = elCodedDescriptions.getChildren("CodedDescription", sddNamespace); + int j = 0; + //for each CodedDescription - String idCD = elCodedDescription.getAttributeValue("id"); + for (Element elCodedDescription : listCodedDescriptions){ - // - // - // - 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); - - // - // - // - // - Element elScope = elCodedDescription.getChild("Scope",sddNamespace); + 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); - String ref = elTaxonName.getAttributeValue("ref"); + ref = elTaxonName.getAttributeValue("ref"); NonViralName taxonNameBase = taxonNameBases.get(ref); - taxon = Taxon.NewInstance(taxonNameBase, null); - taxon.setCreated(created); - taxon.setUpdated(updated); + taxon = Taxon.NewInstance(taxonNameBase, sec); + } + + String refCitation = ""; + String location = ""; + if (elScope != null) { Element elCitation = elScope.getChild("Citation",sddNamespace); - String refCitation = elCitation.getAttributeValue("ref"); - String location = elCitation.getAttributeValue("location"); + if (elCitation != null) { + refCitation = elCitation.getAttributeValue("ref"); + location = elCitation.getAttributeValue("location"); + } + } + + // + Element elSummaryData = elCodedDescription.getChild("SummaryData",sddNamespace); - // - Element elSummaryData = elCodedDescription.getChild("SummaryData",sddNamespace); + if (elSummaryData != null) { // List elCategoricals = elSummaryData.getChildren("Categorical", sddNamespace); @@ -487,8 +936,8 @@ public class SDDDescriptionIO extends SDDIoBase implements ICdmIO { 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); + StateData stateData = stateDatas.get(ref); + categoricalData.addState(stateData); } taxonDescription.addElement(categoricalData); } @@ -506,10 +955,14 @@ public class SDDDescriptionIO extends SDDIoBase implements ICdmIO { MeasurementUnit unit = units.get(ref); String prefix = defaultUnitPrefixes.get(ref); - String u = unit.getLabel(); - u = prefix + u; - unit.setLabel(u); - quantitativeData.setUnit(unit); + 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); @@ -518,7 +971,7 @@ public class SDDDescriptionIO extends SDDIoBase implements ICdmIO { for (Element elMeasure : elMeasures){ if ((++l % modCount) == 0){ logger.info("States handled: " + (l-1));} String type = elMeasure.getAttributeValue("type"); - value = elMeasure.getAttributeValue("value"); + String value = elMeasure.getAttributeValue("value"); float v = Float.parseFloat(value); StatisticalMeasure t = null; if (type.equals("Min")) { @@ -563,249 +1016,381 @@ public class SDDDescriptionIO extends SDDIoBase implements ICdmIO { taxonDescription.addElement(textData); } - taxonDescription.addSource(originalSource); - taxon.addDescription(taxonDescription); - - if (!ref.equals("")){ - citations.put(idCD,refCitation); - } + } - if (!location.equals("")){ - locations.put(idCD, location); - } + if (taxon != null) { + taxon.addDescription(taxonDescription); + } - taxonDescriptions.put(idCD, taxonDescription); + if (!refCitation.equals("")){ + citations.put(idCD,refCitation); + } - } catch (Exception e) { - //FIXME - logger.warn("Import of CodedDescription " + j + " failed."); - success = false; + 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; } - // - Element elAgents = elDataset.getChild("Agents",sddNamespace); + if ((++j % modCount) == 0){ logger.info("CodedDescriptions handled: " + j);} - // - listAgents = elAgents.getChildren("Agent", sddNamespace); - j = 0; - //for each Agent - for (Element elAgent : listAgents){ + } - if ((++j % modCount) == 0){ logger.info("elAgent handled: " + (j-1));} + } +} - 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); - String idA = elAgent.getAttributeValue("id"); + // + List listAgents = elAgents.getChildren("Agent", sddNamespace); + int j = 0; + //for each Agent + for (Element elAgent : listAgents){ - // - // - // 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); + try { - Person person = Person.NewTitledInstance(label); - - // PROBLEME - - String role = elDetail.getAttributeValue("role"); - detail = (String)ImportHelper.getXmlInputValue(elRepresentation, "Detail",sddNamespace); - annotation = Annotation.NewInstance(role + " - " + detail, datasetLanguage); - person.addAnnotation(annotation); - - person.setCreated(created); - person.setUpdated(updated); - + String idA = elAgent.getAttributeValue("id"); - // - Element elLinks = elAgent.getChild("Links",sddNamespace); + // + // + // 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")); - // - List listLinks = elLinks.getChildren("Link", sddNamespace); - int k = 0; - //for each Link - for (Element elLink : listLinks){ + // + Element elLinks = elAgent.getChild("Links",sddNamespace); - if ((++k % modCount) == 0){ logger.info("elLink handled: " + (k-1));} + if (elLinks != null) { - try { + // + List listLinks = elLinks.getChildren("Link", sddNamespace); + int k = 0; + //for each Link + for (Element elLink : listLinks){ - String rel = elLink.getAttributeValue("rel"); - String href = elLink.getAttributeValue("href"); - - if (k==1) { - OriginalSource source = OriginalSource.NewInstance(rel, href); - person.addSource(source); - } - - } catch (Exception e) { - //FIXME - logger.warn("Import of Link " + k + " failed."); - success = false; - } + try { + String rel = elLink.getAttributeValue("rel"); + String href = elLink.getAttributeValue("href"); - } - - if (authors.containsKey(idA)) { - authors.put(idA,person); - } + Media link = Media.NewInstance(); + MediaRepresentation mr = MediaRepresentation.NewInstance(); + mr.addRepresentationPart(MediaRepresentationPart.NewInstance(href, null)); + link.addRepresentation(mr); + person.addMedia(link); - if (editors.containsKey(idA)) { - editors.put(idA, person); + } catch (Exception e) { + //FIXME + logger.warn("Import of Link " + k + " failed."); + success = false; } - } catch (Exception e) { - //FIXME - logger.warn("Import of Agent " + j + " failed."); - success = false; - } + if ((++k % modCount) == 0){ logger.info("Links handled: " + k);} + } + } + if (authors.containsKey(idA)) { + authors.put(idA,person); + } + if (editors.containsKey(idA)) { + editors.put(idA, person); } - // - Element elPublications = elDataset.getChild("Publications",sddNamespace); + } catch (Exception e) { + //FIXME + logger.warn("Import of Agent " + j + " failed."); + success = false; + } + + if ((++j % modCount) == 0){ logger.info("Agents handled: " + j);} - // - List listPublications = elPublications.getChildren("Publication", sddNamespace); - j = 0; - //for each Publication - for (Element elPublication : listPublications){ + } +} - if ((++j % modCount) == 0){ logger.info("elAgent handled: " + (j-1));} +// 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); - try { + if (elPublications != null) { + // + List listPublications = elPublications.getChildren("Publication", sddNamespace); + int j = 0; + //for each Publication + for (Element elPublication : listPublications){ - String idP = elPublication.getAttributeValue("id"); + try { - // - // - // - elRepresentation = elPublication.getChild("Representation",sddNamespace); - label = (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace); - Article publication = Article.NewInstance(); - publication.setTitle(label); + String idP = elPublication.getAttributeValue("id"); - publications.put(idP,publication); + // + // + // + Article publication = Article.NewInstance(); + importRepresentation(elPublication, sddNamespace, publication, idP, sddConfig); - } catch (Exception e) { - //FIXME - logger.warn("Import of Agent " + j + " failed."); - success = false; - } + publications.put(idP,publication); + } catch (Exception e) { + //FIXME + logger.warn("Import of Publication " + j + " failed."); + success = false; } - for (Iterator taxonDescription = taxonDescriptions.values().iterator() ; taxonDescription.hasNext() ;){ - TaxonDescription td = taxonDescription.next(); - /* - if (authors.size()>1) { - for (Iterator author = authors.values().iterator() ; author.hasNext() ;){ - td.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); + // } + // } + } + } } - td.setCreatedBy(team); } - */ - Iterator author = authors.values().iterator(); - if (author.hasNext()){ - td.setCreatedBy(author.next()); - } + } catch (Exception e) { + //FIXME + logger.warn("Could not attached MediaObject " + j + "(SDD: " + id + ") to several objects."); + success = false; + } + + if ((++j % modCount) == 0){ logger.info("MediaObjects handled: " + j); - Iterator editor = editors.values().iterator(); - if (editor.hasNext()){ - td.setUpdatedBy(editor.next()); - } - - if (copyright != null) { - td.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)); +// 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 ((++j % modCount) == 0){ logger.info("DescriptiveConcepts handled: " + j);} + + } + } +} + +// 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); + + // + + if (elCharacterTrees != null) { + List listCharacterTrees = elCharacterTrees.getChildren("CharacterTree", sddNamespace); + int j = 0; + //for each CharacterTree + + for (Element elCharacterTree : listCharacterTrees){ + + 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; + + 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); } - td.addDescriptionSource(publication); + featureTrees.add(groups); } + + } catch (Exception e) { + //FIXME + logger.warn("Import of Character tree " + j + " failed."); + success = false; } - } - logger.info(i + " Datasets handled"); - - ITermService termService = config.getCdmAppController().getTermService(); - TransactionStatus ts = config.getCdmAppController().startTransaction(); - for (Iterator k = states.values().iterator() ; k.hasNext() ;){ - State state = k.next(); - state.setCreated(created); - state.setUpdated(updated); - termService.saveTerm(state); - } - for (Iterator k = features.values().iterator() ; k.hasNext() ;){ - Feature feature = k.next(); - feature.setCreated(created); - feature.setUpdated(updated); - termService.saveTerm(feature); - } - for (Iterator k = units.values().iterator() ; k.hasNext() ;){ - MeasurementUnit unit = k.next(); - unit.setCreated(created); - unit.setUpdated(updated); - termService.saveTerm(unit); - } - for (Iterator k = statisticalMeasures.iterator() ; k.hasNext() ;) { - StatisticalMeasure sm = k.next(); - sm.setCreated(created); - sm.setUpdated(updated); - termService.saveTerm(sm); - } + if ((++j % modCount) == 0){ logger.info("CharacterTrees handled: " + j);} - config.getCdmAppController().commitTransaction(ts); - - IReferenceService referenceService = config.getCdmAppController().getReferenceService(); - - for (Iterator k = publications.values().iterator() ; k.hasNext() ;){ - Article publication = (Article) k.next(); - publication.setCreated(created); - publication.setUpdated(updated); - referenceService.saveReference(publication); - } - - // Returns a CdmApplicationController created by the values of this configuration. - IDescriptionService descriptionService = config.getCdmAppController().getDescriptionService(); - - for (Iterator k = taxonDescriptions.values().iterator() ; k.hasNext() ;){ - TaxonDescription taxonDescription = k.next(); - taxonDescription.setCreated(created); - taxonDescription.setUpdated(updated); - // Persists a Description - descriptionService.saveDescription(taxonDescription); } - // makeNameSpecificData(nameMap); - logger.info("end makeTaxonDescriptions ..."); - return success; - } - /* (non-Javadoc) - * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator) - */ - protected boolean isIgnore(IImportConfigurator config){ - return false; - } - +} }