⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 confirmcommands.cs

📁 基于.Net环境下c#实现的国人自己的Blog平台,基于.Text的核心技术,但做了汉化以及改写了一部分的核心代码,值得研究学习
💻 CS
字号:
#region Disclaimer/Info

///////////////////////////////////////////////////////////////////////////////////////////////////
// .Text WebLog
// 
// .Text is an open source weblog system started by Scott Watermasysk. 
// Blog: http://ScottWater.com/blog 
// RSS: http://scottwater.com/blog/rss.aspx
// Email: Dottext@ScottWater.com
//
// For updated news and information please visit http://scottwater.com/dottext and subscribe to 
// the Rss feed @ http://scottwater.com/dottext/rss.aspx
//
// On its release (on or about August 1, 2003) this application is licensed under the BSD. However, I reserve the 
// right to change or modify this at any time. The most recent and up to date license can always be fount at:
// http://ScottWater.com/License.txt
// 
// Please direct all code related questions to:
// GotDotNet Workspace: http://www.gotdotnet.com/Community/Workspaces/workspace.aspx?id=e99fccb3-1a8c-42b5-90ee-348f6b77c407
// Yahoo Group http://groups.yahoo.com/group/DotText/
// 
///////////////////////////////////////////////////////////////////////////////////////////////////

#endregion

using System;
using System.IO;
using System.Web;
using Dottext.Framework;
using Dottext.Framework.Components;
using Dottext.Framework.Util;

namespace Dottext.Web.Admin
{
	// -----------------------------------------------------------------------------------------
	// Abstract commands
	// -----------------------------------------------------------------------------------------

	#region ConfirmCommand

	[Serializable]
	public abstract class ConfirmCommand
	{
		protected const string DEFAULT_PROMPT = "是否确定?";
		protected const string DEFAULT_EXECUTE_SUCCESS = "操作成功。";
		protected const string DEFAULT_EXECUTE_FAILURE = "操作失败。详细信息: {1}";
		protected const string DEFAULT_CANCEL_SUCCESS = "操作取消。";
		protected const string DEFAULT_CANCEL_FAILURE = "不能取消操作。详细信息: {1}";

		protected string _promptMessage;
		protected string _executeSuccessMessage;
		protected string _executeFailureMessage;
		protected string _cancelSuccessMessage;
		protected string _cancelFailureMessage;

		protected bool _autoRedirect = false;
		protected string _redirectUrl;

		#region Accessors

		public virtual bool AutoRedirect
		{
			get { return _autoRedirect; }
			set { _autoRedirect = value; }
		}

		public virtual string RedirectUrl
		{
			get { return _redirectUrl; }
			set { _redirectUrl = value; }
		}

		public virtual string PromptMessage
		{
			get
			{
				if (!Utilities.IsNullorEmpty(_promptMessage))
					return _promptMessage;
				else
					return DEFAULT_PROMPT;
			}
			set { _promptMessage = value; }
		}

		public virtual string ExecuteSuccessMessage
		{
			get
			{
				if (!Utilities.IsNullorEmpty(_executeSuccessMessage))
					return _executeSuccessMessage;
				else
					return DEFAULT_EXECUTE_SUCCESS;
			}
			set { _executeSuccessMessage = value; }
		}

		public virtual string ExecuteFailureMessage
		{
			get
			{
				if (!Utilities.IsNullorEmpty(_executeFailureMessage))
					return _executeFailureMessage;
				else
					return DEFAULT_EXECUTE_FAILURE;
			}
			set { _executeFailureMessage = value; }
		}

		public virtual string CancelSuccessMessage
		{
			get
			{
				if (!Utilities.IsNullorEmpty(_cancelSuccessMessage))
					return _cancelSuccessMessage;
				else
					return DEFAULT_CANCEL_SUCCESS;
			}
			set { _cancelSuccessMessage = value; }
		}

