commit 0d19c47e70da510c44a35cf664064dfd455bd5aa
Author: Katja Luther <k.luther@bgbm.org>
Date:   Fri Aug 12 10:16:50 2016 +0200

    changes for gbif/abcd import from webservice

diff --git a/eu.etaxonomy.taxeditor.cdmlib/META-INF/MANIFEST.MF b/eu.etaxonomy.taxeditor.cdmlib/META-INF/MANIFEST.MF
index c86d71155..2c91b2a00 100644
--- a/eu.etaxonomy.taxeditor.cdmlib/META-INF/MANIFEST.MF
+++ b/eu.etaxonomy.taxeditor.cdmlib/META-INF/MANIFEST.MF
@@ -82,6 +82,7 @@ Export-Package: com.google.api,
  eu.etaxonomy.cdm.io.specimen,
  eu.etaxonomy.cdm.io.specimen.abcd206.in,
  eu.etaxonomy.cdm.io.specimen.excel.in,
+ eu.etaxonomy.cdm.io.specimen.gbif.in,
  eu.etaxonomy.cdm.io.taxonx,
  eu.etaxonomy.cdm.io.tcsxml,
  eu.etaxonomy.cdm.io.tcsxml.in,
diff --git a/eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/taxeditor/remoting/cache/CacheLoader.java b/eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/taxeditor/remoting/cache/CacheLoader.java
index c99ce01b6..7faec2c9a 100644
--- a/eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/taxeditor/remoting/cache/CacheLoader.java
+++ b/eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/taxeditor/remoting/cache/CacheLoader.java
@@ -71,8 +71,8 @@ public class CacheLoader {
         } else if (obj instanceof Collection) {
             return (T) load((Collection<T>)obj, recursive, update);
         } else if(obj instanceof Pager) {
-             load(((Pager)obj).getRecords(), recursive, update);
-             return obj;
+        	load(((Pager)obj).getRecords(), recursive, update);
+            return obj;
         } else if(obj instanceof MergeResult) {
             return (T) load((MergeResult<CdmBase>)obj, recursive, update);
         }
@@ -153,7 +153,7 @@ public class CacheLoader {
         return map;
     }
 
