package uk.ac.ebi.atlas.thirdpartyintegration.ebeye;

import com.google.common.base.Joiner;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import uk.ac.ebi.atlas.model.differential.DifferentialExperiment;
import uk.ac.ebi.atlas.trader.ExperimentTrader;

import javax.inject.Inject;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: barrera
 * Date: 09/01/2014
 *
 * Generates a tsv url with a List of all contrast details for all differential experiments
 *
 */
@Controller
@Scope("request")
public class DifferentialExperimentContrastsTsvController {

    private ExperimentTrader experimentTrader;

    @Inject
    public DifferentialExperimentContrastsTsvController(ExperimentTrader experimentTrader) {
        this.experimentTrader = experimentTrader;
    }

    @RequestMapping(value = "/api/contrastdetails.tsv", method = RequestMethod.GET)
    public void generateTsvFormat(HttpServletResponse response) throws IOException {
        getAllDifferentialExperimentsContrastLines(response);
    }

    private void getAllDifferentialExperimentsContrastLines(HttpServletResponse response) throws IOException {
        DifferentialExperimentContrastLines diffExperimentContrastLines;
        DifferentialExperiment differentialExperiment;

        Set<String> microArrayExpAccessions = new HashSet<>(experimentTrader.getMicroarrayExperimentAccessions());
        Set<String> rnaSeqDiffExpAccessions = new HashSet<>(experimentTrader.getRnaSeqDifferentialExperimentAccessions());

        List<String> allExperimentsList = new ArrayList<>();
        allExperimentsList.addAll(microArrayExpAccessions);
        allExperimentsList.addAll(rnaSeqDiffExpAccessions);

        for(String experimentAccession : allExperimentsList){
            differentialExperiment = (DifferentialExperiment) experimentTrader.getPublicExperiment(experimentAccession);
            diffExperimentContrastLines = new DifferentialExperimentContrastLines(differentialExperiment);
            extractLinesToTSVFormat(diffExperimentContrastLines, response);
        }
    }

    private void extractLinesToTSVFormat(DifferentialExperimentContrastLines diffExperimentContrastLines, HttpServletResponse response) throws IOException {
        PrintWriter writer = response.getWriter();

        response.setContentType("text/tab-separated-values");

        Iterator<String[]> lines = diffExperimentContrastLines.iterator();

        while (lines.hasNext()){
            String[] line = lines.next();
            String lineTab = Joiner.on("\t").join(line);
            writer.write(lineTab + "\n");
        }
    }
}

@@ -584,182 +584,8 @@
 *;%0A%0A
-/**%0A * Created with IntelliJ IDEA.%0A * User: barrera%0A * Date: 09/01/2014%0A *%0A * Generates a tsv url with a List of all contrast details for all differential experiments%0A *%0A */%0A
 @Con
@@ -2447,25 +2447,21 @@
     
-Iterator%3C
+for (
 String%5B%5D
 %3E li
@@ -2460,17 +2460,15 @@
 ng%5B%5D
-%3E
  line
-s =
+ :
  dif
@@ -2495,96 +2495,11 @@
 ines
-.iterator();%0A%0A        while (lines.hasNext())%7B%0A            String%5B%5D line = lines.next();
+) %7B
 %0A   
@@ -2604,16 +2604,17 @@
       %7D%0A
-
     %7D%0A
+%0A
 %7D%0A

Replace while with for each