/*
# Licensed Materials - Property of IBM
# Copyright IBM Corp. 2016, 2017  
 */
package com.ibm.streamsx.topology.internal.context.remote;

import static com.ibm.streamsx.topology.internal.gson.GsonUtilities.object;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.Future;

import com.google.gson.JsonObject;
import com.ibm.streamsx.topology.internal.streaminganalytics.VcapServices;

public class RemoteBuildAndSubmitRemoteContext extends ZippedToolkitRemoteContext {
	@Override
    public Type getType() {
        return Type.ANALYTICS_SERVICE;
    }
	
	@Override
	public Future<File> submit(JsonObject submission) throws Exception {
		Future<File> archive = super.submit(submission);
		JsonObject deploy = object(submission, "deploy");
		doSubmit(deploy, archive.get());
       return archive;
	}
	
	private void doSubmit(JsonObject deploy, File archive) throws IOException{
		JsonObject service = VcapServices.getVCAPService(key -> deploy.get(key));        
        JsonObject credentials = object(service,  "credentials");
     
        BuildServiceRemoteRESTWrapper wrapper = new BuildServiceRemoteRESTWrapper(credentials);
        wrapper.remoteBuildAndSubmit(deploy, archive);
	}
}

@@ -664,85 +664,183 @@
  %7B%0A%09
-%09Future%3CFile%3E archive = super.submit(submission);%0A%09%09JsonObject deploy = objec
+    JsonObject deploy = object(submission, %22deploy%22);%0A%09    JsonObject service = VcapServices.getVCAPService(key -%3E deploy.get(key));%0A%09    %0A%09%09Future%3CFile%3E archive = super.submi
 t(su
@@ -847,29 +847,22 @@
 bmission
-, %22deploy%22
 );%0A
+%09%09%0A
 %09%09doSubm
@@ -863,32 +863,41 @@
 doSubmit(deploy,
+ service,
  archive.get());
@@ -966,16 +966,36 @@
  deploy,
+ JsonObject service,
  File ar
@@ -1027,81 +1027,8 @@
 %7B%0A%09%09
-JsonObject service = VcapServices.getVCAPService(key -%3E deploy.get(key));
     

Check VCAP info as soon as possible