Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6311 was 6311, checked in by dhohl, 13 years ago

#1499
If error occurred will GetRuns the application does not throw an Exception anymore

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