Transformer.cs :  » Bloggers » SubText » Subtext » Framework » Data » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Bloggers » SubText 
SubText » Subtext » Framework » Data » Transformer.cs
#region Disclaimer/Info

///////////////////////////////////////////////////////////////////////////////////////////////////
// Subtext WebLog
// 
// Subtext is an open source weblog system that is a fork of the .TEXT
// weblog system.
//
// For updated news and information please visit http://subtextproject.com/
// Subtext is hosted at Google Code at http://code.google.com/p/subtext/
// The development mailing list is at subtext@googlegroups.com 
//
// This project is licensed under the BSD license.  See the License.txt file for more information.
///////////////////////////////////////////////////////////////////////////////////////////////////

#endregion

using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Subtext.Framework.Components;
using Subtext.Framework.Providers;
using Subtext.Framework.Routing;

namespace Subtext.Framework.Data{
    /// <summary>
    /// Common Subtext object transformations
    /// </summary>
    public static class Transformer
    {
        /// <summary>
        /// Converts a LinkCategoryCollection into a single LinkCategory with its own LinkCollection.
        /// </summary>
        public static LinkCategory BuildLinks(string title, CategoryType catType, Blog blog)
        {
            ICollection<LinkCategory> links = ObjectProvider.Instance().GetCategories(catType, true /* activeOnly */);
            return MergeLinkCategoriesIntoSingleLinkCategory(title, catType, links, new UrlHelper(null, null), blog);
        }

        /// <summary>
        /// Converts a LinkCategoryCollection into a single LinkCategory with its own LinkCollection.
        /// </summary>
        public static LinkCategory MergeLinkCategoriesIntoSingleLinkCategory(string title, CategoryType catType,
                                                                             IEnumerable<LinkCategory> links,
                                                                             UrlHelper urlHelper, Blog blog)
        {
            if(!links.IsNullOrEmpty())
            {
                var mergedLinkCategory = new LinkCategory {Title = title};

                var merged = from linkCategory in links
                            select GetLinkFromLinkCategory(linkCategory, catType, urlHelper, blog);
                mergedLinkCategory.Links.AddRange(merged);
                return mergedLinkCategory;
            }

            return null;
        }

        private static Link GetLinkFromLinkCategory(LinkCategory linkCategory, CategoryType catType, UrlHelper urlHelper, Blog blog)
        {
            var link = new Link {Title = linkCategory.Title};

            switch(catType)
            {
                case CategoryType.StoryCollection:
                    link.Url = urlHelper.CategoryUrl(linkCategory).ToFullyQualifiedUrl(blog).ToString();
                    break;

                case CategoryType.PostCollection:
                    link.Url = urlHelper.CategoryUrl(linkCategory).ToFullyQualifiedUrl(blog).ToString();
                    link.Rss = urlHelper.CategoryRssUrl(linkCategory);
                    break;

                case CategoryType.ImageCollection:
                    link.Url = urlHelper.GalleryUrl(linkCategory.Id).ToFullyQualifiedUrl(blog).ToString();
                    break;
            }
            link.NewWindow = false;
            return link;
        }

        /// <summary>
        /// Will convert ArchiveCountCollection method from Archives.GetPostsByMonthArchive()
        /// into a <see cref="LinkCategory"/>. LinkCategory is a common item to databind to a web control.
        /// </summary>
        public static LinkCategory BuildMonthLinks(string title, UrlHelper urlHelper, Blog blog)
        {
            ICollection<ArchiveCount> archiveCounts = ObjectProvider.Instance().GetPostCountsByMonth();
            return MergeArchiveCountsIntoLinkCategory(title, archiveCounts, urlHelper, blog);
        }

        public static LinkCategory MergeArchiveCountsIntoLinkCategory(string title,
                                                                      IEnumerable<ArchiveCount> archiveCounts,
                                                                      UrlHelper urlHelper, Blog blog)
        {
            var linkCategory = new LinkCategory {Title = title};
            foreach(ArchiveCount archiveCount in archiveCounts)
            {
                var link = new Link
                {
                    NewWindow = false,
                    IsActive = true,
                    Title = archiveCount.Date.ToString("y") + " (" +
                            archiveCount.Count.ToString(CultureInfo.InvariantCulture) + ")",
                    Url = urlHelper.MonthUrl(archiveCount.Date)
                };

                linkCategory.Links.Add(link);
            }
            return linkCategory;
        }

        /// <summary>
        /// Will convert ArchiveCountCollection method from Archives.GetPostsByCategoryArchive()
        /// into a <see cref="LinkCategory"/>. LinkCategory is a common item to databind to a web control.
        /// </summary>
        public static LinkCategory BuildCategoriesArchiveLinks(string title, UrlHelper urlHelper)
        {
            ICollection<ArchiveCount> acc = Archives.GetPostCountByCategory();

            var category = new LinkCategory {Title = title};
            foreach(ArchiveCount ac in acc)
            {
                var link = new Link
                {
                    IsActive = true,
                    NewWindow = false,
                    Title = string.Format("{0} ({1})", ac.Title, ac.Count.ToString(CultureInfo.InvariantCulture)),
                    Url = urlHelper.CategoryUrl(new Category {Id = ac.Id, Title = ac.Title})
                };
                //Ugh, I hate how categories work in Subtext. So intertwined with links.

                category.Links.Add(link);
            }
            return category;
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.