DatabaseObjectProvider.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 » DatabaseObjectProvider.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;
using System.Collections.Generic;
using System.Data;
using Subtext.Extensibility.Interfaces;
using Subtext.Framework.Components;
using Subtext.Framework.Configuration;
using Subtext.Framework.Properties;
using Subtext.Framework.Providers;
using Subtext.Framework.Text;
using Subtext.Framework.Util;
using Subtext.Framework.Web.HttpModules;

namespace Subtext.Framework.Data{
    /// <summary>
    /// Concrete implementation of <see cref="ObjectProvider"/>. This 
    /// provides objects persisted to a database.
    /// </summary>
    public partial class DatabaseObjectProvider : ObjectProvider
    {
        readonly StoredProcedures _procedures = new StoredProcedures(Config.ConnectionString);

        public int BlogId
        {
            get
            {
                //Fix this up...
                return BlogRequest.Current.IsHostAdminRequest ? NullValue.NullInt32 : BlogRequest.Current.Blog.Id;
            }
        }

        public DateTime CurrentDateTime
        {
            get { return BlogRequest.Current.Blog.TimeZone.Now; }
        }

        private static void ValidateEntry(Entry e)
        {
            //TODO: The following doesn't belong here. It's verification code.
            if(!Config.Settings.AllowScriptsInPosts && HtmlHelper.HasIllegalContent(e.Body))
            {
                throw new IllegalPostCharactersException(Resources.IllegalPostCharacters);
            }

            //Never allow scripts in the title.
            if(HtmlHelper.HasIllegalContent(e.Title))
            {
                throw new IllegalPostCharactersException(Resources.IllegalPostCharacters);
            }

            if(!Config.Settings.AllowScriptsInPosts && HtmlHelper.HasIllegalContent(e.Description))
            {
                throw new IllegalPostCharactersException(Resources.IllegalPostCharacters);
            }

            //never allow scripts in the url.
            if(HtmlHelper.HasIllegalContent(e.EntryName))
            {
                throw new IllegalPostCharactersException(Resources.IllegalPostCharacters);
            }
        }

        public override bool TrackEntry(EntryView entryView)
        {
            return ThreadHelper.FireAndForget(o =>
                                              _procedures.TrackEntry(entryView.EntryId, entryView.BlogId, entryView.ReferralUrl,
                                                                     entryView.PageViewType == PageViewType.WebView), "Exception while tracking an entry");
        }

        public override ICollection<LinkCategory> GetActiveCategories()
        {
            using(IDataReader reader = _procedures.GetActiveCategoriesWithLinkCollection(BlogId.NullIfMinValue()))
            {
                return reader.ReadLinkCategories(true);
            }
        }

        public override IPagedCollection<Referrer> GetPagedReferrers(int pageIndex, int pageSize, int entryId)
        {
            using(
                IDataReader reader = _procedures.GetPageableReferrers(BlogId, entryId.NullIfMinValue(), pageIndex,
                                                                      pageSize))
            {
                return reader.ReadPagedCollection(r => DataHelper.ReadReferrer(r, Config.CurrentBlog));
            }
        }

        public override int Create(MetaTag metaTag)
        {
            return _procedures.InsertMetaTag(metaTag.Content,
                                             metaTag.Name.NullIfEmpty(),
                                             metaTag.HttpEquiv.NullIfEmpty(),
                                             BlogId,
                                             metaTag.EntryId,
                                             metaTag.DateCreated);
        }

        public override bool Update(MetaTag metaTag)
        {
            return _procedures.UpdateMetaTag(metaTag.Id,
                                             metaTag.Content,
                                             metaTag.Name.NullIfEmpty(),
                                             metaTag.HttpEquiv.NullIfEmpty(),
                                             BlogId,
                                             metaTag.EntryId);
        }

        public override IPagedCollection<MetaTag> GetMetaTagsForBlog(Blog blog, int pageIndex, int pageSize)
        {
            using(IDataReader reader = _procedures.GetMetaTags(blog.Id, null, pageIndex, pageSize))
            {
                return reader.ReadPagedCollection(r => r.ReadObject<MetaTag>());
            }
        }

        public override IPagedCollection<MetaTag> GetMetaTagsForEntry(Entry entry, int pageIndex, int pageSize)
        {
            using(IDataReader reader = _procedures.GetMetaTags(entry.BlogId, entry.Id, pageIndex, pageSize))
            {
                return reader.ReadPagedCollection(r => r.ReadObject<MetaTag>());
            }
        }