		public virtual string CancelFailureMessage
		{
			get
			{
				if (!Utilities.IsNullorEmpty(_cancelFailureMessage))
					return _cancelFailureMessage;
				else
					return DEFAULT_CANCEL_FAILURE;
			}
			set { _cancelFailureMessage = value; }
		}

		#endregion

		public virtual string FormatMessage(string message)
		{
			return FormatMessage(message, null);
		}

		public virtual string FormatMessage(string format, params object[] args)
		{
			try
			{
				return String.Format(format, args);
			}
			catch (ArgumentNullException)
			{
				return format;
			}
		}

		public abstract string Cancel();
		public abstract string Execute();
	}

	#endregion

	#region DeleteTargetCommand		

	[Serializable]
	public abstract class DeleteTargetCommand : ConfirmCommand
	{
		protected int _targetID;
		protected string _targetName = "";

		protected DeleteTargetCommand()
		{
			_promptMessage = "确认删除 {0} {1}?";
			_executeSuccessMessage = "{0} {1} 已经被删除。";
			_executeFailureMessage = "{0} {1} 不能被删除。详细信息: {2}";
			_cancelSuccessMessage = "{0} {1} 不能被删除。";
			_cancelFailureMessage = "不能取消删除 {0} {1}。详细信息: {2}";
		}

		protected DeleteTargetCommand(int targetID) : this()
		{
			_targetID = targetID;
		}

		protected DeleteTargetCommand(string targetName, int targetID) : this()
		{
			_targetName = targetName;
			_targetID = targetID;
		}

		public override string PromptMessage
		{
			get { return FormatMessage(base.PromptMessage, _targetName, _targetID); }
			set { _promptMessage = value; }
		}

		public override string Cancel()
		{
			_autoRedirect = true;
			return FormatMessage(CancelSuccessMessage, _targetName, _targetID);
		}
	}

	#endregion

	#region DeleteTitledTargetCommand		

	[Serializable]
	public abstract class DeleteTitledTargetCommand : DeleteTargetCommand
	{
		protected string _itemTitle;

		protected DeleteTitledTargetCommand()
		{
			_promptMessage = "确定删除 {0} \"{1}\"?";
			_executeSuccessMessage = "{0} \"{1}\" 已经被删除。";
			_executeFailureMessage = "{0} \"{1}\" 不能被删除。详细信息: {2}";
			_cancelSuccessMessage = "{0} \"{1}\" 不能被删除。";
			_cancelFailureMessage = "不能取消删除 {0} \"{1}\"。详细信息: {2}";
		}

		public DeleteTitledTargetCommand(int targetID, string itemTitle) : this()
		{
			_targetID = targetID;
			_itemTitle = itemTitle;
		}

		public override string PromptMessage
		{
			get
			{
				if (!Utilities.IsNullorEmpty(_promptMessage))
					return FormatMessage(_promptMessage, _targetName, _itemTitle);
				else
					return base.PromptMessage;
			}
			set { _promptMessage = value; }
		}

		public override string Cancel()
		{
			_autoRedirect = true;
			return FormatMessage(CancelSuccessMessage, _targetName, _itemTitle);
		}
	}

	#endregion

	// -----------------------------------------------------------------------------------------
	// Concrete commands
	// -----------------------------------------------------------------------------------------

	#region DeletePostCommand		

	[Serializable]
	public class DeletePostCommand : DeleteTargetCommand
	{
		public DeletePostCommand(int postID)
		{
			_targetName = "文章";
			_targetID = postID;
		}

		public override string Execute()
		{
			try
			{
				Entries.Delete(_targetID);
				return FormatMessage(ExecuteSuccessMessage, _targetName, _targetID);
			}
			catch (Exception ex)
			{
				return FormatMessage(ExecuteFailureMessage, _targetName, _targetID, ex.Message);
			}
		}
	}

	#endregion

	#region DeleteComment		

	[Serializable]
	public class DeleteCommentCommand : DeleteTargetCommand
	{
		public DeleteCommentCommand(int postID)
		{
			_targetName = "评论";
			_targetID = postID;
		}

