BlogManager.cs :  » Web-Frameworks » nopCommerce » NopSolutions » NopCommerce » BusinessLogic » Content » Blog » 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 » Web Frameworks » nopCommerce 
nopCommerce » NopSolutions » NopCommerce » BusinessLogic » Content » Blog » BlogManager.cs
//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Globalization;
using System.Text;
using System.Web;
using NopSolutions.NopCommerce.BusinessLogic.Caching;
using NopSolutions.NopCommerce.BusinessLogic.Configuration.Settings;
using NopSolutions.NopCommerce.BusinessLogic.CustomerManagement;
using NopSolutions.NopCommerce.BusinessLogic.Localization;
using NopSolutions.NopCommerce.BusinessLogic.Messages;
using NopSolutions.NopCommerce.BusinessLogic.Profile;
using NopSolutions.NopCommerce.BusinessLogic.Utils.Html;
using NopSolutions.NopCommerce.Common.Utils.Html;
using NopSolutions.NopCommerce.DataAccess;
using NopSolutions.NopCommerce.DataAccess.Content.Blog;

namespace NopSolutions.NopCommerce.BusinessLogic.Content.Blog{
    /// <summary>
    /// Blog post manager
    /// </summary>
    public partial class BlogManager
    {
        #region Constants
        private const string BLOGPOST_BY_ID_KEY = "Nop.blogpost.id-{0}";
        private const string BLOGPOST_PATTERN_KEY = "Nop.blogpost.";
        #endregion

        #region Utilities
        private static BlogPostCollection DBMapping(DBBlogPostCollection dbCollection)
        {
            if (dbCollection == null)
                return null;

            var collection = new BlogPostCollection();
            foreach (var dbItem in dbCollection)
            {
                var item = DBMapping(dbItem);
                collection.Add(item);
            }

            return collection;
        }

        private static BlogPost DBMapping(DBBlogPost dbItem)
        {
            if (dbItem == null)
                return null;

            var item = new BlogPost();
            item.BlogPostId = dbItem.BlogPostId;
            item.LanguageId = dbItem.LanguageId;
            item.BlogPostTitle = dbItem.BlogPostTitle;
            item.BlogPostBody = dbItem.BlogPostBody;
            item.BlogPostAllowComments = dbItem.BlogPostAllowComments;
            item.CreatedById = dbItem.CreatedById;
            item.CreatedOn = dbItem.CreatedOn;

            return item;
        }

        private static BlogCommentCollection DBMapping(DBBlogCommentCollection dbCollection)
        {
            if (dbCollection == null)
                return null;

            var collection = new BlogCommentCollection();
            foreach (var dbItem in dbCollection)
            {
                var item = DBMapping(dbItem);
                collection.Add(item);
            }

            return collection;
        }

        private static BlogComment DBMapping(DBBlogComment dbItem)
        {
            if (dbItem == null)
                return null;

            var item = new BlogComment();
            item.BlogCommentId = dbItem.BlogCommentId;
            item.BlogPostId = dbItem.BlogPostId;
            item.CustomerId = dbItem.CustomerId;
            item.IPAddress = dbItem.IPAddress;
            item.CommentText = dbItem.CommentText;
            item.CreatedOn = dbItem.CreatedOn;

            return item;
        }
        #endregion

        #region Methods
        /// <summary>
        /// Deletes an blog post
        /// </summary>
        /// <param name="blogPostId">Blog post identifier</param>
        public static void DeleteBlogPost(int blogPostId)
        {
            DBProviderManager<DBBlogProvider>.Provider.DeleteBlogPost(blogPostId);
            if (BlogManager.CacheEnabled)
            {
                NopCache.RemoveByPattern(BLOGPOST_PATTERN_KEY);
            }
        }

