Click here to Skip to main content
15,888,610 members
Articles / Web Development / HTML

Signum Framework Tutorials Part 2 – Southwind Logic

Rate me:
Please Sign up or sign in to vote.
4.45/5 (6 votes)
15 Nov 2012LGPL325 min read 31.4K   1K   22  
In this part, we will focus on writing business logic, LINQ queries and explain inheritance
using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.ComponentModel;
using System.Linq;
using System.Collections.ObjectModel;
using Signum.Entities;
using System.Windows.Input;
using System.Collections.Generic;
using System.Globalization;
using Signum.Utilities;
using Signum.Entities.DynamicQuery;
using Signum.Entities.Reflection;

namespace Signum.Windows
{
    public partial class FilterBuilder
    {
        public static readonly DependencyProperty FiltersProperty =
            DependencyProperty.Register("Filters", typeof(FreezableCollection<FilterOption>), typeof(FilterBuilder), new UIPropertyMetadata(null));
        public FreezableCollection<FilterOption> Filters
        {
            get { return (FreezableCollection<FilterOption>)GetValue(FiltersProperty); }
            set { SetValue(FiltersProperty, value); }
        }

        public DragController FilterDragController { get; set; }

        public FilterBuilder()
        {
            FilterDragController = new DragController(fe =>
            {
                FilterOption fo = ((FilterOption)((QueryTokenRenderer)fe).DataContext);
                return new FilterOption { Token = fo.Token, Operation = fo.Operation, RealValue = fo.RealValue };
            }, DragDropEffects.Copy);

            this.InitializeComponent();
        }

        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            Grid g = (Grid)sender;
            Common.SetCurrentWindow(g, this.FindCurrentWindow());
            FilterOption f = (FilterOption)g.DataContext;
           
            Common.SetIsReadOnly(g, f.Frozen);

            g.Children.Add(GetValueControl(f)); 
        }

        public static Control GetValueControl(FilterOption f)
        {
            Implementations implementations = f.Token.Implementations();
            Type type = f.Token.Type;
            if (type.IsLite())
            {
                Type cleanType = Reflector.ExtractLite(type);

                if (Reflector.IsLowPopulation(cleanType) && !(implementations is ImplementedByAllAttribute))
                {
                    EntityCombo ec = new EntityCombo
                    {
                        Type = type,
                        //Style = (Style)FindResource("toolTip"),
                        Implementations = implementations
                    };

                    ec.SetBinding(EntityCombo.EntityProperty, new Binding
                    {
                        Path = new PropertyPath(FilterOption.RealValueProperty),
                        NotifyOnValidationError = true,
                        ValidatesOnDataErrors = true,
                        ValidatesOnExceptions = true,
                    });

                    return ec;
                }
                else
                {
                    EntityLine el = new EntityLine
                    {
                        Type = type,
                        Create = false,
                        HideAutoCompleteOnLostFocus = false,
                        Implementations = implementations
                    };

                    el.SetBinding(EntityLine.EntityProperty, new Binding
                    {
                        Path = new PropertyPath(FilterOption.RealValueProperty),
                        NotifyOnValidationError = true,
                        ValidatesOnDataErrors = true,
                        ValidatesOnExceptions = true
                    });

                    return el;
                }
            }
            else if (type.IsEmbeddedEntity())
            {
                EntityLine el = new EntityLine
                {
                    Type = type,
                    Create = false,
                    AutoComplete = false,
                    Find = false,
                    HideAutoCompleteOnLostFocus = false,
                    Implementations = implementations
                };

                el.SetBinding(EntityLine.EntityProperty, new Binding
                {
                    Path = new PropertyPath(FilterOption.RealValueProperty),
                    NotifyOnValidationError = true,
                    ValidatesOnDataErrors = true,
                    ValidatesOnExceptions = true
                });

                return el;
            }
            else
            {
                QueryToken token = f.Token;

                ValueLine vl = new ValueLine()
                {
                    Type = type,
                    Format = token.Format,
                    UnitText = token.Unit
                };

                if (type.UnNullify().IsEnum)
                {
                    vl.ItemSource = EnumProxy.GetValues(type.UnNullify()).PreAndNull(type.IsNullable());
                }

                vl.SetBinding(ValueLine.ValueProperty, new Binding
                {
                    Path = new PropertyPath("RealValue"), //odd
                    NotifyOnValidationError = true,
                    ValidatesOnDataErrors = true,
                    ValidatesOnExceptions = true,
                    Converter = Reflector.IsNumber(type) ? Converters.Identity : null,
                });

                return vl;
            }
        }

        private void btRemove_Click(object sender, RoutedEventArgs e)
        {
            RemoveFilters();
        }

        private void RemoveFilters()
        {
            var toRemove = lvFilters.SelectedItems.Cast<FilterOption>().ToList();
            foreach (var f in toRemove)
            {
                Filters.Remove(f);
            }
        }

        private void lvFilters_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Delete)
            {
                RemoveFilters();
            }
        }

        private void lvFilters_DragOver(object sender, DragEventArgs e)
        {
            e.Effects = e.Data.GetDataPresent(typeof(FilterOption)) ? DragDropEffects.Copy : DragDropEffects.None;
        }

        private void lvFilters_Drop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(FilterOption)))
            {
                FilterOption filter = (FilterOption)e.Data.GetData(typeof(FilterOption));

                Filters.Add(filter);

                RefreshFirstColumn();
            }
        }

        public void RefreshFirstColumn()
        {
            firstColumn.Width = 0;
            firstColumn.Width = double.NaN;
        }

        public void AddFilter(QueryToken queryToken)
        {

            if (queryToken == null)
            {
                MessageBox.Show(Properties.Resources.NoFilterSelected);
                return;
            }

            FilterType ft = QueryUtils.GetFilterType(queryToken.Type);

            FilterOption f = new FilterOption
            {
                Token = queryToken,
                Value = queryToken.Type.IsValueType && !queryToken.Type.IsNullable() ? Activator.CreateInstance(queryToken.Type) : null,
                Operation = QueryUtils.GetFilterOperations(ft).First()
            };

            Filters.Add(f);

            RefreshFirstColumn();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Software Developer (Senior) Signum Software
Spain Spain
I'm Computer Scientist, one of the founders of Signum Software, and the lead developer behind Signum Framework.

www.signumframework.com

I love programming in C#, Linq, Compilers, Algorithms, Functional Programming, Computer Graphics, Maths...

Comments and Discussions