		public override string Execute()
		{
			try
			{
				Entries.Delete(_targetID);
				return FormatMessage(ExecuteSuccessMessage, _targetName, _targetID);
			}
			catch (Exception ex)
			{
				return FormatMessage(ExecuteFailureMessage, _targetName, _targetID, ex.Message);
			}
		}
	}

	#endregion

	#region DeleteCategoryCommand		

	[Serializable]
	public class DeleteCategoryCommand : DeleteTitledTargetCommand
	{
		public DeleteCategoryCommand(int categoryID, string categoryTitle)
		{
			_targetName = "分类";
			_targetID = categoryID;
			_itemTitle = categoryTitle;
		}

		public override string Execute()
		{
			try
			{
				Links.DeleteLinkCategory(_targetID);
				return FormatMessage(ExecuteSuccessMessage, _targetName, _itemTitle);
			}
			catch (Exception ex)
			{
				return FormatMessage(ExecuteFailureMessage, _targetName, _itemTitle, ex.Message);
			}
		}
	}

	#endregion

	#region DeleteGalleryCommand		

	// TODO: derivation is fine, but expose the prompts so you can add a WARNING WILL DELETE 
	// FILES TOO message for this one (makes general sense as well)
	[Serializable]
	public class DeleteGalleryCommand : DeleteTitledTargetCommand
	{
		public DeleteGalleryCommand(int galleryID, string galleryTitle)
		{
			_targetID = galleryID;
			_itemTitle = galleryTitle;
		}

		public override string Execute()
		{
			try
			{
				ImageCollection imageList = Images.GetImagesByCategoryID(_targetID, false);

				// delete the folder
				string galleryFolder = Images.LocalGalleryFilePath(HttpContext.Current, _targetID);
				if (Directory.Exists(galleryFolder))
					Directory.Delete(galleryFolder, true);

				if (imageList.Count > 0)
				{
					// delete from data provider
					foreach (Image currentImage in imageList)
						Images.DeleteImage(currentImage);
				}

				// finally, delete the gallery (category) itself from the data provider
				Links.DeleteLinkCategory(_targetID);
				return FormatMessage(ExecuteSuccessMessage, _targetName, _itemTitle);
			}
			catch (Exception ex)
			{
				return FormatMessage(ExecuteFailureMessage, _targetName, _targetID, ex.Message);
			}
		}
	}

	#endregion

	#region DeleteKeyWordCommand		

	[Serializable]
	public class DeleteKeyWordCommand : DeleteTitledTargetCommand
	{
		public DeleteKeyWordCommand(int keyWordID, string word)
		{
			_autoRedirect = true;
			_targetName = "关键词";
			_targetID = keyWordID;
			_itemTitle = word;
		}

		public override string Execute()
		{
			try
			{
				KeyWords.DeleteKeyWord(_targetID);
				return FormatMessage(ExecuteSuccessMessage, _targetName, _itemTitle);
			}
			catch (Exception ex)
			{
				return FormatMessage(ExecuteFailureMessage, _targetName, _itemTitle, ex.Message);
			}
		}
	}

	#endregion


	#region DeleteLinkCommand		

	[Serializable]
	public class DeleteLinkCommand : DeleteTitledTargetCommand
	{
		public DeleteLinkCommand(int linkID, string linkTitle)
		{
			_autoRedirect = true;
			_targetName = "链接";
			_targetID = linkID;
			_itemTitle = linkTitle;
		}

		public override string Execute()
		{
			try
			{
				Links.DeleteLink(_targetID);
				return FormatMessage(ExecuteSuccessMessage, _targetName, _itemTitle);
			}
			catch (Exception ex)
			{
				return FormatMessage(ExecuteFailureMessage, _targetName, _itemTitle, ex.Message);
			}
		}
	}

	#endregion

	#region DeleteImageCommand

	[Serializable]
	public class DeleteImageCommand : DeleteTitledTargetCommand
	{
		public DeleteImageCommand(int imageID)
		{
			_targetName = "图片";
			_targetID = imageID;
			_itemTitle = imageID.ToString();
		}

