using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using HLWebOKBQueryPlugin.Models; using HLWebOKBQueryPlugin.Helpers; using HLWebOKBQueryPlugin.OKBQueryService; using System.Web.Routing; using HLWebOKBQueryPlugin.Models; namespace HLWebOKBQueryPlugin.Controllers { /// /// Controller for the filter subpage. /// public class FilterController : Controller { /// /// Get run ids from service. /// /// public ActionResult GetRuns() { Session["SelectedSubMenu"] = "Filter"; FilterModel fm = new FilterModel(); fm.Content = (CombinedFilter)Session["Content"]; QueryServiceClient client = Query.GetClientFactory(); long[] ids; try { ids = client.GetRunIds(fm.Content); } catch (Exception e) { ids = new long[0]; } QueryModel qm = new QueryModel(); qm.Runs = ids; return View("Results", qm); // name of usercontrol } public ActionResult Filters(CombinedFilter f) { FilterModel qm = new FilterModel(f); return PartialView("Filters", qm); // name of usercontrol } /// /// Add a new Filter, based on the selected combobox /// value. Posted values will also be set to the /// "right" filter(s). /// /// /// public ActionResult AddFilter(FormCollection collection) { FilterModel fm = new FilterModel((CombinedFilter)Session["Content"]); string submitAction = collection.Get("action"); Response.Write("Submit:" + submitAction); //update model with form data UpdateModel(collection, fm); if ("add".Equals(submitAction)) { string stringid = collection.Get("selectedFilter"); AddNewFilter(collection, fm, stringid); } else if ("delete".Equals(submitAction)) { string stringid= collection.Get("selectedFilter"); DeleteFilter(new Guid(stringid)); } else if ("runs".Equals(submitAction)) { return GetRuns(); } else if ("or".Equals(submitAction)) { AddFilterOr( fm); } // Set Content Session["Content"] = fm.Content; return View("Index", fm); } private void AddNewFilter(FormCollection collection, FilterModel fm, String idKey) { String parentIdKey = idKey; Guid parentId = new Guid(parentIdKey); string filterTypeName = collection.Get(FilterModel.ComboboxName+ "." + idKey); fm.AddFilter(filterTypeName, parentId); } private void UpdateModel(FormCollection collection, FilterModel fm) { // Iterate "posted" values and build up a dictionary for // each filter (id). // => => // => => Dictionary> postValues = CreatePostValueMap(collection); // Update filters with posted data fm.UpdateWithPostValues(postValues); } // Iterate "posted" values and build up a dictionary for // each filter (id). // => => // => => private Dictionary> CreatePostValueMap(FormCollection collection) { Dictionary> postValues = new Dictionary>(); foreach (string key in collection.AllKeys) { if (!key.StartsWith(FilterModel.ComboboxName) && !key.StartsWith(FilterModel.ParentIdName)) { int idx = key.IndexOf("."); if (idx != -1) { string id = key.Substring(0, idx); string type = key.Substring(idx + 1); Guid fid = new Guid(id); if (postValues.ContainsKey(fid)) { Dictionary d = postValues[fid]; d.Add(type, collection.Get(key)); } else { Dictionary d = new Dictionary(); d.Add(type, collection.Get(key)); postValues.Add(fid, d); } } } } return postValues; } /// /// Delete a filter with given id. /// /// /// public void DeleteFilter(Guid id) { // Get Content from Session FilterModel fm = new FilterModel((CombinedFilter)Session["Content"]); // Remove Filter bool filterRemoved = fm.RemoveFilter(id); } /// /// Add a new Filter "Box" for OR connection. /// /// public void AddFilterOr(FilterModel fm) { List orFilters = fm.Content.Filters.ToList(); ; CombinedFilter andFilter = fm.AvailableFilters.OfType().Where(x => x.Operation == BooleanOperation.And).FirstOrDefault(); orFilters.Add(andFilter); fm.Content.Filters = orFilters.ToArray(); Session["Content"] = fm.Content; } /// /// This action will be called when rendering /// the index.aspx. This will hapen the first /// time, so the available Filters in the Model /// will be set. /// /// public ActionResult Index() { // Set submenu Session["SelectedSubMenu"] = "Filter"; FilterModel fm = new FilterModel(); // Init session variable for selected filters if (Session["Content"] == null) { // Add first Filter CombinedFilter orFilter = fm.AvailableFilters.OfType().Where(x => x.Operation == BooleanOperation.Or).FirstOrDefault(); CombinedFilter andFilter = fm.AvailableFilters.OfType().Where(x => x.Operation == BooleanOperation.And).FirstOrDefault(); orFilter.Filters = new Filter[] { andFilter }; // 1st filter Session["Content"] = orFilter; } // Set model CombinedFilter f = (CombinedFilter)Session["Content"]; fm.Content = f; return View(fm); } } }