package org.flymine.web.displayer;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.map.ListOrderedMap;
import org.intermine.api.InterMineAPI;
import org.intermine.api.results.ResultElement;
import org.intermine.model.bio.DataSet;
import org.intermine.model.bio.Gene;
import org.intermine.model.bio.Organism;
import org.intermine.model.bio.Homologue;
import org.intermine.pathquery.Path;
import org.intermine.pathquery.PathException;
import org.intermine.web.displayer.CustomDisplayer;
import org.intermine.web.logic.config.ReportDisplayerConfig;
import org.intermine.web.logic.results.ReportObject;

public class DrosophilaHomologueDisplayer extends CustomDisplayer {

    private static final List<String> SPECIES = Arrays.asList(new String[] {"grimshawi", "virilis",
            "mojavensis", "willistoni", "persimilis", "pseudoobscura", "ananassae", "erecta",
            "yakuba", "melanogaster", "sechellia", "simulans"});
    private static final String HOMOLOGY_DATASET = "Drosophila 12 Genomes Consortium homology";

    public DrosophilaHomologueDisplayer(ReportDisplayerConfig config, InterMineAPI im) {
        super(config, im);
    }

    @Override
    public void display(HttpServletRequest request, ReportObject reportObject) {

        Map<String, Set<ResultElement>> homologues = initMap();
        Map<String, String> organismIds = new HashMap<String, String>();

        Path symbolPath = null;
        try {
            symbolPath = new Path(im.getModel(), "Gene.symbol");
        } catch (PathException e) {
            return;
        }

        Gene gene = (Gene) reportObject.getObject();
        Boolean isRecentred = new Boolean(!"melanogaster".equals(gene.getOrganism().getSpecies()));
        String thisSpecies = gene.getOrganism().getSpecies();
        if (isRecentred) {
            request.setAttribute("origSymbol", gene.getSymbol());
            HOMOLOGUES: for (Homologue homologue : gene.getHomologues()) {
                for (DataSet dataSet : homologue.getDataSets()) {
                    if (HOMOLOGY_DATASET.equals(dataSet.getName())) {
                        String species = homologue.getHomologue().getOrganism().getSpecies();
                        if ("melanogaster".equals(species)) {
                            ResultElement re = new ResultElement(homologue.getHomologue(),
                                    symbolPath, true);
                            addToMap(homologues, species, re);
                            gene = homologue.getHomologue();
                            break HOMOLOGUES;
                        }
                    }
                }
            }
        }

        for (Homologue homologue : gene.getHomologues()) {
            for (DataSet dataSet : homologue.getDataSets()) {
                if (HOMOLOGY_DATASET.equals(dataSet.getName())) {
                    Organism org = homologue.getHomologue().getOrganism();
                    organismIds.put(org.getSpecies(), org.getId().toString());
                    ResultElement re = new ResultElement(homologue.getHomologue(),
                            symbolPath, true);
                    String species = org.getSpecies();
                    if (!isRecentred || (isRecentred && !thisSpecies.equals(species))) {
                        addToMap(homologues, species, re);
                    }
                }
            }
        }

        request.setAttribute("organismIds", organismIds);
        request.setAttribute("isRecentred", isRecentred);
        request.setAttribute("homologues", homologues);
    }

    private Map<String, Set<ResultElement>> initMap() {
        Map homologues = new ListOrderedMap();
        for (String species : SPECIES) {
            addToMap(homologues, species, null);
        }
        return homologues;
    }

    private void addToMap(Map<String, Set<ResultElement>> homologues, String species,
            ResultElement re) {
        Set<ResultElement> speciesHomologues = homologues.get(species);
        if (speciesHomologues == null) {
            speciesHomologues = new HashSet<ResultElement>();
            homologues.put(species, speciesHomologues);
        }
        if (re != null) {
            speciesHomologues.add(re);
        }
    }
}

@@ -29,16 +29,315 @@
 layer;%0A%0A
+/*%0A * Copyright (C) 2002-2011 FlyMine%0A *%0A * This code may be freely distributed and modified under the%0A * terms of the GNU Lesser General Public Licence.  This should%0A * be distributed with the code.  See the LICENSE file for more%0A * information or http://www.gnu.org/copyleft/lesser.html.%0A *%0A */%0A%0A%0A
 import j
@@ -1075,16 +1075,82 @@
 bject;%0A%0A
+/**%0A * Displayer for drosophila homologue data%0A * @author rns%0A */%0A
 public c
@@ -1206,17 +1206,17 @@
 isplayer
- 
+%0A
 %7B%0A%0A    p
@@ -1298,16 +1298,24 @@
 mshawi%22,
+%0A       
  %22virili
@@ -1317,28 +1317,16 @@
 irilis%22,
-%0A           
  %22mojave
@@ -1389,16 +1389,24 @@
 nassae%22,
+%0A       
  %22erecta
@@ -1407,28 +1407,16 @@
 erecta%22,
-%0A           
  %22yakuba
@@ -1558,16 +1558,105 @@
 logy%22;%0A%0A
+    /**%0A     * @param config report object config%0A     * @param im intermine API%0A     */%0A
     publ
@@ -2243,17 +2243,17 @@
         
-B
+b
 oolean i
@@ -2269,20 +2269,8 @@
 d = 
-new Boolean(
 !%22me
@@ -2320,17 +2320,16 @@
 ecies())
-)
 ;%0A      
@@ -2485,20 +2485,16 @@
         
-    
 HOMOLOGU
@@ -2496,16 +2496,28 @@
 OLOGUES:
+%0A           
  for (Ho
@@ -2971,32 +2971,35 @@
                 
+   
 symbolPath, true
@@ -4247,16 +4247,44 @@
      Map
+%3CString, Set%3CResultElement%3E%3E
  homolog

revert merge error