PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/BlogEngine/DotNetSlave.BusinessLogic/Profile.cs

#
C# | 287 lines | 191 code | 30 blank | 66 comment | 1 complexity | 42f94edadfac7f9577e9b11f992307d7 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.IO;
  4. using System.Web.Security;
  5. using System.Xml.Serialization;
  6. namespace BlogEngine.Core
  7. {
  8. /// <summary>
  9. /// This business object is to handle the profiles of users
  10. /// </summary>
  11. [XmlRoot("profile")]
  12. public class Profile : BusinessBase<Profile, string>
  13. {
  14. #region Fields
  15. private string aboutMe;
  16. private string birthDate;
  17. private string cityState;
  18. private string country;
  19. private string displayName;
  20. private string firstName;
  21. private string gender;
  22. private string interests;
  23. private bool isPrivate;
  24. private string lastName;
  25. private string photoURL;
  26. private string regionState;
  27. private string userName;
  28. #endregion
  29. #region Properties
  30. /// <summary>
  31. ///
  32. /// </summary>
  33. [XmlElement("displayName", DataType = "string")]
  34. public string DisplayName
  35. {
  36. get { return displayName; }
  37. set { displayName = value; }
  38. }
  39. /// <summary>
  40. ///
  41. /// </summary>
  42. [XmlElement("interests", DataType = "string")]
  43. public string Interests
  44. {
  45. get { return interests; }
  46. set { interests = value; }
  47. }
  48. /// <summary>
  49. ///
  50. /// </summary>
  51. [XmlElement("userName", DataType = "string")]
  52. public string UserName
  53. {
  54. get { return userName; }
  55. set { userName = value; }
  56. }
  57. ///<summary>
  58. ///</summary>
  59. [XmlElement("aboutme", DataType = "string")]
  60. public string AboutMe
  61. {
  62. get { return aboutMe; }
  63. set { aboutMe = value; }
  64. }
  65. ///<summary>
  66. ///</summary>
  67. [XmlElement("firstName", DataType = "string")]
  68. public string FirstName
  69. {
  70. get { return firstName; }
  71. set { firstName = value; }
  72. }
  73. ///<summary>
  74. ///</summary>
  75. [XmlElement("lastName", DataType = "string")]
  76. public string LastName
  77. {
  78. get { return lastName; }
  79. set { lastName = value; }
  80. }
  81. ///<summary>
  82. ///</summary>
  83. [XmlElement("isPrivate", DataType = "bool")]
  84. public bool IsPrivate
  85. {
  86. get { return isPrivate; }
  87. set { isPrivate = value; }
  88. }
  89. ///<summary>
  90. ///</summary>
  91. [XmlElement("photoURL", DataType = "string")]
  92. public string PhotoURL
  93. {
  94. get { return photoURL; }
  95. set { photoURL = value; }
  96. }
  97. ///<summary>
  98. ///</summary>
  99. [XmlElement("gender", DataType = "string")]
  100. public string Gender
  101. {
  102. get { return gender; }
  103. set { gender = value; }
  104. }
  105. ///<summary>
  106. ///</summary>
  107. [XmlElement("birthDate", DataType = "string")]
  108. public string BirthDate
  109. {
  110. get { return birthDate; }
  111. set { birthDate = value; }
  112. }
  113. ///<summary>
  114. ///</summary>
  115. [XmlElement("cityState", DataType = "string")]
  116. public string CityState
  117. {
  118. get { return cityState; }
  119. set { cityState = value; }
  120. }
  121. ///<summary>
  122. ///</summary>
  123. [XmlElement("regionState", DataType = "string")]
  124. public string RegionState
  125. {
  126. get { return regionState; }
  127. set { regionState = value; }
  128. }
  129. ///<summary>
  130. ///</summary>
  131. [XmlElement("country", DataType = "string")]
  132. public string Country
  133. {
  134. get { return country; }
  135. set { country = value; }
  136. }
  137. #endregion
  138. #region Helpers
  139. ///<summary>
  140. ///</summary>
  141. ///<param name="username"></param>
  142. ///<returns></returns>
  143. public static Profile GetProfile(string username)
  144. {
  145. Profile profile = new Profile();
  146. try
  147. {
  148. // Deserialize the specified file to a Theater object.
  149. XmlSerializer xs = new XmlSerializer(typeof (Profile));
  150. FileStream fs =
  151. new FileStream(BlogSettings.Instance.StorageLocation + username.ToLowerInvariant() + ".xml",
  152. FileMode.Open);
  153. profile = (Profile) xs.Deserialize(fs);
  154. }
  155. catch (Exception x)
  156. {
  157. Console.WriteLine("Exception: " + x.Message);
  158. }
  159. return profile;
  160. }
  161. /// <summary>
  162. ///
  163. /// </summary>
  164. /// <param name="userProfile"></param>
  165. public static void SaveProfile(Profile userProfile)
  166. {
  167. try
  168. {
  169. // Serialize the Profile object to an XML file.
  170. XmlSerializer xs = new XmlSerializer(typeof (Profile));
  171. FileStream fs =
  172. new FileStream(
  173. BlogSettings.Instance.StorageLocation + userProfile.userName.ToLowerInvariant() + ".xml",
  174. FileMode.Create);
  175. xs.Serialize(fs, userProfile);
  176. }
  177. catch (Exception x)
  178. {
  179. Console.WriteLine("Exception: " + x.Message);
  180. }
  181. }
  182. /// <summary>
  183. ///
  184. /// </summary>
  185. /// <returns></returns>
  186. public static List<Profile> GetProfiles()
  187. {
  188. List<Profile> profiles = new List<Profile>();
  189. foreach (MembershipUser user in Membership.GetAllUsers())
  190. {
  191. Profile userProfile = GetProfile(user.UserName);
  192. profiles.Add(userProfile);
  193. }
  194. return profiles;
  195. }
  196. #endregion
  197. #region businessBase
  198. /// <summary>
  199. /// Reinforces the business rules by adding additional rules to the
  200. /// broken rules collection.
  201. /// </summary>
  202. ///
  203. ///
  204. protected override void ValidationRules()
  205. {
  206. throw new NotImplementedException();
  207. }
  208. /// <summary>
  209. /// Retrieves the object from the data store and populates it.
  210. /// </summary>
  211. /// <param name="id">The unique identifier of the object.</param>
  212. /// <returns>True if the object exists and is being populated successfully</returns>
  213. protected override Profile DataSelect(string id)
  214. {
  215. Profile profile = GetProfile(id);
  216. return profile;
  217. }
  218. /// <summary>
  219. /// Updates the object in its data store.
  220. /// </summary>
  221. protected override void DataUpdate()
  222. {
  223. SaveProfile(this);
  224. }
  225. /// <summary>
  226. /// Inserts a new object to the data store.
  227. /// </summary>
  228. protected override void DataInsert()
  229. {
  230. SaveProfile(this);
  231. }
  232. /// <summary>
  233. /// Deletes the object from the data store.
  234. /// </summary>
  235. protected override void DataDelete()
  236. {
  237. string path = BlogSettings.Instance.StorageLocation + userName.ToLowerInvariant() + ".xml";
  238. if (IsDeleted)
  239. {
  240. try
  241. {
  242. File.Delete(path);
  243. Dispose();
  244. }
  245. catch (FileNotFoundException)
  246. {
  247. throw new FileNotFoundException("File was not found", path);
  248. }
  249. catch (FieldAccessException e)
  250. {
  251. throw new FieldAccessException("File was not found", e.InnerException);
  252. }
  253. }
  254. }
  255. #endregion
  256. }
  257. }