Profile.cs :  » Bloggers » BlogEngine.NET » BlogEngine.Core » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Bloggers » BlogEngine.NET 
BlogEngine.NET » BlogEngine.Core » Profile.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Web.Security;
using System.Xml.Serialization;

namespace BlogEngine.Core{
    /// <summary>
    /// This business object is to handle the profiles of users
    /// </summary>
    [XmlRoot("profile")]
    public class Profile : BusinessBase<Profile, string>
    {
        #region Fields

        private string aboutMe;
        private string birthDate;
        private string cityState;
        private string country;
        private string displayName;
        private string firstName;
        private string gender;
        private string interests;
        private bool isPrivate;
        private string lastName;
        private string photoURL;
        private string regionState;
        private string userName;

        #endregion

        #region Properties

        /// <summary>
        /// 
        /// </summary>
        [XmlElement("displayName", DataType = "string")]
        public string DisplayName
        {
            get { return displayName; }
            set { displayName = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        [XmlElement("interests", DataType = "string")]
        public string Interests
        {
            get { return interests; }
            set { interests = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        [XmlElement("userName", DataType = "string")]
        public string UserName
        {
            get { return userName; }
            set { userName = value; }
        }

        ///<summary>
        ///</summary>
        [XmlElement("aboutme", DataType = "string")]
        public string AboutMe
        {
            get { return aboutMe; }
            set { aboutMe = value; }
        }

        ///<summary>
        ///</summary>
        [XmlElement("firstName", DataType = "string")]
        public string FirstName
        {
            get { return firstName; }
            set { firstName = value; }
        }

        ///<summary>
        ///</summary>
        [XmlElement("lastName", DataType = "string")]
        public string LastName
        {
            get { return lastName; }
            set { lastName = value; }
        }

        ///<summary>
        ///</summary>
        [XmlElement("isPrivate", DataType = "bool")]
        public bool IsPrivate
        {
            get { return isPrivate; }
            set { isPrivate = value; }
        }

        ///<summary>
        ///</summary>
        [XmlElement("photoURL", DataType = "string")]
        public string PhotoURL
        {
            get { return photoURL; }
            set { photoURL = value; }
        }

        ///<summary>
        ///</summary>
        [XmlElement("gender", DataType = "string")]
        public string Gender
        {
            get { return gender; }
            set { gender = value; }
        }

        ///<summary>
        ///</summary>
        [XmlElement("birthDate", DataType = "string")]
        public string BirthDate
        {
            get { return birthDate; }
            set { birthDate = value; }
        }

        ///<summary>
        ///</summary>
        [XmlElement("cityState", DataType = "string")]
        public string CityState
        {
            get { return cityState; }
            set { cityState = value; }
        }

        ///<summary>
        ///</summary>
        [XmlElement("regionState", DataType = "string")]
        public string RegionState
        {
            get { return regionState; }
            set { regionState = value; }
        }

        ///<summary>
        ///</summary>
        [XmlElement("country", DataType = "string")]
        public string Country
        {
            get { return country; }
            set { country = value; }
        }

        #endregion

        #region Helpers

        ///<summary>
        ///</summary>
        ///<param name="username"></param>
        ///<returns></returns>
        public static Profile GetProfile(string username)
        {
            Profile profile = new Profile();
            try
            {
                // Deserialize the specified file to a Theater object.
                XmlSerializer xs = new XmlSerializer(typeof (Profile));
                FileStream fs =
                    new FileStream(BlogSettings.Instance.StorageLocation + username.ToLowerInvariant() + ".xml",
                                   FileMode.Open);
                profile = (Profile) xs.Deserialize(fs);
            }
            catch (Exception x)
            {
                Console.WriteLine("Exception: " + x.Message);
            }
            return profile;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userProfile"></param>
        public static void SaveProfile(Profile userProfile)
        {
            try
            {
                // Serialize the Profile object to an XML file.
                XmlSerializer xs = new XmlSerializer(typeof (Profile));
                FileStream fs =
                    new FileStream(
                        BlogSettings.Instance.StorageLocation + userProfile.userName.ToLowerInvariant() + ".xml",
                        FileMode.Create);
                xs.Serialize(fs, userProfile);
            }
            catch (Exception x)
            {
                Console.WriteLine("Exception: " + x.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static List<Profile> GetProfiles()
        {
            List<Profile> profiles = new List<Profile>();
            foreach (MembershipUser user in Membership.GetAllUsers())
            {
                Profile userProfile = GetProfile(user.UserName);
                profiles.Add(userProfile);
            }
            return profiles;
        }

        #endregion

        #region businessBase

        /// <summary>
        /// Reinforces the business rules by adding additional rules to the 
        /// broken rules collection.
        /// </summary>
        /// 
        /// 
        protected override void ValidationRules()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Retrieves the object from the data store and populates it.
        /// </summary>
        /// <param name="id">The unique identifier of the object.</param>
        /// <returns>True if the object exists and is being populated successfully</returns>
        protected override Profile DataSelect(string id)
        {
            Profile profile = GetProfile(id);
            return profile;
        }

        /// <summary>
        /// Updates the object in its data store.
        /// </summary>
        protected override void DataUpdate()
        {
            SaveProfile(this);
        }

        /// <summary>
        /// Inserts a new object to the data store.
        /// </summary>
        protected override void DataInsert()
        {
            SaveProfile(this);
        }

        /// <summary>
        /// Deletes the object from the data store.
        /// </summary>
        protected override void DataDelete()
        {
            string path = BlogSettings.Instance.StorageLocation + userName.ToLowerInvariant() + ".xml";
            if (IsDeleted)
            {
                try
                {
                    File.Delete(path);
                    Dispose();
                }
                catch (FileNotFoundException)
                {
                    throw new FileNotFoundException("File was not found", path);
                }
                catch (FieldAccessException e)
                {
                    throw new FieldAccessException("File was not found", e.InnerException);
                }
            }
        }

        #endregion
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.