/*
 * Copyright 2008-2012 Microarray Informatics Team, EMBL-European Bioinformatics Institute
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *
 * For further details of the Gene Expression Atlas project, including source code,
 * downloads and documentation, please see:
 *
 * http://gxa.github.com/gxa
 */

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

import com.google.gson.Gson;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import uk.ac.ebi.atlas.model.Experiment;
import uk.ac.ebi.atlas.model.caches.ExperimentsCache;
import uk.ac.ebi.atlas.web.ApplicationProperties;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Scope("singleton")
public class HomePageController {

    private ApplicationProperties properties;

    private ExperimentsCache experimentsCache;

    private Map<String, Double> counts;

    private Map<String, List<String>> speciesToExperiments;

    @Inject
    public HomePageController(ApplicationProperties properties, ExperimentsCache experimentsCache) {
        this.properties = properties;
        this.experimentsCache = experimentsCache;
        extractFactorValueCounts();
    }

    @RequestMapping("/home")
    public String getHomePage(Model model) {

        ArrayList<WordWeight> wordList = new ArrayList<>();

        for (String factor : counts.keySet()) {
            wordList.add(new WordWeight(factor, counts.get(factor)));
        }

        // does the serialisation to JSON
        Gson gson = new Gson();

        // add data to model
        model.addAttribute("wordlist", gson.toJson(wordList));
        model.addAttribute("speciesToExperiments", speciesToExperiments);

        return "home";
    }

    /**
     * Gets all experiments in data directory and collates experimental factor occurrences into map
     */
    private void extractFactorValueCounts() {

        counts = new HashMap<>();

        speciesToExperiments = new HashMap<>();

        int totalNumberExperiments = 0;

        // check mage-tab directory for its children
        for (String experimentAccession : properties.getExperimentIdentifiers()) {

            // get experiment for directory name
            Experiment experiment = experimentsCache.getExperiment(experimentAccession);

            totalNumberExperiments++;

            if (!speciesToExperiments.containsKey(experiment.getSpecie()))
                speciesToExperiments.put(experiment.getSpecie(), new ArrayList<String>());
            speciesToExperiments.get(experiment.getSpecie()).add(experimentAccession);

            // count per experiment and sum across all experiments
            for (String factor : experiment.getAllExperimentalFactors()) {
                if (!counts.containsKey(factor))
                    counts.put(factor, 0.0);
                counts.put(factor, counts.get(factor) +
                        1.0);
            }

        }

        // normalise for total number of experiments
        for (String factor : counts.keySet()) {
            counts.put(factor, counts.get(factor) / totalNumberExperiments);
        }
    }

    private class WordWeight {

        String factorValue;
        double weight;

        WordWeight(String factorValue, double weight) {
            this.factorValue = factorValue;
            this.weight = weight;
        }

    }
}
@@ -1834,44 +1834,8 @@
 he;%0A
-        extractFactorValueCounts();%0A
     
@@ -1908,24 +1908,124 @@
 l model) %7B%0A%0A
+        // lazy initialisation%0A        if (counts == null)%0A            extractFactorValueCounts();%0A%0A
         Arra
@@ -3030,32 +3030,74 @@
 entAccession);%0A%0A
+            if (experiment != null) %7B%0A    
             tota
@@ -3119,16 +3119,20 @@
 nts++;%0A%0A
+    
         
@@ -3206,32 +3206,36 @@
                 
+  
+  
 speciesToExperim
@@ -3289,24 +3289,28 @@
 String%3E());%0A
+    
             
@@ -3393,24 +3393,28 @@
             
+    
 // count per
@@ -3460,32 +3460,36 @@
 nts%0A            
+  
+  
 for (String fact
@@ -3531,24 +3531,28 @@
 actors()) %7B%0A
+    
             
@@ -3600,32 +3600,36 @@
                 
+    
 counts.put(facto
@@ -3645,32 +3645,36 @@
                 
+  
+  
 counts.put(facto
@@ -3725,14 +3725,36 @@
     
+    
 1.0);%0A
+                %7D%0A
     
@@ -3755,33 +3755,32 @@
 %7D%0A            %7D%0A
-%0A
         %7D%0A%0A     
@@ -4016,27 +4016,20 @@
  String 
-factorValue
+text
 ;%0A      
@@ -4119,27 +4119,20 @@
    this.
-factorValue
+text
  = facto

fix for wordcloud on homepage