/*
 * The Gemma project
 * 
 * Copyright (c) 2006 University of British Columbia
 * 
 * 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.
 *
 */
package ubic.gemma.web.controller.common;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import ubic.gemma.analysis.report.WhatsNew;
import ubic.gemma.analysis.report.WhatsNewService;
import ubic.gemma.model.expression.arrayDesign.ArrayDesignService;
import ubic.gemma.model.expression.bioAssay.BioAssayService;
import ubic.gemma.model.expression.experiment.ExpressionExperimentService;
import ubic.gemma.model.genome.Taxon;
import ubic.gemma.util.monitor.Monitored;

/**
 * Responsible for display of the Gemma home page.
 * 
 * @author joseph
 * @version $Id$
 */
@Controller
public class HomePageController {

    @Autowired
    private ExpressionExperimentService expressionExperimentService;

    @Autowired
    private BioAssayService bioAssayService;

    @Autowired
    private ArrayDesignService arrayDesignService;

    @Autowired
    private WhatsNewService whatsNewService;

    private ModelAndView mav = new ModelAndView();

    private Taxon otherTaxa;

    public HomePageController() {
        otherTaxa = Taxon.Factory.newInstance();
        otherTaxa.setId( -1L );
        otherTaxa.setCommonName( "Other" );
        otherTaxa.setScientificName( "Other" );
        otherTaxa.setAbbreviation( "Other" );
        otherTaxa.setIsGenesUsable( false );
        otherTaxa.setIsSpecies( false );
    }

    @RequestMapping("/mainMenu.html")
    public ModelAndView showHomePage() throws Exception {

        /*
         * Note that this needs to be fast. The queries involved almost always result in a O(1) cache hit. Don't add new
         * functionality here without considering that.
         */
        updateCounts();
        return mav;
    }

    /**
     * FIXME : I wish we could use @Scheduled to update this kind of thing periodically.
     * 
     * @param
     */
    public void updateCounts() {
        Map<String, Long> stats = new HashMap<String, Long>();

        long bioAssayCount = bioAssayService.countAll();
        long arrayDesignCount = arrayDesignService.countAll();

        Map<Taxon, Long> eesPerTaxon = expressionExperimentService.getPerTaxonCount();
        long expressionExperimentCount = 0;
        long otherTaxaEECount = 0;
        for ( Iterator<Taxon> it = eesPerTaxon.keySet().iterator(); it.hasNext(); ) {
            Taxon t = it.next();
            Long c = eesPerTaxon.get( t );
            // TODO problem with this is we want to make a link to them.
            if ( c < 10 ) {
                otherTaxaEECount += c;
                it.remove();
            }
            expressionExperimentCount += c;
        }
        if ( otherTaxaEECount > 0 ) {
            // eesPerTaxon.put( otherTaxa, otherTaxaEECount );
        }

        WhatsNew wn = whatsNewService.retrieveReport();

        stats.put( "bioAssayCount", bioAssayCount );
        stats.put( "arrayDesignCount", arrayDesignCount );

        mav.addObject( "stats", stats );
        mav.addObject( "taxonCount", eesPerTaxon );
        mav.addObject( "expressionExperimentCount", expressionExperimentCount );
        if ( wn != null && wn.getDate() != null ) {
            mav.addObject( "whatsNew", wn );
        }

    }

}

@@ -685,16 +685,45 @@
 ommon;%0A%0A
+import java.util.Comparator;%0A
 import j
@@ -740,16 +740,16 @@
 ashMap;%0A
-
 import j
@@ -788,16 +788,42 @@
 til.Map;
+%0Aimport java.util.TreeMap;
 %0A%0Aimport
@@ -1385,50 +1385,8 @@
 xon;
-%0Aimport ubic.gemma.util.monitor.Monitored;
 %0A%0A/*
@@ -2935,38 +2935,362 @@
     
-Map%3CTaxon, Long%3E eesPerTaxon =
+/*%0A         * Sort taxa by name.%0A         */%0A        TreeMap%3CTaxon, Long%3E eesPerTaxon = new TreeMap%3CTaxon, Long%3E( new Comparator%3CTaxon%3E() %7B%0A            @Override%0A            public int compare( Taxon o1, Taxon o2 ) %7B%0A                return o1.getScientificName().compareTo( o2.getScientificName() );%0A            %7D%0A        %7D );%0A        eesPerTaxon.putAll(
  exp
@@ -3332,18 +3332,21 @@
 nCount()
+ )
 ;%0A
+%0A
         
@@ -3675,24 +3675,105 @@
  c %3C 10 ) %7B%0A
+                // temporary, hide 'uncommon' taxa from this table. See bug 2052%0A
             
@@ -4460,17 +4460,16 @@
      %7D%0A%0A
+
     %7D%0A
-%0A
 %7D%0A

sort taxa by name