package edu.columbia.gemma.genome.gene;


import java.util.ArrayList;
import java.util.Collection;

import edu.columbia.gemma.BaseDAOTestCase;
import edu.columbia.gemma.genome.Gene;
import edu.columbia.gemma.genome.GeneDao;
import edu.columbia.gemma.genome.Taxon;
import edu.columbia.gemma.genome.TaxonDao;

/**
 *
 *
 * <hr>
 * <p>Copyright (c) 2004, 2005 Columbia University
 * @author daq2101
 * @version $Id$
 */ 
public class CandidateGeneListImplTest extends BaseDAOTestCase {

    private TaxonDao daoTaxon = null;
    private Taxon t = null;
    private GeneDao daoGene = null;
    private CandidateGeneList cgl = null;
    private CandidateGeneListDao daoCGL = null;
    ArrayList dummygenes = null;
    
    private Gene makeGene(String officialName ){
        
        Gene g = null;
        Collection c = daoGene.findByOfficalSymbol(officialName);
		if( c.isEmpty() ){
		    g = Gene.Factory.newInstance();
			g.setName(officialName);
			g.setOfficialSymbol(officialName);
			g.setOfficialName(officialName);
			g.setTaxon(t);
			daoGene.create(g);
		}
		else
		    g = (Gene)c.iterator().next();
		
		return g;	
    }
    protected void setUp() throws Exception {
        super.setUp();
        // create dummy taxon for Mouse
		daoTaxon = (TaxonDao) ctx.getBean("taxonDao");
		daoGene = (GeneDao) ctx.getBean("geneDao");
		
		t = daoTaxon.findByCommonName("mouse");
		if( t==null){
		    t = Taxon.Factory.newInstance();
		    t.setCommonName("mouse");
			t.setScientificName("mouse");
			daoTaxon.create(t);
		}
		
		// create new list for fun
		daoCGL = (CandidateGeneListDao) ctx.getBean("candidateGeneListDao");
		cgl = CandidateGeneList.Factory.newInstance();
		cgl.setDescription("Test my candidate list");
		cgl.setName("Test Candidates");
	    daoCGL.create(cgl);
    }
    
    protected void tearDown() throws Exception {
        super.tearDown();
        daoGene = null;
		daoTaxon.remove(t);
		daoTaxon=null;
		daoCGL=null;
    }

    public void testUseCandidateGeneList() {
        
        CandidateGeneListServiceImpl cglService = new CandidateGeneListServiceImpl();
        
        Gene gene1 = makeGene("foo1");
        Gene gene2 = makeGene("foo2");
        Gene gene3 = makeGene("foo3");
        
        // add some new genes as candidategenes (using makeGene shortcut)
        CandidateGene cg1 = cglService.handleAddCandidateToList(cgl,gene1);
        CandidateGene cg2 = cglService.handleAddCandidateToList(cgl,gene2);
        CandidateGene cg3 = cglService.handleAddCandidateToList(cgl,gene3);
        
        cglService.handleRemoveCandidateFromList(cgl, cg1);
        cglService.handleRemoveCandidateFromList(cgl, cg2);
        cglService.handleRemoveCandidateFromList(cgl, cg3);
        daoGene.remove(gene1);
        daoGene.remove(gene2);
        daoGene.remove(gene3);
    }
}

@@ -665,24 +665,67 @@
 CGL = null;%0A
+    private CandidateGeneDao daoCG = null;%0A
     ArrayLis
@@ -1663,24 +1663,86 @@
 eListDao%22);%0A
+%09%09daoCG = (CandidateGeneDao) ctx.getBean(%22candidateGeneDao%22);%0A
 %09%09cgl = Cand
@@ -2142,20 +2142,16 @@
 tService
-Impl
  cglServ
@@ -2160,13 +2160,49 @@
 e = 
-new C
+(CandidateGeneListService) ctx.getBean(%22c
 andi
@@ -2224,13 +2224,9 @@
 vice
-Impl(
+%22
 );%0A 
@@ -2350,32 +2350,34 @@
 foo3%22);%0A        
+/*
 %0A        // add 
@@ -2373,42 +2373,37 @@
     
-// add some new genes as c
+CandidateGene cg1 = C
 andidate
 gene
@@ -2402,39 +2402,63 @@
 date
-genes (using makeGene shortcut)
+Gene.Factory.newInstance();%0A        cg1.setGene(gene1);
 %0A   
@@ -2482,57 +2482,74 @@
 e cg
-1 = cglService.handleAddCandidateToList(cgl,
+2 = CandidateGene.Factory.newInstance();%0A        cg2.setGene(
 gene
-1
+2
 );%0A 
@@ -2575,57 +2575,74 @@
 e cg
-2 = cglService.handleAddCandidateToList(cgl,
+3 = CandidateGene.Factory.newInstance();%0A        cg3.setGene(
 gene
-2
+3
 );%0A 
@@ -2652,264 +2652,411 @@
     
-CandidateGene cg3 = cglService.handleAddCandidateToList(cgl,gene3);%0A        %0A        cglService.handleRemoveCandidateFromList(cgl, cg1);%0A        cglService.handleRemoveCandidateFromList(cgl, cg2);%0A        cglService.handleRemoveCandidateFromList(cgl, cg3);
+daoCG.create(cg1);%0A        daoCG.create(cg2);%0A        daoCG.create(cg3);%0A        cgl.addCandidate(cg1);%0A        cgl.addCandidate(cg2);%0A        cgl.addCandidate(cg3);%0A        daoCGL.update(cgl);%0A        %0A        cgl.removeCandidate(cg3);%0A        cgl.removeCandidate(cg2);%0A        cgl.removeCandidate(cg1);%0A        %0A        daoCG.remove(cg1);%0A        daoCG.remove(cg2);%0A        daoCG.remove(cg3);%0A        
 %0A   
@@ -3141,20 +3141,35 @@
 ove(gene3);%0A
+        */%0A    
     %7D%0A%7D%0A

Work in progress