/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.mskcc.cbio.oncokb.controller;

import io.swagger.annotations.ApiParam;
import org.mskcc.cbio.oncokb.bo.GeneBo;
import org.mskcc.cbio.oncokb.model.Gene;
import org.mskcc.cbio.oncokb.service.JsonResultFactory;
import org.mskcc.cbio.oncokb.util.ApplicationContextSingleton;
import org.mskcc.cbio.oncokb.util.GeneUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jgao
 */
@Controller
public class GeneController {

    @RequestMapping(value = "/legacy-api/gene.json")
    public @ResponseBody
    List<Gene> getGene(
        @RequestParam(value = "entrezGeneId", required = false) List<Integer> entrezGeneIds
        , @RequestParam(value = "hugoSymbol", required = false) List<String> hugoSymbols
        , @RequestParam(value = "fields", required = false) String fields
    ) {

        GeneBo geneBo = ApplicationContextSingleton.getGeneBo();

        if (entrezGeneIds == null && hugoSymbols == null) {
            return geneBo.findAll();
        }

        List<Gene> genes = new ArrayList<Gene>();
        if (entrezGeneIds != null) {
            genes.addAll(geneBo.findGenesByEntrezGeneId(entrezGeneIds));
        } else if (hugoSymbols != null) {
            genes.addAll(geneBo.findGenesByHugoSymbol(hugoSymbols));
        }

        return JsonResultFactory.getGene(genes, fields);
    }

    @RequestMapping(value = "/legacy-api/genes/update/{hugoSymbol}", method = RequestMethod.POST)
    public @ResponseBody
    String updateGene(@ApiParam(value = "hugoSymbol", required = true) @PathVariable("hugoSymbol") String hugoSymbol,
                      @RequestBody(required = true) Gene queryGene) {
        if (!hugoSymbol.equalsIgnoreCase(queryGene.getHugoSymbol())) {
            return "error";
        }
        GeneBo geneBo = ApplicationContextSingleton.getGeneBo();
        Gene gene = GeneUtils.getGeneByHugoSymbol(hugoSymbol);
        gene.setTSG(queryGene.getTSG());
        gene.setOncogene(queryGene.getOncogene());
        geneBo.update(gene);

        return "success";
    }
}

@@ -567,19 +567,67 @@
 va.util.
-Lis
+HashSet;%0Aimport java.util.List;%0Aimport java.util.Se
 t;%0A%0A/**%0A
@@ -1058,75 +1058,8 @@
 ) %7B%0A
-%0A        GeneBo geneBo = ApplicationContextSingleton.getGeneBo();%0A%0A
     
@@ -1137,23 +1137,47 @@
 urn 
-geneBo.findAll(
+new ArrayList%3C%3E(GeneUtils.getAllGenes()
 );%0A 
@@ -1190,27 +1190,26 @@
  %7D%0A%0A        
-Lis
+Se
 t%3CGene%3E gene
@@ -1220,22 +1220,16 @@
 new 
-ArrayList%3CGene
+HashSet%3C
 %3E();
@@ -1282,45 +1282,100 @@
     
-genes.addAll(geneBo.findGenes
+for (Integer enterz : entrezGeneIds) %7B%0A                Gene gene = GeneUtils.getGene
 ByEntrez
 Gene
@@ -1374,115 +1374,264 @@
 trez
-Gene
 Id(ent
-rezGeneIds));%0A        %7D else if (hugoSymbols != null) %7B%0A            genes.addAll(geneBo.find
+erz);%0A                if (gene != null) %7B%0A                    genes.add(gene);%0A                %7D%0A            %7D%0A        %7D else if (hugoSymbols != null) %7B%0A            for (String hugoSymbol : hugoSymbols) %7B%0A                Gene gene = GeneUtils.get
 Gene
-s
 ByHu
@@ -1653,12 +1653,115 @@
 mbol
-s)
 );
+%0A                if (gene != null) %7B%0A                    genes.add(gene);%0A                %7D%0A            %7D
 %0A   
@@ -1809,21 +1809,38 @@
 getGene(
+new ArrayList%3C%3E(
 genes
+)
 , fields
@@ -2285,61 +2285,91 @@
 Gene
-Bo
  gene
-Bo
  = 
-ApplicationContextSingleton.getGeneBo();%0A
+GeneUtils.getGeneByHugoSymbol(hugoSymbol);%0A        if (gene != null) %7B%0A    
     
@@ -2380,56 +2380,58 @@
 Gene
+Bo
  gene
+Bo
  = 
-GeneUtils.getGeneByHugoSymbol(hugoSymbol
+ApplicationContextSingleton.getGeneBo(
 );%0A 
@@ -2429,32 +2429,36 @@
 neBo();%0A        
+  
+  
 gene.setTSG(quer
@@ -2470,24 +2470,28 @@
 .getTSG());%0A
+    
         gene
@@ -2525,24 +2525,28 @@
 ncogene());%0A
+    
         gene
@@ -2562,16 +2562,25 @@
 (gene);%0A
+        %7D
 %0A       

Use GeneUtils over GeneBo; Fix issue when updating gene not in the system