package uk.ac.ebi.atlas.controllers.page;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.gson.Gson;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import uk.ac.ebi.atlas.experiments.ExperimentInfoListService;
import static uk.ac.ebi.atlas.model.experiment.ExperimentType.*;
import uk.ac.ebi.atlas.species.SpeciesInfoListService;
import uk.ac.ebi.atlas.species.SpeciesProperties;
import uk.ac.ebi.atlas.species.SpeciesPropertiesTrader;
import uk.ac.ebi.atlas.trader.ExperimentTrader;

import javax.inject.Inject;
import java.util.Random;

@Controller
public class HomeController {

    private static final String NORMAL_SEPARATOR = "━━━━━━━━━━━━━━━━━";
    private static final String BEST_SEPARATOR = "(╯°□°）╯︵ ┻━┻";
    private static final Random RANDOM = new Random();

    private static final Gson gson = new Gson();

    private final SpeciesPropertiesTrader speciesPropertiesTrader;
    private final ExperimentInfoListService experimentInfoListService;
    private final SpeciesInfoListService speciesInfoListService;

    @Autowired
    private Environment env;

    @Inject
    public HomeController(SpeciesPropertiesTrader speciesPropertiesTrader, ExperimentTrader experimentTrader){
        this.speciesPropertiesTrader = speciesPropertiesTrader;
        experimentInfoListService = new ExperimentInfoListService(experimentTrader,
                ImmutableSet.of(RNASEQ_MRNA_BASELINE, PROTEOMICS_BASELINE, RNASEQ_MRNA_DIFFERENTIAL,
                        MICROARRAY_1COLOUR_MICRORNA_DIFFERENTIAL, MICROARRAY_1COLOUR_MRNA_DIFFERENTIAL, MICROARRAY_2COLOUR_MRNA_DIFFERENTIAL));
        speciesInfoListService = new SpeciesInfoListService(speciesPropertiesTrader, experimentInfoListService);
    }

    @RequestMapping(value = "/home")
    public String getHome(Model model) {
        ImmutableMap.Builder<String, String> topSixSelectBuilder = ImmutableMap.builder();
        for (String speciesName: speciesInfoListService.getTopSixSpecies()) {
            topSixSelectBuilder.put(speciesName, StringUtils.capitalize(speciesName));
        }
        model.addAttribute("topSixByExperimentCount", topSixSelectBuilder.build());

        model.addAttribute("separator", RANDOM.nextDouble() < 0.001 ? BEST_SEPARATOR : NORMAL_SEPARATOR);

        ImmutableMap.Builder<String, String> organismSelectBuilder = ImmutableMap.builder();
        organismSelectBuilder.put("", "Any");
        for (SpeciesProperties speciesProperties : speciesPropertiesTrader.getAll()) {
            organismSelectBuilder.put(speciesProperties.referenceName(), StringUtils.capitalize(speciesProperties.referenceName()));
        }

        model.addAttribute("organisms", organismSelectBuilder.build());
        model.addAttribute("organismPath", ""); // Required by Spring form tag

        model.addAllAttributes(experimentInfoListService.getLatestExperimentsListAttributes());

        model.addAttribute("resourcesVersion", env.getProperty("resources.version"));

        model.addAttribute("speciesList", gson.toJson(speciesInfoListService.getTopSixSpeciesByExperimentCount()));
        model.addAttribute("animalsList", gson.toJson(speciesInfoListService.getFilterByKingdom("animals")));
        model.addAttribute("plantsList", gson.toJson(speciesInfoListService.getFilterByKingdom("plants")));
        model.addAttribute("fungiList", gson.toJson(speciesInfoListService.getFilterByKingdom("fungi")));

        model.addAttribute("organismPath", ""); // Required by Spring form tag

        return "foundation-home";
    }

}

@@ -208,120 +208,8 @@
 ls;%0A
-import org.springframework.beans.factory.annotation.Autowired;%0Aimport org.springframework.core.env.Environment;%0A
 impo
@@ -381,45 +381,51 @@
 las.
-experiments.ExperimentInfoListService
+controllers.HtmlExceptionHandlingController
 ;%0Aim
@@ -429,23 +429,16 @@
 %0Aimport 
-static 
 uk.ac.eb
@@ -445,22 +445,16 @@
 i.atlas.
-model.
 experime
@@ -455,16 +455,17 @@
 periment
+s
 .Experim
@@ -471,14 +471,23 @@
 ment
-Type.*
+InfoListService
 ;%0Aim
@@ -752,37 +752,143 @@
 m;%0A%0A
-@Controller%0Apublic class Home
+import static uk.ac.ebi.atlas.model.experiment.ExperimentType.*;%0A%0A@Controller%0Apublic class HomeController extends HtmlExceptionHandling
 Cont
@@ -1348,53 +1348,8 @@
 e;%0A%0A
-    @Autowired%0A    private Environment env;%0A%0A
     

Make HomeController a subclass of HtmlExceptionHandlingController