package org.intermine.web.struts;

/*
 * Copyright (C) 2002-2008 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.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

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.path.Path;
import org.intermine.pathquery.PathNode;
import org.intermine.pathquery.PathQuery;
import org.intermine.web.logic.Constants;
import org.intermine.web.logic.query.MainHelper;

/**
 * Controller for the main paths tile.
 * @author Thomas Riley
 */
public class QueryBuilderPathsController extends TilesAction
{
    /**
     * {@inheritDoc}
     */
    public ActionForward execute(@SuppressWarnings("unused") ComponentContext context,
                                 @SuppressWarnings("unused") ActionMapping mapping,
                                 @SuppressWarnings("unused") ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
        throws Exception {
        populateRequest(request, response);
        return null;
    }

    private static void populateRequest(HttpServletRequest request,
                                        @SuppressWarnings("unused") HttpServletResponse response) {
        HttpSession session = request.getSession();
        PathQuery query = (PathQuery) session.getAttribute(Constants.QUERY);
        // First merge the query and the view
        PathQuery q = query.clone();
        Map<String, PathNode> qNodes = q.getNodes();
        for (Path p : q.getView()) {
            String path = p.toStringNoConstraints();
            if (!qNodes.containsKey(path)) {
                q.addNode(path);
            }
        }
        
        Set<String> constrainedPaths = new HashSet<String>();
        for (Map.Entry<String, PathNode> entry : q.getNodes().entrySet()) {
            if (entry.getValue().isAttribute()) {
                PathNode node = entry.getValue();
                if (!node.getConstraints().isEmpty()) {
                    constrainedPaths.add(node.getPrefix());
                }
            }
        }
        
        Set<String> clickableNodes = new HashSet<String>();
		for (Map.Entry<String, PathNode> entry : q.getNodes().entrySet()) {
            PathNode node = entry.getValue();
			if (!entry.getValue().isAttribute()) {
				if (entry.getKey().indexOf('.') == -1
						&& entry.getKey().indexOf(':') == -1) {
				} else if ((!node.isOuterJoin())
						&& node.isReference()
						&& ((!node.getConstraints().isEmpty()) || constrainedPaths
								.contains(entry.getKey()))) {
				} else {
					clickableNodes.add(entry.getKey());
				}
			}
		}
        
		request.setAttribute("clickableNodes", clickableNodes);
        request.setAttribute("qNodes", q.getNodes());
        request.setAttribute("constraintDisplayValues", MainHelper.makeConstraintDisplayMap(query));
    }
}

@@ -373,18 +373,8 @@
 til.
-LinkedHash
 Map;
@@ -395,11 +395,10 @@
 til.
-Lis
+Se
 t;%0Ai
@@ -417,33 +417,15 @@
 til.
-Map;%0Aimport java.util.Set
+TreeMap
 ;%0A%0Ai
@@ -1260,37 +1260,16 @@
 ontext,%0A
-                     
         
@@ -1323,37 +1323,16 @@
 apping,%0A
-                     
         
@@ -1380,37 +1380,16 @@
 m form,%0A
-                     
         
@@ -1420,37 +1420,16 @@
 equest,%0A
-                     
         
@@ -1462,20 +1462,16 @@
 sponse)%0A
-    
     thro
@@ -1629,36 +1629,8 @@
 st,%0A
-                            
     
@@ -1913,61 +1913,8 @@
 ();%0A
-        Map%3CString, PathNode%3E qNodes = q.getNodes();%0A
     
@@ -2017,21 +2017,27 @@
   if (!q
+.get
 Nodes
+()
 .contain
@@ -2045,24 +2045,28 @@
 Key(path)) %7B
+    
 %0A           
@@ -2107,32 +2107,24 @@
 %7D%0A        %7D%0A
-        
 %0A        Set
@@ -2508,24 +2508,16 @@
       %7D%0A
-        
 %0A       
@@ -2569,18 +2569,24 @@
 ing%3E();%0A
-%09%09
+        
 for (Map
@@ -2691,19 +2691,28 @@
 alue();%0A
-%09%09%09
+            
 if (!ent
@@ -2742,20 +2742,32 @@
 te()) %7B%0A
-%09%09%09%09
+                
 if (entr
@@ -2796,22 +2796,40 @@
 ) == -1%0A
-%09%09%09%09%09%09
+                        
 && entry
@@ -2856,28 +2856,40 @@
 ') == -1) %7B%0A
-%09%09%09%09
+                
 %7D else if ((
@@ -2913,219 +2913,415 @@
 ())%0A
-%09%09%09%09%09%09&& node.isReference()%0A%09%09%09%09%09%09&& ((!node.getConstraints().isEmpty()) %7C%7C constrainedPaths%0A%09%09%09%09%09%09%09%09.contains(entry.getKey()))) %7B%0A%09%09%09%09%7D else %7B%0A%09%09%09%09%09clickableNodes.add(entry.getKey());%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%0A        %0A%09%09
+                        && node.isReference()%0A                        && ((!node.getConstraints().isEmpty()) %7C%7C constrainedPaths%0A                                .contains(entry.getKey()))) %7B%0A                %7D else %7B%0A                    clickableNodes.add(entry.getKey());%0A                %7D%0A            %7D%0A        %7D%0A        Map%3CString, PathNode%3E qNodes = new TreeMap%3CString, PathNode%3E(q.getNodes());%0A%0A        
 requ
@@ -3412,27 +3412,21 @@
 odes%22, q
-.get
 Nodes
-()
 );%0A     

Sort the query summary display by path name. Refs #1957.