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.Extensions;
using SiberTek.Xenta.Utils;
namespace SiberTek.Xenta.Managers
{
/// <summary>
/// Contains methods to manage forum posts
/// </summary>
public class ForumPostManager : IManager
{
#region Fields
private static ForumPostManager _instance = null;
private IForumPostDataProvider _dataProvider;
private bool _initialized;
#endregion
#region Constructors
private ForumPostManager()
{
_dataProvider = null;
_initialized = false;
}
#endregion
#region Events
public event EntityEventHandler PostCreated;
public event EntityEventHandler PostUpdated;
public event EntityEventHandler PostDeleted;
#endregion
#region Properties
public static ForumPostManager Instance
{
get
{
if(_instance == null)
{
_instance = new ForumPostManager();
}
return _instance;
}
}
private IForumPostDataProvider DataProvider
{
get
{
return _dataProvider;
}
}
#endregion
#region Methods
public void Initialize()
{
if(!_initialized)
{
_dataProvider = DataAccessManager.GetProviderInstance<IForumPostDataProvider>("ForumPostDataProvider");
ForumProfileManager.Instance.ProfileDeleted += OnForumProfileDeleted;
ForumTopicManager.Instance.TopicDeleted += OnForumTopicDeleted;
_initialized = true;
}
}
public ForumPostInfo CreateForumPost(int parentPostID, int topicID, int authorID, string title, string text, bool isActive)
{
if(ForumTopicManager.Instance.GetForumTopic(topicID) == null)
{
throw new XentaException("Forum topic does not exist", (int)ForumError.TopicDoesNotExist);
}
if(UserManager.Instance.GetUser(authorID) == null)
{
throw new XentaException("User does not exist", (int)CoreError.UserDoesNotExist);
}
ForumPostInfo post = null;
int postID;
title = StringHelper.TrimToLength(title, 1000);
text = StringHelper.TrimToLength(text, Int32.MaxValue);
if(DataProvider.InsertForumPost(parentPostID, topicID, authorID, title, text, isActive, DateTime.UtcNow, DateTime.UtcNow, out postID))
{
post = GetForumPost(postID);
FirePostCreated(post);
MessageManager.Instance.SendForumPostNotification(postID);
}
return post;
}
public ForumPostInfo GetForumPost(int postID)
{
return DataMapper.Map(DataProvider.GetForumPost(postID));
}
public ForumPostInfoCollection GetForumPostsByParentPostID(int parentPostID)
{
return GetForumPostsByAuthorID(parentPostID, false);
}
public ForumPostInfoCollection GetForumPostsByParentPostID(int parentPostID, bool showHidden)
{
return GetAllForumPosts(String.Empty, null, parentPostID, null, null, null, showHidden);
}
public ForumPostInfoCollection GetForumPostsByAuthorID(int authorID)
{
return GetForumPostsByAuthorID(authorID, false);
}
public ForumPostInfoCollection GetForumPostsByAuthorID(int authorID, bool showHidden)
{
return GetAllForumPosts(String.Empty, null, null, authorID, null, null, showHidden);
}
public ForumPostInfoCollection GetForumPostsByTopicID(int topicID)
{
return GetForumPostsByTopicID(topicID, false);
}
public ForumPostInfoCollection GetForumPostsByTopicID(int topicID, bool showHidden)
{
return GetAllForumPosts(String.Empty, topicID, null, null, null, null, showHidden);
}
public ForumPostInfoCollection GetAllForumPosts(string searchTerm, int? topicID, int? parentPostID, int? authorID, DateTime? createdOnStart, DateTime? createdOnEnd, bool showHidden)
{
int totalCount = 0;
return GetAllForumPosts(searchTerm, topicID, parentPostID, authorID, createdOnStart, createdOnEnd, showHidden, 0, Int32.MaxValue, out totalCount);
}
public ForumPostInfoCollection GetAllForumPosts(string searchTerm, int? topicID, int? parentPostID, int? authorID, DateTime? createdOnStart, DateTime? createdOnEnd, bool showHidden, int startIndex, int count, out int totalCount)
{
return DataMapper.Map(DataProvider.GetAllForumPosts(searchTerm, topicID, parentPostID, authorID, createdOnStart, createdOnEnd, showHidden, startIndex, count, out totalCount));
}
public ForumPostInfo UpdateForumPost(int postID, int parentPostID, int topicID, int authorID, string title, string text, bool isActive)
{
if(ForumTopicManager.Instance.GetForumTopic(topicID) == null)
{
throw new XentaException("Forum topic does not exist", (int)ForumError.TopicDoesNotExist);
}
if(UserManager.Instance.GetUser(authorID) == null)
{
throw new XentaException("User does not exist", (int)CoreError.UserDoesNotExist);
}
ForumPostInfo post = GetForumPost(postID);
if(post == null)
{
throw new XentaException("Forum post does not exist", (int)ForumError.PostDoesNotExist);
}
title = StringHelper.TrimToLength(title, 1000);
text = StringHelper.TrimToLength(text, Int32.MaxValue);
if(DataProvider.UpdateForumPost(postID, parentPostID, topicID, authorID, title, text, isActive, post.CreatedOn, DateTime.UtcNow))
{
post = GetForumPost(postID);
FirePostUpdated(post);
}
return post;
}
public void DeleteForumPost(int postID)
{
ForumPostInfo post = GetForumPost(postID);
if(post != null)
{
FirePostDeleted(post);
foreach(ForumPostInfo childPost in GetForumPostsByParentPostID(post.PostID))
{
UpdateForumPost(childPost.PostID, post.ParentPostID, childPost.TopicID, childPost.AuthorID, childPost.Title, childPost.Text, childPost.IsActive);
}
DataProvider.DeleteForumPost(postID);
}
}
#endregion
#region Handlers
private void OnForumProfileDeleted(object sender, EntityEventArgs e)
{
ForumProfileInfo profile = e.Entity as ForumProfileInfo;
if(profile != null)
{
UserInfo guest = UserManager.Instance.GetGuestUser();
foreach(ForumPostInfo post in profile.Posts)
{
UpdateForumPost(post.PostID, post.ParentPostID, post.TopicID, guest.UserID, post.Title, post.Text, post.IsActive);
}
}
}
private void OnForumTopicDeleted(object sender, EntityEventArgs e)
{
ForumTopicInfo topic = e.Entity as ForumTopicInfo;
if(topic != null)
{
foreach(ForumPostInfo post in topic.Posts)
{
DeleteForumPost(post.PostID);
}
}
}
#endregion
#region Utilties
private void FirePostCreated(ForumPostInfo post)
{
if(PostCreated != null)
{
PostCreated(this, new EntityEventArgs(post));
}
}
private void FirePostUpdated(ForumPostInfo post)
{
if(PostUpdated != null)
{
PostUpdated(this, new EntityEventArgs(post));
}
}
private void FirePostDeleted(ForumPostInfo post)
{
if(PostDeleted != null)
{
PostDeleted(this, new EntityEventArgs(post));
}
}
#endregion
}
}