package com.github.jmchilton.blend4j.galaxy;

import com.github.jmchilton.blend4j.galaxy.beans.Dataset;
import com.github.jmchilton.blend4j.galaxy.beans.History;
import com.github.jmchilton.blend4j.galaxy.beans.ToolInputs;
import com.sun.jersey.api.client.ClientRequest;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.multipart.file.FileDataBodyPart;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class ToolsClientImpl extends ClientImpl implements ToolsClient {

  ToolsClientImpl(GalaxyInstanceImpl galaxyInstance) {
    super(galaxyInstance, "tools");
  }

  public List<Dataset> create(History history, ToolInputs inputs) {
    inputs.setHistoryId(history.getId());
    super.create(inputs);
    // XXX Datasets not yet properly returned from Tool creation
    return new ArrayList();
  }
  
  public ClientResponse fileUploadRequest(final String historyId, 
                                          final String fileType, 
                                          final String dbKey, 
                                          final File file) {
    final FileDataBodyPart fdbp = new FileDataBodyPart("files_0|file_data", file);
    final Map<String, String> uploadParameters = new HashMap<String, String>();
    uploadParameters.put("dbkey", dbKey);
    uploadParameters.put("file_type", fileType);
    // Following line needed?
    uploadParameters.put("files_0|NAME", file.getName());
    uploadParameters.put("files_0|type", "upload_dataset");
    uploadParameters.put("history_id", historyId);
    final Map<String, Object> requestParameters = new HashMap<String, Object>();
    requestParameters.put("tool_id", "upload1");
    requestParameters.put("inputs", write(uploadParameters));
    requestParameters.put("type", "upload_dataset");        
    return multipartPost(getWebResource(), requestParameters, Arrays.asList(fdbp));
  }
  
}

@@ -1428,38 +1428,8 @@
 e);%0A
-    // Following line needed?%0A
     
@@ -1478,24 +1478,24 @@
 getName());%0A
+
     uploadPa
@@ -1546,59 +1546,8 @@
 %22);%0A
-    uploadParameters.put(%22history_id%22, historyId);%0A
     
@@ -1619,24 +1619,24 @@
  Object%3E();%0A
-
     requestP
@@ -1668,24 +1668,76 @@
 %22upload1%22);%0A
+    requestParameters.put(%22history_id%22, historyId);%0A
     requestP

Fix ToolsClient upload parameters. Uploading files to Galaxy now works