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

/BlogEngine/DotNetSlave.BusinessLogic/AuthorProfile.cs

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