Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebApplication/MVC2/HLWebOKBQueryPlugin/ViewModels/FilterModel.cs @ 6170

Last change on this file since 6170 was 6170, checked in by mjesner, 13 years ago

#1499 design, cleanup, remove button

File size: 6.0 KB
RevLine 
[6097]1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Web;
5using HLWebOKBQueryPlugin.OKBQueryService;
[6141]6using HLWebOKBQueryPlugin.Helpers;
[6097]7
8namespace 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    {
[6141]17
[6170]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"; } }
[6141]22
[6170]23
24        /// <summary>
25        /// Constructor
26        /// </summary>
27        public FilterModel()
[6141]28        {
29
[6170]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        {
[6141]42            get
43            {
[6170]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            {
[6141]62                QueryServiceClient client = Query.GetClientFactory("okbtester", "okbtester");
[6170]63                return client.GetFilters().ToList<Filter>();
[6141]64            }
[6170]65        }
[6141]66
[6170]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            }
[6141]82        }
[6170]83
84        /// <summary>
85        /// Content
86        /// </summary>
[6141]87        public CombinedFilter Content { get; set; }
88
[6170]89     
[6141]90
[6170]91        /// <summary>
92        /// Delete a filter with given id.
93        /// </summary>
94        /// <param name="id">filter id</param>
95        internal bool RemoveFilter(Guid id)
[6141]96        {
[6170]97            bool removed = false;
98            List<Filter> orFilters = Content.Filters.ToList<Filter>();
[6141]99
[6170]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;
[6141]115        }
116
[6170]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)
[6141]123        {
[6170]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>();
[6141]149        }
150
[6170]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)
[6163]157        {
[6170]158            // Set values for the existing Filters
159            foreach (CombinedFilter cf in Content.Filters)
[6163]160            {
[6170]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                }
[6163]185            }
186        }
[6097]187    }
188}
Note: See TracBrowser for help on using the repository browser.