Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1499
Remove unused code in the filter classes

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