/XmlLayer/BlogData.cs
# · C# · 355 lines · 282 code · 67 blank · 6 comment · 27 complexity · 19c588836260cdbd8d597e5d00310d04 MD5 · raw file
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Xml;
- using System.Globalization;
- using LiteBlog.Common;
-
- namespace LiteBlog.XmlLayer
- {
- public class BlogData : IBlogData
- {
-
-
- public List<PostInfo> GetBlogItems()
- {
- List<PostInfo> posts = new List<PostInfo>();
-
- XmlDocument blogDoc = new XmlDocument();
-
- try
- {
- blogDoc.Load(_path);
- }
- catch (Exception ex)
- {
- Logger.Log(NO_FILE_ERROR, ex);
- throw new ApplicationException(NO_FILE_ERROR, ex);
- }
-
- XmlNodeList blogList = blogDoc.SelectNodes("/Posts/Post");
- if (blogList == null)
- return posts;
-
- try
- {
- foreach (XmlNode blogNode in blogList)
- {
- PostInfo post = new PostInfo();
- post.Title = blogNode.Attributes["Title"].Value;
- if (blogNode.Attributes["Author"] != null)
- post.Author = blogNode.Attributes["Author"].Value;
- post.FileID = blogNode.Attributes["FileID"].Value;
- post.CatID = blogNode.Attributes["CatID"].Value;
-
- try
- {
- post.Time = DateTime.ParseExact(blogNode.Attributes["Time"].Value, DataContext.DateTimeFormat, CultureInfo.InvariantCulture);
- }
- catch (Exception ex)
- {
- string msg = String.Format(DATE_FORMAT_ERROR, post.FileID );
- Logger.Log(msg, ex);
- post.Time = DateTime.MinValue;
- }
- post.MonthID = blogNode.Attributes["MonthID"].Value;
- posts.Add(post);
- }
- }
- catch (Exception ex)
- {
- Logger.Log(XML_FORMAT_ERROR, ex);
- throw new ApplicationException(XML_FORMAT_ERROR, ex);
- }
-
-
- return posts;
- }
-
- public void Create(PostInfo postInfo)
- {
- XmlDocument blogDoc = new XmlDocument();
-
- try
- {
- blogDoc.Load(_path);
- }
- catch (Exception ex)
- {
- Logger.Log(NO_FILE_ERROR, ex);
- throw new ApplicationException(NO_FILE_ERROR, ex);
- }
-
-
- string xpath = "/Posts/Post[@FileID='{0}']";
- XmlNode postNode = blogDoc.SelectSingleNode(String.Format(xpath, postInfo.FileID));
- if (postNode != null)
- {
- Logger.Log(DUP_POST_ERROR);
- throw new ApplicationException(DUP_POST_ERROR);
- }
-
- XmlElement postElem = blogDoc.CreateElement("Post");
- XmlAttribute titleAttr = blogDoc.CreateAttribute("Title");
- titleAttr.Value = postInfo.Title;
- XmlAttribute urlAttr = blogDoc.CreateAttribute("FileID");
- urlAttr.Value = postInfo.FileID;
- XmlAttribute authAttr = blogDoc.CreateAttribute("Author");
- authAttr.Value = postInfo.Author;
- XmlAttribute catAttr = blogDoc.CreateAttribute("CatID");
- catAttr.Value = postInfo.CatID;
- XmlAttribute monthAttr = blogDoc.CreateAttribute("MonthID");
- monthAttr.Value = postInfo.MonthID;
- XmlAttribute timeAttr = blogDoc.CreateAttribute("Time");
- timeAttr.Value = postInfo.Time.ToString(DataContext.DateTimeFormat, CultureInfo.InvariantCulture);
- postElem.Attributes.Append(titleAttr);
- postElem.Attributes.Append(urlAttr);
- postElem.Attributes.Append(authAttr);
- postElem.Attributes.Append(catAttr);
- postElem.Attributes.Append(monthAttr);
- postElem.Attributes.Append(timeAttr);
-
-
- try
- {
-
- XmlNode blogNode = blogDoc.SelectSingleNode("/Posts");
- bool lastNode = true;
- if (blogNode.ChildNodes.Count > 0)
- {
- foreach (XmlNode postNode2 in blogNode.ChildNodes)
- {
-
- DateTime refDate;
- try
- {
- refDate = DateTime.ParseExact(postNode2.Attributes["Time"].Value, DataContext.DateTimeFormat, CultureInfo.InvariantCulture);
- }
- catch (Exception ex)
- {
- string msg = String.Format(DATE_FORMAT_ERROR, postNode2.Attributes["FileID"].Value);
- Logger.Log(msg, ex);
- refDate = DateTime.MaxValue;
- }
-
-
- if (postInfo.Time > refDate)
- {
- blogNode.InsertBefore(postElem, postNode2);
- lastNode = false;
- break;
- }
- }
-
- }
-
- if (lastNode)
- blogNode.AppendChild(postElem);
-
- blogDoc.Save(_path);
- }
- catch (Exception ex)
- {
- Logger.Log(XML_FORMAT_ERROR, ex);
- throw new ApplicationException(XML_FORMAT_ERROR, ex);
- }
- }
-
- public void Update(string fileID, string title, string catID)
- {
- XmlDocument blogDoc = new XmlDocument();
-
- try
- {
- blogDoc.Load(_path);
- }
- catch (Exception ex)
- {
- Logger.Log(NO_FILE_ERROR, ex);
- throw new ApplicationException(NO_FILE_ERROR, ex);
- }
-
-
- string xpath = "/Posts/Post[@FileID='{0}']";
- XmlNode postNode = blogDoc.SelectSingleNode(String.Format(xpath, fileID));
- if (postNode == null)
- {
- string msg = String.Format(NO_POST_ERROR, fileID);
- Logger.Log(msg);
- throw new ApplicationException(msg);
- }
-
- postNode.Attributes["Title"].Value = title;
- postNode.Attributes["CatID"].Value = catID;
-
- blogDoc.Save(_path);
-
- }
-
- /// <summary>
- /// If author is deleted or edited
- /// </summary>
- public void ChangeAuthor(string fileID, string author)
- {
- XmlDocument blogDoc = new XmlDocument();
-
- try
- {
- blogDoc.Load(_path);
- }
- catch (Exception ex)
- {
- Logger.Log(NO_FILE_ERROR, ex);
- throw new ApplicationException(NO_FILE_ERROR, ex);
- }
-
-
- string xpath = "/Posts/Post[@FileID='{0}']";
- XmlNode postNode = blogDoc.SelectSingleNode(String.Format(xpath, fileID));
- if (postNode == null)
- {
- string msg = String.Format(NO_POST_ERROR, fileID);
- Logger.Log(msg);
- throw new ApplicationException(msg);
- }
-
- XmlAttribute authAttr = postNode.Attributes["Author"];
- if (authAttr == null)
- {
- authAttr = blogDoc.CreateAttribute("Author");
- XmlAttribute catAttr = postNode.Attributes["CatID"];
- postNode.Attributes.InsertBefore(authAttr, catAttr);
- }
-
- authAttr.Value = author;
-
- blogDoc.Save(_path);
-
- }
-
- /// <summary>
- /// If category is deleted or edited
- /// </summary>
- public void ChangeCategory(string fileID, string oldCatID, string newCatID)
- {
- XmlDocument blogDoc = new XmlDocument();
-
- try
- {
- blogDoc.Load(_path);
- }
- catch (Exception ex)
- {
- Logger.Log(NO_FILE_ERROR, ex);
- throw new ApplicationException(NO_FILE_ERROR, ex);
- }
-
-
- string xpath = "/Posts/Post[@FileID='{0}']";
- XmlNode postNode = blogDoc.SelectSingleNode(String.Format(xpath, fileID));
- if (postNode == null)
- {
- string msg = String.Format(NO_POST_ERROR, fileID);
- Logger.Log(msg);
- throw new ApplicationException(msg);
- }
-
-
- string[] catIDs = postNode.Attributes["CatID"].Value.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
- string newCatIDs = "";
- foreach (string catID in catIDs)
- {
- if (catID == oldCatID)
- {
- if (newCatID != "")
- newCatIDs += newCatID + ",";
- }
- else
- {
- newCatIDs += catID + ",";
- }
- }
-
- if (newCatIDs.Length > 0)
- if (newCatIDs[newCatIDs.Length - 1] == ',')
- newCatIDs = newCatIDs.Substring(0, newCatIDs.Length - 1);
-
-
- postNode.Attributes["CatID"].Value = newCatIDs;
-
- blogDoc.Save(_path);
-
- }
-
-
- public void Delete(string fileID)
- {
- XmlDocument blogDoc = new XmlDocument();
-
- try
- {
- blogDoc.Load(_path);
- }
- catch (Exception ex)
- {
- Logger.Log(NO_FILE_ERROR, ex);
- throw new ApplicationException(NO_FILE_ERROR, ex);
- }
-
-
- string xpath = "/Posts/Post[@FileID='{0}']";
- XmlNode postNode = blogDoc.SelectSingleNode(String.Format(xpath, fileID));
- if (postNode == null)
- {
- string msg = String.Format(NO_POST_ERROR, fileID);
- Logger.Log(msg);
- throw new ApplicationException(msg);
- }
-
- blogDoc.DocumentElement.RemoveChild(postNode);
- blogDoc.Save(_path);
-
-
- }
-
- internal bool PostExists(string fileID)
- {
-
- XmlDocument blogDoc = new XmlDocument();
-
- try
- {
- blogDoc.Load(_path);
- }
- catch (Exception ex)
- {
- Logger.Log(NO_FILE_ERROR, ex);
- throw new ApplicationException(NO_FILE_ERROR, ex);
- }
-
-
- string xpath = "/Posts/Post[@FileID='{0}']";
- XmlNode postNode = blogDoc.SelectSingleNode(String.Format(xpath, fileID));
- return ((postNode == null) ? false : true);
- }
-
- internal static string Path
- {
- get { return DataContext.Path + "Blog.xml"; }
- }
-
- public BlogData()
- {
- _path = DataContext.Path + "Blog.xml";
- }
-
- private string _path;
- private const string NO_FILE_ERROR = "Blog meta file could not be found";
- private const string XML_FORMAT_ERROR = "Blog meta file is not in the right format";
- private const string DUP_POST_ERROR = "A duplicate post was found in the blog meta file";
- private const string DATE_FORMAT_ERROR = "Time field for {0} is not in the right format";
- private const string NO_POST_ERROR = "Post = {0} could not be found";
-
- }
- }