1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Web;
|
---|
5 | using HLWebOKBQueryPlugin.OKBQueryService;
|
---|
6 | using HLWebOKBQueryPlugin.Helpers;
|
---|
7 |
|
---|
8 | namespace HLWebOKBQueryPlugin.Models
|
---|
9 | {
|
---|
10 | /// <summary>
|
---|
11 | /// Model for Filter Data.
|
---|
12 | /// Current Filters (selected),
|
---|
13 | /// and available Filters.
|
---|
14 | /// </summary>
|
---|
15 | public class FilterModel
|
---|
16 | {
|
---|
17 |
|
---|
18 | public static string ComboboxName { get { return "filtersCombobox"; } }
|
---|
19 | public static string ParentIdName { get { return "parentId"; } }
|
---|
20 | public static string ValueTextbox { get { return "valueTextbox"; } }
|
---|
21 | public static string ValueDropDownList { get { return "valueDropDownList"; } }
|
---|
22 |
|
---|
23 |
|
---|
24 | /// <summary>
|
---|
25 | /// Constructor
|
---|
26 | /// </summary>
|
---|
27 | public FilterModel()
|
---|
28 | {
|
---|
29 |
|
---|
30 | }
|
---|
31 |
|
---|
32 | public FilterModel(CombinedFilter filter)
|
---|
33 | {
|
---|
34 | Content = filter;
|
---|
35 | }
|
---|
36 |
|
---|
37 | /// <summary>
|
---|
38 | /// Get Bool States.
|
---|
39 | /// </summary>
|
---|
40 | public List<Boolean> BoolStates
|
---|
41 | {
|
---|
42 | get
|
---|
43 | {
|
---|
44 | List<Boolean> boolStates = new List<Boolean>();
|
---|
45 |
|
---|
46 | boolStates = new List<Boolean>();
|
---|
47 | boolStates.Add(true);
|
---|
48 | boolStates.Add(false);
|
---|
49 |
|
---|
50 | return boolStates;
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 |
|
---|
55 | /// <summary>
|
---|
56 | /// Available Filters
|
---|
57 | /// </summary>
|
---|
58 | public List<Filter> AvailableFilters
|
---|
59 | {
|
---|
60 | get
|
---|
61 | {
|
---|
62 | QueryServiceClient client = Query.GetClientFactory("okbtester", "okbtester");
|
---|
63 | return client.GetFilters().ToList<Filter>();
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 |
|
---|
68 | /// <summary>
|
---|
69 | /// Available Filters without AND and OR.
|
---|
70 | /// </summary>
|
---|
71 | public List<Filter> AvailableFilterForCombobox
|
---|
72 | {
|
---|
73 | get
|
---|
74 | {
|
---|
75 | List<Filter> filters = AvailableFilters;
|
---|
76 |
|
---|
77 | filters.Remove(filters.OfType<CombinedFilter>().Where(x => x.Operation == BooleanOperation.Or).FirstOrDefault());
|
---|
78 | filters.Remove(filters.OfType<CombinedFilter>().Where(x => x.Operation == BooleanOperation.And).FirstOrDefault());
|
---|
79 |
|
---|
80 | return filters;
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | /// <summary>
|
---|
85 | /// Content
|
---|
86 | /// </summary>
|
---|
87 | public CombinedFilter Content { get; set; }
|
---|
88 |
|
---|
89 |
|
---|
90 |
|
---|
91 | /// <summary>
|
---|
92 | /// Delete a filter with given id.
|
---|
93 | /// </summary>
|
---|
94 | /// <param name="id">filter id</param>
|
---|
95 | internal bool RemoveFilter(Guid id)
|
---|
96 | {
|
---|
97 | bool removed = false;
|
---|
98 | List<Filter> orFilters = Content.Filters.ToList<Filter>();
|
---|
99 |
|
---|
100 | foreach (CombinedFilter f in orFilters)
|
---|
101 | {
|
---|
102 | Filter deleteFilter = f.Filters.Where(x => x.Id == id).FirstOrDefault();
|
---|
103 | if (deleteFilter != null)
|
---|
104 | {
|
---|
105 | List<Filter> masterFilters = f.Filters.ToList<Filter>();
|
---|
106 | masterFilters.Remove(deleteFilter);
|
---|
107 | f.Filters = masterFilters.ToArray<Filter>();
|
---|
108 | removed = true;
|
---|
109 | break;
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | Content.Filters = orFilters.ToArray<Filter>();
|
---|
114 | return removed;
|
---|
115 | }
|
---|
116 |
|
---|
117 | /// <summary>
|
---|
118 | /// Add a new filters
|
---|
119 | /// </summary>
|
---|
120 | /// <param name="filterTypeName">type name</param>
|
---|
121 | /// <param name="parentId">parent filter id</param>
|
---|
122 | internal void AddFilter(string filterTypeName, Guid parentId)
|
---|
123 | {
|
---|
124 |
|
---|
125 | // get selected filter (combobox)
|
---|
126 | Filter filter = AvailableFilters.Where(x => x.FilterTypeName.Equals(filterTypeName)).FirstOrDefault();
|
---|
127 |
|
---|
128 |
|
---|
129 | // find the partent filterr
|
---|
130 | List<Filter> orFilters = Content.Filters.ToList<Filter>(); ;
|
---|
131 | CombinedFilter parentFilter = (CombinedFilter)orFilters.Where(e => e.Id.Equals(parentId)).FirstOrDefault();
|
---|
132 |
|
---|
133 | // remove
|
---|
134 | orFilters.Remove(parentFilter);
|
---|
135 |
|
---|
136 | List<Filter> filterList = parentFilter.Filters.ToList<Filter>();
|
---|
137 |
|
---|
138 |
|
---|
139 | // Add the new filter
|
---|
140 | filterList.Add(filter);
|
---|
141 | //Response.Write("addx:" + filter.Id);
|
---|
142 |
|
---|
143 | parentFilter.Filters = filterList.ToArray<Filter>();
|
---|
144 |
|
---|
145 | // Add the current filter (AND)
|
---|
146 | orFilters.Add(parentFilter);
|
---|
147 |
|
---|
148 | Content.Filters = orFilters.ToArray<Filter>();
|
---|
149 | }
|
---|
150 |
|
---|
151 | /// <summary>
|
---|
152 | /// Update existing filters with posted value
|
---|
153 | /// </summary>
|
---|
154 | /// <param name="parentId">parent filter id</param>
|
---|
155 | /// <param name="postValues">dictionary with posted values</param>
|
---|
156 | internal void UpdateWithPostValues(Dictionary<Guid, Dictionary<string, string>> postValues)
|
---|
157 | {
|
---|
158 | // Set values for the existing Filters
|
---|
159 | foreach (CombinedFilter cf in Content.Filters)
|
---|
160 | {
|
---|
161 | foreach (Filter f in cf.Filters)
|
---|
162 | {
|
---|
163 |
|
---|
164 | Dictionary<string, string> dict = postValues.Where(x => x.Key.Equals(f.Id)).FirstOrDefault().Value;
|
---|
165 | if (dict != null)
|
---|
166 | {
|
---|
167 | foreach (KeyValuePair<string, string> kvp in dict)
|
---|
168 | {
|
---|
169 | if ("StringComparisonFilter".Equals(f.GetType().Name))
|
---|
170 | {
|
---|
171 | if (kvp.Key.Equals(FilterModel.ValueTextbox))
|
---|
172 | {
|
---|
173 | ((StringComparisonFilter)f).Value = kvp.Value;
|
---|
174 | }
|
---|
175 | // To this for alle keys
|
---|
176 |
|
---|
177 | }
|
---|
178 | else
|
---|
179 | {
|
---|
180 | // To this for all filters
|
---|
181 | }
|
---|
182 | }
|
---|
183 | }
|
---|
184 | }
|
---|
185 | }
|
---|
186 | }
|
---|
187 | }
|
---|
188 | } |
---|