1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Web;
|
---|
5 | using System.Web.Mvc;
|
---|
6 | using HLWebOKBQueryPlugin.Models;
|
---|
7 | using HLWebOKBQueryPlugin.Helpers;
|
---|
8 | using HLWebOKBQueryPlugin.OKBQueryService;
|
---|
9 |
|
---|
10 | namespace HLWebOKBQueryPlugin.Controllers
|
---|
11 | {
|
---|
12 | /// <summary>
|
---|
13 | /// Controller for the filter subpage.
|
---|
14 | /// </summary>
|
---|
15 | public class FilterController : Controller
|
---|
16 | {
|
---|
17 | //
|
---|
18 | // GET: /Filter/
|
---|
19 |
|
---|
20 |
|
---|
21 | [ChildActionOnly]
|
---|
22 | public ActionResult Filters()
|
---|
23 | {
|
---|
24 | // For ModelViewuUserControl
|
---|
25 | return PartialView("Filters"); // name of usercontrol
|
---|
26 | }
|
---|
27 |
|
---|
28 |
|
---|
29 | /// <summary>
|
---|
30 | /// This action will be called from the Filters.ascx
|
---|
31 | /// when submitting the form.
|
---|
32 | /// </summary>
|
---|
33 | /// <param name="collection"></param>
|
---|
34 | /// <returns></returns>
|
---|
35 | public ActionResult AddFilter(FormCollection collection)
|
---|
36 | {
|
---|
37 | // Model
|
---|
38 | FilterModel qm = new FilterModel();
|
---|
39 |
|
---|
40 | // Get filters from service and set the model
|
---|
41 | QueryServiceClient client = Query.GetClientFactory("okbtester", "okbtester");
|
---|
42 | List<Filter> filterList = client.GetFilters().ToList();
|
---|
43 | qm.Filters = filterList;
|
---|
44 |
|
---|
45 |
|
---|
46 | // Selected filter (from combobox)
|
---|
47 | string filterType = collection.Get("filtersCombobox");
|
---|
48 | Filter filter = filterList.Where(x => x.FilterTypeName.Equals(filterType)).First();
|
---|
49 |
|
---|
50 |
|
---|
51 | // Get selected filters form session
|
---|
52 | qm.SelectedFilters = (List<Filter>)Session["selectedFilters"];
|
---|
53 |
|
---|
54 | // Add Filter only if not exists
|
---|
55 | if (qm.SelectedFilters.Where(x => x.FilterTypeName.Equals(filter.FilterTypeName)).Count() == 0)
|
---|
56 | {
|
---|
57 |
|
---|
58 | qm.SelectedFilters.Add(filter);
|
---|
59 | }
|
---|
60 |
|
---|
61 | // Set values for the selected filters
|
---|
62 | foreach (string s in collection.AllKeys)
|
---|
63 | {
|
---|
64 | if (s.StartsWith("valueTextbox"))
|
---|
65 | {
|
---|
66 | // Get the type of the textbox
|
---|
67 | string type = s.Substring(s.IndexOf(".") + 1);
|
---|
68 |
|
---|
69 | // Get the filter from the selected filters to set the value(s)
|
---|
70 | Filter f = qm.SelectedFilters.Where(x => x.GetType().Name.Equals(type)).First();
|
---|
71 | if ("StringComparisonFilter".Equals(type))
|
---|
72 | {
|
---|
73 | ((StringComparisonFilter)f).Value = collection.Get(s);
|
---|
74 | }
|
---|
75 | else if ("NameStringComparisonFilter".Equals(type))
|
---|
76 | {
|
---|
77 | ((NameStringComparisonFilter)f).Value = collection.Get(s);
|
---|
78 | }
|
---|
79 | // TODO: add for all filter types and will the values (etc.)
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 |
|
---|
85 | // Set session variable with actual selected filters
|
---|
86 | Session["selectedFilters"] = qm.SelectedFilters;
|
---|
87 |
|
---|
88 | // "Index" ist the view name. We use here the Index view
|
---|
89 | // because the index view renders the Filters() action
|
---|
90 | // that will return to the "Filters.ascx" partial view.
|
---|
91 | return View("Index", qm);
|
---|
92 | }
|
---|
93 |
|
---|
94 |
|
---|
95 | /// <summary>
|
---|
96 | /// This action will be called when rendering
|
---|
97 | /// the index.aspx. This will hapen the first
|
---|
98 | /// time, so the available Filters in the Model
|
---|
99 | /// will be set.
|
---|
100 | /// </summary>
|
---|
101 | /// <returns></returns>
|
---|
102 | public ActionResult Index()
|
---|
103 | {
|
---|
104 | // Set submenu
|
---|
105 | Session["SelectedSubMenu"] = "Filter";
|
---|
106 |
|
---|
107 | // Init session variable for selected filters
|
---|
108 | if (Session["selectedFilters"] == null)
|
---|
109 | {
|
---|
110 | Session["selectedFilters"] = new List<Filter>();
|
---|
111 | }
|
---|
112 |
|
---|
113 | // Get available filters from service
|
---|
114 | QueryServiceClient client = Query.GetClientFactory("okbtester", "okbtester");
|
---|
115 | List<Filter> filterList = client.GetFilters().ToList();
|
---|
116 |
|
---|
117 | // Set model
|
---|
118 | FilterModel qm = new FilterModel();
|
---|
119 | qm.Filters = filterList;
|
---|
120 |
|
---|
121 | return View(qm);
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 | }
|
---|
126 | }
|
---|