Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

GoalBook - A Hybrid Smart Client

, 25 Sep 2009 CPOL
A WPF hybrid smart client that synchronises your goals with the Toodledo online To-do service.
GoalBook.zip
GoalBook
Dependencies
CAG WPF 2
Microsoft.Practices.Composite.dll
Microsoft.Practices.Composite.Presentation.dll
Microsoft.Practices.Composite.UnityExtensions.dll
Microsoft.Practices.ObjectBuilder2.dll
Microsoft.Practices.ServiceLocation.dll
Microsoft.Practices.Unity.dll
vssver2.scc
CSLA
Csla.dll
Csla.XmlSerializers.dll
vssver2.scc
Infragistics
Infragistics3.Wpf.DataPresenter.v9.1.Express.dll
Infragistics3.Wpf.Editors.v9.1.Express.dll
Infragistics3.Wpf.v9.1.Express.dll
vssver2.scc
WPFToolkit
vssver2.scc
WPFToolkit.dll
GoalBook.Controls
CheckedList
vssver2.scc
Properties
vssver2.scc
vssver2.scc
GoalBook.Goals
GoalBook.Goals.csproj.user
Properties
vssver2.scc
Views
vssver2.scc
vssver2.scc
GoalBook.Infrastructure
Comparers
vssver2.scc
Constants
vssver2.scc
Controls
Converters
vssver2.scc
Enums
vssver2.scc
Events
vssver2.scc
GoalBook.Infrastructure.csproj.user
Helpers
vssver2.scc
Interfaces
vssver2.scc
ObjectModel
vssver2.scc
Printing
vssver2.scc
Properties
vssver2.scc
vssver2.scc
GoalBook.Notes
GoalBook.Notes.csproj.user
Properties
vssver2.scc
Resources
arrow_redo.png
arrow_undo.png
cut.png
page_copy.png
paste_plain.png
text_bold.png
text_indent.png
text_indent_remove.png
text_italic.png
text_list_bullets.png
text_list_numbers.png
vssver2.scc
world_link.png
Views
vssver2.scc
vssver2.scc
GoalBook.Public
Encryption
vssver2.scc
HtmlConverter
vssver2.scc
HtmlParser
vssver2.scc
Misc
vssver2.scc
Properties
vssver2.scc
vssver2.scc
GoalBook.Shell
App.ico
Commands
vssver2.scc
GoalBook.Shell.csproj.user
Journal.ico
Journal48.ico
Misc
vssver2.scc
Modules
vssver2.scc
Properties
licenses.licx
vssver2.scc
Resources
arrow_refresh.png
arrow_undo.png
checked.gif
cross.png
disk.png
email.png
erase.png
error.png
exclamation.png
flag_red.png
group.png
help.png
information.png
magnifier.png
page_red.png
pencil.png
printer.png
user.png
vssver2.scc
Wave.jpg
world.png
world_link.png
Services
vssver2.scc
Splash.png
Views
vssver2.scc
vssver2.scc
Windows
vssver2.scc
GoalBook.snk
GoalBook.Synchronisation
Events
vssver2.scc
Properties
vssver2.scc
ToodleDo
vssver2.scc
vssver2.scc
GoalBook.Tasks
Controls
vssver2.scc
Properties
vssver2.scc
Views
vssver2.scc
vssver2.scc
vssver2.scc
//===============================================================================
// Goal Book.
// Copyright © 2009 Mark Brownsword. 
//===============================================================================

#region Using Statements
using System;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using Csla;
using Csla.Validation;
using GoalBook.Infrastructure.Properties;
#endregion

