PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/BlogEngine/DotNetSlave.BusinessLogic/Providers/XmlProvider/Blogs.cs

#
C# | 188 lines | 105 code | 25 blank | 58 comment | 13 complexity | dcca2f38b560be72cd11b3091880cdfe MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Web;
  7. using System.Xml;
  8. using System.Web.Hosting;
  9. using System.Globalization;
  10. namespace BlogEngine.Core.Providers
  11. {
  12. public partial class XmlBlogProvider : BlogProvider
  13. {
  14. #region Public Methods
  15. /// <summary>
  16. /// Deletes a Blog
  17. /// </summary>
  18. /// <param name="blog">
  19. /// The blog.
  20. /// </param>
  21. public override void DeleteBlog(Blog blog)
  22. {
  23. this.WriteBlogsFile(Blog.Blogs.Where(b => b.Id != blog.Id).ToList());
  24. }
  25. /// <summary>
  26. /// Deletes the blog's storage container.
  27. /// </summary>
  28. /// <param name="blog">
  29. /// The blog.
  30. /// </param>
  31. public override bool DeleteBlogStorageContainer(Blog blog)
  32. {
  33. return blog.DeleteBlogFolder();
  34. }
  35. /// <summary>
  36. /// Fills an unsorted list of Blogs.
  37. /// </summary>
  38. /// <returns>
  39. /// A List&lt;Blog&gt; of all Blogs
  40. /// </returns>
  41. public override List<Blog> FillBlogs()
  42. {
  43. var blogs = new List<Blog>();
  44. // we want the folder of the primary blog instance, as "Blogs" is site-wide, not per-blog.
  45. // pass null to GetFolder() to get the base storage location.
  46. var fileName = this.GetFolder(null) + "blogs.xml";
  47. if (!File.Exists(fileName))
  48. {
  49. return blogs;
  50. }
  51. var doc = new XmlDocument();
  52. doc.Load(fileName);
  53. var nodes = doc.SelectNodes("blogs/item");
  54. if (nodes != null)
  55. {
  56. foreach (XmlNode node in nodes)
  57. {
  58. var b = new Blog()
  59. {
  60. Id = node.Attributes["id"] == null ? Guid.NewGuid() : new Guid(node.Attributes["id"].Value),
  61. Name = node.Attributes["name"] == null ? string.Empty : node.Attributes["name"].Value,
  62. StorageContainerName = node.Attributes["storageContainerName"] == null ? string.Empty : node.Attributes["storageContainerName"].Value,
  63. Hostname = node.Attributes["hostName"] == null ? string.Empty : node.Attributes["hostName"].Value,
  64. IsAnyTextBeforeHostnameAccepted = node.Attributes["isAnyTextBeforeHostnameAccepted"] == null ? false : bool.Parse(node.Attributes["isAnyTextBeforeHostnameAccepted"].Value),
  65. VirtualPath = node.Attributes["virtualPath"] == null ? string.Empty : node.Attributes["virtualPath"].Value,
  66. IsPrimary = node.Attributes["isPrimary"] == null ? false : bool.Parse(node.Attributes["isPrimary"].Value),
  67. IsActive = node.Attributes["isActive"] == null ? false : bool.Parse(node.Attributes["isActive"].Value)
  68. };
  69. blogs.Add(b);
  70. b.MarkOld();
  71. }
  72. }
  73. return blogs;
  74. }
  75. /// <summary>
  76. /// Inserts a Blog
  77. /// </summary>
  78. /// <param name="blog">
  79. /// The Blog.
  80. /// </param>
  81. public override void InsertBlog(Blog blog)
  82. {
  83. this.WriteBlogsFile(new List<Blog>(Blog.Blogs).Concat(new List<Blog>() { blog } ).ToList());
  84. }
  85. /// <summary>
  86. /// Gets a Blog based on a Guid.
  87. /// </summary>
  88. /// <param name="id">
  89. /// The Blog's Guid.
  90. /// </param>
  91. /// <returns>
  92. /// A matching Blog
  93. /// </returns>
  94. public override Blog SelectBlog(Guid id)
  95. {
  96. var blog = Blog.Blogs.Find(b => b.Id == id) ?? new Blog();
  97. blog.MarkOld();
  98. return blog;
  99. }
  100. /// <summary>
  101. /// Updates a Blog
  102. /// </summary>
  103. /// <param name="blog">
  104. /// The blog.
  105. /// </param>
  106. public override void UpdateBlog(Blog blog)
  107. {
  108. var blogs = Blog.Blogs;
  109. this.WriteBlogsFile(blogs);
  110. }
  111. #endregion
  112. #region Methods
  113. /// <summary>
  114. /// Saves blogs to a file.
  115. /// </summary>
  116. /// <param name="blogs">
  117. /// The blogs.
  118. /// </param>
  119. private void WriteBlogsFile(List<Blog> blogs)
  120. {
  121. // pass null to GetFolder() to get the base storage location.
  122. var fileName = this.GetFolder(null) + "blogs.xml";
  123. using (var writer = new XmlTextWriter(fileName, Encoding.UTF8))
  124. {
  125. writer.Formatting = Formatting.Indented;
  126. writer.Indentation = 4;
  127. writer.WriteStartDocument(true);
  128. writer.WriteStartElement("blogs");
  129. foreach (var b in blogs)
  130. {
  131. writer.WriteStartElement("item");
  132. writer.WriteAttributeString("id", b.Id.ToString());
  133. writer.WriteAttributeString("name", (b.Name ?? string.Empty).Trim());
  134. writer.WriteAttributeString("hostName", (b.Hostname ?? string.Empty).Trim());
  135. writer.WriteAttributeString("isAnyTextBeforeHostnameAccepted", b.IsAnyTextBeforeHostnameAccepted.ToString());
  136. writer.WriteAttributeString("storageContainerName", (b.StorageContainerName ?? string.Empty).Trim());
  137. writer.WriteAttributeString("virtualPath", (b.VirtualPath ?? string.Empty).Trim());
  138. writer.WriteAttributeString("isPrimary", b.IsPrimary.ToString());
  139. writer.WriteAttributeString("isActive", b.IsActive.ToString());
  140. writer.WriteEndElement();
  141. b.MarkOld();
  142. }
  143. writer.WriteEndElement();
  144. }
  145. }
  146. /// <summary>
  147. /// Sets up the required storage files/tables for a new Blog instance, from an existing blog instance.
  148. /// </summary>
  149. /// <param name="existingBlog">
  150. /// The existing blog to copy from.
  151. /// </param>
  152. /// <param name="newBlog">
  153. /// The new blog to copy to.
  154. /// </param>
  155. public override bool SetupBlogFromExistingBlog(Blog existingBlog, Blog newBlog)
  156. {
  157. bool copyResult = newBlog.CopyExistingBlogFolderToNewBlogFolder(existingBlog);
  158. // All we need to do for the XmlBlogProvider is to copy the folders, as done above.
  159. return copyResult;
  160. }
  161. #endregion
  162. }
  163. }