Click here to Skip to main content
15,894,720 members
Articles / Desktop Programming / WPF

GoalBook - A Hybrid Smart Client

Rate me:
Please Sign up or sign in to vote.
4.86/5 (24 votes)
25 Sep 2009CPOL10 min read 79.4K   834   69  
A WPF hybrid smart client that synchronises your goals with the Toodledo online To-do service.
//===============================================================================
// Goal Book.
// Copyright © 2009 Mark Brownsword. 
//===============================================================================

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

namespace GoalBook.Infrastructure.ObjectModel
{
    /// <summary>
    /// Task Type.
    /// </summary>    
    [Serializable] public class Task : BusinessBase<Task>
    {        
        #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 Task()
        {
            MarkAsChild();
        }

        /// <summary>
        /// Constructor. Use this constructor for new records.
        /// </summary>
        public Task(Guid taskID) : this()
        {
            LoadProperty(IdProperty, taskID);
            
            //Execute business rules against each property.
            ValidationRules.CheckRules();
        }

        /// <summary>
        /// Constructor. Use this constructor for new records created by GoalBook.
        /// </summary>        
        public Task(Guid taskID, int parentTask, int children, string title, string tag, Guid folderID, Guid contextID, Guid goalID,
            DateTime? added, DateTime? modified, DateTime? start, DateTime? due, string dueModifier, TimeInfo? dueTime, string startTime, DateTime? completed,
            int repeat, string repeatAdvanced, int status, bool star, int priority, int length, int timer, string note)
            : this()
        {
            LoadProperty(IdProperty, taskID);
            LoadProperty(ParentTaskProperty, parentTask);
            LoadProperty(ChildrenProperty, children);
            LoadProperty(TitleProperty, title);
            LoadProperty(TagProperty, tag);
            LoadProperty(FolderIdProperty, folderID);
            LoadProperty(ContextIdProperty, contextID);
            LoadProperty(GoalIdProperty, goalID);
            LoadProperty(AddedProperty, added);
            LoadProperty(ModifiedProperty, modified);
            LoadProperty(StartProperty, start);            
            LoadProperty(DueProperty, due);
            LoadProperty(DueModifierProperty, dueModifier);
            LoadProperty(DueTimeProperty, dueTime);
            LoadProperty(StartTimeProperty, startTime);
            LoadProperty(CompletedProperty, completed);
            LoadProperty(RepeatProperty, repeat);
            LoadProperty(RepeatAdvancedProperty, repeatAdvanced);
            LoadProperty(StatusProperty, status);
            LoadProperty(StarProperty, star);
            LoadProperty(PriorityProperty, priority);
            LoadProperty(LengthProperty, length);
            LoadProperty(TimerProperty, timer);
            LoadProperty(NoteProperty, note);
            
            //Execute business rules against each property.
            ValidationRules.CheckRules();
                        
            MarkOld();
        }
        /// <summary>
        /// Constructor. Use this constructor for new records created by Toodledo.
        /// </summary>        
        public Task(Guid taskID, int parentTask, int children, string title, string tag, Guid folderID, Guid contextID, Guid goalID,
            DateTime? added, DateTime? modified, DateTime? start, DateTime? due, string dueModifier, TimeInfo? dueTime, string startTime, DateTime? completed, 
            int repeat, string repeatAdvanced, int status, bool star, int priority, int length, int timer, string note, int externalIdentifier)
            : this()
        {
            LoadProperty(IdProperty, taskID);
            LoadProperty(ParentTaskProperty, parentTask);
            LoadProperty(ChildrenProperty, children);
            LoadProperty(TitleProperty, title);
            LoadProperty(TagProperty, tag);            
            LoadProperty(FolderIdProperty, folderID);
            LoadProperty(ContextIdProperty, contextID);
            LoadProperty(GoalIdProperty, goalID);
            LoadProperty(AddedProperty, added);
            LoadProperty(ModifiedProperty, modified);
            LoadProperty(StartProperty, start);            
            LoadProperty(DueProperty, due);
            LoadProperty(DueModifierProperty, dueModifier);
            LoadProperty(DueTimeProperty, dueTime);
            LoadProperty(StartTimeProperty, startTime);
            LoadProperty(CompletedProperty, completed);
            LoadProperty(RepeatProperty, repeat);
            LoadProperty(RepeatAdvancedProperty, repeatAdvanced);
            LoadProperty(StatusProperty, status);
            LoadProperty(StarProperty, star);
            LoadProperty(PriorityProperty, priority);
            LoadProperty(LengthProperty, length);
            LoadProperty(TimerProperty, timer);
            LoadProperty(NoteProperty, note);
            LoadProperty(ExternalIdentifierProperty, externalIdentifier);

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

            MarkOld();
        }
        #endregion

        #region Properties
        public static PropertyInfo<Guid> IdProperty = RegisterProperty(typeof(Task), new PropertyInfo<Guid>("TaskID"));
        public static PropertyInfo<int> ParentTaskProperty = RegisterProperty(typeof(Task), new PropertyInfo<int>("ParentTask"));
        public static PropertyInfo<int> ChildrenProperty = RegisterProperty(typeof(Task), new PropertyInfo<int>("Children"));
        public static PropertyInfo<string> TitleProperty = RegisterProperty(typeof(Task), new PropertyInfo<string>("Title"));
        public static PropertyInfo<string> TagProperty = RegisterProperty(typeof(Task), new PropertyInfo<string>("Tag"));
        public static PropertyInfo<Guid> FolderIdProperty = RegisterProperty(typeof(Task), new PropertyInfo<Guid>("FolderID", "Folder"));
        public static PropertyInfo<Guid> ContextIdProperty = RegisterProperty(typeof(Task), new PropertyInfo<Guid>("ContextID", "Context"));
        public static PropertyInfo<Guid> GoalIdProperty = RegisterProperty(typeof(Task), new PropertyInfo<Guid>("GoalID", "Goal"));
        public static PropertyInfo<DateTime?> AddedProperty = RegisterProperty(typeof(Task), new PropertyInfo<DateTime?>("Added"));
        public static PropertyInfo<DateTime?> ModifiedProperty = RegisterProperty(typeof(Task), new PropertyInfo<DateTime?>("Modified"));
        public static PropertyInfo<DateTime?> StartProperty = RegisterProperty(typeof(Task), new PropertyInfo<DateTime?>("Start"));
        public static PropertyInfo<DateTime?> DueProperty = RegisterProperty(typeof(Task), new PropertyInfo<DateTime?>("Due"));
        public static PropertyInfo<string> DueModifierProperty = RegisterProperty(typeof(Task), new PropertyInfo<string>("DueModifier"));
        public static PropertyInfo<TimeInfo> DueTimeProperty = RegisterProperty(typeof(Task), new PropertyInfo<TimeInfo>("DueTime"));
        public static PropertyInfo<string> StartTimeProperty = RegisterProperty(typeof(Task), new PropertyInfo<string>("StartTime"));
        public static PropertyInfo<DateTime?> CompletedProperty = RegisterProperty(typeof(Task), new PropertyInfo<DateTime?>("Completed"));
        public static PropertyInfo<int> RepeatProperty = RegisterProperty(typeof(Task), new PropertyInfo<int>("Repeat"));
        public static PropertyInfo<string> RepeatAdvancedProperty = RegisterProperty(typeof(Task), new PropertyInfo<string>("RepeatAdvanced"));
        public static PropertyInfo<int> StatusProperty = RegisterProperty(typeof(Task), new PropertyInfo<int>("Status"));
        public static PropertyInfo<bool> StarProperty = RegisterProperty(typeof(Task), new PropertyInfo<bool>("Star"));
        public static PropertyInfo<int> PriorityProperty = RegisterProperty(typeof(Task), new PropertyInfo<int>("Priority"));
        public static PropertyInfo<int> LengthProperty = RegisterProperty(typeof(Task), new PropertyInfo<int>("Length"));
        public static PropertyInfo<int> TimerProperty = RegisterProperty(typeof(Task), new PropertyInfo<int>("Timer"));
        public static PropertyInfo<string> NoteProperty = RegisterProperty(typeof(Task), new PropertyInfo<string>("Note"));
        public static PropertyInfo<int> ExternalIdentifierProperty = RegisterProperty(typeof(Task), new PropertyInfo<int>("ExternalIdentifier"));

        /*
        <task>
		        <id>1234</id>
		        <parent>1122</parent>
		        <children>0</children>
		        <title>Buy Milk</title>
		        <tag>After work</tag>
		        <folder>123</folder>
		        <context id="123">Home</context>
		        <goal id="123">Get a Raise</goal>
		        <added>2006-01-23</added>
		        <modified>2006-01-25 05:12:45</modified>
		        <startdate></startdate>
		        <duedate modifier=""></duedate>
		        <duetime>2:00pm</duetime>
		        <starttime>1:00pm</starttime>
		        <completed></completed>
		        <repeat>1</repeat>
		        <rep_advanced></rep_advanced>
		        <status>4</status>
		        <star>0</star>
		        <priority>2</priority>
		        <length>20</length>
		        <timer onfor="0">0</timer>
		        <note></note>
	    </task>
         */

        /// <summary>
        /// Reference to TaskID field. This is the unique identifier for the Task.
        /// </summary>        
        public Guid TaskID
        {
            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 ParentTask.
        /// </summary>        
        public int ParentTask
        {
            get
            {
                CanReadProperty(ParentTaskProperty.Name, true);
                return GetProperty(ParentTaskProperty);
            }
            set
            {
                CanWriteProperty(ParentTaskProperty.Name, true);
                if (!GetProperty(ParentTaskProperty).Equals(value))
                {
                    SetProperty(ParentTaskProperty, value);
                    PropertyHasChanged(ParentTaskProperty.Name);
                }
            }
        }        
        
        /// <summary>
        /// Reference to Children.
        /// </summary>        
        public int Children 
        {
            get
            {
                CanReadProperty(ChildrenProperty.Name, true);
                return GetProperty(ChildrenProperty);
            }
            set
            {
                CanWriteProperty(ChildrenProperty.Name, true);
                if (!GetProperty(ChildrenProperty).Equals(value))
                {
                    SetProperty(ChildrenProperty, value);
                    PropertyHasChanged(ChildrenProperty.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 Tag field.
        /// </summary>
        public string Tag
        {
            get
            {
                CanReadProperty(TagProperty.Name, true);
                return GetProperty(TagProperty);
            }
            set 
            {
                CanWriteProperty(TagProperty.Name, true);
                if (!GetProperty(TagProperty).Equals(value))
                {
                    SetProperty(TagProperty, value);
                    PropertyHasChanged(TagProperty.Name);                    
                }
            }
        }  
                
        /// <summary>
        /// Reference to FolderID field.
        /// </summary>        
        public Guid FolderID
        {
            get
            {
                CanReadProperty(FolderIdProperty.Name, true);
                return GetProperty(FolderIdProperty);
            }
            set
            {
                CanWriteProperty(FolderIdProperty.Name, true);
                if (!GetProperty(FolderIdProperty).Equals(value))
                {
                    SetProperty(FolderIdProperty, value);
                    PropertyHasChanged(FolderIdProperty.Name);
                }
            }
        }
        
        /// <summary>
        /// Reference to ContextID field.
        /// </summary>        
        public Guid ContextID
        {
            get
            {
                CanReadProperty(ContextIdProperty.Name, true);
                return GetProperty(ContextIdProperty);
            }
            set
            {
                CanWriteProperty(ContextIdProperty.Name, true);
                if (!GetProperty(ContextIdProperty).Equals(value))
                {
                    SetProperty(ContextIdProperty, value);
                    PropertyHasChanged(ContextIdProperty.Name);
                }
            }
        }
        
        /// <summary>
        /// Reference to GoalID field.
        /// </summary>        
        public Guid GoalID
        {
            get
            {
                CanReadProperty(GoalIdProperty.Name, true);
                return GetProperty(GoalIdProperty);
            }
            set
            {
                CanWriteProperty(GoalIdProperty.Name, true);
                if (!GetProperty(GoalIdProperty).Equals(value))
                {
                    SetProperty(GoalIdProperty, value);
                    PropertyHasChanged(GoalIdProperty.Name);
                }
            }
        }
             

        /// <summary>
        /// Reference to Added field.
        /// </summary>        
        public DateTime? Added
        {
            get
            {
                CanReadProperty(AddedProperty.Name, true);
                return GetProperty(AddedProperty);
            }
            set
            {
                CanWriteProperty(AddedProperty.Name, true);
                if (!GetProperty(AddedProperty).Equals(value))
                {
                    SetProperty(AddedProperty, value);
                    PropertyHasChanged(AddedProperty.Name);
                }
            }            
        }

        /// <summary>
        /// Reference to Modified field.
        /// </summary>        
        public DateTime? Modified
        {
            get
            {
                CanReadProperty(ModifiedProperty.Name, true);
                return GetProperty(ModifiedProperty);
            }
            set
            {
                CanWriteProperty(ModifiedProperty.Name, true);
                if (!GetProperty(ModifiedProperty).Equals(value))
                {
                    SetProperty(ModifiedProperty, value);
                    PropertyHasChanged(ModifiedProperty.Name);
                }
            }            
        }
                
        /// <summary>
        /// Reference to Start field.
        /// </summary>        
        public DateTime? Start
        {
            get
            {
                CanReadProperty(StartProperty.Name, true);
                return GetProperty(StartProperty);
            }
            set
            {
                CanWriteProperty(StartProperty.Name, true);
                if (!GetProperty(StartProperty).Equals(value))
                {
                    SetProperty(StartProperty, value);
                    PropertyHasChanged(StartProperty.Name);
                }
            }            
        }
        
        /// <summary>
        /// Reference to Due field.
        /// </summary>        
        public DateTime? Due
        {
            get
            {
                CanReadProperty(DueProperty.Name, true);
                return GetProperty(DueProperty);
            }
            set
            {
                CanWriteProperty(DueProperty.Name, true);
                if (!GetProperty(DueProperty).Equals(value))
                {
                    SetProperty(DueProperty, value);
                    PropertyHasChanged(DueProperty.Name);
                }
            }            
        }
                
        /// <summary>
        /// Reference to DueTime field.
        /// </summary>
        public TimeInfo? DueTime
        {
            get
            {
                CanReadProperty(DueTimeProperty.Name, true);
                return GetProperty(DueTimeProperty);
            }
            set 
            {
                CanWriteProperty(DueTimeProperty.Name, true);
                if (!GetProperty(DueTimeProperty).Equals(value))
                {
                    SetProperty(DueTimeProperty, value);
                    PropertyHasChanged(DueTimeProperty.Name);                    
                }
            }
        }

        /// <summary>
        /// Reference to DueModifier field.
        /// </summary>
        public string DueModifier
        {
            get
            {
                CanReadProperty(DueModifierProperty.Name, true);
                return GetProperty(DueModifierProperty);
            }
            set
            {
                CanWriteProperty(DueModifierProperty.Name, true);
                if (!GetProperty(DueModifierProperty).Equals(value))
                {
                    SetProperty(DueModifierProperty, value);
                    PropertyHasChanged(DueModifierProperty.Name);
                }
            }
        } 
        
        /// <summary>
        /// Reference to StartTime field.
        /// </summary>
        public string StartTime
        {
            get
            {
                CanReadProperty(StartTimeProperty.Name, true);
                return GetProperty(StartTimeProperty);
            }
            set 
            {
                CanWriteProperty(StartTimeProperty.Name, true);
                if (!GetProperty(StartTimeProperty).Equals(value))
                {
                    SetProperty(StartTimeProperty, value);
                    PropertyHasChanged(StartTimeProperty.Name);                    
                }
            }
        } 
        
        /// <summary>
        /// Reference to Completed field.
        /// </summary>        
        public DateTime? Completed
        {
            get
            {
                CanReadProperty(CompletedProperty.Name, true);
                return GetProperty(CompletedProperty);
            }
            set
            {
                CanWriteProperty(CompletedProperty.Name, true);
                if (!GetProperty(CompletedProperty).Equals(value))
                {
                    SetProperty(CompletedProperty, value);
                    PropertyHasChanged(CompletedProperty.Name);
                }
            }            
        }
        
        /// <summary>
        /// Reference to Repeat field.
        /// </summary>        
        public int Repeat
        {
            get
            {
                CanReadProperty(RepeatProperty.Name, true);
                return GetProperty(RepeatProperty);
            }
            set
            {
                CanWriteProperty(RepeatProperty.Name, true);
                if (!GetProperty(RepeatProperty).Equals(value))
                {
                    SetProperty(RepeatProperty, value);
                    PropertyHasChanged(RepeatProperty.Name);
                }
            }            
        }
        
        /// <summary>
        /// Reference to RepeatAdvanced field.
        /// </summary>
        public string RepeatAdvanced
        {
            get
            {
                CanReadProperty(RepeatAdvancedProperty.Name, true);
                return GetProperty(RepeatAdvancedProperty);
            }
            set 
            {
                CanWriteProperty(RepeatAdvancedProperty.Name, true);
                if (!GetProperty(RepeatAdvancedProperty).Equals(value))
                {
                    SetProperty(RepeatAdvancedProperty, value);
                    PropertyHasChanged(RepeatAdvancedProperty.Name);                    
                }
            }
        } 
        
        /// <summary>
        /// Reference to Status.
        /// </summary>        
        public int Status 
        {
            get
            {
                CanReadProperty(StatusProperty.Name, true);
                return GetProperty(StatusProperty);
            }
            set
            {
                CanWriteProperty(StatusProperty.Name, true);
                if (!GetProperty(StatusProperty).Equals(value))
                {
                    SetProperty(StatusProperty, value);
                    PropertyHasChanged(StatusProperty.Name);
                }
            }
        }
        
        /// <summary>
        /// Reference to Star field.
        /// </summary>        
        public bool Star
        {
            get
            {
                CanReadProperty(StarProperty.Name, true);
                return GetProperty(StarProperty);
            }
            set
            {
                CanWriteProperty(StarProperty.Name, true);
                if (!GetProperty(StarProperty).Equals(value))
                {
                    SetProperty(StarProperty, value);
                    PropertyHasChanged(StarProperty.Name);
                }
            }            
        }
        
        /// <summary>
        /// Reference to Priority.
        /// </summary>        
        public int Priority 
        {
            get
            {
                CanReadProperty(PriorityProperty.Name, true);
                return GetProperty(PriorityProperty);
            }
            set
            {
                CanWriteProperty(PriorityProperty.Name, true);
                if (!GetProperty(PriorityProperty).Equals(value))
                {
                    SetProperty(PriorityProperty, value);
                    PropertyHasChanged(PriorityProperty.Name);
                }
            }
        } 
        
        /// <summary>
        /// Reference to Length.
        /// </summary>        
        public int Length 
        {
            get
            {
                CanReadProperty(LengthProperty.Name, true);
                return GetProperty(LengthProperty);
            }
            set
            {
                CanWriteProperty(LengthProperty.Name, true);
                if (!GetProperty(LengthProperty).Equals(value))
                {
                    SetProperty(LengthProperty, value);
                    PropertyHasChanged(LengthProperty.Name);
                }
            }
        } 
        
        /// <summary>
        /// Reference to Timer.
        /// </summary>        
        public int Timer 
        {
            get
            {
                CanReadProperty(TimerProperty.Name, true);
                return GetProperty(TimerProperty);
            }
            set
            {
                CanWriteProperty(TimerProperty.Name, true);
                if (!GetProperty(TimerProperty).Equals(value))
                {
                    SetProperty(TimerProperty, value);
                    PropertyHasChanged(TimerProperty.Name);
                }
            }
        }
        
        /// <summary>
        /// Reference to Note field.
        /// </summary>
        public string Note
        {
            get
            {
                CanReadProperty(NoteProperty.Name, true);
                return GetProperty(NoteProperty);
            }
            set 
            {
                CanWriteProperty(NoteProperty.Name, true);
                if (!GetProperty(NoteProperty).Equals(value))
                {
                    SetProperty(NoteProperty, value);
                    PropertyHasChanged(NoteProperty.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 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 taskElement = new XElement(Constants.TaskSerializationConstants.SERIALIZATION_TASK,
                new XAttribute(Constants.TaskSerializationConstants.SERIALIZATION_TASKID, this.TaskID),
                new XAttribute(Constants.TaskSerializationConstants.SERIALIZATION_PARENTTASK, this.ParentTask),
                new XAttribute(Constants.TaskSerializationConstants.SERIALIZATION_CHILDREN, this.Children),
                new XAttribute(Constants.TaskSerializationConstants.SERIALIZATION_TITLE, this.Title),                                               
                new XAttribute(Constants.TaskSerializationConstants.SERIALIZATION_TAG, this.Tag),
                new XAttribute(Constants.TaskSerializationConstants.SERIALIZATION_FOLDERID, this.FolderID),
                new XAttribute(Constants.TaskSerializationConstants.SERIALIZATION_CONTEXTID, this.ContextID),
                new XAttribute(Constants.TaskSerializationConstants.SERIALIZATION_GOALID, this.GoalID),
                new XAttribute(Constants.TaskSerializationConstants.SERIALIZATION_ADDED, this.Added.HasValue ? this.Added.Value.ToString() : string.Empty),
                new XAttribute(Constants.TaskSerializationConstants.SERIALIZATION_MODIFIED, this.Modified.HasValue ? this.Modified.Value.ToString() : string.Empty),
                new XAttribute(Constants.TaskSerializationConstants.SERIALIZATION_START, this.Start.HasValue ? this.Start.Value.ToString() : string.Empty),
                new XAttribute(Constants.TaskSerializationConstants.SERIALIZATION_DUE, this.Due.HasValue ? this.Due.Value.ToString() : string.Empty),
                new XAttribute(Constants.TaskSerializationConstants.SERIALIZATION_DUEMODIFIER, this.DueModifier),
                new XAttribute(Constants.TaskSerializationConstants.SERIALIZATION_DUETIME, this.DueTime.ToString()),
                new XAttribute(Constants.TaskSerializationConstants.SERIALIZATION_STARTTIME, this.StartTime),
                new XAttribute(Constants.TaskSerializationConstants.SERIALIZATION_COMPLETED, this.Completed.HasValue ? this.Completed.Value.ToString() : string.Empty),
                new XAttribute(Constants.TaskSerializationConstants.SERIALIZATION_REPEAT, this.Repeat),
                new XAttribute(Constants.TaskSerializationConstants.SERIALIZATION_REPEATADVANCED, this.RepeatAdvanced),
                new XAttribute(Constants.TaskSerializationConstants.SERIALIZATION_STATUS, this.Status),
                new XAttribute(Constants.TaskSerializationConstants.SERIALIZATION_STAR, this.Star),
                new XAttribute(Constants.TaskSerializationConstants.SERIALIZATION_PRIORITY, this.Priority),
                new XAttribute(Constants.TaskSerializationConstants.SERIALIZATION_LENGTH, this.Length),
                new XAttribute(Constants.TaskSerializationConstants.SERIALIZATION_TIMER, this.Timer),
                new XAttribute(Constants.TaskSerializationConstants.SERIALIZATION_NOTE, this.Note),
                new XAttribute(Constants.TaskSerializationConstants.SERIALIZATION_EXTERNALIDENTIFIER, this.ExternalIdentifier), //?
                new XAttribute(Constants.TaskSerializationConstants.SERIALIZATION_SYNCREQUIRED, this.SyncRequired));

            return taskElement;
        }

        /// <summary>
        /// Marks the Task as Old.
        /// </summary>        
        public Task MarkTaskOld()
        {
            if (base.IsDirty) { base.MarkOld(); }
            return this;
        }

        /// <summary>
        /// Reference to EditLevel.
        /// </summary>
        public int GetEditLevel()
        {
            return base.EditLevel;
        }

        /// <summary>
        /// MapFields. 
        /// </summary>        
        public void MapFields(Task task)
        {
            this.ParentTask = task.ParentTask;
            this.Children = task.Children;
            this.Title = task.Title;
            this.Tag = task.Tag;
            this.FolderID = task.FolderID;
            this.ContextID = task.ContextID;
            this.GoalID = task.GoalID;
            this.Added = task.Added;
            this.Modified = task.Modified;
            this.Start = task.Start;
            this.Due = task.Due;
            this.DueModifier = task.DueModifier;
            this.DueTime = task.DueTime;
            this.StartTime = task.StartTime;
            this.Completed = task.Completed;
            this.Repeat = task.Repeat;
            this.RepeatAdvanced = task.RepeatAdvanced;
            this.Status = task.Status;
            this.Star = task.Star;
            this.Priority = task.Priority;
            this.Length = task.Length;
            this.Timer = task.Timer;
            this.Note = task.Note;
            this.ExternalIdentifier = task.ExternalIdentifier;
                                                                                                      
            ValidationRules.CheckRules();
        }
        #endregion

        #region Validation Handlers
        /// <summary>
        /// Validate NoteID is not empty Guid. 
        /// Called by BusinessBase RulesEngine.
        /// </summary>        
        public static bool IdValidator<T>(T target, RuleArgs e) where T : Task
        {
            if (target.TaskID == Guid.Empty) 
            {
                e.Description = Resources.ValidationIDMessage;
                return false; 
            }

            return true;
        }

        /// <summary>
        /// DueTime Validator.
        /// </summary>        
        public static bool DueTimeValidator<T>(T target, RuleArgs e) where T : Task
        {
            if (target.DueTime.HasValue)
            {
                if (string.IsNullOrEmpty(target.DueTime.Value.Hour) &&
                    string.IsNullOrEmpty(target.DueTime.Value.Minute) &&
                    target.DueTime.Value.Format == null)
                {
                    return true; // All fields are null.
                }
                else if (string.IsNullOrEmpty(target.DueTime.Value.Hour) ||
                    string.IsNullOrEmpty(target.DueTime.Value.Minute) ||
                    target.DueTime.Value.Format == null)
                {
                    e.Description = Resources.ValidationTimeMessage;
                    return false; // Some fields are null.
                }
            }

            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 SyncRequired when propertyName has a value
            //(value is empty when Note is marked clean).
            if (!string.IsNullOrEmpty(propertyName))
            {                          
                SyncRequired = true;                
            }
        }

        /// <summary>
        /// Add Business Rules. Defines basic validation rules to execute against specified fields.         
        /// </summary>
        protected override void AddBusinessRules()
        {
            // Id - Validate value is Guid.
            ValidationRules.AddRule<Task>(IdValidator, IdProperty);  
                        
            // Title
            ValidationRules.AddRule(CommonRules.StringRequired, TitleProperty);
            ValidationRules.AddRule(CommonRules.StringMaxLength, new CommonRules.MaxLengthRuleArgs(TitleProperty, 255));

            // DueTime
            ValidationRules.AddRule<Task>(DueTimeValidator, DueTimeProperty);

            /*
                <id>1234</id>
                ?<parent>1122</parent>
                ?<children>0</children>
                <title>Buy Milk</title>
                ?<tag>After work</tag>
                ?<folder>123</folder>
                ?<context id="123">Home</context>
                ?<goal id="123">Get a Raise</goal>
                ?<added>2006-01-23</added>
                ?<modified>2006-01-25 05:12:45</modified>
                ?<startdate></startdate>
                ?<duedate modifier=""></duedate>
                <duetime>2:00pm</duetime>
                <starttime>1:00pm</starttime>
                ?<completed></completed>
                ?<repeat>1</repeat>
                ?<rep_advanced></rep_advanced>
                ?<status>4</status>
                ?<star>0</star>
                ?<priority>2</priority>
                ?<length>20</length>
                ?<timer onfor="0">0</timer>
                ?<note></note>
           */
        }

        /// <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)


Written By
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.

Comments and Discussions