        public override bool DeleteMetaTag(int metaTagId)
        {
            return _procedures.DeleteMetaTag(metaTagId);
        }

        public override int Create(Enclosure enclosure)
        {
            return _procedures.InsertEnclosure(enclosure.Title ?? string.Empty,
                                               enclosure.Url,
                                               enclosure.MimeType,
                                               enclosure.Size,
                                               enclosure.AddToFeed,
                                               enclosure.ShowWithPost,
                                               enclosure.EntryId);
        }

        public override bool Update(Enclosure enclosure)
        {
            return _procedures.UpdateEnclosure(enclosure.Title,
                                               enclosure.Url,
                                               enclosure.MimeType,
                                               enclosure.Size,
                                               enclosure.AddToFeed,
                                               enclosure.ShowWithPost,
                                               enclosure.EntryId,
                                               enclosure.Id);
        }

        public override bool DeleteEnclosure(int enclosureId)
        {
            return _procedures.DeleteEnclosure(enclosureId);
        }

        public override KeyWord GetKeyWord(int keyWordId)
        {
            using(IDataReader reader = _procedures.GetKeyWord(keyWordId, BlogId))
            {
                KeyWord kw = null;
                while(reader.Read())
                {
                    kw = reader.ReadObject<KeyWord>();
                    break;
                }
                return kw;
            }
        }

        public override ICollection<KeyWord> GetKeyWords()
        {
            using(IDataReader reader = _procedures.GetBlogKeyWords(BlogId))
            {
                return reader.ReadCollection<KeyWord>();
            }
        }

        public override IPagedCollection<KeyWord> GetPagedKeyWords(int pageIndex, int pageSize)
        {
            using(IDataReader reader = _procedures.GetPageableKeyWords(BlogId, pageIndex, pageSize))
            {
                return reader.ReadPagedCollection(r => r.ReadObject<KeyWord>());
            }
        }

        public override bool UpdateKeyWord(KeyWord keyWord)
        {
            return _procedures.UpdateKeyWord(keyWord.Id,
                                             keyWord.Word,
                                             keyWord.Rel,
                                             keyWord.Text,
                                             keyWord.ReplaceFirstTimeOnly,
                                             keyWord.OpenInNewWindow,
                                             keyWord.CaseSensitive,
                                             keyWord.Url,
                                             keyWord.Title,
                                             BlogId);
        }

        public override int InsertKeyWord(KeyWord keyWord)
        {
            return _procedures.InsertKeyWord(keyWord.Word,
                                             keyWord.Rel,
                                             keyWord.Text,
                                             keyWord.ReplaceFirstTimeOnly,
                                             keyWord.OpenInNewWindow,
                                             keyWord.CaseSensitive,
                                             keyWord.Url,
                                             keyWord.Title,
                                             BlogId);
        }

        public override bool DeleteKeyWord(int id)
        {
            return _procedures.DeleteKeyWord(id, BlogId);
        }

        public override ImageCollection GetImagesByCategoryId(int categoryId, bool activeOnly)
        {
            using(IDataReader reader = _procedures.GetImageCategory(categoryId, activeOnly, BlogId))
            {
                var ic = new ImageCollection();
                while(reader.Read())
                {
                    ic.Category = reader.ReadLinkCategory();
                    break;
                }
                reader.NextResult();
                while(reader.Read())
                {
                    ic.Add(reader.ReadImage());
                }
                return ic;
            }
        }

        public override Image GetImage(int imageId, bool activeOnly)
        {
            using(IDataReader reader = _procedures.GetSingleImage(imageId, activeOnly, BlogId))
            {
                Image image = null;
                while(reader.Read())
                {
                    image = reader.ReadImage();
                }
                return image;
            }
        }

        public override int InsertImage(Image image)
        {
            return _procedures.InsertImage(image.Title,
                                           image.CategoryID,
                                           image.Width,
                                           image.Height,
                                           image.FileName,
                                           image.IsActive,
                                           BlogId,
                                           image.Url);
        }

        public override bool UpdateImage(Image image)
        {
            return _procedures.UpdateImage(image.Title,
                                           image.CategoryID,
                                           image.Width,
                                           image.Height,
                                           image.FileName,
                                           image.IsActive,
                                           BlogId,
                                           image.ImageID,
                                           image.Url);
        }

        public override bool DeleteImage(int imageId)
        {
            return _procedures.DeleteImage(BlogId, imageId);
        }

        private static LinkCategory ReadLinkCategory(IDataReader reader)
        {
            return !reader.Read() ? null : reader.ReadLinkCategory();
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.