Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6170 was 6170, checked in by mjesner, 13 years ago

#1499 design, cleanup, remove button

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