Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1499 refactor, first part of the comparison combobox for the different filters, Controls for the Filters

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