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;
namespace HLWebOKBQueryPlugin.Controllers
{
///
/// Controller for the filter subpage.
///
public class FilterController : Controller
{
///
/// Get run ids from service.
///
///
public ActionResult GetRuns()
{
FilterModel qm = new FilterModel();
qm.Content = (CombinedFilter)Session["Content"];
QueryServiceClient client = Query.GetClientFactory("okbtester", "okbtester");
long[] ids = client.GetRunIds(qm.Content);
Response.Write("Found <" + ids.Count() + "> Runs.");
return View("Index", 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 AddFilterAnd(FormCollection collection)
{
FilterModel fm = new FilterModel((CombinedFilter)Session["Content"]);
// Add Filter
String idKey = null;
String parentIdKey = null;
foreach (string key in collection.AllKeys)
{
if (key.StartsWith(FilterModel.ComboboxName))
{
idKey = key;
}
if (key.StartsWith(FilterModel.ParentIdName))
{
parentIdKey = key;
}
}
Guid parentId = new Guid(collection.Get(parentIdKey));
string filterTypeName = collection.Get(idKey);
fm.AddFilter(filterTypeName, parentId);
// Iterate "posted" values and build up a dictionary for
// each filter (id).
// => =>
// => =>
Dictionary> postValues = new Dictionary>();
foreach (string key in collection.AllKeys)
{
if (!key.StartsWith(FilterModel.ComboboxName) && !key.StartsWith(FilterModel.ParentIdName))
{
int idx = key.IndexOf(".");
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);
}
}
}
// Update filters with posted data
fm.UpdateWithPostValues(postValues);
// Set Content
Session["Content"] = fm.Content;
return View("Index", fm);
}
///
/// Delete a filter with given id.
///
///
///
public ActionResult DeleteFilter(Guid id)
{
// Get Content from Session
FilterModel fm = new FilterModel((CombinedFilter)Session["Content"]);
// Remove Filter
bool filterRemoved = fm.RemoveFilter(id);
// Set Content
Session["Content"] = fm.Content;
return View("Index", fm);
}
///
/// Add a new Filter "Box" for OR connection.
///
///
public ActionResult AddFilterOr()
{
FilterModel fm = new FilterModel((CombinedFilter)Session["Content"]);
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;
return View("Index", fm);
}
///
/// 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);
}
}
}