Index: branches/WebApplication/MVC2/HLWebOKBQueryPlugin/Controllers/FilterController.cs
===================================================================
--- branches/WebApplication/MVC2/HLWebOKBQueryPlugin/Controllers/FilterController.cs	(revision 6118)
+++ branches/WebApplication/MVC2/HLWebOKBQueryPlugin/Controllers/FilterController.cs	(revision 6141)
@@ -7,4 +7,5 @@
 using HLWebOKBQueryPlugin.Helpers;
 using HLWebOKBQueryPlugin.OKBQueryService;
+using System.Web.Routing;
 
 namespace HLWebOKBQueryPlugin.Controllers
@@ -15,138 +16,178 @@
     public class FilterController : Controller
     {
-        //
-        // GET: /Filter/
-
-        [ChildActionOnly]
-        public ActionResult Filters()
-        {
-            // Model     
+        /// <summary>
+        /// Get run ids from service.
+        /// </summary>
+        /// <returns></returns>
+        public ActionResult GetRuns()
+        {
             FilterModel qm = new FilterModel();
-
-
-            //TODO nicht immer laden
+            qm.Content = (CombinedFilter)Session["Content"];
+
             QueryServiceClient client = Query.GetClientFactory("okbtester", "okbtester");
-            List<Filter> filterList = client.GetFilters().ToList();
-            qm.Filters = filterList;
-            qm.SelectedFilters = new List<Filter>();
-
-
-            // For ModelViewuUserControl
+           long[] ids =  client.GetRunIds(qm.Content);
+
+            Response.Write("Found <" + ids.Count() + "> Runs.");
+
+            return View("Index", qm); // name of usercontrol
+        }
+
+        public ActionResult Filters(CombinedFilter f)
+        {
+            FilterModel qm = new FilterModel(f);
             return PartialView("Filters", qm); // name of usercontrol
         }
 
-        //List<Filter> currentFilters
-
-        [ChildActionOnly]
-        public ActionResult Filters(FilterModel qms)
-        {
-            // Model     
-            FilterModel qm = new FilterModel();
-
-
-            //TODO nicht immer laden
-            QueryServiceClient client = Query.GetClientFactory("okbtester", "okbtester");
-            List<Filter> filterList = client.GetFilters().ToList();
-            qm.Filters = filterList;
-            qm.SelectedFilters =qms.SelectedFilters;
-            if (qm.SelectedFilters == null)
-            {
-
-                qm.SelectedFilters = new List<Filter>();
-            }
-
-
-
-            // For ModelViewuUserControl
-            return PartialView("Filters", qm); // name of usercontrol
-        }
-
-
-      
-
-
-        /// <summary>
-        /// This action will be called from the Filters.ascx
-        /// when submitting the form.
+
+        /// <summary>
+        /// Add a new Filter, based on the selected combobox
+        /// value. Posted values will also be set to the 
+        /// "right" filter(s).
         /// </summary>
         /// <param name="collection"></param>
         /// <returns></returns>
-        public ActionResult AddFilter(FormCollection collection)
-        {
+        public ActionResult AddFilterAnd(FormCollection collection)
+        {
+            CombinedFilter content = (CombinedFilter)Session["Content"];
+            FilterModel fm = new FilterModel(content);
+
             
 
-            // Model     
-            FilterModel qm = new FilterModel();
-
-            // Get filters from service and set the model
-            QueryServiceClient client = Query.GetClientFactory("okbtester", "okbtester");
-            List<Filter> filterList = client.GetFilters().ToList();
-            qm.Filters = filterList;
-
-
-            // Selected filter (from combobox)
-            string filterType = collection.Get("filtersCombobox");
-
-            if (filterType != null)
-            {
-
-                Filter filter = filterList.Where(x => x.FilterTypeName.CompareTo(filterType) == 0).First();
-
-
-                 if (filter != null)
-                {
-                    // Get selected filters form session
-                    qm.SelectedFilters = (List<Filter>)Session["selectedFilters"];
-                    if (qm.SelectedFilters == null)
+            String idKey = null;
+            String parentIdKey = null;
+
+            foreach (string key in collection.AllKeys)
+            {
+                if (key.StartsWith("filtersCombobox"))
+                {
+                    idKey = key;
+                }
+                if (key.StartsWith("parentId"))
+                {
+                    parentIdKey = key;
+                }
+            }
+
+            Guid parentId = new Guid(collection.Get(parentIdKey));
+            string filterTypeName = collection.Get(idKey);
+
+            // get selected filter (combobox)
+            Filter filter = fm.Filters.Where(x => x.FilterTypeName.Equals(filterTypeName)).FirstOrDefault();
+
+
+
+            // find the partent filterr
+            List<Filter> orFilters = content.Filters.ToList<Filter>(); ;
+            CombinedFilter parent = (CombinedFilter)orFilters.Where(e => e.Id.Equals(parentId)).FirstOrDefault();
+
+            // remove
+            orFilters.Remove(parent);
+
+            List<Filter> filterList = parent.Filters.ToList<Filter>();
+
+
+
+
+            // Iterate "posted" values and build up a dictionary for
+            // each filter (id).
+            // <id>     =>      <key> => <value>
+            //          =>      <key> => <value>
+            Dictionary<Guid, Dictionary<string, string>> postValues = new Dictionary<Guid, Dictionary<string, string>>();
+
+            foreach (string key in collection.AllKeys)
+            {
+
+                if (!key.StartsWith("filtersCombobox") && !key.StartsWith("parentId"))
+                {
+                    int idx = key.IndexOf(".");
+                    string id = key.Substring(0, idx);
+                    string type = key.Substring(idx + 1);
+
+                    Guid fid = new Guid(id);
+
+                    if (postValues.ContainsKey(fid))
                     {
-
-                        qm.SelectedFilters = new List<Filter>();
+                        Dictionary<string, string> d = postValues[fid];
+                        d.Add(type, collection.Get(key));
                     }
-
-
-                    // Add Filter only if not exists
-                    if (qm.SelectedFilters.Where(x => x.FilterTypeName.Equals(filter.FilterTypeName)).Count() == 0)
+                    else
                     {
-
-                        qm.SelectedFilters.Add(filter);
+                        Dictionary<string, string> d = new Dictionary<string, string>();
+                        d.Add(type, collection.Get(key));
+                        postValues.Add(fid, d);
                     }
-
-                    // Set values for the selected filters
-                    foreach (string s in collection.AllKeys)
+                }
+            }
+
+
+            // Set values for the existing Filters
+            foreach (Filter f in filterList)
+            {
+
+                Dictionary<string, string> dict = postValues.Where(x => x.Key.Equals(f.Id)).FirstOrDefault().Value;
+                if (dict != null)
+                {
+                    foreach (KeyValuePair<string, string> kvp in dict)
                     {
-                        if (s.StartsWith("valueTextbox"))
+                        if ("StringComparisonFilter".Equals(f.GetType().Name))
                         {
-                            // Get the type of the textbox
-                            string type = s.Substring(s.IndexOf(".") + 1);
-
-                            // Get the filter from the selected filters to set the value(s)
-                            Filter f = qm.SelectedFilters.Where(x => x.GetType().Name.Equals(type)).First();
-                            if ("StringComparisonFilter".Equals(type))
+                            if (kvp.Key.Equals("valueTextbox"))
                             {
-                                ((StringComparisonFilter)f).Value = collection.Get(s);
+                                ((StringComparisonFilter)f).Value = kvp.Value;
                             }
-                            else if ("NameStringComparisonFilter".Equals(type))
-                            {
-                                ((NameStringComparisonFilter)f).Value = collection.Get(s);
-                            }
-                            else if ("CombinedFilter".Equals(type))
-                            {
-                                //((CombinedFilter)f).Filters
-                            }
-                            // TODO: add for all filter types and will the values (etc.)
+
+                        }
+                        else
+                        {
+                            // To this for all filters
                         }
                     }
                 }
-            }
-
-
-            // Set session variable with actual selected filters
-            Session["selectedFilters"] = qm.SelectedFilters;
-
-            // "Index" ist the view name. We use here the Index view
-            // because the index view renders the Filters() action 
-            // that will return to the "Filters.ascx" partial view.
-            return View("Index", qm);
-        }
+
+            }
+
+
+            // Add the new filter
+            filterList.Add(filter);
+            parent.Filters = filterList.ToArray<Filter>();
+
+            // Add the current filter (AND)
+            orFilters.Add(parent);
+
+            content.Filters = orFilters.ToArray<Filter>();
+
+            // Set Content
+            Session["Content"] = content;
+
+
+
+            return View("Index", fm);
+        }
+
+
+        /// <summary>
+        /// Add a new Filter "Box" for OR connection.
+        /// </summary>
+        /// <returns></returns>
+        public ActionResult AddFilterOr()
+        {
+
+
+            CombinedFilter content = (CombinedFilter)Session["Content"];
+            FilterModel fm = new FilterModel(content);
+
+            List<Filter> orFilters = content.Filters.ToList<Filter>(); ;
+
+            CombinedFilter andFilter = fm.Filters.OfType<CombinedFilter>().Where(x => x.Operation == BooleanOperation.And).FirstOrDefault();
+            orFilters.Add(andFilter);
+            content.Filters = orFilters.ToArray<Filter>();
+
+
+            Session["Content"] = content;
+
+            return View("Index", fm);
+        }
+
+
 
 
@@ -160,22 +201,27 @@
         public ActionResult Index()
         {
+           
             // Set submenu
             Session["SelectedSubMenu"] = "Filter";
+            FilterModel fm = new FilterModel();
+
 
             // Init session variable for selected filters
-            if (Session["selectedFilters"] == null)
-            {
-                Session["selectedFilters"] = new List<Filter>();
-            }
-
-            // Get available filters from service
-            QueryServiceClient client = Query.GetClientFactory("okbtester", "okbtester");
-            List<Filter> filterList = client.GetFilters().ToList();
+            if (Session["Content"] == null)
+            {
+                // Add first Filter 
+                CombinedFilter orFilter = fm.Filters.OfType<CombinedFilter>().Where(x => x.Operation == BooleanOperation.Or).FirstOrDefault();
+                CombinedFilter andFilter = fm.Filters.OfType<CombinedFilter>().Where(x => x.Operation == BooleanOperation.And).FirstOrDefault();
+                orFilter.Filters = new Filter[] { andFilter }; // 1st filter
+
+                Session["Content"] = orFilter;
+            }
 
             // Set model
-            FilterModel qm = new FilterModel();
-            qm.Filters = filterList;
-
-            return View(qm);
+            CombinedFilter f = (CombinedFilter)Session["Content"];
+            fm.Content = f;
+            
+           
+            return View(fm);
         }
 