        /// <summary>
        /// Gets an blog post
        /// </summary>
        /// <param name="blogPostId">Blog post identifier</param>
        /// <returns>Blog post</returns>
        public static BlogPost GetBlogPostById(int blogPostId)
        {
            if (blogPostId == 0)
                return null;

            string key = string.Format(BLOGPOST_BY_ID_KEY, blogPostId);
            object obj2 = NopCache.Get(key);
            if (BlogManager.CacheEnabled && (obj2 != null))
            {
                return (BlogPost)obj2;
            }

            var dbItem = DBProviderManager<DBBlogProvider>.Provider.GetBlogPostById(blogPostId);
            var blogPost = DBMapping(dbItem);

            if (BlogManager.CacheEnabled)
            {
                NopCache.Max(key, blogPost);
            }
            return blogPost;
        }

        /// <summary>
        /// Gets all blog posts
        /// </summary>
        /// <param name="languageId">Language identifier. 0 if you want to get all news</param>
        /// <returns>Blog posts</returns>
        public static BlogPostCollection GetAllBlogPosts(int languageId)
        {
            int totalRecords;
            return GetAllBlogPosts(languageId, Int32.MaxValue, 0, out totalRecords);
        }

        /// <summary>
        /// Gets all blog posts
        /// </summary>
        /// <param name="languageId">Language identifier. 0 if you want to get all news</param>
        /// <param name="pageSize">Page size</param>
        /// <param name="pageIndex">Page index</param>
        /// <param name="totalRecords">Total records</param>
        /// <returns>Blog posts</returns>
        public static BlogPostCollection GetAllBlogPosts(int languageId, int pageSize,
            int pageIndex, out int totalRecords)
        {
            if(pageSize <= 0)
                pageSize = 10;
            if(pageSize == Int32.MaxValue)
                pageSize = Int32.MaxValue - 1;
            if(pageIndex < 0)
                pageIndex = 0;
            if(pageIndex == Int32.MaxValue)
                pageIndex = Int32.MaxValue - 1;
            return DBMapping(DBProviderManager<DBBlogProvider>.Provider.GetAllBlogPosts(languageId, pageSize, pageIndex, out totalRecords));
        }

        /// <summary>
        /// Inserts an blog post
        /// </summary>
        /// <param name="languageId">The language identifier</param>
        /// <param name="blogPostTitle">The blog post title</param>
        /// <param name="blogPostBody">The blog post title</param>
        /// <param name="blogPostAllowComments">A value indicating whether the blog post comments are allowed</param>
        /// <param name="createdById">The user identifier who created the blog post</param>
        /// <param name="createdOn">The date and time of instance creation</param>
        /// <returns>Blog post</returns>
        public static BlogPost InsertBlogPost(int languageId, string blogPostTitle,
            string blogPostBody, bool blogPostAllowComments,
            int createdById, DateTime createdOn)
        {
            createdOn = DateTimeHelper.ConvertToUtcTime(createdOn);

            var dbItem = DBProviderManager<DBBlogProvider>.Provider.InsertBlogPost(languageId, 
                blogPostTitle, blogPostBody, blogPostAllowComments, 
                createdById, createdOn);
            var blogPost = DBMapping(dbItem);

            if (BlogManager.CacheEnabled)
            {
                NopCache.RemoveByPattern(BLOGPOST_PATTERN_KEY);
            }

            return blogPost;
        }

        /// <summary>
        /// Updates the blog post
        /// </summary>
        /// <param name="blogPostId">The blog post identifier</param>
        /// <param name="languageId">The language identifier</param>
        /// <param name="blogPostTitle">The blog post title</param>
        /// <param name="blogPostBody">The blog post title</param>
        /// <param name="blogPostAllowComments">A value indicating whether the blog post comments are allowed</param>
        /// <param name="createdById">The user identifier who created the blog post</param>
        /// <param name="createdOn">The date and time of instance creation</param>
        /// <returns>Blog post</returns>
        public static BlogPost UpdateBlogPost(int blogPostId,
            int languageId, string blogPostTitle,
            string blogPostBody, bool blogPostAllowComments,
            int createdById, DateTime createdOn)
        {
            createdOn = DateTimeHelper.ConvertToUtcTime(createdOn);

            var dbItem = DBProviderManager<DBBlogProvider>.Provider.UpdateBlogPost(blogPostId, 
                languageId, blogPostTitle, blogPostBody,
                blogPostAllowComments, createdById, createdOn);
            var blogPost = DBMapping(dbItem);

            if (BlogManager.CacheEnabled)
            {
                NopCache.RemoveByPattern(BLOGPOST_PATTERN_KEY);
            }

            return blogPost;
        }