-    public <T extends Object> Collection<T> load(Collection<T> collection, boolean recursive, boolean update){
+    public <T extends Object> Collection<T> load(Collection<T> collection,  boolean recursive, boolean update){
 
         Collection<T> loadedCollection;
         if(isRecursiveEnabled && recursive) {
@@ -355,7 +355,7 @@ public class CacheLoader {
                     CdmBase cachedCdmEntityInSubGraph = cdmCacher.getFromCache(cdmEntityInSubGraph);
 
                     if(cachedCdmEntityInSubGraph != null) {
-                        if(cachedCdmEntityInSubGraph != cdmEntityInSubGraph) {
+                        if(!cachedCdmEntityInSubGraph.equals(cdmEntityInSubGraph)) {
                             // exception : is the case where
                             // the field has been already initialised, cached and
                             // is not the same as the one in the cache, in which case we set the value
diff --git a/eu.etaxonomy.taxeditor.molecular.lib/.classpath b/eu.etaxonomy.taxeditor.molecular.lib/.classpath
index 55f260ddb..5918c4c4d 100644
--- a/eu.etaxonomy.taxeditor.molecular.lib/.classpath
+++ b/eu.etaxonomy.taxeditor.molecular.lib/.classpath
@@ -1,5 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <classpath>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
+	<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
 	<classpathentry exported="true" kind="lib" path="lib/bioinfweb-commons-bio-2.0.0-preview-1.jar"/>
 	<classpathentry exported="true" kind="lib" path="lib/bioinfweb-commons-core-2.0.0-preview-1.jar"/>
 	<classpathentry exported="true" kind="lib" path="lib/bioinfweb-commons-swing-2.0.0-preview-1.jar"/>
@@ -11,7 +13,5 @@
 	<classpathentry exported="true" kind="lib" path="lib/sequencing-1.9.2-SNAPSHOT.jar"/>
 	<classpathentry exported="true" kind="lib" path="lib/tic-core-2.0.0-preview-1.jar"/>
 	<classpathentry exported="true" kind="lib" path="lib/tic-swt-2.0.0-preview-1.jar"/>
-	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
-	<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
 	<classpathentry kind="output" path="target/classes"/>
 </classpath>
diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/io/ImportManager.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/io/ImportManager.java
index 34a9fcf96..539db22f7 100644
--- a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/io/ImportManager.java
+++ b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/io/ImportManager.java
@@ -32,7 +32,6 @@ import org.eclipse.ui.PlatformUI;
 import eu.etaxonomy.cdm.api.application.CdmApplicationState;
 import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
 import eu.etaxonomy.cdm.common.monitor.IRemotingProgressMonitor;
-import eu.etaxonomy.cdm.ext.occurrence.OccurenceQuery;
 import eu.etaxonomy.cdm.io.common.CdmDefaultImport;
 import eu.etaxonomy.cdm.io.common.IImportConfigurator;
 import eu.etaxonomy.cdm.io.common.IImportConfigurator.SOURCE_TYPE;
@@ -43,8 +42,12 @@ import eu.etaxonomy.cdm.io.jaxb.JaxbImportConfigurator;
 import eu.etaxonomy.cdm.io.reference.endnote.in.EndnoteImportConfigurator;
 import eu.etaxonomy.cdm.io.sdd.in.SDDImportConfigurator;
 import eu.etaxonomy.cdm.io.service.IIOService;
+import eu.etaxonomy.cdm.io.specimen.SpecimenImportConfiguratorBase;
 import eu.etaxonomy.cdm.io.specimen.abcd206.in.Abcd206ImportConfigurator;
+import eu.etaxonomy.cdm.io.specimen.abcd206.in.Abcd206ImportState;
 import eu.etaxonomy.cdm.io.specimen.excel.in.SpecimenCdmExcelImportConfigurator;
+import eu.etaxonomy.cdm.io.specimen.gbif.in.GbifImportConfigurator;
+import eu.etaxonomy.cdm.io.specimen.gbif.in.GbifImportState;
 import eu.etaxonomy.cdm.io.tcsxml.in.TcsXmlImportConfigurator;
 import eu.etaxonomy.taxeditor.model.AbstractUtility;
 import eu.etaxonomy.taxeditor.model.CdmProgressMonitorAdapter;
@@ -413,7 +416,7 @@ public class ImportManager extends AbstractIOManager<IImportConfigurator> implem
 
     }
 
-	/*public Job createIOServiceJob(final DwcaImportConfigurator configurator) {
+	public Job createIOServiceJob(final GbifImportConfigurator<GbifImportState, InputStream> configurator) {
 		 Assert.isNotNull(configurator, "Configuration may not be null");
 	        final Display display = Display.getDefault();
 	        Job job = new Job("Import: " + " Dwca") {
@@ -440,7 +443,67 @@ public class ImportManager extends AbstractIOManager<IImportConfigurator> implem
 
 	        return job;
 
-	}*/
+	}
+	
+	public Job createIOServiceJob(final Abcd206ImportConfigurator<Abcd206ImportState, InputStream> configurator) {
+		 Assert.isNotNull(configurator, "Configuration may not be null");
+	        final Display display = Display.getDefault();
+	        Job job = new Job("Import: " + " Dwca") {
+
+	            @Override
+	            protected IStatus run(IProgressMonitor monitor) {
+	                monitor.beginTask("Importing data", IProgressMonitor.UNKNOWN);
+	                IIOService ioService = CdmApplicationState.getIOService();
+
+	                ImportResult result = ioService.importDataFromStream(configurator);
+	                monitor.done();
+
+	                display.asyncExec(new Runnable() {
+
+	                    @Override
+	                    public void run() {
+	                        CdmStore.getContextManager().notifyContextRefresh();
+	                    }
+	                });
+
+	                return Status.OK_STATUS;
+	            }
+	        };
+
+	        return job;
+
+	}
+
+	public Job createIOServiceJob(
+			final List<Abcd206ImportConfigurator> abcdConfigurators) {
+		 Assert.isNotNull(abcdConfigurators, "Configuration may not be null");
+	        final Display display = Display.getDefault();
+	        Job job = new Job("Import: " + " Dwca") {
+
+	            @Override
+	            protected IStatus run(IProgressMonitor monitor) {
+	                monitor.beginTask("Importing data", IProgressMonitor.UNKNOWN);
+	                IIOService ioService = CdmApplicationState.getIOService();
+
+	                ImportResult result = ioService.importDataFromStream(abcdConfigurators);
+	                monitor.done();
+
+	                display.asyncExec(new Runnable() {
+
+	                    @Override
+	                    public void run() {
+	                        CdmStore.getContextManager().notifyContextRefresh();
+	                    }
+	                });
+
+	                return Status.OK_STATUS;
+	            }
+	        };
+
+	        return job;
+	}
+
+	
 
 
 }
diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/io/wizard/ClassificationChooserWizardPage.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/io/wizard/ClassificationChooserWizardPage.java
index 377ccc84c..c0db37b9a 100644
--- a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/io/wizard/ClassificationChooserWizardPage.java
+++ b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/io/wizard/ClassificationChooserWizardPage.java
@@ -52,7 +52,7 @@ public class ClassificationChooserWizardPage extends WizardPage implements Liste
 
 	}
 
-	protected static ClassificationChooserWizardPage createPage(){
+	public static ClassificationChooserWizardPage createPage(){
 		return new ClassificationChooserWizardPage("Configure import destinations", "Note: Selecting no classification will create a default one.");
 	}
 
diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/AbstractEntityCollectionElementWizardPage.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/AbstractEntityCollectionElementWizardPage.java
index 4e78d2c4e..43bb78dca 100644
--- a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/AbstractEntityCollectionElementWizardPage.java
+++ b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/AbstractEntityCollectionElementWizardPage.java
@@ -14,24 +14,30 @@ import org.eclipse.jface.wizard.WizardPage;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.ui.forms.widgets.TableWrapLayout;
 
+import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
+import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
+import eu.etaxonomy.taxeditor.store.CdmStore;
 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
 import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
 import eu.etaxonomy.taxeditor.ui.element.RootElement;
+import eu.etaxonomy.taxeditor.webapp.ICDMServerError;
 
 /**
  * @author pplitzner
  * @date Mar 30, 2016
  *
  */
-public abstract class AbstractEntityCollectionElementWizardPage extends WizardPage implements
+public abstract class AbstractEntityCollectionElementWizardPage extends WizardPage implements 
 IPropertyChangeListener {
 
     protected CdmFormFactory formFactory;
+    
 
     protected RootElement rootElement;
 
     protected AbstractEntityCollectionElementWizardPage(String pageName) {
         super(pageName);
+        
     }
 
     /**
@@ -59,6 +65,7 @@ IPropertyChangeListener {
     public void dispose() {
         rootElement.removeElements();
         formFactory.removePropertyChangeListener(this);
+       
         super.dispose();
     }
 
diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/classification/TaxonNodeDetailElement.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/classification/TaxonNodeDetailElement.java
index 6d347db62..7da137dc9 100644
--- a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/classification/TaxonNodeDetailElement.java
+++ b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/classification/TaxonNodeDetailElement.java
@@ -173,17 +173,11 @@ public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITaxonTreeN
 				selection_reuseExistingName.setEnabled(enabled);
 				if (!enabled){
                     setTaxon(selection_reuseExistingTaxon.getEntity());
-                }
-				if (isCreateNew()){
-				    textNewTaxonName.setEnabled(enabled);
-				    if (!enabled){
-				        setTaxon(selection_reuseExistingTaxon.getEntity());
-				    } else{
-				        textNewTaxonName.setText(null);
-				    }
-
-				    complete = !textNewTaxonName.getText().isEmpty();
-				}
+                }else{
+			        textNewTaxonName.setText(null);
+			    }
+				complete = !textNewTaxonName.getText().isEmpty();
+				
 
 		} else if (eventSource == selection_reuseExistingName) {
 		    boolean enabled = selection_reuseExistingName.getEntity() == null;
@@ -291,6 +285,9 @@ public class TaxonNodeDetailElement extends AbstractCdmDetailElement<ITaxonTreeN
 	public String getTaxonName() {
         return textNewTaxonName.getText();
     }
+	public Taxon getTaxon() {
+        return selection_reuseExistingTaxon.getEntity();
+    }
 
 	public Reference getReference(){
 	    return selection_SecRef.getSelection();
diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/classification/TaxonNodeWizardPage.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/classification/TaxonNodeWizardPage.java
index dc90c3109..dd54afa1f 100644
--- a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/classification/TaxonNodeWizardPage.java
+++ b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/classification/TaxonNodeWizardPage.java
@@ -148,10 +148,14 @@ public class TaxonNodeWizardPage extends AbstractCdmEntityWizardPage<ITaxonTreeN
 	}
 
 	public Taxon createTaxon() {
-		String taxonName = ((TaxonNodeDetailElement) getDetailElement()).getTaxonName();
-		Reference reference = ((TaxonNodeDetailElement) getDetailElement()).getReference();
-		NonViralName parseReferencedName = ParseHandler.parseReferencedName(taxonName, null);
-		return Taxon.NewInstance(parseReferencedName, reference);
+		if (((TaxonNodeDetailElement) getDetailElement()).getTaxon() == null){
+			String taxonName = ((TaxonNodeDetailElement) getDetailElement()).getTaxonName();
+			Reference reference = ((TaxonNodeDetailElement) getDetailElement()).getReference();
+			NonViralName parseReferencedName = ParseHandler.parseReferencedName(taxonName, null);
+			return Taxon.NewInstance(parseReferencedName, reference);
+		} else{
+			return ((TaxonNodeDetailElement) getDetailElement()).getTaxon();
+		}
 	}
 
 	/*
diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/description/detail/TaxonInteractionDetailElement.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/description/detail/TaxonInteractionDetailElement.java
index 8e5e5bc88..3a56e5c1f 100644
--- a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/description/detail/TaxonInteractionDetailElement.java
+++ b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/description/detail/TaxonInteractionDetailElement.java
@@ -12,6 +12,7 @@ package eu.etaxonomy.taxeditor.ui.section.description.detail;
 
 import org.eclipse.swt.SWT;
 
+import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
 import eu.etaxonomy.cdm.model.description.TaxonInteraction;
 import eu.etaxonomy.cdm.model.taxon.Taxon;
 import eu.etaxonomy.taxeditor.store.CdmStore;
@@ -69,6 +70,7 @@ public class TaxonInteractionDetailElement extends
 
 		interactionDescription = formFactory.createMultiLineTextWithLabel(this,
 				"Relationship to the taxon:", 200, SWT.WRAP);
+		entity = HibernateProxyHelper.deproxy(entity, TaxonInteraction.class);
 		interactionDescription.setText(entity.getDescription(CdmStore
 				.getDefaultLanguage()));
 	}
diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/name/NameRelationshipWizard.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/name/NameRelationshipWizard.java
index e7abd5666..0bea2ae1e 100644
--- a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/name/NameRelationshipWizard.java
+++ b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/name/NameRelationshipWizard.java
@@ -10,12 +10,21 @@
 
 package eu.etaxonomy.taxeditor.ui.section.name;
 
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
 import org.eclipse.jface.wizard.Wizard;
 
 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
+import eu.etaxonomy.cdm.model.common.CdmBase;
 import eu.etaxonomy.cdm.model.name.NameRelationship;
 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
+import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
+import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
+import eu.etaxonomy.taxeditor.store.CdmStore;
 
 /**
  * <p>NameRelationshipWizard class.</p>
@@ -24,10 +33,14 @@ import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
  * @created Jun 1, 2010
  * @version 1.0
  */
-public class NameRelationshipWizard extends Wizard implements IConversationEnabled{
+public class NameRelationshipWizard extends Wizard implements IConversationEnabled, ICdmEntitySessionEnabled{
 
 	private static NameRelationshipDetailSection callingSection;
 	
+	private ICdmEntitySession cdmEntitySession;
+    private ICdmEntitySession previousCdmEntitySession;
+    private CdmBase rootElement;
+	
 	/**
 	 * <p>Constructor for NameRelationshipWizard.</p>
 	 *
@@ -35,6 +48,12 @@ public class NameRelationshipWizard extends Wizard implements IConversationEnabl
 	 */
 	public NameRelationshipWizard(NameRelationshipDetailSection callingSection) {
 		NameRelationshipWizard.callingSection = callingSection;
+		rootElement = callingSection.getEntity();
+		if (CdmStore.isActive() && CdmStore.getCurrentSessionManager().isRemoting()) {
+            previousCdmEntitySession = CdmStore.getCurrentSessionManager().getActiveSession();
+            cdmEntitySession = CdmStore.getCurrentSessionManager().newSession(this, true);
+            cdmEntitySession.bind();
+        }
 	}
 		
 	private NameRelationshipWizardPage page;
@@ -56,7 +75,16 @@ public class NameRelationshipWizard extends Wizard implements IConversationEnabl
 		return page.isPageComplete();
 	}
 
-
+	@Override
+    public void dispose() {
+        super.dispose();
+        if(cdmEntitySession != null) {
+            cdmEntitySession.dispose();
+        }
+        if(previousCdmEntitySession!=null){
+            previousCdmEntitySession.bind();
+        }
+    }
 
 	/**
 	 * <p>getNameRelationship</p>
@@ -78,4 +106,19 @@ public class NameRelationshipWizard extends Wizard implements IConversationEnabl
 
 	/** {@inheritDoc} */
 	public void update(CdmDataChangeMap changeEvents) {}
+
+	 @Override
+	    public ICdmEntitySession getCdmEntitySession() {
+	        return cdmEntitySession;
+	    }
+
+	    @Override
+	    public java.util.Collection<CdmBase> getRootEntities() {
+	        return Collections.singleton(rootElement);
+	    }
+
+	    @Override
+	    public Map<Object, List<String>> getPropertyPathsMap() {
+	        return null;
+	    }
 }
diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/dataimport/GbifResponseImportView.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/dataimport/GbifResponseImportView.java
index df613bca3..96938f2bb 100644
--- a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/dataimport/GbifResponseImportView.java
+++ b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/dataimport/GbifResponseImportView.java
@@ -14,6 +14,7 @@ import java.io.InputStream;
 import java.net.URISyntaxException;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Comparator;
 import java.util.HashSet;
 import java.util.Set;
 
@@ -96,5 +97,10 @@ public class GbifResponseImportView extends DataImportView<GbifResponse> {
 //        }
 
   //  }
+    
+   
+    
+
+	
 
 }
diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/dataimport/SaveImportedSpecimenAction.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/dataimport/SaveImportedSpecimenAction.java
index b97e5f8ed..a22231177 100644
--- a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/dataimport/SaveImportedSpecimenAction.java
+++ b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/dataimport/SaveImportedSpecimenAction.java
@@ -1,12 +1,20 @@
 package eu.etaxonomy.taxeditor.view.dataimport;
 
+import java.io.IOException;
+import java.net.URI;
+import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Set;
 
+import org.apache.http.client.ClientProtocolException;
 import org.apache.log4j.Logger;
 import org.eclipse.core.runtime.jobs.Job;
 import org.eclipse.jface.action.Action;
+import org.eclipse.jface.text.source.Annotation;
 import org.eclipse.swt.widgets.Table;
 import org.eclipse.swt.widgets.TableItem;
 import org.eclipse.ui.IWorkbenchPart;
@@ -17,9 +25,12 @@ import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
 import eu.etaxonomy.cdm.api.service.ITermService;
 import eu.etaxonomy.cdm.ext.occurrence.OccurenceQuery;
+import eu.etaxonomy.cdm.ext.occurrence.gbif.DataSetResponse;
 import eu.etaxonomy.cdm.ext.occurrence.gbif.GbifDataSetProtocol;
+import eu.etaxonomy.cdm.ext.occurrence.gbif.GbifQueryServiceWrapper;
 import eu.etaxonomy.cdm.ext.occurrence.gbif.GbifResponse;
-import eu.etaxonomy.cdm.io.dwca.in.DwcaImportConfigurator;
+import eu.etaxonomy.cdm.io.specimen.abcd206.in.Abcd206ImportConfigurator;
+import eu.etaxonomy.cdm.io.specimen.gbif.in.*;
 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
 import eu.etaxonomy.cdm.model.common.ICdmBase;
 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
@@ -29,6 +40,8 @@ import eu.etaxonomy.taxeditor.store.CdmStore;
 public class SaveImportedSpecimenAction extends Action {
 
     private final Logger logger = Logger.getLogger(SaveImportedSpecimenAction.class);
+    
+    private Comparator<GbifResponse> comparator;
 
     /* (non-Javadoc)
      * @see org.eclipse.jface.action.Action#run()
@@ -42,7 +55,10 @@ public class SaveImportedSpecimenAction extends Action {
             Table table = dataImportView.getTable();
             Collection<GbifResponse> checkedResults = new HashSet<GbifResponse>();
             Object data;
-            Set<String[]> unitIds = new HashSet<String[]>();
+            Set<String[]> unitIdsGbif = new HashSet<String[]>();
+            Set<String[]> unitIdsBioCase = new HashSet<String[]>();
+            List<GbifResponse> biocaseResponses = new ArrayList<GbifResponse>();
+            
             for(TableItem item:table.getItems()){
                 if(item.getChecked()){
                     //Save Specimen
@@ -52,21 +68,66 @@ public class SaveImportedSpecimenAction extends Action {
                 	 }
                 }
             }
+            String[] tripleId ;
                 for (GbifResponse response:checkedResults){
                 	if (response != null){
-                		/*String[] tripleId = response.getTripleID();
-                		unitIds.add(tripleId);
-	                		*/
+                		tripleId = response.getTripleID();
+                		
+                		if (response.getDataSetProtocol().equals(GbifDataSetProtocol.BIOCASE)){
+                			biocaseResponses.add(response);
+                			
+                		}else{
+                			unitIdsGbif.add(tripleId);
+                		}
+                		
+	                		
                 	}
                 }
+                Collections.sort(biocaseResponses, getComparator()); 
+                URI dataSetUri = null;
+                Abcd206ImportConfigurator configuratorAbcd;
+                List<Abcd206ImportConfigurator> abcdConfigurators = new ArrayList<Abcd206ImportConfigurator>();
+                DataSetResponse dataSetResponse;
+                OccurenceQuery bioCaseOccurrenceQuery = null;
+                for (GbifResponse response: biocaseResponses){
+                	try {
+						dataSetResponse = new GbifQueryServiceWrapper().queryOriginalDataSet(response);
+						dataSetUri = dataSetResponse.getEndpoint();
+					} catch (ClientProtocolException e) {
+						// TODO Auto-generated catch block
+						e.printStackTrace();
+					} catch (IOException e) {
+						// TODO Auto-generated catch block
+						e.printStackTrace();
+					}
+                	if (!response.getDataSetUri().equals(dataSetUri)){
+                		configuratorAbcd = Abcd206ImportConfigurator.NewInstance(dataSetUri, null, false);
+                		if (configuratorAbcd != null){
+                			abcdConfigurators.add(configuratorAbcd);
+                		}
+                		unitIdsBioCase = new HashSet<String[]>();
+                		bioCaseOccurrenceQuery = new OccurenceQuery(unitIdsBioCase);
+                		configuratorAbcd.setOccurenceQuery(bioCaseOccurrenceQuery);
+                	}
+                	tripleId = response.getTripleID();
+                	unitIdsBioCase.add(tripleId);
+                }
+              
+                OccurenceQuery query = new OccurenceQuery(unitIdsGbif);
+                GbifImportConfigurator configurator = GbifImportConfigurator.newInstance(query);
+                configurator.setSourceReferenceTitle("Import Gbif data");
                 
-                DwcaImportConfigurator configurator = DwcaImportConfigurator.NewInstance(null, null);
-               /* OccurenceQuery query = new OccurenceQuery(unitIds);
-                configurator.setQuery(query);
-               */
-                /*Job job = CdmStore.getImportManager().createIOServiceJob(configurator);
-                CdmStore.getImportManager().run(job);
-                */
+               
+                
+                //  configurator.setQuery(query);
+               
+               Job bioCaseJob = CdmStore.getImportManager().createIOServiceJob(abcdConfigurators);
+               CdmStore.getImportManager().run(bioCaseJob);
+               
+               Job gbifJob = CdmStore.getImportManager().createIOServiceJob(configurator);
+               CdmStore.getImportManager().run(gbifJob);
+               
+               
                 
             }
         }
@@ -80,8 +141,7 @@ public class SaveImportedSpecimenAction extends Action {
             }
         }
     }
-
-
+     
     /* (non-Javadoc)
      * @see org.eclipse.jface.action.Action#getText()
      */
@@ -89,5 +149,13 @@ public class SaveImportedSpecimenAction extends Action {
     public String getText() {
         return "Import";
     }
-
+    
+    private Comparator<GbifResponse> getComparator(){
+    	if (comparator == null){
+    		comparator = new GbifResponseComparator();
+    	}
+    	return comparator;
+    }
+    
+    
 }
