/BlogEngine/DotNetSlave.BusinessLogic/AuthorProfile.cs

# · C# · 551 lines · 316 code · 80 blank · 155 comment · 17 complexity · a7417354a5b56c167b3583d464261fb1 MD5 · raw file

  1. namespace BlogEngine.Core
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using BlogEngine.Core.Providers;
  6. /// <summary>
  7. /// The author profile.
  8. /// </summary>
  9. public class AuthorProfile : BusinessBase<AuthorProfile, string>
  10. {
  11. #region Constants and Fields
  12. /// <summary>
  13. /// The sync root.
  14. /// </summary>
  15. private static readonly object SyncRoot = new object();
  16. /// <summary>
  17. /// The profiles.
  18. /// </summary>
  19. private static Dictionary<Guid, List<AuthorProfile>> profiles;
  20. /// <summary>
  21. /// The about me.
  22. /// </summary>
  23. private string aboutMe;
  24. /// <summary>
  25. /// The birthday.
  26. /// </summary>
  27. private DateTime birthday;
  28. /// <summary>
  29. /// The city town.
  30. /// </summary>
  31. private string cityTown;
  32. /// <summary>
  33. /// The company.
  34. /// </summary>
  35. private string company;
  36. /// <summary>
  37. /// The country.
  38. /// </summary>
  39. private string country;
  40. /// <summary>
  41. /// The display name.
  42. /// </summary>
  43. private string displayName;
  44. /// <summary>
  45. /// The email address.
  46. /// </summary>
  47. private string emailAddress;
  48. /// <summary>
  49. /// The first name.
  50. /// </summary>
  51. private string firstName;
  52. /// <summary>
  53. /// The is private.
  54. /// </summary>
  55. private bool isprivate;
  56. /// <summary>
  57. /// The last name.
  58. /// </summary>
  59. private string lastName;
  60. /// <summary>
  61. /// The middle name.
  62. /// </summary>
  63. private string middleName;
  64. /// <summary>
  65. /// The phone fax.
  66. /// </summary>
  67. private string phoneFax;
  68. /// <summary>
  69. /// The phone main.
  70. /// </summary>
  71. private string phoneMain;
  72. /// <summary>
  73. /// The phone mobile.
  74. /// </summary>
  75. private string phoneMobile;
  76. /// <summary>
  77. /// The photo url.
  78. /// </summary>
  79. private string photoUrl;
  80. /// <summary>
  81. /// The region state.
  82. /// </summary>
  83. private string regionState;
  84. #endregion
  85. #region Constructors and Destructors
  86. /// <summary>
  87. /// Initializes a new instance of the <see cref="AuthorProfile"/> class.
  88. /// </summary>
  89. public AuthorProfile()
  90. {
  91. }
  92. /// <summary>
  93. /// Initializes a new instance of the <see cref="AuthorProfile"/> class.
  94. /// </summary>
  95. /// <param name="username">
  96. /// The username.
  97. /// </param>
  98. public AuthorProfile(string username)
  99. {
  100. this.Id = username;
  101. }
  102. static AuthorProfile()
  103. {
  104. Blog.Saved += (s, e) =>
  105. {
  106. if (e.Action == SaveAction.Delete)
  107. {
  108. Blog blog = s as Blog;
  109. if (blog != null)
  110. {
  111. // remove deleted blog from static 'profiles'
  112. if (profiles != null && profiles.ContainsKey(blog.Id))
  113. profiles.Remove(blog.Id);
  114. }
  115. }
  116. };
  117. }
  118. #endregion
  119. #region Properties
  120. /// <summary>
  121. /// Gets an unsorted list of all pages.
  122. /// </summary>
  123. public static List<AuthorProfile> Profiles
  124. {
  125. get
  126. {
  127. Blog blog = Blog.CurrentInstance;
  128. if (profiles == null || !profiles.ContainsKey(blog.Id))
  129. {
  130. lock (SyncRoot)
  131. {
  132. if (profiles == null || !profiles.ContainsKey(blog.Id))
  133. {
  134. if (profiles == null)
  135. profiles = new Dictionary<Guid, List<AuthorProfile>>();
  136. profiles[blog.Id] = BlogService.FillProfiles();
  137. }
  138. }
  139. }
  140. return profiles[blog.Id];
  141. }
  142. }
  143. /// <summary>
  144. /// Gets or sets AboutMe.
  145. /// </summary>
  146. public string AboutMe
  147. {
  148. get
  149. {
  150. return this.aboutMe;
  151. }
  152. set
  153. {
  154. base.SetValue("AboutMe", value, ref this.aboutMe);
  155. }
  156. }
  157. /// <summary>
  158. /// Gets or sets Birthday.
  159. /// </summary>
  160. public DateTime Birthday
  161. {
  162. get
  163. {
  164. return this.birthday;
  165. }
  166. set
  167. {
  168. base.SetValue("Birthday", value, ref this.birthday);
  169. }
  170. }
  171. /// <summary>
  172. /// Gets or sets CityTown.
  173. /// </summary>
  174. public string CityTown
  175. {
  176. get
  177. {
  178. return this.cityTown;
  179. }
  180. set
  181. {
  182. base.SetValue("CityTown", value, ref this.cityTown);
  183. }
  184. }
  185. /// <summary>
  186. /// Gets or sets Company.
  187. /// </summary>
  188. public string Company
  189. {
  190. get
  191. {
  192. return this.company;
  193. }
  194. set
  195. {
  196. base.SetValue("Company", value, ref this.company);
  197. }
  198. }
  199. /// <summary>
  200. /// Gets or sets Country.
  201. /// </summary>
  202. public string Country
  203. {
  204. get
  205. {
  206. return this.country;
  207. }
  208. set
  209. {
  210. base.SetValue("Country", value, ref this.country);
  211. }
  212. }
  213. /// <summary>
  214. /// Gets or sets DisplayName.
  215. /// </summary>
  216. public string DisplayName
  217. {
  218. get
  219. {
  220. return this.displayName;
  221. }
  222. set
  223. {
  224. base.SetValue("DisplayName", value, ref this.displayName);
  225. }
  226. }
  227. /// <summary>
  228. /// Gets or sets EmailAddress.
  229. /// </summary>
  230. public string EmailAddress
  231. {
  232. get
  233. {
  234. return this.emailAddress;
  235. }
  236. set
  237. {
  238. base.SetValue("EmailAddress", value, ref this.emailAddress);
  239. }
  240. }
  241. /// <summary>
  242. /// Gets or sets FirstName.
  243. /// </summary>
  244. public string FirstName
  245. {
  246. get
  247. {
  248. return this.firstName;
  249. }
  250. set
  251. {
  252. base.SetValue("FirstName", value, ref this.firstName);
  253. }
  254. }
  255. /// <summary>
  256. /// Gets FullName.
  257. /// </summary>
  258. public string FullName
  259. {
  260. get
  261. {
  262. return string.Format("{0} {1} {2}", this.FirstName, this.MiddleName, this.LastName).Replace(" ", " ");
  263. }
  264. }
  265. /// <summary>
  266. /// Gets or sets a value indicating whether Private.
  267. /// </summary>
  268. public bool Private
  269. {
  270. get
  271. {
  272. return this.isprivate;
  273. }
  274. set
  275. {
  276. base.SetValue("Private", value, ref this.isprivate);
  277. }
  278. }
  279. /// <summary>
  280. /// Gets or sets LastName.
  281. /// </summary>
  282. public string LastName
  283. {
  284. get
  285. {
  286. return this.lastName;
  287. }
  288. set
  289. {
  290. base.SetValue("LastName", value, ref this.lastName);
  291. }
  292. }
  293. /// <summary>
  294. /// Gets or sets MiddleName.
  295. /// </summary>
  296. public string MiddleName
  297. {
  298. get
  299. {
  300. return this.middleName;
  301. }
  302. set
  303. {
  304. base.SetValue("MiddleName", value, ref this.middleName);
  305. }
  306. }
  307. /// <summary>
  308. /// Gets or sets PhoneFax.
  309. /// </summary>
  310. public string PhoneFax
  311. {
  312. get
  313. {
  314. return this.phoneFax;
  315. }
  316. set
  317. {
  318. base.SetValue("PhoneFax", value, ref this.phoneFax);
  319. }
  320. }
  321. /// <summary>
  322. /// Gets or sets PhoneMain.
  323. /// </summary>
  324. public string PhoneMain
  325. {
  326. get
  327. {
  328. return this.phoneMain;
  329. }
  330. set
  331. {
  332. base.SetValue("PhoneMain", value, ref this.phoneMain);
  333. }
  334. }
  335. /// <summary>
  336. /// Gets or sets PhoneMobile.
  337. /// </summary>
  338. public string PhoneMobile
  339. {
  340. get
  341. {
  342. return this.phoneMobile;
  343. }
  344. set
  345. {
  346. base.SetValue("PhoneMobile", value, ref this.phoneMobile);
  347. }
  348. }
  349. /// <summary>
  350. /// Gets or sets PhotoURL.
  351. /// </summary>
  352. public string PhotoUrl
  353. {
  354. get
  355. {
  356. return this.photoUrl;
  357. }
  358. set
  359. {
  360. base.SetValue("PhotoUrl", value, ref this.photoUrl);
  361. }
  362. }
  363. /// <summary>
  364. /// Gets or sets RegionState.
  365. /// </summary>
  366. public string RegionState
  367. {
  368. get
  369. {
  370. return this.regionState;
  371. }
  372. set
  373. {
  374. base.SetValue("RegionState", value, ref this.regionState);
  375. }
  376. }
  377. /// <summary>
  378. /// Gets RelativeLink.
  379. /// </summary>
  380. public string RelativeLink
  381. {
  382. get
  383. {
  384. return string.Format("{0}author/{1}.aspx", Utils.RelativeWebRoot, this.Id);
  385. }
  386. }
  387. /// <summary>
  388. /// Gets UserName.
  389. /// </summary>
  390. public string UserName
  391. {
  392. get
  393. {
  394. return this.Id;
  395. }
  396. }
  397. #endregion
  398. #region Public Methods
  399. /// <summary>
  400. /// Gets the profile.
  401. /// </summary>
  402. /// <param name="username">The username.</param>
  403. /// <returns>The AuthorProfile.</returns>
  404. public static AuthorProfile GetProfile(string username)
  405. {
  406. return
  407. Profiles.Find(p => p.UserName.Equals(username, StringComparison.OrdinalIgnoreCase));
  408. }
  409. /// <summary>
  410. /// Returns a <see cref="System.String"/> that represents this instance.
  411. /// </summary>
  412. /// <returns>
  413. /// A <see cref="System.String"/> that represents this instance.
  414. /// </returns>
  415. public override string ToString()
  416. {
  417. return this.FullName;
  418. }
  419. #endregion
  420. #region Methods
  421. /// <summary>
  422. /// Datas the delete.
  423. /// </summary>
  424. protected override void DataDelete()
  425. {
  426. BlogService.DeleteProfile(this);
  427. if (Profiles.Contains(this))
  428. {
  429. Profiles.Remove(this);
  430. }
  431. }
  432. /// <summary>
  433. /// Datas the insert.
  434. /// </summary>
  435. protected override void DataInsert()
  436. {
  437. BlogService.InsertProfile(this);
  438. if (this.New)
  439. {
  440. Profiles.Add(this);
  441. }
  442. }
  443. /// <summary>
  444. /// Datas the select.
  445. /// </summary>
  446. /// <param name="id">The AuthorProfile id.</param>
  447. /// <returns>The AuthorProfile.</returns>
  448. protected override AuthorProfile DataSelect(string id)
  449. {
  450. return BlogService.SelectProfile(id);
  451. }
  452. /// <summary>
  453. /// Updates the data.
  454. /// </summary>
  455. protected override void DataUpdate()
  456. {
  457. BlogService.UpdateProfile(this);
  458. }
  459. /// <summary>
  460. /// Validates based on rules.
  461. /// </summary>
  462. protected override void ValidationRules()
  463. {
  464. this.AddRule(
  465. "Id",
  466. "Id must be set to the username of the user who the profile belongs to",
  467. string.IsNullOrEmpty(this.Id));
  468. }
  469. #endregion
  470. }
  471. }