PageRenderTime 33ms CodeModel.GetById 2ms app.highlight 22ms RepoModel.GetById 2ms app.codeStats 0ms

/BlogEngine/DotNetSlave.BusinessLogic/Profile.cs

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