package org.flymine.web.results;

/*
 * 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 java.util.HashMap;
import java.util.Map;
import java.util.IdentityHashMap;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;

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

import org.flymine.objectstore.query.Results;

/**
 * Implementation of <strong>TilesAction</strong>. Assembles data for
 * a results view.
 *
 * @author Andrew Varley
 */
public class ResultsViewController extends TilesAction
{
    /**
     * The name of the attribute used to store the results map in the
     * session object
     */
    public static final String DISPLAYABLERESULTS_NAME = "resultsTable";

    /** 
     * The name of the attribute used to store the saved bags Map in the
     * session object
     */
    public static final String SAVEDBAGS_NAME = "savedBags";

    /**
     * The name of the attribute used to store the saved bags to saved bag
     * name Map in the session object
     */ 
    public static final String SAVEDBAGSINVERSE_NAME = "savedBagsInverse";

    /**
     * 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 context The Tiles ComponentContext
     * @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 ServletException if a servlet error occurs
     */
    public ActionForward execute(ComponentContext context, ActionMapping mapping, ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws ServletException {
        HttpSession session = request.getSession();

        Results results = (Results) session.getAttribute("results");

        // Do we already have a configuration object? If we do, update the
        // new one to inherit properties off the old one
        DisplayableResults drOrig = (DisplayableResults)
            session.getAttribute(DISPLAYABLERESULTS_NAME);
        DisplayableResults drNew = new DisplayableResults(results);
        if (drOrig != null) {
            drNew.update(drOrig);
        }

        // Do we have any saved bags - if not add an empty map to the session
        Map savedBags = (Map) session.getAttribute(SAVEDBAGS_NAME);
        if (savedBags == null) {
            savedBags = new HashMap();
        }

        Map savedBagsInverse = (Map) session.getAttribute(SAVEDBAGSINVERSE_NAME);
        if (savedBagsInverse == null) {
            savedBagsInverse = new IdentityHashMap();
        }

        // Put required things on the request or session or tile context

        // The DisplayableResults object
        session.setAttribute(DISPLAYABLERESULTS_NAME, drNew);

        // The savedBags Map
        session.setAttribute(SAVEDBAGS_NAME, savedBags);
        session.setAttribute(SAVEDBAGSINVERSE_NAME, savedBagsInverse);

        return null;
    }
}

@@ -861,24 +861,58 @@
 ery.Results;
+%0Aimport org.flymine.web.Constants;
 %0A%0A/**%0A * Imp
@@ -1087,570 +1087,8 @@
 n%0A%7B%0A
-    /**%0A     * The name of the attribute used to store the results map in the%0A     * session object%0A     */%0A    public static final String DISPLAYABLERESULTS_NAME = %22resultsTable%22;%0A%0A    /** %0A     * The name of the attribute used to store the saved bags Map in the%0A     * session object%0A     */%0A    public static final String SAVEDBAGS_NAME = %22savedBags%22;%0A%0A    /**%0A     * The name of the attribute used to store the saved bags to saved bag%0A     * name Map in the session object%0A     */ %0A    public static final String SAVEDBAGSINVERSE_NAME = %22savedBagsInverse%22;%0A%0A
     
@@ -2484,35 +2484,34 @@
 tribute(
-DISPLAYABLE
+Constants.
 RESULTS_
 NAME);%0A 
@@ -2502,19 +2502,20 @@
 RESULTS_
-NAM
+TABL
 E);%0A    
@@ -2652,25 +2652,24 @@
       %7D%0A
-%0A
         
 // Do we
@@ -2664,77 +2664,62 @@
     
-// Do we have any saved bags - if not add an empty map to the session
+session.setAttribute(Constants.RESULTS_TABLE, drNew);%0A
 %0A   
@@ -2766,30 +2766,36 @@
 tribute(
+Constants.
 SAVED
+_
 BAGS
-_NAME
 );%0A     
@@ -2835,26 +2835,57 @@
        s
-avedBags =
+ession.setAttribute(Constants.SAVED_BAGS,
  new Has
@@ -2882,32 +2882,33 @@
 S, new HashMap()
+)
 ;%0A        %7D%0A%0A   
@@ -2962,37 +2962,44 @@
 tribute(
+Constants.
 SAVED
+_
 BAGS
+_
 INVERSE
-_NAME
 );%0A     
@@ -3050,276 +3050,8 @@
    s
-avedBagsInverse = new IdentityHashMap();%0A        %7D%0A%0A        // Put required things on the request or session or tile context%0A%0A        // The DisplayableResults object%0A        session.setAttribute(DISPLAYABLERESULTS_NAME, drNew);%0A%0A        // The savedBags Map%0A        s
 essi
@@ -3070,106 +3070,71 @@
 ute(
+Constants.
 SAVED
+_
 BAGS_
-NAME, savedBags);%0A        session.setAttribute(SAVEDBAGSINVERSE_NAME, savedBagsInverse);
+INVERSE, new IdentityHashMap());%0A        %7D
 %0A%0A  

Use Constants class for session parameter names