package uk.ac.ebi.biosamples.export;

import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.zip.GZIPOutputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.hateoas.Resource;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.databind.ObjectMapper;
import uk.ac.ebi.biosamples.client.BioSamplesClient;
import uk.ac.ebi.biosamples.model.Sample;

@Component
public class ExportRunner implements ApplicationRunner {

	private final Logger log = LoggerFactory.getLogger(getClass());
	private final BioSamplesClient bioSamplesClient;	
	private final ObjectMapper objectMapper;
	
	public ExportRunner(BioSamplesClient bioSamplesClient, ObjectMapper objectMapper) {
		this.bioSamplesClient = bioSamplesClient;
		this.objectMapper = objectMapper;
	}
	

	@Override
	public void run(ApplicationArguments args) throws Exception {
		String jsonSampleFilename = args.getNonOptionArgs().get(0);
		long oldTime = System.nanoTime();	
		int sampleCount = 0;
		try {
			boolean first = true;
			try (
				Writer jsonSampleWriter = args.getOptionValues("gzip") == null 
					? new OutputStreamWriter(new FileOutputStream(jsonSampleFilename), "UTF-8")
					: new OutputStreamWriter(new GZIPOutputStream(new FileOutputStream(jsonSampleFilename)), "UTF-8");	
			) {
				jsonSampleWriter.write("[\n");
				for (Resource<Sample> sampleResource : bioSamplesClient.fetchSampleResourceAll()) {
					log.trace("Handling "+sampleResource);
					Sample sample = sampleResource.getContent();
					if (sample == null) {
						throw new RuntimeException("Sample should not be null");
					}
					if (!first) {
						jsonSampleWriter.write(",\n");
					}
					jsonSampleWriter.write(objectMapper.writeValueAsString(sample));
					first = false;
					sampleCount += 1;
				}
				jsonSampleWriter.write("\n]");
			} finally {
			}
		} finally {			
		}
		long elapsed = System.nanoTime()-oldTime;
		log.info("Exported "+sampleCount+" samples in "+(elapsed/1000000000l)+"s");
	}
	
}

@@ -880,16 +880,185 @@
 pper) %7B%0A
+%09%09//ensure the client is public%0A%09%09if (bioSamplesClient.getPublicClient().isPresent()) %7B%0A%09%09%09this.bioSamplesClient = bioSamplesClient.getPublicClient().get();%0A%09%09%7D else %7B%0A%09
 %09%09this.b
@@ -1093,16 +1093,20 @@
 Client;%0A
+%09%09%7D%0A
 %09%09this.o

make sure export public samples only