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.goAnnotation.relationships.childTerm.identifier,"
                + "Protein.genes.goAnnotation.relationships.childTerm.identifier,"
                + "Protein.genes.goAnnotation.relationships.parentTerm.identifier,"
                + "Protein.genes.goAnnotation.relationships.parentTerm.identifier";
        } else {
            pathStrings = "Gene.secondaryIdentifier,Gene.primaryIdentifier,"
                + "Gene.name,Gene.organism.name"
                + "Gene.goAnnotation.relationships.childTerm.identifier,"
                + "Gene.goAnnotation.relationships.childTerm.name,"
                + "Gene.goAnnotation.relationships.parentTerm.identifier,"
                + "Gene.goAnnotation.relationships.parentTerm.name";
        }
        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.goAnnotation.qualifier"
                                                         : "Gene.goAnnotation.qualifier");
        q.addConstraint(pathString, Constraints.isNull());

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

@@ -1556,36 +1556,32 @@
 tation.relations
-hips
 .childTerm.ident
@@ -1635,36 +1635,32 @@
 tation.relations
-hips
 .childTerm.ident
@@ -1650,34 +1650,28 @@
 s.childTerm.
-identifier
+name
 ,%22%0A         
@@ -1708,36 +1708,32 @@
 tation.relations
-hips
 .parentTerm.iden
@@ -1788,36 +1788,32 @@
 tation.relations
-hips
 .parentTerm.iden
@@ -1804,34 +1804,28 @@
 .parentTerm.
-identifier
+name
 %22;%0A        %7D
@@ -1996,36 +1996,32 @@
 tation.relations
-hips
 .childTerm.ident
@@ -2066,36 +2066,32 @@
 tation.relations
-hips
 .childTerm.name,
@@ -2130,36 +2130,32 @@
 tation.relations
-hips
 .parentTerm.iden
@@ -2209,20 +2209,16 @@
 elations
-hips
 .parentT
@@ -2804,18 +2804,15 @@
 ions
-hips.child
+.parent
 Term

fix URL query. parent GO terms are being passed to the widget