import java.io.IOException;
import java.io.InputStream;
+import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
+import java.util.UUID;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade.DerivedUnitType;
-import eu.etaxonomy.cdm.common.mediaMetaData.ImageMetaData;
import eu.etaxonomy.cdm.io.common.ICdmIO;
-import eu.etaxonomy.cdm.io.specimen.SpecimenIoBase;
+import eu.etaxonomy.cdm.io.specimen.SpecimenImportBase;
import eu.etaxonomy.cdm.io.specimen.UnitsGatheringArea;
import eu.etaxonomy.cdm.io.specimen.UnitsGatheringEvent;
import eu.etaxonomy.cdm.model.agent.Institution;
import eu.etaxonomy.cdm.model.agent.Team;
+import eu.etaxonomy.cdm.model.common.DescriptionElementSource;
+import eu.etaxonomy.cdm.model.description.Feature;
+import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
+import eu.etaxonomy.cdm.model.description.TaxonDescription;
import eu.etaxonomy.cdm.model.location.NamedArea;
-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.name.BacterialName;
import eu.etaxonomy.cdm.model.name.BotanicalName;
import eu.etaxonomy.cdm.model.name.CultivarPlantName;
* @version 1.0
*/
@Component
-public class Abcd206Import extends SpecimenIoBase<Abcd206ImportConfigurator, Abcd206ImportState> implements ICdmIO<Abcd206ImportState> {
+public class Abcd206Import extends SpecimenImportBase<Abcd206ImportConfigurator, Abcd206ImportState> implements ICdmIO<Abcd206ImportState> {
private static final Logger logger = Logger.getLogger(Abcd206Import.class);
public boolean doInvoke(Abcd206ImportState state){
logger.info("INVOKE Specimen Import from ABCD2.06 XML File");
boolean result = true;
- Abcd206ImportConfigurator config = state.getConfig();
//AbcdIO test = new AbcdIO();
- String sourceName = config.getSource();
+ URI sourceName = state.getConfig().getSource();
NodeList unitsList = getUnitsNodeList(sourceName);
if (unitsList != null){
- logger.info("nb units to insert: "+unitsList.getLength());
+ String message = "nb units to insert: "+unitsList.getLength();
+ logger.info(message);
+ updateProgress(state, message);
Abcd206DataHolder dataHolder = new Abcd206DataHolder();
for (int i=0 ; i<unitsList.getLength() ; i++){
this.setUnitPropertiesXML((Element)unitsList.item(i), dataHolder);
- result &= this.handleSingleUnit(config, dataHolder);
+ result &= this.handleSingleUnit(state, dataHolder);
//compare the ABCD elements added in to the CDM and the unhandled ABCD elements
compareABCDtoCDM(sourceName, dataHolder.knownABCDelements, dataHolder);
-
+
//reset the ABCD elements added in CDM
//knownABCDelements = new ArrayList<String>();
dataHolder.allABCDelements = new HashMap<String,String>();
/*
* Store the unit with its Gathering informations in the CDM
*/
- private boolean handleSingleUnit(Abcd206ImportConfigurator config, Abcd206DataHolder dataHolder){
+ private boolean handleSingleUnit(Abcd206ImportState state, Abcd206DataHolder dataHolder){
boolean result = true;
+ Abcd206ImportConfigurator config = state.getConfig();
+
TransactionStatus tx = startTransaction();
try {
+ updateProgress(state, "Importing data for unit: " + dataHolder.unitID);
+
// ReferenceBase sec = Database.NewInstance();
// sec.setTitleCache("XML DATA");
ReferenceBase sec = config.getTaxonReference();
derivedUnitFacade.setLocality(gatheringEvent.getLocality());
derivedUnitFacade.setExactLocation(gatheringEvent.getExactLocation());
derivedUnitFacade.setCollector(gatheringEvent.getCollector());
- derivedUnitFacade.addCollectingArea(areaCountry);
- //FIXME setCountry
- derivedUnitFacade.addCollectingArea(areaCountry);
+ derivedUnitFacade.setCountry(areaCountry);
derivedUnitFacade.addCollectingAreas(unitsGatheringArea.getAreas());
//TODO exsiccatum
// //add Multimedia URLs
if(dataHolder.multimediaObjects.size() > 0){
- MediaRepresentation representation;
- Media media;
- ImageMetaData imd ;
- URL url ;
- ImageFile imf;
for (String multimediaObject : dataHolder.multimediaObjects){
- if( multimediaObject != null){
- imd = ImageMetaData.newInstance();
- url = new URL(multimediaObject);
- try {
- imd.readMetaData(url.toURI(), 0);
- } catch (Exception e) {
- String message = "An error occurred when trying to read image meta data: " + e.getMessage();
- logger.warn(message);
- }
- //TODO do we really want to check the url?
- if (imd != null){
- imf = ImageFile.NewInstance(multimediaObject, null, imd);
- representation = MediaRepresentation.NewInstance();
- representation.addRepresentationPart(imf);
- media = Media.NewInstance();
- media.addRepresentation(representation);
-
- derivedUnitFacade.addFieldObjectMedia(media);
- }
- }
+ Media media = getImageMedia(multimediaObject, true);
+ derivedUnitFacade.addDerivedUnitMedia(media);
}
}
}
getTermService().saveLanguageData(unitsGatheringEvent.getLocality());//TODO needs to be saved ?? save it sooner
- getOccurrenceService().save(derivedUnitFacade.getDerivedUnit());
+ getOccurrenceService().save(derivedUnitFacade.innerDerivedUnit());
logger.info("saved ABCD specimen ...");
return result;
}
-
private void setCollectionData(Abcd206ImportConfigurator config,
Abcd206DataHolder dataHolder, DerivedUnitFacade derivedUnitFacade) {
//set catalogue number (unitID)
* @param fileName: the file's location
* @return the list of root nodes ("Unit")
*/
- private static NodeList getUnitsNodeList(String urlFileName){
+ private static NodeList getUnitsNodeList(URI urlFileName){
NodeList unitList = null;
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
- URL url = new URL(urlFileName);
+ URL url = urlFileName.toURL();
Object o = url.getContent();
InputStream is = (InputStream)o;
Document document = builder.parse(is);
private void setUnitPropertiesXML(Element root, Abcd206DataHolder dataHolder){
try{
NodeList group;
-
+
// try{afficherInfos(racine, 0);}catch (Exception e) {logger.info(e);}
group = root.getChildNodes();
// logger.info("ABCD ELEMENT not stored: "+group.item(i).getNodeName().toString()+" - value: "+group.item(i).getTextContent());
path="";
dataHolder.longitude = Double.valueOf(group.item(0).getTextContent());
} catch (NullPointerException e) {
- dataHolder.longitude=0.0;
+ dataHolder.longitude=null;
}
try {
group = root.getElementsByTagName("LatitudeDecimal");
path="";
dataHolder.latitude = Double.valueOf(group.item(0).getTextContent());
} catch (NullPointerException e) {
- dataHolder.latitude=0.0;
+ dataHolder.latitude=null;
}
try {
group = root.getElementsByTagName("Country");
}catch(Exception e){
institutions=new ArrayList<Institution>();
}
- if (institutions.size() ==0 || !config.getReUseExistingMetadata()){
+ if (institutions.size() ==0 || !config.isReUseExistingMetadata()){
logger.info("Institution (agent) unknown or not allowed to reuse existing metadata");
//create institution
institution = Institution.NewInstance();
}catch(Exception e){
collections=new ArrayList<Collection>();
}
- if (collections.size() ==0 || !config.getReUseExistingMetadata()){
+ if (collections.size() ==0 || !config.isReUseExistingMetadata()){
logger.info("Collection not found or do not reuse existing metadata " + dataHolder.collectionCode);
//create new collection
collection.setCode(dataHolder.collectionCode);
if (fullScientificNameString.indexOf("_code_") != -1){
dataHolder.nomenclatureCode = fullScientificNameString.split("_code_")[1];
}
- if (config.getDoAutomaticParsing() || dataHolder.atomisedIdentificationList == null || dataHolder.atomisedIdentificationList.size()==0){
+ if (config.isDoAutomaticParsing() || dataHolder.atomisedIdentificationList == null || dataHolder.atomisedIdentificationList.size()==0){
taxonName = this.parseScientificName(scientificName, dataHolder);
} else {
if (dataHolder.atomisedIdentificationList != null || dataHolder.atomisedIdentificationList.size()>0){
taxonName = NonViralName.NewInstance(null);
taxonName.setFullTitleCache(scientificName);
}
- if (config.getDoReUseTaxon()){
+
+ // --- cascade through several options in order to find an appropriate taxon ---
+
+ if (config.isDoMatchTaxa()){
+ taxon = getTaxonService().findBestMatchingTaxon(scientificName);
+ }
+
+ if (taxon == null && config.isDoReUseTaxon()){
try{
names = getTaxonService().searchTaxaByName(scientificName, sec);
taxon = (Taxon)names.get(0);
} catch(Exception e){
taxon=null;
}
+ } else {
+ logger.info("Matching to existing Taxon : " + taxon.getTitleCache());
}
-// taxonName = NonViralName.NewInstance(null);
-// taxonName.setFullTitleCache(scientificName);
- if (!config.getDoReUseTaxon() || taxon == null){
+ if (!config.isDoReUseTaxon() || taxon == null){
getNameService().save(taxonName);
taxon = Taxon.NewInstance(taxonName, sec); //TODO sec set null
}
+
+// taxonName = NonViralName.NewInstance(null);
+// taxonName.setFullTitleCache(scientificName);
+
+ // --- taxon is found now ---
+
determinationEvent = DeterminationEvent.NewInstance();
determinationEvent.setTaxon(taxon);
determinationEvent.setPreferredFlag(preferredFlag);
determinationEvent.addReference(reference);
}
facade.addDetermination(determinationEvent);
+
+ if(config.isDoCreateIndividualsAssociations()){
+ TaxonDescription taxonDescription = null;
+ if(config.isDoMatchToExistingDescription()){
+ logger.error("The import option 'DoMatchToExistingDescription' is not yet implemented.");
+ } else {
+ UUID taxonDescriptionUUID = config.getTaxonToDescriptionMap().get(taxon.getUuid()); // rather put in state
+ taxonDescription = (TaxonDescription) getDescriptionService().load(taxonDescriptionUUID);
+ if(taxonDescription == null){
+ taxonDescription = TaxonDescription.NewInstance(taxon);
+ config.getTaxonToDescriptionMap().put(taxon.getUuid(), taxonDescription.getUuid());
+ if(taxonDescriptionUUID == null){
+ logger.info("Creating new TaxonDescription for " + taxon.getTitleCache());
+ } else {
+ logger.fatal("TaxonDescription with UUID " + taxonDescriptionUUID + " not found --> creating a new one.");
+ }
+ }
+ }
+ IndividualsAssociation individualsAssociation = IndividualsAssociation.NewInstance();
+ individualsAssociation.setAssociatedSpecimenOrObservation(facade.innerDerivedUnit());
+ individualsAssociation.setFeature(Feature.INDIVIDUALS_ASSOCIATION());
+ for(ReferenceBase citation : determinationEvent.getReferences()){
+ individualsAssociation.addSource(DescriptionElementSource.NewInstance(null, null, citation, null));
+ }
+ taxonDescription.addElement(individualsAssociation);
+ getDescriptionService().saveOrUpdate(taxonDescription);
+ }
}
}
return value;
}
- private void compareABCDtoCDM(String urlFileName, ArrayList<String> knownElts, Abcd206DataHolder dataHolder){
+ private void compareABCDtoCDM(URI urlFileName, ArrayList<String> knownElts, Abcd206DataHolder dataHolder){
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder constructeur = factory.newDocumentBuilder();
- URL url = new URL(urlFileName);
+ URL url = urlFileName.toURL();
Object o = url.getContent();
InputStream is = (InputStream)o;
Document document = constructeur.parse(is);