PageRenderTime 65ms CodeModel.GetById 31ms app.highlight 25ms RepoModel.GetById 2ms app.codeStats 0ms

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

#
C# | 217 lines | 146 code | 35 blank | 36 comment | 37 complexity | 10ac7bc191005916dbb99a70e36542b7 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.Xml;
  8
  9    /// <summary>
 10    /// The xml blog provider.
 11    /// </summary>
 12    public partial class XmlBlogProvider : BlogProvider
 13    {
 14        #region Public Methods
 15
 16        /// <summary>
 17        /// The delete profile.
 18        /// </summary>
 19        /// <param name="profile">
 20        /// The profile.
 21        /// </param>
 22        public override void DeleteProfile(AuthorProfile profile)
 23        {
 24            var fileName = string.Format("{0}profiles{1}{2}.xml", this.Folder, Path.DirectorySeparatorChar, profile.Id);
 25            if (File.Exists(fileName))
 26            {
 27                File.Delete(fileName);
 28            }
 29
 30            if (AuthorProfile.Profiles.Contains(profile))
 31            {
 32                AuthorProfile.Profiles.Remove(profile);
 33            }
 34        }
 35
 36        /// <summary>
 37        /// The fill profiles.
 38        /// </summary>
 39        /// <returns>
 40        /// A list of AuthorProfile.
 41        /// </returns>
 42        public override List<AuthorProfile> FillProfiles()
 43        {
 44            var folder = string.Format("{0}profiles{1}", this.Folder, Path.DirectorySeparatorChar);
 45
 46            return (from file in Directory.GetFiles(folder, "*.xml", SearchOption.TopDirectoryOnly)
 47                    select new FileInfo(file)
 48                    into info 
 49                    select info.Name.Replace(".xml", string.Empty)
 50                    into username
 51                    select AuthorProfile.Load(username)).ToList();
 52        }
 53
 54        /// <summary>
 55        /// The insert profile.
 56        /// </summary>
 57        /// <param name="profile">
 58        /// The profile.
 59        /// </param>
 60        public override void InsertProfile(AuthorProfile profile)
 61        {
 62            if (!Directory.Exists(string.Format("{0}profiles", this.Folder)))
 63            {
 64                Directory.CreateDirectory(string.Format("{0}profiles", this.Folder));
 65            }
 66
 67            var fileName = string.Format("{0}profiles{1}{2}.xml", this.Folder, Path.DirectorySeparatorChar, profile.Id);
 68            var settings = new XmlWriterSettings { Indent = true };
 69
 70            using (var writer = XmlWriter.Create(fileName, settings))
 71            {
 72                writer.WriteStartDocument(true);
 73                writer.WriteStartElement("profileData");
 74
 75                writer.WriteElementString("DisplayName", profile.DisplayName);
 76                writer.WriteElementString("FirstName", profile.FirstName);
 77                writer.WriteElementString("MiddleName", profile.MiddleName);
 78                writer.WriteElementString("LastName", profile.LastName);
 79
 80                writer.WriteElementString("CityTown", profile.CityTown);
 81                writer.WriteElementString("RegionState", profile.RegionState);
 82                writer.WriteElementString("Country", profile.Country);
 83
 84                writer.WriteElementString("Birthday", profile.Birthday.ToString("yyyy-MM-dd"));
 85                writer.WriteElementString("AboutMe", profile.AboutMe);
 86                writer.WriteElementString("PhotoURL", profile.PhotoUrl);
 87
 88                writer.WriteElementString("Company", profile.Company);
 89                writer.WriteElementString("EmailAddress", profile.EmailAddress);
 90                writer.WriteElementString("PhoneMain", profile.PhoneMain);
 91                writer.WriteElementString("PhoneMobile", profile.PhoneMobile);
 92                writer.WriteElementString("PhoneFax", profile.PhoneFax);
 93
 94                writer.WriteElementString("IsPrivate", profile.Private.ToString());
 95
 96                writer.WriteEndElement();
 97            }
 98        }
 99
100        /// <summary>
101        /// Retrieves a Page from the provider based on the specified id.
102        /// </summary>
103        /// <param name="id">The AuthorProfile id.</param>
104        /// <returns>An AuthorProfile.</returns>
105        public override AuthorProfile SelectProfile(string id)
106        {
107            var fileName = string.Format("{0}profiles{1}{2}.xml", this.Folder, Path.DirectorySeparatorChar, id);
108            var doc = new XmlDocument();
109            doc.Load(fileName);
110
111            var profile = new AuthorProfile(id);
112
113            if (doc.SelectSingleNode("//DisplayName") != null)
114            {
115                profile.DisplayName = doc.SelectSingleNode("//DisplayName").InnerText;
116            }
117
118            if (doc.SelectSingleNode("//FirstName") != null)
119            {
120                profile.FirstName = doc.SelectSingleNode("//FirstName").InnerText;
121            }
122
123            if (doc.SelectSingleNode("//MiddleName") != null)
124            {
125                profile.MiddleName = doc.SelectSingleNode("//MiddleName").InnerText;
126            }
127
128            if (doc.SelectSingleNode("//LastName") != null)
129            {
130                profile.LastName = doc.SelectSingleNode("//LastName").InnerText;
131            }
132
133            // profile.Address1 = doc.SelectSingleNode("//Address1").InnerText;
134            // profile.Address2 = doc.SelectSingleNode("//Address2").InnerText;
135            if (doc.SelectSingleNode("//CityTown") != null)
136            {
137                profile.CityTown = doc.SelectSingleNode("//CityTown").InnerText;
138            }
139
140            if (doc.SelectSingleNode("//RegionState") != null)
141            {
142                profile.RegionState = doc.SelectSingleNode("//RegionState").InnerText;
143            }
144
145            if (doc.SelectSingleNode("//Country") != null)
146            {
147                profile.Country = doc.SelectSingleNode("//Country").InnerText;
148            }
149
150            if (doc.SelectSingleNode("//Birthday") != null)
151            {
152                DateTime date;
153                if (DateTime.TryParse(doc.SelectSingleNode("//Birthday").InnerText, out date))
154                {
155                    profile.Birthday = date;
156                }
157            }
158
159            if (doc.SelectSingleNode("//AboutMe") != null)
160            {
161                profile.AboutMe = doc.SelectSingleNode("//AboutMe").InnerText;
162            }
163
164            if (doc.SelectSingleNode("//PhotoURL") != null)
165            {
166                profile.PhotoUrl = doc.SelectSingleNode("//PhotoURL").InnerText;
167            }
168
169            if (doc.SelectSingleNode("//Company") != null)
170            {
171                profile.Company = doc.SelectSingleNode("//Company").InnerText;
172            }
173
174            if (doc.SelectSingleNode("//EmailAddress") != null)
175            {
176                profile.EmailAddress = doc.SelectSingleNode("//EmailAddress").InnerText;
177            }
178
179            if (doc.SelectSingleNode("//PhoneMain") != null)
180            {
181                profile.PhoneMain = doc.SelectSingleNode("//PhoneMain").InnerText;
182            }
183
184            if (doc.SelectSingleNode("//PhoneMobile") != null)
185            {
186                profile.PhoneMobile = doc.SelectSingleNode("//PhoneMobile").InnerText;
187            }
188
189            if (doc.SelectSingleNode("//PhoneFax") != null)
190            {
191                profile.PhoneFax = doc.SelectSingleNode("//PhoneFax").InnerText;
192            }
193
194            if (doc.SelectSingleNode("//IsPrivate") != null)
195            {
196                profile.Private = doc.SelectSingleNode("//IsPrivate").InnerText == "true";
197            }
198
199            // page.DateCreated = DateTime.Parse(doc.SelectSingleNode("page/datecreated").InnerText, CultureInfo.InvariantCulture);
200            // page.DateModified = DateTime.Parse(doc.SelectSingleNode("page/datemodified").InnerText, CultureInfo.InvariantCulture);
201            return profile;
202        }
203
204        /// <summary>
205        /// The update profile.
206        /// </summary>
207        /// <param name="profile">
208        /// The profile.
209        /// </param>
210        public override void UpdateProfile(AuthorProfile profile)
211        {
212            this.InsertProfile(profile);
213        }
214
215        #endregion
216    }
217}