Click here to Skip to main content
15,883,745 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.3K   1K   22  
In this part, we will focus on writing business logic, LINQ queries and explain inheritance
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Signum.Entities.DynamicQuery;
using Signum.Utilities;
using Signum.Entities;
using System.Web;
using System.Web.Mvc;
using Signum.Entities.Reflection;
using Signum.Web.Properties;
using System.Linq.Expressions;

namespace Signum.Web
{
    public class QuerySettings
    {
        public QuerySettings(object queryName)
        {
            this.QueryName = queryName; 
        }

        public object QueryName { get; private set; }

        public Func<string> Title { get; set; }
        public int? Top { get; set; }
        public string WebQueryName { get; set; }

        public Func<object, bool> IsFindable;

        public bool OnIsFindable()
        {
            if (IsFindable != null)
                foreach (Func<object, bool> item in IsFindable.GetInvocationList())
                {
                    if (!item(QueryName))
                        return false;
                }

            return true;
        }

        public static List<FormatterRule> FormatRules { get; set; }
        public static List<EntityFormatterRule> EntityFormatRules { get; set; }

        public static Dictionary<PropertyRoute, Func<HtmlHelper, object, MvcHtmlString>> PropertyFormatters { get; set; }

        Dictionary<string, Func<HtmlHelper, object, MvcHtmlString>> formatters;
        public Dictionary<string, Func<HtmlHelper, object, MvcHtmlString>> Formatters
        {
            get { return formatters ?? (formatters = new Dictionary<string, Func<HtmlHelper, object, MvcHtmlString>>()); }
            set { formatters = value; }
        }

        static QuerySettings()
        {
            FormatRules = new List<FormatterRule>
            {
                new FormatterRule(c=>true, c=> (h,o) =>
                {
                    return o != null ? o.ToString().EncodeHtml() : MvcHtmlString.Empty;
                }),

                new FormatterRule(c => c.Type.UnNullify().IsEnum, c => (h,o) => 
                {
                    return o != null ? ((Enum)o).NiceToString().EncodeHtml() : MvcHtmlString.Empty;
                }),
                new FormatterRule(c => c.Type.UnNullify().IsLite(), c => (h,o) => 
                {
                    return h.LightEntityLine((Lite)o, false);
                }),
                new FormatterRule(c=>c.Type.UnNullify() == typeof(DateTime), c => (h,o) => 
                {
                    return o != null ? ((DateTime)o).ToUserInterface().TryToString(c.Format).EncodeHtml() : MvcHtmlString.Empty;
                }),
                new FormatterRule(c=> Reflector.IsNumber(c.Type), c => (h,o) => 
                {
                    if (o != null)
                    {
                        string s = ((IFormattable)o).TryToString(c.Format);
                        if (c.Unit.HasText())
                            s += " " + c.Unit;
                        return s.EncodeHtml();
                    }
                    return MvcHtmlString.Empty;
                }),
                new FormatterRule(c=>c.Type == typeof(bool?), c => (h,o) => 
                {
                    return o != null ? AlignCenter(h.CheckBox("", (bool)o, false)) : MvcHtmlString.Empty;
                }),
                new FormatterRule(c=>c.Type == typeof(bool), c => (h,o) => 
                {
                    return o != null ? AlignCenter(h.CheckBox("", (bool)o, false)) : MvcHtmlString.Empty;
                })
            };

            EntityFormatRules = new List<EntityFormatterRule>
            {
                new EntityFormatterRule(l => true, (h,l) => 
                {
                    if (Navigator.IsViewable(l.RuntimeType, true))
                        return h.Href(Navigator.ViewRoute(l.RuntimeType, l.Id), h.Encode(Resources.View));
                    else
                        return MvcHtmlString.Empty;
                }),
            };

            PropertyFormatters = new Dictionary<PropertyRoute, Func<HtmlHelper, object, MvcHtmlString>>();
        }

        public static MvcHtmlString AlignCenter(MvcHtmlString innerHTML)
        {
            return new HtmlTag("div")
                .Attrs(new { style = "text-align:center" })
                .InnerHtml(innerHTML)
                .ToHtml();
        }
           

        public Func<HtmlHelper, object, MvcHtmlString> GetFormatter(Column column)
        {
            Func<HtmlHelper, object, MvcHtmlString> cf;
            if (formatters != null && formatters.TryGetValue(column.Name, out cf))
                return cf; 

            PropertyRoute route = column.Token.GetPropertyRoute();
            if (route != null)
            {
                var formatter = QuerySettings.PropertyFormatters.TryGetC(route);
                if (formatter != null)
                    return formatter;
            }

            return FormatRules.Last(cfr => cfr.IsApplyable(column)).Formatter(column);
        }


        public static void RegisterPropertyFormat<T>(Expression<Func<T, object>> property, Func<HtmlHelper, object, MvcHtmlString> formatter)
         where T : IRootEntity
        {
            PropertyFormatters.Add(PropertyRoute.Construct(property), formatter);
        }
    }

    public class FormatterRule
    {
        public Func<Column, Func<HtmlHelper, object, MvcHtmlString>> Formatter { get; set; }
        public Func<Column, bool> IsApplyable { get; set; }

        public FormatterRule(Func<Column, bool> isApplyable, Func<Column, Func<HtmlHelper, object, MvcHtmlString>> formatter)
        {
            Formatter = formatter;
            IsApplyable = isApplyable;
        }
    }

    public class EntityFormatterRule
    {
        public Func<HtmlHelper, Lite, MvcHtmlString> Formatter { get; set; }
        public Func<Lite, bool> IsApplyable { get; set; }

        public EntityFormatterRule(Func<Lite, bool> isApplyable, Func<HtmlHelper, Lite, MvcHtmlString> formatter)
        {
            Formatter = formatter;
            IsApplyable = isApplyable;
        }
    }
}

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