		public DeleteImageCommand(int imageID, string imageTitle)
		{
			_targetName = "图片";
			_targetID = imageID;
			_itemTitle = imageTitle;
		}

		public override string Execute()
		{
			try
			{
				Image currentImage = Images.GetSingleImage(_targetID, false);

				// The following line should be fully encapsulated and handle files + data
				// For now, I commented out the file trys in the the object so it can do just
				// data without exception. I'll do the files locally until we decide to really
				// do the framework class

				Images.DeleteImage(currentImage);

				// now delete the associated files if they exist
				string galleryFolder = Images.LocalGalleryFilePath(HttpContext.Current, currentImage.CategoryID);
				if (Directory.Exists(galleryFolder))
				{
					DeleteFile(galleryFolder, currentImage.OriginalFile);
					DeleteFile(galleryFolder, currentImage.ResizedFile);
					DeleteFile(galleryFolder, currentImage.ThumbNailFile);
				}

				return FormatMessage(ExecuteSuccessMessage, _targetName, _itemTitle);
			}
			catch (Exception ex)
			{
				return FormatMessage(ExecuteFailureMessage, _targetName, _targetID, ex.Message);
			}
		}

		private void DeleteFile(string path, string filename)
		{
			string localPath = Path.Combine(path, filename);
			if (File.Exists(localPath))
				File.Delete(localPath);
		}
	}

	#endregion

	#region ImportLinksCommand

	[Serializable]
	public class ImportLinksCommand : ConfirmCommand
	{
		protected OpmlItemCollection _linksToImport;
		protected LinkCollection _allLinks;
		protected int _categoryID = -1;

		protected ImportLinksCommand()
		{
			_promptMessage = "在文件中共找到{0}个链接。<p/>重复的链接将被删除。<p/>确定导入链接吗?";
			_executeSuccessMessage = "共有{0}个链接成功导入。";
			_executeFailureMessage = "导入失败。详细信息:{0}";
			_cancelSuccessMessage = "导入操作被取消了。";
			_cancelFailureMessage = "不能取消导入操作。详细信息:{0}";
		}

		public ImportLinksCommand(OpmlItemCollection links, int catID) : this()
		{
			_linksToImport = links;
			_categoryID = catID;

		}

		public override string PromptMessage
		{
			get
			{
				if (!Utilities.IsNullorEmpty(_promptMessage))
					return FormatMessage(_promptMessage, _linksToImport.Count);
				else
					return base.PromptMessage;
			}
			set { _promptMessage = value; }
		}

		public override string Execute()
		{
			try
			{
				// we could do this in the provider or, better yet, just make a get all links method
				//				PagedLinkCollection allLinks = Links.GetPagedLinks(1, 1);
				//				_allLinks = Links.GetPagedLinks(1, allLinks.MaxItems);

				// process import collection
				foreach (OpmlItem item in _linksToImport)
					ImportOpmlItem(item);

				return FormatMessage(ExecuteSuccessMessage, _linksToImport.Count);
			}
			catch (Exception ex)
			{
				return FormatMessage(ExecuteFailureMessage, ex.Message);
			}
		}

		public override string Cancel()
		{
			_autoRedirect = false;
			return FormatMessage(CancelSuccessMessage);
		}

		private void ImportOpmlItem(OpmlItem item)
		{
			foreach (OpmlItem childItem in item.ChildItems)
				ImportOpmlItem(childItem);

			Link newLink = new Link();
			newLink.Title = item.Title;
			newLink.Url = item.HtmlUrl;
			newLink.Rss = item.XmlUrl;
			newLink.CategoryID = _categoryID;

			// TODO: let user specify and pass as command props
			newLink.IsActive = true;
			newLink.NewWindow = false;

			// this isn't a valid collision test really
			if (!_allLinks.Contains(newLink))
				Links.CreateLink(newLink);
		}
	}

	#endregion

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -