Click here to Skip to main content
15,886,816 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.IO;
using System.Linq;
using System.Reflection;
using Signum.Utilities;
using System.Web.Hosting;
using System.Globalization;
using System.Text;
using System.Collections;
using System.Resources;
using System.Collections.Concurrent;
using System.Web;
using System.Web.Mvc;
using Signum.Web.Properties;
using System.Web.Script.Serialization;

namespace Signum.Web.PortableAreas
{
    public class CalendarLocalizedJavaScriptRepository : IFileRepository
    {
        public readonly string VirtualPathPrefix;
        
        readonly ConcurrentDictionary<CultureInfo, StaticContentResult> cachedFiles = new ConcurrentDictionary<CultureInfo, StaticContentResult>();

        public CalendarLocalizedJavaScriptRepository(string virtualPathPrefix)
        {
            if (string.IsNullOrEmpty(virtualPathPrefix))
                throw new ArgumentNullException("virtualPath");

            this.VirtualPathPrefix = virtualPathPrefix.ToLower();
        }

        public ActionResult GetFile(string file)
        {
            CultureInfo culture = GetCultureInfo(file);

            if (culture == null)
                return null;

            return this.cachedFiles.GetOrAdd(culture, ci => new StaticContentResult(CreateFile(ci), file));
        }

        byte[] CreateFile(CultureInfo ci)
        {
            using(Sync.ChangeBothCultures(ci))
            using (MemoryStream ms = new MemoryStream())
            {
                using (StreamWriter sw = new StreamWriter(ms, Encoding.UTF8))
                {
                    var config = new
                    {
                        closeText = Resources.CalendarClose,
                        prevText = Resources.CalendarPrevious,
                        nextText = Resources.CalendarNext,
                        currentText = Resources.CalendarToday,
                        monthNames = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames,
                        monthNamesShort = CultureInfo.CurrentCulture.DateTimeFormat.AbbreviatedMonthNames,
                        dayNames = CultureInfo.CurrentCulture.DateTimeFormat.DayNames,
                        dayNamesShort = CultureInfo.CurrentCulture.DateTimeFormat.AbbreviatedDayNames,
                        dayNamesMin = CultureInfo.CurrentCulture.DateTimeFormat.ShortestDayNames,
                        dateFormat = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern + " " + CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern,
                        firstDay = (int)CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek
                    };

                    sw.WriteLine("$.datepicker.regional['{0}'] = {1};".Formato(
                        DatePickerOptions.DefaultCulture,
                        new JavaScriptSerializer().Serialize(config)));

                    sw.WriteLine("$.datepicker.setDefaults($.datepicker.regional['" + DatePickerOptions.DefaultCulture + "']);");

                    sw.WriteLine("var SF = SF || {}; SF.Locale = SF.Locale || {};");
                    sw.WriteLine("SF.Locale.defaultDatepickerOptions = {0};".Formato(new DatePickerOptions() { Format = "g" }.ToString()));
                }

                return ms.ToArray();
            }
        }
    
        public bool FileExists(string file)
        {
            return GetCultureInfo(file) != null;
        }

        CultureInfo GetCultureInfo(string virtualPath)
        {
            if (!virtualPath.StartsWith(VirtualPathPrefix, StringComparison.InvariantCultureIgnoreCase))
                return null;

            var fileName = virtualPath.Substring(VirtualPathPrefix.Length);

            if (Path.GetExtension(fileName) != ".js")
                return null;

            try
            {
                return CultureInfo.GetCultureInfo(Path.GetFileNameWithoutExtension(fileName));
            }
            catch (CultureNotFoundException)
            {
                return null;
            }
        }

        public override string ToString()
        {
            return "CalendarLocalizedJavaScript {0}".Formato(VirtualPathPrefix);
        }
    }
}

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