package org.intermine.web;

/*
 * Copyright (C) 2002-2004 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 javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.tiles.ComponentContext;
import org.apache.struts.tiles.actions.TilesAction;

import org.intermine.objectstore.query.SimpleConstraint;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;

/**
 * Controller for the template page
 * @author Mark Woodbridge
 */
public class TemplateController extends TilesAction
{
    /**
     * @see TilesAction#execute
     */
    public ActionForward execute(ComponentContext context,
                                 ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
        throws Exception {
        HttpSession session = request.getSession();
        ServletContext servletContext = session.getServletContext();
        Map templateQueries = (Map) servletContext.getAttribute(Constants.TEMPLATE_QUERIES);

        String queryName = request.getParameter("name");
        if (queryName == null) {
            //validation failed last time
            queryName = (String) session.getAttribute("queryName");
        }

        //this to work around struts crappiness - the controller should be configured to expect
        //a templateForm, but when it is the controller isn't called on page redisplay if validation
        //fails. so we retrieve it manually, creating it if necessary.
        boolean populate = true;//false;
        TemplateForm templateForm = (TemplateForm) session.getAttribute("templateForm");
        if (templateForm == null) {
            templateForm = new TemplateForm();
            session.setAttribute("templateForm", templateForm);
            populate = true;
        }

        TemplateQuery template = (TemplateQuery) templateQueries.get(queryName);

        Map ops = new HashMap();
        Map names = new HashMap();

        //for each node with an editable constraint, store the valid ops for those constraints
        //and the human-readable "name" for each node (Department.company.name -> "Company namae")
        for (Iterator i = template.getNodes().iterator(); i.hasNext();) {
            PathNode node = (PathNode) i.next();

            ops.put(node, MainHelper.mapOps(SimpleConstraint.validOps(MainHelper.
                                                                      getClass(node.getType()))));

            PathNode parent = (PathNode) template.getQuery().getNodes().
                get(node.getPath().substring(0, node.getPath().lastIndexOf(".")));
            names.put(node, parent.getType() + " "
                      + node.getPath().substring(node.getPath().lastIndexOf(".") + 1));
            
            if (populate) {
                Constraint c = (Constraint) node.getConstraints().get(0);
                int j = template.getNodes().indexOf(node);
                templateForm.setAttributeValues("" + (j + 1), "" + c.getValue());
                templateForm.setAttributeOps("" + (j + 1), "" + c.getOp().getIndex());
            }
        }

        session.setAttribute("queryName", queryName);
        request.setAttribute("templateQuery", template);
        request.setAttribute("ops", ops);
        request.setAttribute("names", names);

        return null;
    }
}

@@ -500,45 +500,77 @@
 ort 
-org.apache.struts.action.ActionF
+java.util.Map;%0Aimport java.util.HashMap;%0Aimport java.util.Iterat
 or
-m
 ;%0A
+%0A
 impo
@@ -603,23 +603,16 @@
 n.Action
-Forward
 ;%0Aimport
@@ -643,23 +643,20 @@
 n.Action
-Mapping
+Form
 ;%0Aimport
@@ -678,30 +678,28 @@
 uts.
-tiles.ComponentContext
+action.ActionForward
 ;%0Aim
@@ -725,33 +725,28 @@
 uts.
-tiles.actions.TilesAction
+action.ActionMapping
 ;%0A%0Ai
@@ -804,83 +804,8 @@
 int;
-%0Aimport java.util.Map;%0Aimport java.util.HashMap;%0Aimport java.util.Iterator;
 %0A%0A/*
@@ -913,21 +913,16 @@
 extends 
-Tiles
 Action%0A%7B
@@ -1007,67 +1007,8 @@
 ute(
-ComponentContext context,%0A                                 
 Acti
@@ -1227,16 +1227,64 @@
 ption %7B%0A
+        org.intermine.Logger.log(%22controller%22);%0A
         
@@ -1486,24 +1486,99 @@
 _QUERIES);%0A%0A
+        boolean populate = true;%0A        TemplateForm templateForm = null;%0A
         Stri
@@ -1669,16 +1669,63 @@
       //
+have been directed to this page as a result of 
 validati
@@ -1735,19 +1735,10 @@
 fail
-ed last tim
+ur
 e%0A  
@@ -1815,340 +1815,11 @@
     
-%7D%0A%0A        //this to work around struts crappiness - the controller should be configured to expect%0A        //a templateForm, but when it is the controller isn't called on page redisplay if validation%0A        //fails. so we retrieve it manually, creating it if necessary.%0A        boolean populate = true;//false;%0A        TemplateForm
+   
  tem
@@ -1895,33 +1895,44 @@
     
-if (templateForm == null)
+    populate = false;%0A        %7D else
  %7B%0A 
@@ -2045,37 +2045,8 @@
 m);%0A
-            populate = true;%0A
     

Fix template query form repopulation