Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebApplication/MVC2/HLWebOKBQueryPlugin/Controllers/FilterController.cs @ 6318

Last change on this file since 6318 was 6318, checked in by cfleisch, 13 years ago

#1499 styling filter and bubblechart, filter bugfixes

File size: 7.5 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Web;
5using System.Web.Mvc;
6using HLWebOKBQueryPlugin.Models;
7using HLWebOKBQueryPlugin.Helpers;
8using HLWebOKBQueryPlugin.OKBQueryService;
9using System.Web.Routing;
10using HLWebOKBQueryPlugin.Models;
11
12namespace HLWebOKBQueryPlugin.Controllers
13{
14    /// <summary>
15    /// Controller for the filter subpage.
16    /// </summary>
17    public class FilterController : Controller
18    {
19        /// <summary>
20        /// Get run ids from service.
21        /// </summary>
22        /// <returns></returns>
23        public ActionResult GetRuns()
24        {
25            Session["SelectedSubMenu"] = "Filter";
26
27            FilterModel fm = new FilterModel();
28            fm.Content = (CombinedFilter)Session["Content"];
29            QueryServiceClient client = Query.GetClientFactory();
30            long[] ids;
31
32            try
33            {
34                ids = client.GetRunIds(fm.Content);
35
36            }
37            catch (Exception e)
38            {
39                ids = new long[0];
40            }
41
42            QueryModel qm = new QueryModel();
43            qm.Runs = ids;
44            return View("Results", qm); // name of usercontrol
45        }
46       
47
48        public ActionResult Filters(CombinedFilter f)
49        {
50            FilterModel qm = new FilterModel(f);
51            return PartialView("Filters", qm); // name of usercontrol
52        }
53
54
55        /// <summary>
56        /// Add a new Filter, based on the selected combobox
57        /// value. Posted values will also be set to the
58        /// "right" filter(s).
59        /// </summary>
60        /// <param name="collection"></param>
61        /// <returns></returns>
62        public ActionResult AddFilter(FormCollection collection)
63        {
64            FilterModel fm = new FilterModel((CombinedFilter)Session["Content"]);
65            string submitAction = collection.Get("action");
66            Response.Write("Submit:" + submitAction);
67           
68            //update model with form data
69            UpdateModel(collection, fm);
70
71            if ("add".Equals(submitAction))
72            {
73                string stringid = collection.Get("selectedFilter");
74                AddNewFilter(collection, fm, stringid);
75            }
76            else if ("delete".Equals(submitAction))
77            {
78               string stringid= collection.Get("selectedFilter");
79               DeleteFilter(new Guid(stringid));
80            }
81            else if ("runs".Equals(submitAction))
82            {
83                return GetRuns();
84            }
85            else if ("or".Equals(submitAction))
86            {
87                AddFilterOr( fm);
88            }
89           
90            // Set Content
91            Session["Content"] = fm.Content;
92            return View("Index", fm);
93                       
94        }
95
96
97        private void AddNewFilter(FormCollection collection, FilterModel fm, String idKey)
98        {           
99            String parentIdKey = idKey;
100
101            Guid parentId = new Guid(parentIdKey);
102            string filterTypeName = collection.Get(FilterModel.ComboboxName+ "." + idKey);
103            fm.AddFilter(filterTypeName, parentId);           
104        }
105
106        private void UpdateModel(FormCollection collection, FilterModel fm)
107        {
108            // Iterate "posted" values and build up a dictionary for
109            // each filter (id).
110            // <id>     =>      <key> => <value>
111            //          =>      <key> => <value>
112            Dictionary<Guid, Dictionary<string, string>> postValues = CreatePostValueMap(collection);
113
114            // Update filters with posted data
115            fm.UpdateWithPostValues(postValues);
116        }
117
118
119        // Iterate "posted" values and build up a dictionary for
120        // each filter (id).
121        // <id>     =>      <key> => <value>
122        //          =>      <key> => <value>
123        private Dictionary<Guid, Dictionary<string, string>> CreatePostValueMap(FormCollection collection)
124        {
125            Dictionary<Guid, Dictionary<string, string>> postValues = new Dictionary<Guid, Dictionary<string, string>>();
126
127            foreach (string key in collection.AllKeys)
128            {
129                if (!key.StartsWith(FilterModel.ComboboxName) && !key.StartsWith(FilterModel.ParentIdName))
130                {
131                    int idx = key.IndexOf(".");
132                    if (idx != -1)
133                    {
134                        string id = key.Substring(0, idx);
135                        string type = key.Substring(idx + 1);
136
137                        Guid fid = new Guid(id);
138
139                        if (postValues.ContainsKey(fid))
140                        {
141                            Dictionary<string, string> d = postValues[fid];
142                            d.Add(type, collection.Get(key));
143                        }
144                        else
145                        {
146                            Dictionary<string, string> d = new Dictionary<string, string>();
147                            d.Add(type, collection.Get(key));
148                            postValues.Add(fid, d);
149                        }
150                    }
151                }
152            }
153            return postValues;
154        }
155
156
157        /// <summary>
158        /// Delete a filter with given id.
159        /// </summary>
160        /// <param name="id"></param>
161        /// <returns></returns>
162        public void DeleteFilter(Guid id)
163        {
164            // Get Content from Session
165            FilterModel fm = new FilterModel((CombinedFilter)Session["Content"]);
166
167            // Remove Filter
168            bool filterRemoved = fm.RemoveFilter(id);         
169        }
170       
171
172        /// <summary>
173        /// Add a new Filter "Box" for OR connection.
174        /// </summary>
175        /// <returns></returns>
176        public void AddFilterOr(FilterModel fm)
177        {           
178            List<Filter> orFilters = fm.Content.Filters.ToList<Filter>(); ;
179
180            CombinedFilter andFilter = fm.AvailableFilters.OfType<CombinedFilter>().Where(x => x.Operation == BooleanOperation.And).FirstOrDefault();
181            orFilters.Add(andFilter);
182            fm.Content.Filters = orFilters.ToArray<Filter>();
183                     
184            Session["Content"] = fm.Content;           
185        }
186
187
188        /// <summary>
189        /// This action will be called when rendering
190        /// the index.aspx. This will hapen the first
191        /// time, so the available Filters in the Model
192        /// will be set.
193        /// </summary>
194        /// <returns></returns>
195        public ActionResult Index()
196        {
197            // Set submenu
198            Session["SelectedSubMenu"] = "Filter";
199            FilterModel fm = new FilterModel();
200
201            // Init session variable for selected filters
202            if (Session["Content"] == null)
203            {
204                // Add first Filter
205                CombinedFilter orFilter = fm.AvailableFilters.OfType<CombinedFilter>().Where(x => x.Operation == BooleanOperation.Or).FirstOrDefault();
206                CombinedFilter andFilter = fm.AvailableFilters.OfType<CombinedFilter>().Where(x => x.Operation == BooleanOperation.And).FirstOrDefault();
207                orFilter.Filters = new Filter[] { andFilter }; // 1st filter
208
209                Session["Content"] = orFilter;
210            }
211
212            // Set model
213            CombinedFilter f = (CombinedFilter)Session["Content"];
214            fm.Content = f;
215
216            return View(fm);
217        }
218    }
219}
Note: See TracBrowser for help on using the repository browser.