        /// <summary>
        /// Deletes an blog comment
        /// </summary>
        /// <param name="blogCommentId">Blog comment identifier</param>
        public static void DeleteBlogComment(int blogCommentId)
        {
            DBProviderManager<DBBlogProvider>.Provider.DeleteBlogComment(blogCommentId);
        }

        /// <summary>
        /// Gets an blog comment
        /// </summary>
        /// <param name="blogCommentId">Blog comment identifier</param>
        /// <returns>A blog comment</returns>
        public static BlogComment GetBlogCommentById(int blogCommentId)
        {
            if (blogCommentId == 0)
                return null;

            var dbItem = DBProviderManager<DBBlogProvider>.Provider.GetBlogCommentById(blogCommentId);
            var blogComment = DBMapping(dbItem);
            return blogComment;
        }

        /// <summary>
        /// Gets a collection of blog comments by blog post identifier
        /// </summary>
        /// <param name="blogPostId">Blog post identifier</param>
        /// <returns>A collection of blog comments</returns>
        public static BlogCommentCollection GetBlogCommentsByBlogPostId(int blogPostId)
        {
            var dbCollection = DBProviderManager<DBBlogProvider>.Provider.GetBlogCommentsByBlogPostId(blogPostId);
            var collection = DBMapping(dbCollection);
            return collection;
        }

        /// <summary>
        /// Gets all blog comments
        /// </summary>
        /// <returns>Blog comments</returns>
        public static BlogCommentCollection GetAllBlogComments()
        {
            var dbCollection = DBProviderManager<DBBlogProvider>.Provider.GetAllBlogComments();
            var collection = DBMapping(dbCollection);
            return collection;
        }

        /// <summary>
        /// Inserts a blog comment
        /// </summary>
        /// <param name="blogPostId">The blog post identifier</param>
        /// <param name="customerId">The customer identifier who commented the blog post</param>
        /// <param name="commentText">The comment text</param>
        /// <param name="createdOn">The date and time of instance creation</param>
        /// <returns>Blog comment</returns>
        public static BlogComment InsertBlogComment(int blogPostId,
            int customerId, string commentText, DateTime createdOn)
        {
            return InsertBlogComment(blogPostId, customerId, commentText,
                createdOn, BlogManager.NotifyAboutNewBlogComments);
        }

        /// <summary>
        /// Inserts a blog comment
        /// </summary>
        /// <param name="blogPostId">The blog post identifier</param>
        /// <param name="customerId">The customer identifier who commented the blog post</param>
        /// <param name="commentText">The comment text</param>
        /// <param name="createdOn">The date and time of instance creation</param>
        /// <param name="notify">A value indicating whether to notify the store owner</param>
        /// <returns>Blog comment</returns>
        public static BlogComment InsertBlogComment(int blogPostId,
            int customerId, string commentText, DateTime createdOn, bool notify)
        {
            string IPAddress = string.Empty;
            if(HttpContext.Current != null && HttpContext.Current.Request != null)
            {
                IPAddress = HttpContext.Current.Request.UserHostAddress;
            }
            return InsertBlogComment(blogPostId, customerId, IPAddress, commentText, createdOn, notify);
        }

