package org.intermine.bio.web.widget;

/*
 * Copyright (C) 2002-2009 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 org.intermine.objectstore.ObjectStore;
import org.intermine.pathquery.Constraints;
import org.intermine.pathquery.PathQuery;
import org.intermine.web.logic.bag.InterMineBag;
import org.intermine.web.logic.widget.WidgetURLQuery;


/**
 * {@inheritDoc}
 * @author Julie Sullivan
 */
public class GoStatURLQuery implements WidgetURLQuery
{
    private ObjectStore os;
    private InterMineBag bag;
    private String key;

    /**
     * @param os object store
     * @param key go terms user selected
     * @param bag bag page they were on
     */
    public GoStatURLQuery(ObjectStore os, InterMineBag bag, String key) {
        this.bag = bag;
        this.key = key;
        this.os = os;
    }

    /**
     * {@inheritDoc}
     */
    public PathQuery generatePathQuery() {

        PathQuery q = new PathQuery(os.getModel());
        String bagType = bag.getType();
        String pathStrings;

        if (bagType.equals("Protein")) {
            pathStrings = "Protein.genes.primaryAccession,Protein.genes.primaryIdentifier,"
                + "Protein.genes.name,Protein.genes.organism.name,"
                + "Protein.primaryIdentifier,Protein.primaryAccession"
                + "Protein.genes.allGoAnnotation.identifier,"
                + "Protein.genes.allGoAnnotation.name,"
                + "Protein.genes.allGoAnnotation.actualGoTerms.name,"
                + "Protein.genes.allGoAnnotation.actualGoTerms.identifier";
        } else {
            pathStrings = "Gene.secondaryIdentifier,Gene.primaryIdentifier,"
                + "Gene.name,Gene.organism.name"
                + "Gene.allGoAnnotation.identifier,"
                + "Gene.allGoAnnotation.name,"
                + "Gene.allGoAnnotation.actualGoTerms.name,"
                + "Gene.allGoAnnotation.actualGoTerms.identifier";
        }
        q.setView(pathStrings);
        q.setOrderBy(pathStrings);

        q.addConstraint(bagType, Constraints.in(bag.getName()));
        // can't be a NOT relationship!
        String pathString = (bagType.equals("Protein") ? "Protein.genes.allGoAnnotation.qualifier"
                                                         : "Gene.allGoAnnotation.qualifier");
        q.addConstraint(pathString, Constraints.isNull());

        // go term
        pathString = (bagType.equals("Protein") ? "Protein.genes.allGoAnnotation.property"
                                                  : "Gene.allGoAnnotation.property");
        q.addConstraint(pathString, Constraints.lookup(key), "C", "GOTerm");
        q.setConstraintLogic("A and B and C");
        q.syncLogicExpression("and");
        return q;
    }
}

@@ -1534,36 +1534,33 @@
  %22Protein.genes.
-allG
+g
 oAnnotation.iden
@@ -1547,32 +1547,56 @@
 es.goAnnotation.
+relationships.childTerm.
 identifier,%22%0A   
@@ -1617,36 +1617,33 @@
  %22Protein.genes.
-allG
+g
 oAnnotation.name
@@ -1630,36 +1630,66 @@
 es.goAnnotation.
-name
+relationships.childTerm.identifier
 ,%22%0A             
@@ -1700,36 +1700,33 @@
  %22Protein.genes.
-allG
+g
 oAnnotation.actu
@@ -1721,34 +1721,51 @@
 otation.
-actualGoTerms.name
+relationships.parentTerm.identifier
 ,%22%0A     
@@ -1784,36 +1784,33 @@
  %22Protein.genes.
-allG
+g
 oAnnotation.actu
@@ -1801,37 +1801,48 @@
 oAnnotation.
-actualGo
+relationships.parent
 Term
-s
 .identifier%22
@@ -2002,36 +2002,33 @@
         + %22Gene.
-allG
+g
 oAnnotation.iden
@@ -2023,16 +2023,40 @@
 otation.
+relationships.childTerm.
 identifi
@@ -2076,36 +2076,33 @@
         + %22Gene.
-allG
+g
 oAnnotation.name
@@ -2097,16 +2097,40 @@
 otation.
+relationships.childTerm.
 name,%22%0A 
@@ -2144,36 +2144,33 @@
         + %22Gene.
-allG
+g
 oAnnotation.actu
@@ -2169,26 +2169,43 @@
 ion.
-actualGoTerms.name
+relationships.parentTerm.identifier
 ,%22%0A 
@@ -2223,28 +2223,25 @@
     + %22Gene.
-allG
+g
 oAnnotation.
@@ -2244,32 +2244,37 @@
 ion.
-actualGoTerms.identifier
+relationships.parentTerm.name
 %22;%0A 
@@ -2519,36 +2519,33 @@
  %22Protein.genes.
-allG
+g
 oAnnotation.qual
@@ -2608,36 +2608,33 @@
         : %22Gene.
-allG
+g
 oAnnotation.qual
@@ -2789,119 +2789,93 @@
 enes
-.allGoAnnotation.property%22%0A                                                  : %22Gene.allGoAnnotation.property%22)
+%22 : %22Gene%22);%0A        pathString += %22.goAnnotation.relationships.childTerm.identifier%22
 ;%0A  

fix widget URL query