package org.ndexbio.task;

import java.io.FileInputStream;
import java.util.Map;

import org.ndexbio.common.importexport.ExporterExecutor;
import org.ndexbio.common.importexport.ImporterExporterEntry;
import org.ndexbio.model.object.Task;
import org.ndexbio.rest.Configuration;

public class NetworkExportTask extends NdexTask {
	
	//private Task task;
	
	public NetworkExportTask(Task t) {
		super(t);
	}
	
	
	@Override
	public Task call_aux() throws Exception {
		Map<String, Object> attrs = task.getAttributes();
		String converterName = (String)attrs.get("name");
		
		ImporterExporterEntry entry = Configuration.getInstance().getImpExpEntry(converterName);
		
		ExporterExecutor executor = new ExporterExecutor (entry);
		
		try (FileInputStream input = new FileInputStream (Configuration.getInstance().getNdexRoot() + "/data/"+task.getResource() + "/network.cx")) {
		
			executor.export(input, task.getExternalId(), task.getTaskOwnerId());
	
		}
		return getTask();
		
		/*	   System.out.println("Creating the GZIP output stream.");
		   
		   String outFileName = Configuration.getInstance().getNdexRoot() + "/exported-networks/"
				   + task.getExternalId() + "." +
				   task.getAttribute("downloadFileExtension").toString().toLowerCase() + ".gz";
        try (GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(outFileName)) ) {
     	
     	   try (FileInputStream in = new FileInputStream(Configuration.getInstance().getNdexRoot() +
     			   "/data/" + task.getResource() + "/network.cx")) {
            
     		   byte[] buf = new byte[8192];
     		   int len;
     		   while((len = in.read(buf)) > 0) {
     			   out.write(buf, 0, len);
     		   }
     	   	}
     	   
        }       
      */
	}

}

@@ -194,16 +194,56 @@
 rEntry;%0A
+import org.ndexbio.model.object.Status;%0A
 import o
@@ -911,16 +911,31 @@
 %7B%0A%09%09%0A%09%09%09
+int exitCode = 
 executor
@@ -996,16 +996,131 @@
 Id());%0A%09
+%09%09if (exitCode == 0) %7B%0A%09%09%09%09task.setStatus(Status.COMPLETED);%0A%09%09%09%7D%0A%09%09%09else %7B%0A%09%09%09%09task.setStatus(Status.FAILED);%0A%09%09%09%7D
 %0A%09%09%7D%0A%09%09r

set status of task based on exit code of command line script. 0 for success otherwise failure