namespace GoalBook.Infrastructure.ObjectModel
{
    /// <summary>
    /// Goal Type.
    /// </summary>    
    [Serializable] public class Goal : BusinessBase<Goal>
    {        
        #region Constants and Enums 
        #endregion

        #region Inner Classes and Structures
        #endregion

        #region Delegates and Events
        #endregion

        #region Instance and Shared Fields
        #endregion

        #region Constructors
        /// <summary>
        /// Constructor.
        /// </summary>
        private Goal()
        {
            MarkAsChild();
        }
        /// <summary>
        /// Constructor. Use this constructor for new records.
        /// </summary>
        public Goal(Guid goalID) : this()
        {
            LoadProperty(IdProperty, goalID);
            
            //Execute business rules against each property.
            ValidationRules.CheckRules();
        }
        /// <summary>
        /// Constructor. Use this constructor for existing records.
        /// </summary>        
        public Goal(Guid goalID, int levelID, Guid contributesID,
            bool archived, string title) : this()
        {
            LoadProperty(IdProperty, goalID);            
            LoadProperty(LevelProperty, levelID);
            LoadProperty(ContributesProperty, contributesID);
            LoadProperty(ArchivedProperty, archived);
            LoadProperty(TitleProperty, title);            
            
            //Execute business rules against each property.
            ValidationRules.CheckRules();
                        
            MarkOld();
        }
        /// <summary>
        /// Constructor. Use this constructor for existing records.
        /// </summary>        
        public Goal(Guid goalID, int levelID, Guid contributesID,
            bool archived, string title, int externalIdentifier)
            : this()
        {
            LoadProperty(IdProperty, goalID);
            LoadProperty(LevelProperty, levelID);
            LoadProperty(ContributesProperty, contributesID);
            LoadProperty(ArchivedProperty, archived);
            LoadProperty(TitleProperty, title);
            LoadProperty(ExternalIdentifierProperty, externalIdentifier);

            //Execute business rules against each property.
            ValidationRules.CheckRules();

            MarkOld();
        }
        #endregion

        #region Properties
        public static PropertyInfo<Guid> IdProperty = RegisterProperty(typeof(Goal), new PropertyInfo<Guid>("GoalID"));
        public static PropertyInfo<int> LevelProperty = RegisterProperty(typeof(Goal), new PropertyInfo<int>("LevelID", "Level", -1));
        public static PropertyInfo<Guid> ContributesProperty = RegisterProperty(typeof(Goal), new PropertyInfo<Guid>("ContributesID", "Contributes"));
        public static PropertyInfo<bool> ArchivedProperty = RegisterProperty(typeof(Goal), new PropertyInfo<bool>("Archived"));
        public static PropertyInfo<string> TitleProperty = RegisterProperty(typeof(Goal), new PropertyInfo<string>("Title"));
        public static PropertyInfo<int> ExternalIdentifierProperty = RegisterProperty(typeof(Goal), new PropertyInfo<int>("ExternalIdentifier"));
        
        /// <summary>
        /// Reference to GoalID field. This is the unique identifier for the Goal.
        /// </summary>        
        public Guid GoalID
        {
            get 
            { 
                CanReadProperty(IdProperty.Name, true); 
                return GetProperty(IdProperty); 
            }
            set
            {
                CanWriteProperty(IdProperty.Name, true);
                if (!GetProperty(IdProperty).Equals(value))
                {
                    SetProperty(IdProperty, value);
                    PropertyHasChanged(IdProperty.Name);
                }
            }            
        }        
        /// <summary>
        /// Reference to Level field. Valid values are {0=Lifetime; 1=Long-term; 2=Short-term}.
        /// </summary>
        public int LevelID
        {
            get
            {
                CanReadProperty(LevelProperty.Name, true);
                return GetProperty(LevelProperty);
            }
            set 
            {
                CanWriteProperty(LevelProperty.Name, true);                
                if (!GetProperty(LevelProperty).Equals(value))
                {
                    SetProperty(LevelProperty, value);
                    PropertyHasChanged(LevelProperty.Name);                    
                }
            }
        }
        /// <summary>
        /// Reference to Contributes field. The higher level goal that this goal contributes to.
        /// </summary>        
        public Guid ContributesID
        {
            get
            {
                CanReadProperty(ContributesProperty.Name, true);
                return GetProperty(ContributesProperty);
            }
            set
            {
                CanWriteProperty(ContributesProperty.Name, true);                
                if (!GetProperty(ContributesProperty).Equals(value))
                {
                    SetProperty(ContributesProperty, value);
                    PropertyHasChanged(ContributesProperty.Name);
                }
            }
        }
        /// <summary>
        /// Reference to Archived field.
        /// </summary>        
        public bool Archived
        {
            get
            {
                CanReadProperty(ArchivedProperty.Name, true);
                return GetProperty(ArchivedProperty);
            }
            set
            {
                CanWriteProperty(ArchivedProperty.Name, true);                
                if (!GetProperty(ArchivedProperty).Equals(value))
                {
                    SetProperty(ArchivedProperty, value);
                    PropertyHasChanged(ArchivedProperty.Name);
                }
            }
        }
        /// <summary>
        /// Reference to Title field.
        /// </summary>        
        public string Title
        {
            get
            {
                CanReadProperty(TitleProperty.Name, true);
                return GetProperty(TitleProperty);
            }
            set
            {
                CanWriteProperty(TitleProperty.Name, true);                
                if (!GetProperty(TitleProperty).Equals(value))
                {
                    SetProperty(TitleProperty, value);
                    PropertyHasChanged(TitleProperty.Name);
                }
            }            
        }        
        /// <summary>
        /// Reference to ExternalIdentifier.
        /// </summary>        
        public int ExternalIdentifier 
        {
            get
            {
                CanReadProperty(ExternalIdentifierProperty.Name, true);
                return GetProperty(ExternalIdentifierProperty);
            }
            set
            {
                CanWriteProperty(ExternalIdentifierProperty.Name, true);
                if (!GetProperty(ExternalIdentifierProperty).Equals(value))
                {
                    SetProperty(ExternalIdentifierProperty, value);
                    PropertyHasChanged(ExternalIdentifierProperty.Name);
                }
            }
        }
        /// <summary>
        /// Reference to LastModified.
        /// </summary>        
        public DateTime LastModified { get; set; }
        /// <summary>
        /// Reference to SyncRequired (client -> server).
        /// </summary>        
        public bool SyncRequired { get; set; }        
        #endregion

        #region Private and Protected Methods
        #endregion

        #region Public and internal Methods

        #region External
        /// <summary>
        /// Serialize As XElement.
        /// </summary>        
        public XElement SerializeAsXElement()
        {
            XElement goalElement = new XElement(Constants.GoalSerializationContants.SERIALIZATION_GOAL,
                    new XAttribute(Constants.GoalSerializationContants.SERIALIZATION_GOALID, this.GoalID),
                    new XAttribute(Constants.GoalSerializationContants.SERIALIZATION_LEVELID, this.LevelID),
                    new XAttribute(Constants.GoalSerializationContants.SERIALIZATION_CONTRIBUTESID, this.ContributesID),
                    new XAttribute(Constants.GoalSerializationContants.SERIALIZATION_ARCHIVED, this.Archived),
                    new XAttribute(Constants.GoalSerializationContants.SERIALIZATION_TITLE, this.Title),
                    new XAttribute(Constants.GoalSerializationContants.SERIALIZATION_EXTERNALIDENTIFIER, this.ExternalIdentifier),
                    new XAttribute(Constants.GoalSerializationContants.SERIALIZATION_LASTMODIFIED, this.LastModified),
                    new XAttribute(Constants.GoalSerializationContants.SERIALIZATION_SYNCREQUIRED, this.SyncRequired)
                    );                       

            return goalElement;
        }
        /// <summary>
        /// Marks the Goal as Old.
        /// </summary>        
        public Goal MarkGoalOld()
        {
            if (base.IsDirty) { base.MarkOld(); }
            return this;
        }
        /// <summary>
        /// Reference to EditLevel.
        /// </summary>
        public int GetEditLevel()
        {
            return base.EditLevel;
        }
        /// <summary>
        /// MapFields. 
        /// </summary>        
        public void MapFields(Goal goal)
        {
            if (this.GoalID == goal.GoalID)
            {
                this.LevelID = goal.LevelID;
                this.ContributesID = goal.ContributesID;
                this.Archived = goal.Archived;
                this.Title = goal.Title;
                this.ExternalIdentifier = goal.ExternalIdentifier;

                ValidationRules.CheckRules();
            }
        }
        #endregion

        #region Validation Handlers
        /// <summary>
        /// TitleValidator.
        /// Called by BusinessBase RulesEngine.
        /// </summary>        
        public static bool TitleValidator<T>(T target, RuleArgs e) where T : Goal
        {
            //Validate Title.  
            const string titleRegex = @"^[a-zA-Z'`-´~!@#$%^&*()_+=/?.,<>0-9\/\s]{1,255}$";

            if (Regex.IsMatch(target.Title, titleRegex)) { return true; }
            else { e.Description = Resources.ValidationTitleMessage; }

            return false;
        }
        /// <summary>
        /// LevelValidator.
        /// Called by BusinessBase RulesEngine.
        /// </summary>        
        public static bool LevelValidator<T>(T target, RuleArgs e) where T : Goal
        {
            if (target.LevelID >= 0) { return true; }
            else { e.Description = Resources.ValidationLevelMessage; }
            
            return false;
        }
        /// <summary>
        /// Validate GoalID is not empty Guid. 
        /// Called by BusinessBase RulesEngine.
        /// </summary>        
        public static bool IdValidator<T>(T target, RuleArgs e) where T : Goal
        {
            if (target.GoalID == Guid.Empty) 
            {
                e.Description = Resources.ValidationIDMessage;
                return false; 
            }

            return true;
        }
        #endregion
        
        #endregion

        #region Event Handlers
        /// <summary>
        /// OnDeserialized Handler.
        /// </summary>        
        [OnDeserialized()]
        private void OnDeserializedHandler(StreamingContext context)
        {
            base.OnDeserialized(context);

            //Execute business rules against each property
            //when the item is deserialized.

            //ValidationRules.SuppressRuleChecking = true;
            ValidationRules.CheckRules();
        }
        #endregion

        #region Base Class Overrides
        /// <summary>
        /// OnPropertyChanged. Override to record LastModified.
        /// </summary>        
        protected override void OnPropertyChanged(string propertyName)
        {
            base.OnPropertyChanged(propertyName);

            //Set LastModified when propertyName has a value
            //(value is empty when Goal is marked clean).
            if (!string.IsNullOrEmpty(propertyName))
            {
                LastModified = DateTime.Now;                
                SyncRequired = true;                
            }
        }        
        /// <summary>
        /// Add Business Rules. Defines basic validation rules to execute against specified fields.         
        /// </summary>
        protected override void AddBusinessRules()
        {
            //Title.
            ValidationRules.AddRule<Goal>(TitleValidator, TitleProperty);
            
            //Level - Validate required values {0=Lifetime; 1=Long-term; 2=Short-term}
            ValidationRules.AddRule<Goal>(LevelValidator, LevelProperty);                 
            
            //Id - Validate value is Guid.
            ValidationRules.AddRule<Goal>(IdValidator, IdProperty);                        
        }
        /// <summary>
        /// Override for GetIdValue.
        /// </summary>        
        protected override object GetIdValue()
        {
            return GetProperty(IdProperty);
        }        
        /// <summary>
        /// Override for ToString.
        /// </summary>        
        public override string ToString()
        {
            return GetProperty(TitleProperty).ToString();
        }
        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Mark Brownsword
Software Developer (Senior)
Australia Australia
I've been working as a software developer since 2000 and hold a Bachelor of Business degree from The Open Polytechnic of New Zealand. Computers are for people and I aim to build applications for people that they would want to use.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 25 Sep 2009
Article Copyright 2009 by Mark Brownsword
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid