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