/XmlLayer/BlogData.cs

# · C# · 355 lines · 282 code · 67 blank · 6 comment · 27 complexity · 19c588836260cdbd8d597e5d00310d04 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Xml;
  6. using System.Globalization;
  7. using LiteBlog.Common;
  8. namespace LiteBlog.XmlLayer
  9. {
  10. public class BlogData : IBlogData
  11. {
  12. public List<PostInfo> GetBlogItems()
  13. {
  14. List<PostInfo> posts = new List<PostInfo>();
  15. XmlDocument blogDoc = new XmlDocument();
  16. try
  17. {
  18. blogDoc.Load(_path);
  19. }
  20. catch (Exception ex)
  21. {
  22. Logger.Log(NO_FILE_ERROR, ex);
  23. throw new ApplicationException(NO_FILE_ERROR, ex);
  24. }
  25. XmlNodeList blogList = blogDoc.SelectNodes("/Posts/Post");
  26. if (blogList == null)
  27. return posts;
  28. try
  29. {
  30. foreach (XmlNode blogNode in blogList)
  31. {
  32. PostInfo post = new PostInfo();
  33. post.Title = blogNode.Attributes["Title"].Value;
  34. if (blogNode.Attributes["Author"] != null)
  35. post.Author = blogNode.Attributes["Author"].Value;
  36. post.FileID = blogNode.Attributes["FileID"].Value;
  37. post.CatID = blogNode.Attributes["CatID"].Value;
  38. try
  39. {
  40. post.Time = DateTime.ParseExact(blogNode.Attributes["Time"].Value, DataContext.DateTimeFormat, CultureInfo.InvariantCulture);
  41. }
  42. catch (Exception ex)
  43. {
  44. string msg = String.Format(DATE_FORMAT_ERROR, post.FileID );
  45. Logger.Log(msg, ex);
  46. post.Time = DateTime.MinValue;
  47. }
  48. post.MonthID = blogNode.Attributes["MonthID"].Value;
  49. posts.Add(post);
  50. }
  51. }
  52. catch (Exception ex)
  53. {
  54. Logger.Log(XML_FORMAT_ERROR, ex);
  55. throw new ApplicationException(XML_FORMAT_ERROR, ex);
  56. }
  57. return posts;
  58. }
  59. public void Create(PostInfo postInfo)
  60. {
  61. XmlDocument blogDoc = new XmlDocument();
  62. try
  63. {
  64. blogDoc.Load(_path);
  65. }
  66. catch (Exception ex)
  67. {
  68. Logger.Log(NO_FILE_ERROR, ex);
  69. throw new ApplicationException(NO_FILE_ERROR, ex);
  70. }
  71. string xpath = "/Posts/Post[@FileID='{0}']";
  72. XmlNode postNode = blogDoc.SelectSingleNode(String.Format(xpath, postInfo.FileID));
  73. if (postNode != null)
  74. {
  75. Logger.Log(DUP_POST_ERROR);
  76. throw new ApplicationException(DUP_POST_ERROR);
  77. }
  78. XmlElement postElem = blogDoc.CreateElement("Post");
  79. XmlAttribute titleAttr = blogDoc.CreateAttribute("Title");
  80. titleAttr.Value = postInfo.Title;
  81. XmlAttribute urlAttr = blogDoc.CreateAttribute("FileID");
  82. urlAttr.Value = postInfo.FileID;
  83. XmlAttribute authAttr = blogDoc.CreateAttribute("Author");
  84. authAttr.Value = postInfo.Author;
  85. XmlAttribute catAttr = blogDoc.CreateAttribute("CatID");
  86. catAttr.Value = postInfo.CatID;
  87. XmlAttribute monthAttr = blogDoc.CreateAttribute("MonthID");
  88. monthAttr.Value = postInfo.MonthID;
  89. XmlAttribute timeAttr = blogDoc.CreateAttribute("Time");
  90. timeAttr.Value = postInfo.Time.ToString(DataContext.DateTimeFormat, CultureInfo.InvariantCulture);
  91. postElem.Attributes.Append(titleAttr);
  92. postElem.Attributes.Append(urlAttr);
  93. postElem.Attributes.Append(authAttr);
  94. postElem.Attributes.Append(catAttr);
  95. postElem.Attributes.Append(monthAttr);
  96. postElem.Attributes.Append(timeAttr);
  97. try
  98. {
  99. XmlNode blogNode = blogDoc.SelectSingleNode("/Posts");
  100. bool lastNode = true;
  101. if (blogNode.ChildNodes.Count > 0)
  102. {
  103. foreach (XmlNode postNode2 in blogNode.ChildNodes)
  104. {
  105. DateTime refDate;
  106. try
  107. {
  108. refDate = DateTime.ParseExact(postNode2.Attributes["Time"].Value, DataContext.DateTimeFormat, CultureInfo.InvariantCulture);
  109. }
  110. catch (Exception ex)
  111. {
  112. string msg = String.Format(DATE_FORMAT_ERROR, postNode2.Attributes["FileID"].Value);
  113. Logger.Log(msg, ex);
  114. refDate = DateTime.MaxValue;
  115. }
  116. if (postInfo.Time > refDate)
  117. {
  118. blogNode.InsertBefore(postElem, postNode2);
  119. lastNode = false;
  120. break;
  121. }
  122. }
  123. }
  124. if (lastNode)
  125. blogNode.AppendChild(postElem);
  126. blogDoc.Save(_path);
  127. }
  128. catch (Exception ex)
  129. {
  130. Logger.Log(XML_FORMAT_ERROR, ex);
  131. throw new ApplicationException(XML_FORMAT_ERROR, ex);
  132. }
  133. }
  134. public void Update(string fileID, string title, string catID)
  135. {
  136. XmlDocument blogDoc = new XmlDocument();
  137. try
  138. {
  139. blogDoc.Load(_path);
  140. }
  141. catch (Exception ex)
  142. {
  143. Logger.Log(NO_FILE_ERROR, ex);
  144. throw new ApplicationException(NO_FILE_ERROR, ex);
  145. }
  146. string xpath = "/Posts/Post[@FileID='{0}']";
  147. XmlNode postNode = blogDoc.SelectSingleNode(String.Format(xpath, fileID));
  148. if (postNode == null)
  149. {
  150. string msg = String.Format(NO_POST_ERROR, fileID);
  151. Logger.Log(msg);
  152. throw new ApplicationException(msg);
  153. }
  154. postNode.Attributes["Title"].Value = title;
  155. postNode.Attributes["CatID"].Value = catID;
  156. blogDoc.Save(_path);
  157. }
  158. /// <summary>
  159. /// If author is deleted or edited
  160. /// </summary>
  161. public void ChangeAuthor(string fileID, string author)
  162. {
  163. XmlDocument blogDoc = new XmlDocument();
  164. try
  165. {
  166. blogDoc.Load(_path);
  167. }
  168. catch (Exception ex)
  169. {
  170. Logger.Log(NO_FILE_ERROR, ex);
  171. throw new ApplicationException(NO_FILE_ERROR, ex);
  172. }
  173. string xpath = "/Posts/Post[@FileID='{0}']";
  174. XmlNode postNode = blogDoc.SelectSingleNode(String.Format(xpath, fileID));
  175. if (postNode == null)
  176. {
  177. string msg = String.Format(NO_POST_ERROR, fileID);
  178. Logger.Log(msg);
  179. throw new ApplicationException(msg);
  180. }
  181. XmlAttribute authAttr = postNode.Attributes["Author"];
  182. if (authAttr == null)
  183. {
  184. authAttr = blogDoc.CreateAttribute("Author");
  185. XmlAttribute catAttr = postNode.Attributes["CatID"];
  186. postNode.Attributes.InsertBefore(authAttr, catAttr);
  187. }
  188. authAttr.Value = author;
  189. blogDoc.Save(_path);
  190. }
  191. /// <summary>
  192. /// If category is deleted or edited
  193. /// </summary>
  194. public void ChangeCategory(string fileID, string oldCatID, string newCatID)
  195. {
  196. XmlDocument blogDoc = new XmlDocument();
  197. try
  198. {
  199. blogDoc.Load(_path);
  200. }
  201. catch (Exception ex)
  202. {
  203. Logger.Log(NO_FILE_ERROR, ex);
  204. throw new ApplicationException(NO_FILE_ERROR, ex);
  205. }
  206. string xpath = "/Posts/Post[@FileID='{0}']";
  207. XmlNode postNode = blogDoc.SelectSingleNode(String.Format(xpath, fileID));
  208. if (postNode == null)
  209. {
  210. string msg = String.Format(NO_POST_ERROR, fileID);
  211. Logger.Log(msg);
  212. throw new ApplicationException(msg);
  213. }
  214. string[] catIDs = postNode.Attributes["CatID"].Value.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
  215. string newCatIDs = "";
  216. foreach (string catID in catIDs)
  217. {
  218. if (catID == oldCatID)
  219. {
  220. if (newCatID != "")
  221. newCatIDs += newCatID + ",";
  222. }
  223. else
  224. {
  225. newCatIDs += catID + ",";
  226. }
  227. }
  228. if (newCatIDs.Length > 0)
  229. if (newCatIDs[newCatIDs.Length - 1] == ',')
  230. newCatIDs = newCatIDs.Substring(0, newCatIDs.Length - 1);
  231. postNode.Attributes["CatID"].Value = newCatIDs;
  232. blogDoc.Save(_path);
  233. }
  234. public void Delete(string fileID)
  235. {
  236. XmlDocument blogDoc = new XmlDocument();
  237. try
  238. {
  239. blogDoc.Load(_path);
  240. }
  241. catch (Exception ex)
  242. {
  243. Logger.Log(NO_FILE_ERROR, ex);
  244. throw new ApplicationException(NO_FILE_ERROR, ex);
  245. }
  246. string xpath = "/Posts/Post[@FileID='{0}']";
  247. XmlNode postNode = blogDoc.SelectSingleNode(String.Format(xpath, fileID));
  248. if (postNode == null)
  249. {
  250. string msg = String.Format(NO_POST_ERROR, fileID);
  251. Logger.Log(msg);
  252. throw new ApplicationException(msg);
  253. }
  254. blogDoc.DocumentElement.RemoveChild(postNode);
  255. blogDoc.Save(_path);
  256. }
  257. internal bool PostExists(string fileID)
  258. {
  259. XmlDocument blogDoc = new XmlDocument();
  260. try
  261. {
  262. blogDoc.Load(_path);
  263. }
  264. catch (Exception ex)
  265. {
  266. Logger.Log(NO_FILE_ERROR, ex);
  267. throw new ApplicationException(NO_FILE_ERROR, ex);
  268. }
  269. string xpath = "/Posts/Post[@FileID='{0}']";
  270. XmlNode postNode = blogDoc.SelectSingleNode(String.Format(xpath, fileID));
  271. return ((postNode == null) ? false : true);
  272. }
  273. internal static string Path
  274. {
  275. get { return DataContext.Path + "Blog.xml"; }
  276. }
  277. public BlogData()
  278. {
  279. _path = DataContext.Path + "Blog.xml";
  280. }
  281. private string _path;
  282. private const string NO_FILE_ERROR = "Blog meta file could not be found";
  283. private const string XML_FORMAT_ERROR = "Blog meta file is not in the right format";
  284. private const string DUP_POST_ERROR = "A duplicate post was found in the blog meta file";
  285. private const string DATE_FORMAT_ERROR = "Time field for {0} is not in the right format";
  286. private const string NO_POST_ERROR = "Post = {0} could not be found";
  287. }
  288. }