PageRenderTime 33ms CodeModel.GetById 20ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
C# | 179 lines | 112 code | 24 blank | 43 comment | 8 complexity | 48c16970e4cad216725e44e36f7e4d39 MD5 | raw file
  1namespace BlogEngine.Core.Providers
  2{
  3    using System;
  4    using System.Collections.Generic;
  5    using System.IO;
  6    using System.Linq;
  7    using System.Text;
  8    using System.Xml;
  9
 10    /// <summary>
 11    /// A storage provider for BlogEngine that uses XML files.
 12    ///     <remarks>
 13    /// To build another provider, you can just copy and modify
 14    ///         this one. Then add it to the web.config's BlogEngine section.
 15    ///     </remarks>
 16    /// </summary>
 17    public partial class XmlBlogProvider : BlogProvider
 18    {
 19        #region Public Methods
 20
 21        /// <summary>
 22        /// Deletes a Category
 23        /// </summary>
 24        /// <param name="category">
 25        /// Must be a valid Category object.
 26        /// </param>
 27        public override void DeleteCategory(Category category)
 28        {
 29            var categories = Category.Categories;
 30            categories.Remove(category);
 31
 32            if (Category.Categories.Contains(category))
 33            {
 34                Category.Categories.Remove(category);
 35            }
 36
 37            this.WriteToFile();
 38        }
 39
 40        /// <summary>
 41        /// Fills an unsorted list of categories.
 42        /// </summary>
 43        /// <returns>
 44        /// A List&lt;Category&gt; of all Categories.
 45        /// </returns>
 46        public override List<Category> FillCategories()
 47        {
 48            var fileName = this.Folder + "categories.xml";
 49            if (!File.Exists(fileName))
 50            {
 51                return null;
 52            }
 53
 54            var doc = new XmlDocument();
 55            doc.Load(fileName);
 56            var categories = new List<Category>();
 57
 58            foreach (XmlNode node in doc.SelectNodes("categories/category"))
 59            {
 60                var category = new Category { Id = new Guid(node.Attributes["id"].InnerText), Title = node.InnerText };
 61
 62                if (node.Attributes["description"] != null)
 63                {
 64                    category.Description = node.Attributes["description"].InnerText;
 65                }
 66                else
 67                {
 68                    category.Description = string.Empty;
 69                }
 70
 71                if (node.Attributes["parent"] != null)
 72                {
 73                    if (String.IsNullOrEmpty(node.Attributes["parent"].InnerText))
 74                    {
 75                        category.Parent = null;
 76                    }
 77                    else
 78                    {
 79                        category.Parent = new Guid(node.Attributes["parent"].InnerText);
 80                    }
 81                }
 82                else
 83                {
 84                    category.Parent = null;
 85                }
 86
 87                categories.Add(category);
 88                category.MarkOld();
 89            }
 90
 91            return categories;
 92        }
 93
 94        /// <summary>
 95        /// Inserts a Category
 96        /// </summary>
 97        /// <param name="category">
 98        /// Must be a valid Category object.
 99        /// </param>
100        public override void InsertCategory(Category category)
101        {
102            var categories = Category.Categories;
103            categories.Add(category);
104            categories.Sort();
105
106            this.WriteToFile();
107        }
108
109        /// <summary>
110        /// Gets a Category based on a Guid
111        /// </summary>
112        /// <param name="id">
113        /// The category's Guid.
114        /// </param>
115        /// <returns>
116        /// A matching Category
117        /// </returns>
118        public override Category SelectCategory(Guid id)
119        {
120            var categories = Category.Categories;
121
122            var category = categories.FirstOrDefault(cat => cat.Id == id) ?? new Category();
123            category.MarkOld();
124            return category;
125        }
126
127        /// <summary>
128        /// Updates a Category
129        /// </summary>
130        /// <param name="category">
131        /// Must be a valid Category object.
132        /// </param>
133        public override void UpdateCategory(Category category)
134        {
135            var categories = Category.Categories;
136            categories.Remove(category);
137            categories.Add(category);
138            categories.Sort();
139
140            this.WriteToFile();
141        }
142
143        #endregion
144
145        #region Methods
146
147        /// <summary>
148        /// Saves the Categories to disk.
149        /// </summary>
150        private void WriteToFile()
151        {
152            var categories = Category.Categories;
153            var fileName = string.Format("{0}categories.xml", this.Folder);
154
155            using (var writer = new XmlTextWriter(fileName, Encoding.UTF8))
156            {
157                writer.Formatting = Formatting.Indented;
158                writer.Indentation = 4;
159                writer.WriteStartDocument(true);
160                writer.WriteStartElement("categories");
161
162                foreach (var cat in categories)
163                {
164                    writer.WriteStartElement("category");
165                    writer.WriteAttributeString("id", cat.Id.ToString());
166                    writer.WriteAttributeString("description", cat.Description);
167                    writer.WriteAttributeString("parent", cat.Parent.ToString());
168                    writer.WriteValue(cat.Title);
169                    writer.WriteEndElement();
170                    cat.MarkOld();
171                }
172
173                writer.WriteEndElement();
174            }
175        }
176
177        #endregion
178    }
179}