Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1499 new (non-recursive) view/architecture

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