        /// <summary>
        /// Inserts a blog comment
        /// </summary>
        /// <param name="blogPostId">The blog post identifier</param>
        /// <param name="customerId">The customer identifier who commented the blog post</param>
        /// <param name="ipAddress">The IP address</param>
        /// <param name="commentText">The comment text</param>
        /// <param name="createdOn">The date and time of instance creation</param>
        /// <param name="notify">A value indicating whether to notify the store owner</param>
        /// <returns>Blog comment</returns>
        public static BlogComment InsertBlogComment(int blogPostId,
            int customerId, string ipAddress, string commentText, DateTime createdOn, bool notify)
        {
            createdOn = DateTimeHelper.ConvertToUtcTime(createdOn);

            var dbItem = DBProviderManager<DBBlogProvider>.Provider.InsertBlogComment(blogPostId,
                customerId, ipAddress, commentText, createdOn);
            var blogComment = DBMapping(dbItem);

            if (notify)
            {
                MessageManager.SendBlogCommentNotificationMessage(blogComment, LocalizationManager.DefaultAdminLanguage.LanguageId);
            }

            return blogComment;
        }

        /// <summary>
        /// Updates the blog comment
        /// </summary>
        /// <param name="blogCommentId">The blog comment identifier</param>
        /// <param name="blogPostId">The blog post identifier</param>
        /// <param name="customerId">The customer identifier who commented the blog post</param>
        /// <param name="ipAddress">The IP address</param>
        /// <param name="commentText">The comment text</param>
        /// <param name="createdOn">The date and time of instance creation</param>
        /// <returns>Blog comment</returns>
        public static BlogComment UpdateBlogComment(int blogCommentId, int blogPostId,
            int customerId, string ipAddress, string commentText, DateTime createdOn)
        {
            createdOn = DateTimeHelper.ConvertToUtcTime(createdOn);

            var dbItem = DBProviderManager<DBBlogProvider>.Provider.UpdateBlogComment(blogCommentId,
                blogPostId, customerId, ipAddress, commentText, createdOn);
            var blogComment = DBMapping(dbItem);
            return blogComment;
        }
        
        /// <summary>
        /// Formats the text
        /// </summary>
        /// <param name="text">Text</param>
        /// <returns>Formatted text</returns>
        public static string FormatCommentText(string text)
        {
            if (String.IsNullOrEmpty(text))
                return string.Empty;

            text = HtmlHelper.FormatText(text, false, true, false, false, false, false);
            return text;
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets a value indicating whether cache is enabled
        /// </summary>
        public static bool CacheEnabled
        {
            get
            {
                return SettingManager.GetSettingValueBoolean("Cache.BlogManager.CacheEnabled");
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether blog is enabled
        /// </summary>
        public static bool BlogEnabled
        {
            get
            {
                return SettingManager.GetSettingValueBoolean("Common.EnableBlog");
            }
            set
            {
                SettingManager.SetParam("Common.EnableBlog", value.ToString());
            }
        }

        /// <summary>
        /// Gets or sets the page size for posts
        /// </summary>
        public static int PostsPageSize
        {
            get
            {
                return SettingManager.GetSettingValueInteger("Blog.PostsPageSize", 10);
            }
            set
            {
                SettingManager.SetParam("Blog.PostsPageSize", value.ToString());
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether not registered user can leave comments
        /// </summary>
        public static bool AllowNotRegisteredUsersToLeaveComments
        {
            get
            {
                return SettingManager.GetSettingValueBoolean("Blog.AllowNotRegisteredUsersToLeaveComments");
            }
            set
            {
                SettingManager.SetParam("Blog.AllowNotRegisteredUsersToLeaveComments", value.ToString());
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether to notify about new blog comments
        /// </summary>
        public static bool NotifyAboutNewBlogComments
        {
            get
            {
                return SettingManager.GetSettingValueBoolean("Blog.NotifyAboutNewBlogComments");
            }
            set
            {
                SettingManager.SetParam("Blog.NotifyAboutNewBlogComments", value.ToString());
            }
        }
        #endregion
    }
}
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.