package org.intermine.bio.web.widget;

/*
 * Copyright (C) 2002-2007 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

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

import org.intermine.objectstore.query.ConstraintOp;

import org.intermine.metadata.Model;
import org.intermine.objectstore.ObjectStore;
import org.intermine.web.logic.bag.InterMineBag;
import org.intermine.web.logic.query.Constraint;
import org.intermine.web.logic.query.MainHelper;
import org.intermine.web.logic.query.PathNode;
import org.intermine.web.logic.query.PathQuery;
import org.intermine.web.logic.widget.EnrichmentWidgetURLQuery;


/**
 * Builds a query to get all the genes (in bag) associated with specified go term.
 * @author Julie Sullivan
 */
public class GoStatURLQuery implements EnrichmentWidgetURLQuery
{
    ObjectStore os;
    InterMineBag bag;
    String key;
    /**
     * @param os
     * @param key
     * @param bag
     */
     public GoStatURLQuery(ObjectStore os, InterMineBag bag, String key) {
         this.bag = bag;
         this.key = key;
         this.os = os;
     }
    
    /**
     * @return Query a query to generate the results needed
     */
     public PathQuery generatePathQuery() {
        
         Model model = os.getModel();
         PathQuery q = new PathQuery(model);
         
         List view = new ArrayList();
         view.add(MainHelper.makePath(model, q, "Gene.identifier"));
         view.add(MainHelper.makePath(model, q, "Gene.organismDbId"));
         view.add(MainHelper.makePath(model, q, "Gene.name"));
         view.add(MainHelper.makePath(model, q, "Gene.organism.name"));
         view.add(MainHelper.makePath(model, q, "Gene.allGoAnnotation.identifier"));
         view.add(MainHelper.makePath(model, q, "Gene.allGoAnnotation.name"));
         
         q.setView(view);
         
         String bagType = bag.getType();
         ConstraintOp constraintOp = ConstraintOp.IN;
         String constraintValue = bag.getName();        
         String label = null, id = null, code = q.getUnusedConstraintCode();
         Constraint c = new Constraint(constraintOp, constraintValue, false, label, code, id, null);
         q.addNode(bagType).getConstraints().add(c);
         
         // can't be a NOT relationship!
         constraintOp = ConstraintOp.IS_NULL;
         code = q.getUnusedConstraintCode();
         PathNode qualifierNode = q.addNode("Gene.allGoAnnotation.qualifier");
         Constraint qualifierConstraint 
                         = new Constraint(constraintOp, null, false, label, code, id, null);
         qualifierNode.getConstraints().add(qualifierConstraint);
         
         // go term
         constraintOp = ConstraintOp.EQUALS;
         code = q.getUnusedConstraintCode();
         PathNode goTermNode = q.addNode("Gene.allGoAnnotation.identifier");
         Constraint goTermConstraint 
                         = new Constraint(constraintOp, key, false, label, code, id, null);
         goTermNode.getConstraints().add(goTermConstraint);
         
         q.setConstraintLogic("A and B and C");
         q.syncLogicExpression("and");
        
        return q;
    }
}

@@ -1988,32 +1988,224 @@
 tation.name%22));%0A
+         view.add(MainHelper.makePath(model, q, %22Gene.allGoAnnotation.actualGoTerms.identifier%22));%0A         view.add(MainHelper.makePath(model, q, %22Gene.allGoAnnotation.actualGoTerms.name%22));%0A
          %0A      

include actual go terms in widget query. Refs #1333