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