Click here to Skip to main content
15,881,833 members
Articles / Web Development / ASP.NET

Open Source Extensible Enterprise n-tier Application Framework with Multilayered Architecture

Rate me:
Please Sign up or sign in to vote.
4.67/5 (2 votes)
3 Dec 2010MIT3 min read 36.8K   770   41  
Xenta architecture overview
using System;
using SiberTek.Xenta.Data;
using SiberTek.Xenta.Data.Providers;
using SiberTek.Xenta.Entities;
using SiberTek.Xenta.Entities.Collections;
using SiberTek.Xenta.Enums;
using SiberTek.Xenta.Events;
using SiberTek.Xenta.Exceptions;
using SiberTek.Xenta.Utils;

namespace SiberTek.Xenta.Managers
{
    /// <summary>
    /// Contains methods to manage forums
    /// </summary>
    public class ForumManager : IManager
    {
        #region Fields
        private static ForumManager _instance = null;
        private IForumDataProvider _dataProvider;
        private bool _initialized;
        #endregion

        #region Constructors
        private ForumManager()
        {
            _dataProvider = null;
            _initialized = false;
        }
        #endregion

        #region Events
        public event EntityEventHandler ForumCreated;
        public event EntityEventHandler ForumUpdated;
        public event EntityEventHandler ForumDeleted;
        #endregion

        #region Properties
        public static ForumManager Instance
        {
            get
            {
                if(_instance == null)
                {
                    _instance = new ForumManager();
                }
                return _instance;
            }
        }

        private IForumDataProvider DataProvider
        {
            get
            {
                return _dataProvider;
            }
        }
        #endregion

        #region Methods
        public void Initialize()
        {
            if(!_initialized)
            {
                _dataProvider = DataAccessManager.GetProviderInstance<IForumDataProvider>("ForumDataProvider");

                _initialized = true;
            }
        }

        public ForumInfo CreateForum(int parentForumID, string title, string description, int displayOrder, bool isActive)
        {
            if(parentForumID != 0 && GetForum(parentForumID) == null)
            {
                throw new XentaException("Forum does not exist", (int)ForumError.ForumDoesNotExist);
            }

            title = StringHelper.TrimToLength(title, 1000);
            ForumInfo forum = null;
            int forumID;

            if(DataProvider.InsertForum(parentForumID, title, description, displayOrder, isActive, DateTime.UtcNow, DateTime.UtcNow, out forumID))
            {
                forum = GetForum(forumID);

                FireForumCreated(forum);

                if(parentForumID != 0)
                {
                    foreach(RoleInfo role in RoleManager.Instance.GetAllRoles(true))
                    {
                        if(IsRoleAssignedToForum(parentForumID, role.RoleID))
                        {
                            AssignRoleToForum(forumID, role.RoleID);
                        }
                    }
                }
            }
            return forum;
        }

        public bool AssignRoleToForum(int forumID, int roleID)
        {
            if(RoleManager.Instance.GetRole(roleID) == null)
            {
                return false;
            }

            ForumInfo forum = GetForum(forumID);
            bool res = false;

            if(forum != null)
            {
                if(IsRoleAssignedToForum(forumID, roleID))
                {
                    res = true;
                }
                else
                {
                    res = DataProvider.InsertForumRoleMapping(forumID, roleID);
                }
                if(res)
                {
                    foreach(ForumInfo childForum in forum.ChildForums)
                    {
                        AssignRoleToForum(childForum.ForumID, roleID);
                    }
                    foreach(ForumTopicInfo topic in forum.Topics)
                    {
                        ForumTopicManager.Instance.AssignRoleToForumTopic(topic.TopicID, roleID);
                    }
                }
            }

            return res;
        }

        public ForumInfo GetForum(int forumID)
        {
            return DataMapper.Map(DataProvider.GetForum(forumID));
        }

        public ForumInfoCollection GetAllForums()
        {
            return GetAllForums(false);
        }

        public ForumInfoCollection GetAllForums(bool showHidden)
        {
            return GetAllForums(null, null, null, showHidden);
        }

        public ForumInfoCollection GetForumsByParentForumID(int parentForumID)
        {
            return GetForumsByParentForumID(parentForumID, false);
        }

        public ForumInfoCollection GetForumsByParentForumID(int parentForumID, bool showHidden)
        {
            return GetAllForums(parentForumID, null, null, showHidden);
        }

        public ForumInfoCollection GetAllForums(int? parentForumID, DateTime? createdOnStart, DateTime? createdOnEnd, bool showHidden)
        {
            return DataMapper.Map(DataProvider.GetAllForums(parentForumID, createdOnStart, createdOnEnd, showHidden));
        }

        public bool IsRoleAssignedToForum(int forumID, int roleID)
        {
            return DataProvider.ForumRoleMappingExists(forumID, roleID);
        }

        public ForumInfo UpdateForum(int forumID, int parentForumID, string title, string description, int displayOrder, bool isActive)
        {
            ForumInfo forum = GetForum(forumID);
            
            if(forum == null)
            {
                throw new XentaException("Forum does not exist", (int)ForumError.ForumDoesNotExist);
            }
            if(parentForumID != 0 && GetForum(parentForumID) == null)
            {
                throw new XentaException("Forum does not exist", (int)ForumError.ForumDoesNotExist);
            }

            title = StringHelper.TrimToLength(title, 1000);

            if(DataProvider.UpdateForum(forumID, parentForumID, title, description, displayOrder, isActive, forum.CreatedOn, DateTime.UtcNow))
            {
                forum = GetForum(forumID);

                FireForumUpdated(forum);
            }
            return forum;
        }

        public void DeleteForum(int forumID)
        {
            ForumInfo forum = GetForum(forumID);

            if(forum != null)
            {
                FireForumDeleted(forum);

                foreach(RoleInfo role in RoleManager.Instance.GetAllRoles(true))
                {
                    if(IsRoleAssignedToForum(forumID, role.RoleID))
                    {
                        UnassignRoleFromForum(forumID, role.RoleID);
                    }
                }

                DataProvider.DeleteForum(forumID);
            }
        }

        public void UnassignRoleFromForum(int forumID, int roleID)
        {
            ForumInfo forum = GetForum(forumID);

            if(forum != null)
            {
                foreach(ForumInfo childForum in forum.ChildForums)
                {
                    UnassignRoleFromForum(childForum.ForumID, roleID);
                }
                foreach(ForumTopicInfo topic in forum.Topics)
                {
                    ForumTopicManager.Instance.UnassignRoleFromForumTopic(topic.TopicID, roleID);
                }

                DataProvider.DeleteForumRoleMapping(forumID, roleID);
            }
        }
        #endregion

        #region Utilties
        private void FireForumCreated(ForumInfo forum)
        {
            if(ForumCreated != null)
            {
                ForumCreated(this, new EntityEventArgs(forum));
            }
        }

        private void FireForumUpdated(ForumInfo forum)
        {
            if(ForumUpdated != null)
            {
                ForumUpdated(this, new EntityEventArgs(forum));
            }
        }

        private void FireForumDeleted(ForumInfo forum)
        {
            if(ForumDeleted != null)
            {
                ForumDeleted(this, new EntityEventArgs(forum));
            }
        }
        #endregion
    }
}

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 MIT License


Written By
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions