Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/07/11 10:41:02 (13 years ago)
Author:
mjesner
Message:

#1499 new (non-recursive) view/architecture

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/WebApplication/MVC2/HLWebOKBQueryPlugin/Controllers/FilterController.cs

    r6102 r6141  
    77using HLWebOKBQueryPlugin.Helpers;
    88using HLWebOKBQueryPlugin.OKBQueryService;
     9using System.Web.Routing;
    910
    1011namespace HLWebOKBQueryPlugin.Controllers
     
    1516    public class FilterController : Controller
    1617    {
    17         //
    18         // GET: /Filter/
    19 
    20         [ChildActionOnly]
    21         public ActionResult Filters()
    22         {
    23             // Model     
     18        /// <summary>
     19        /// Get run ids from service.
     20        /// </summary>
     21        /// <returns></returns>
     22        public ActionResult GetRuns()
     23        {
    2424            FilterModel qm = new FilterModel();
    25 
    26 
    27             //TODO nicht immer laden
     25            qm.Content = (CombinedFilter)Session["Content"];
     26
    2827            QueryServiceClient client = Query.GetClientFactory("okbtester", "okbtester");
    29             List<Filter> filterList = client.GetFilters().ToList();
    30             qm.Filters = filterList;
    31             qm.SelectedFilters = new List<Filter>();
    32 
    33 
    34             // For ModelViewuUserControl
     28           long[] ids =  client.GetRunIds(qm.Content);
     29
     30            Response.Write("Found <" + ids.Count() + "> Runs.");
     31
     32            return View("Index", qm); // name of usercontrol
     33        }
     34
     35        public ActionResult Filters(CombinedFilter f)
     36        {
     37            FilterModel qm = new FilterModel(f);
    3538            return PartialView("Filters", qm); // name of usercontrol
    3639        }
    3740
    38         //List<Filter> currentFilters
    39 
    40         [ChildActionOnly]
    41         public ActionResult Filters(FilterModel qms)
    42         {
    43             // Model     
    44             FilterModel qm = new FilterModel();
    45 
    46 
    47             //TODO nicht immer laden
    48             QueryServiceClient client = Query.GetClientFactory("okbtester", "okbtester");
    49             List<Filter> filterList = client.GetFilters().ToList();
    50             qm.Filters = filterList;
    51             qm.SelectedFilters =qms.SelectedFilters;
    52             if (qm.SelectedFilters == null)
    53             {
    54 
    55                 qm.SelectedFilters = new List<Filter>();
    56             }
    57 
    58 
    59 
    60             // For ModelViewuUserControl
    61             return PartialView("Filters", qm); // name of usercontrol
    62         }
    63 
    64 
    65      
    66 
    67 
    68         /// <summary>
    69         /// This action will be called from the Filters.ascx
    70         /// when submitting the form.
     41
     42        /// <summary>
     43        /// Add a new Filter, based on the selected combobox
     44        /// value. Posted values will also be set to the
     45        /// "right" filter(s).
    7146        /// </summary>
    7247        /// <param name="collection"></param>
    7348        /// <returns></returns>
    74         public ActionResult AddFilter(FormCollection collection)
    75         {
     49        public ActionResult AddFilterAnd(FormCollection collection)
     50        {
     51            CombinedFilter content = (CombinedFilter)Session["Content"];
     52            FilterModel fm = new FilterModel(content);
     53
    7654           
    7755
    78             // Model     
    79             FilterModel qm = new FilterModel();
    80 
    81             // Get filters from service and set the model
    82             QueryServiceClient client = Query.GetClientFactory("okbtester", "okbtester");
    83             List<Filter> filterList = client.GetFilters().ToList();
    84             qm.Filters = filterList;
    85 
    86 
    87             // Selected filter (from combobox)
    88             string filterType = collection.Get("filtersCombobox");
    89 
    90             if (filterType != null)
    91             {
    92 
    93                 Filter filter = filterList.Where(x => x.FilterTypeName.CompareTo(filterType) == 0).First();
    94 
    95 
    96                  if (filter != null)
    97                 {
    98                     // Get selected filters form session
    99                     qm.SelectedFilters = (List<Filter>)Session["selectedFilters"];
    100                     if (qm.SelectedFilters == null)
     56            String idKey = null;
     57            String parentIdKey = null;
     58
     59            foreach (string key in collection.AllKeys)
     60            {
     61                if (key.StartsWith("filtersCombobox"))
     62                {
     63                    idKey = key;
     64                }
     65                if (key.StartsWith("parentId"))
     66                {
     67                    parentIdKey = key;
     68                }
     69            }
     70
     71            Guid parentId = new Guid(collection.Get(parentIdKey));
     72            string filterTypeName = collection.Get(idKey);
     73
     74            // get selected filter (combobox)
     75            Filter filter = fm.Filters.Where(x => x.FilterTypeName.Equals(filterTypeName)).FirstOrDefault();
     76
     77
     78
     79            // find the partent filterr
     80            List<Filter> orFilters = content.Filters.ToList<Filter>(); ;
     81            CombinedFilter parent = (CombinedFilter)orFilters.Where(e => e.Id.Equals(parentId)).FirstOrDefault();
     82
     83            // remove
     84            orFilters.Remove(parent);
     85
     86            List<Filter> filterList = parent.Filters.ToList<Filter>();
     87
     88
     89
     90
     91            // Iterate "posted" values and build up a dictionary for
     92            // each filter (id).
     93            // <id>     =>      <key> => <value>
     94            //          =>      <key> => <value>
     95            Dictionary<Guid, Dictionary<string, string>> postValues = new Dictionary<Guid, Dictionary<string, string>>();
     96
     97            foreach (string key in collection.AllKeys)
     98            {
     99
     100                if (!key.StartsWith("filtersCombobox") && !key.StartsWith("parentId"))
     101                {
     102                    int idx = key.IndexOf(".");
     103                    string id = key.Substring(0, idx);
     104                    string type = key.Substring(idx + 1);
     105
     106                    Guid fid = new Guid(id);
     107
     108                    if (postValues.ContainsKey(fid))
    101109                    {
    102 
    103                         qm.SelectedFilters = new List<Filter>();
     110                        Dictionary<string, string> d = postValues[fid];
     111                        d.Add(type, collection.Get(key));
    104112                    }
    105 
    106 
    107                     // Add Filter only if not exists
    108                     if (qm.SelectedFilters.Where(x => x.FilterTypeName.Equals(filter.FilterTypeName)).Count() == 0)
     113                    else
    109114                    {
    110 
    111                         qm.SelectedFilters.Add(filter);
     115                        Dictionary<string, string> d = new Dictionary<string, string>();
     116                        d.Add(type, collection.Get(key));
     117                        postValues.Add(fid, d);
    112118                    }
    113 
    114                     // Set values for the selected filters
    115                     foreach (string s in collection.AllKeys)
     119                }
     120            }
     121
     122
     123            // Set values for the existing Filters
     124            foreach (Filter f in filterList)
     125            {
     126
     127                Dictionary<string, string> dict = postValues.Where(x => x.Key.Equals(f.Id)).FirstOrDefault().Value;
     128                if (dict != null)
     129                {
     130                    foreach (KeyValuePair<string, string> kvp in dict)
    116131                    {
    117                         if (s.StartsWith("valueTextbox"))
     132                        if ("StringComparisonFilter".Equals(f.GetType().Name))
    118133                        {
    119                             // Get the type of the textbox
    120                             string type = s.Substring(s.IndexOf(".") + 1);
    121 
    122                             // Get the filter from the selected filters to set the value(s)
    123                             Filter f = qm.SelectedFilters.Where(x => x.GetType().Name.Equals(type)).First();
    124                             if ("StringComparisonFilter".Equals(type))
     134                            if (kvp.Key.Equals("valueTextbox"))
    125135                            {
    126                                 ((StringComparisonFilter)f).Value = collection.Get(s);
     136                                ((StringComparisonFilter)f).Value = kvp.Value;
    127137                            }
    128                             else if ("NameStringComparisonFilter".Equals(type))
    129                             {
    130                                 ((NameStringComparisonFilter)f).Value = collection.Get(s);
    131                             }
    132                             else if ("CombinedFilter".Equals(type))
    133                             {
    134                                 //((CombinedFilter)f).Filters
    135                             }
    136                             // TODO: add for all filter types and will the values (etc.)
     138
     139                        }
     140                        else
     141                        {
     142                            // To this for all filters
    137143                        }
    138144                    }
    139145                }
    140             }
    141 
    142 
    143             // Set session variable with actual selected filters
    144             Session["selectedFilters"] = qm.SelectedFilters;
    145 
    146             // "Index" ist the view name. We use here the Index view
    147             // because the index view renders the Filters() action
    148             // that will return to the "Filters.ascx" partial view.
    149             return View("Index", qm);
    150         }
     146
     147            }
     148
     149
     150            // Add the new filter
     151            filterList.Add(filter);
     152            parent.Filters = filterList.ToArray<Filter>();
     153
     154            // Add the current filter (AND)
     155            orFilters.Add(parent);
     156
     157            content.Filters = orFilters.ToArray<Filter>();
     158
     159            // Set Content
     160            Session["Content"] = content;
     161
     162
     163
     164            return View("Index", fm);
     165        }
     166
     167
     168        /// <summary>
     169        /// Add a new Filter "Box" for OR connection.
     170        /// </summary>
     171        /// <returns></returns>
     172        public ActionResult AddFilterOr()
     173        {
     174
     175
     176            CombinedFilter content = (CombinedFilter)Session["Content"];
     177            FilterModel fm = new FilterModel(content);
     178
     179            List<Filter> orFilters = content.Filters.ToList<Filter>(); ;
     180
     181            CombinedFilter andFilter = fm.Filters.OfType<CombinedFilter>().Where(x => x.Operation == BooleanOperation.And).FirstOrDefault();
     182            orFilters.Add(andFilter);
     183            content.Filters = orFilters.ToArray<Filter>();
     184
     185
     186            Session["Content"] = content;
     187
     188            return View("Index", fm);
     189        }
     190
     191
    151192
    152193
     
    160201        public ActionResult Index()
    161202        {
     203           
    162204            // Set submenu
    163205            Session["SelectedSubMenu"] = "Filter";
     206            FilterModel fm = new FilterModel();
     207
    164208
    165209            // Init session variable for selected filters
    166             if (Session["selectedFilters"] == null)
    167             {
    168                 Session["selectedFilters"] = new List<Filter>();
    169             }
    170 
    171             // Get available filters from service
    172             QueryServiceClient client = Query.GetClientFactory("okbtester", "okbtester");
    173             List<Filter> filterList = client.GetFilters().ToList();
     210            if (Session["Content"] == null)
     211            {
     212                // Add first Filter
     213                CombinedFilter orFilter = fm.Filters.OfType<CombinedFilter>().Where(x => x.Operation == BooleanOperation.Or).FirstOrDefault();
     214                CombinedFilter andFilter = fm.Filters.OfType<CombinedFilter>().Where(x => x.Operation == BooleanOperation.And).FirstOrDefault();
     215                orFilter.Filters = new Filter[] { andFilter }; // 1st filter
     216
     217                Session["Content"] = orFilter;
     218            }
    174219
    175220            // Set model
    176             FilterModel qm = new FilterModel();
    177             qm.Filters = filterList;
    178 
    179             return View(qm);
     221            CombinedFilter f = (CombinedFilter)Session["Content"];
     222            fm.Content = f;
     223           
     224           
     225            return View(fm);
    180226        }
    181227
Note: See TracChangeset for help on using the changeset viewer.