package org.flymine.web;

/*
 * Copyright (C) 2002-2003 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.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

import org.apache.struts.actions.LookupDispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import org.flymine.metadata.ClassDescriptor;
import org.flymine.metadata.presentation.DisplayClassDescriptor;
import org.flymine.objectstore.query.*;
import org.flymine.objectstore.query.presentation.QueryCreator;

/**
 * Implementation of <strong>Action</strong> that runs a Query
 *
 * @author Andrew Varley
 */

public class QueryAction extends LookupDispatchAction
{
    /**
     * Process the specified HTTP request, and create the corresponding HTTP
     * response (or forward to another web component that will create it).
     * Return an <code>ActionForward</code> instance describing where and how
     * control should be forwarded, or <code>null</code> if the response has
     * already been completed.
     *
     * @param mapping The ActionMapping used to select this instance
     * @param form The optional ActionForm bean for this request (if any)
     * @param request The HTTP request we are processing
     * @param response The HTTP response we are creating
     * @return an ActionForward object defining where control goes next
     *
     * @exception Exception if the application business logic throws
     *  an exception
     */
    public ActionForward submit(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
        throws Exception {
        HttpSession session = request.getSession();

        Query query = (Query) session.getAttribute("query");
        if (query == null) {
            query = new Query();
        }


        ClassDescriptor cld = ((DisplayClassDescriptor) session.getAttribute("cld"))
            .getClassDescriptor();
        QueryForm queryForm = (QueryForm) form;
        QueryCreator.addToQuery(query, cld.getName(), queryForm.getFields());

        session.setAttribute("query", query);
        session.removeAttribute("cld");

        return (mapping.findForward("buildquery"));
    }

    /**
     * Distributes the actions to the necessary methods, by providing a Map from action to
     * the name of a method.
     *
     * @return a Map
     */
    protected Map getKeyMethodMap() {
        Map map = new HashMap();
        map.put("button.submit", "submit");
        map.put("button.view", "view");
        return map;
    }
}


@@ -1006,17 +1006,16 @@
 ley%0A */%0A
-%0A
 public c
@@ -2157,17 +2157,25 @@
 sion();%0A
+        
 %0A
-
         
@@ -2300,17 +2300,16 @@
      %7D%0A%0A
-%0A
         
@@ -2416,24 +2416,77 @@
 scriptor();%0A
+        session.removeAttribute(%22cld%22);%0A            %0A
         Quer
@@ -2599,17 +2599,59 @@
 lds());%0A
+        queryForm.reset(mapping, request);
 %0A
-
         
@@ -2700,40 +2700,8 @@
     
-session.removeAttribute(%22cld%22);%0A
 %0A   
@@ -2712,17 +2712,16 @@
  return 
-(
 mapping.
@@ -2745,17 +2745,16 @@
 dquery%22)
-)
 ;%0A    %7D%0A
@@ -2985,24 +2985,24 @@
  HashMap();%0A
+
         map.
@@ -3037,48 +3037,8 @@
 %22);%0A
-        map.put(%22button.view%22, %22view%22);%0A
     

reset the form after a submit