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.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Csla;
using GoalBook.Infrastructure.Interfaces;
#endregion

namespace GoalBook.Infrastructure.ObjectModel
{
    [Serializable]
    public sealed class NoteList : BusinessListBase<NoteList, Note>, IXDocSerializable
    {                             
        #region Constants and Enums
        private const string SERIALIZATION_ROOT = "NoteList";
        private const string SERIALIZATION_NOTES = "Notes";
        private const string SERIALIZATION_DELETED_NOTES = "DeletedNotes";
        private const string SERIALIZATION_LAST_SERVER_NOTE_EDIT = "LastServerNoteEdit";
        #endregion

        #region Inner Classes and Structures
        #endregion

        #region Delegates and Events
        #endregion

        #region Instance and Shared Fields
        private List<Note> _deleted;
        private DateTime _lastServerNoteEdit;
        private bool _lastServerNoteEditChanged = false;
        #endregion

        #region Constructors

        /// <summary>
        /// Constructor.
        /// </summary>
        public NoteList()
        {
            AllowEdit =
            AllowNew =
            AllowRemove = true;       
        }   
         
        #endregion

        #region Properties

        /// <summary>
        /// Expose the EditLevel so can call ApplyEdit the
        /// correct number of times.
        /// </summary>
        public new int EditLevel { get { return base.EditLevel; } }   
     
        /// <summary>
        /// Reference to LastServerNoteEdit. A timestamp that indicates 
        /// the last time an edit occurred on the server.
        /// </summary>        
        public DateTime LastServerNoteEdit 
        {
            get { return _lastServerNoteEdit;}
            set { _lastServerNoteEdit = value; _lastServerNoteEditChanged = true; } 
        }

        /// <summary>
        /// Reference to SyncRequired. A flag indicating if any
        /// notes have been edited and require synchronisation.
        /// </summary>
        public bool SyncRequired 
        {
            get 
            {                
                foreach (Note note in this)
                {
                    if (note.SyncRequired) { return true; }
                }

                foreach (Note note in this.DeletedList)
                {
                    if (note.ExternalIdentifier > 0) { return true; }
                }

                if (this.DeletedPendingSync.Count > 0) { return true; }

                return false;
            }
        }  
      
        /// <summary>
        /// Deleted notes. For synchronisation.
        /// </summary>        
        public List<Note> DeletedPendingSync 
        {
            get 
            {                
                if (_deleted == null) { DeletedPendingSync = new List<Note>(); }
                return _deleted;
            }
            set { _deleted = value; } 
        }
        #endregion

        #region Private and Protected Methods

        /// <summary>
        /// Get Note FromElement.
        /// </summary>        
        private Note GetNoteFromElement(XElement element)
        {
            Guid noteID = new Guid(GetAttributeFromElement(element, Constants.NoteSerializationContants.SERIALIZATION_NOTEID));
            string title = GetAttributeFromElement(element, Constants.NoteSerializationContants.SERIALIZATION_TITLE);
            string text = GetAttributeFromElement(element, Constants.NoteSerializationContants.SERIALIZATION_TEXT);
            Guid folderID = new Guid(GetAttributeFromElement(element, Constants.NoteSerializationContants.SERIALIZATION_FOLDERID));
            DateTime added = DateTime.Parse(GetAttributeFromElement(element, Constants.NoteSerializationContants.SERIALIZATION_ADDED));
            DateTime modified = DateTime.Parse(GetAttributeFromElement(element, Constants.NoteSerializationContants.SERIALIZATION_MODIFIED));
            bool isPrivate = bool.Parse(GetAttributeFromElement(element, Constants.NoteSerializationContants.SERIALIZATION_ISPRIVATE));
            int externalIdentifier = int.Parse(GetAttributeFromElement(element, Constants.NoteSerializationContants.SERIALIZATION_EXTERNALIDENTIFIER));

            Note note = new Note(noteID, title, text, folderID, added, modified, isPrivate, externalIdentifier);
            note.SyncRequired = bool.Parse(GetAttributeFromElement(element, Constants.NoteSerializationContants.SERIALIZATION_SYNCREQUIRED));

            return note;
        }

        /// <summary>
        /// Get Note with the specified id.
        /// </summary>        
        private Note GetNote(NoteList noteList, Guid id)
        {
            var targetList = from g in noteList where g.NoteID == id select g;
            foreach (Note note in targetList)
            {
                return note; //One only can exist.
            }
            return null;
        }

        /// <summary>
        /// Get Attribute From Element.
        /// </summary>        
        private string GetAttributeFromElement(XElement element, string attribute)
        {
            if (element.Attribute(attribute) == null) { return null; }
            return element.Attribute(attribute).Value;
        }
        #endregion

        #region Public and internal Methods

        #region IXDocSerializable Members

        /// <summary>
        /// Get Goals List as XDocument.
        /// </summary>        
        public XDocument GetXDocument()
        {
            //Serialize the NotesList Element.            
            XElement notesElement = new XElement(SERIALIZATION_NOTES, new XAttribute(SERIALIZATION_LAST_SERVER_NOTE_EDIT, 
                LastServerNoteEdit.ToString()));

            //Serialize the Notes.
            foreach (Note note in this) 
            { 
                notesElement.Add(note.SerializeAsXElement()); 
            }

            XElement deletedPendingSyncElement = new XElement(SERIALIZATION_DELETED_NOTES);

            //Serialize the deleted Notes.
            foreach (Note note in this.DeletedPendingSync)
            {
                deletedPendingSyncElement.Add(note.SerializeAsXElement());
            }

            XElement rootElement = new XElement(SERIALIZATION_ROOT, notesElement, deletedPendingSyncElement);
            return new XDocument(rootElement);
        }

        /// <summary>
        /// Create NotesList From XDocument.
        /// </summary>        
        public void CreateFromXDocument(XDocument xDoc)
        {
            if (xDoc == null) return;

            XElement root = xDoc.Element(SERIALIZATION_ROOT);
            XElement notes = root.Element(SERIALIZATION_NOTES);
            XElement deletedNotes = root.Element(SERIALIZATION_DELETED_NOTES);

            //LastServerNoteEdit
            if (!string.IsNullOrEmpty(GetAttributeFromElement(notes, SERIALIZATION_LAST_SERVER_NOTE_EDIT)))
            {
                this._lastServerNoteEdit = Convert.ToDateTime(GetAttributeFromElement(notes, SERIALIZATION_LAST_SERVER_NOTE_EDIT));
            }

            //Load the Notes.
            foreach (XElement element in notes.Descendants(Constants.NoteSerializationContants.SERIALIZATION_NOTE))
            {
                this.Add(GetNoteFromElement(element));
            }

            //Load the deletedPendingSync Notes. 
            this.DeletedPendingSync.Clear();
            foreach (XElement element in deletedNotes.Descendants(Constants.NoteSerializationContants.SERIALIZATION_NOTE))
            {
                this.DeletedPendingSync.Add(GetNoteFromElement(element));
            }
        }

        /// <summary>
        /// MarkListOld. Mark all list items as old.
        /// </summary>        
        public void MarkListOld()
        {
            //Wind back the EditLevel.
            while (this.EditLevel > 0) { this.ApplyEdit(); }

            //Mark each child as old (clean).        
            foreach (Note note in this) { note.MarkNoteOld(); }

            //Move the Deleted items with an ExternalIdentifier to the Deleted list. The DeletedList 
            //holds notes that have been deleted on the client but have not yet been synchronised.            
            var deleted = from g in DeletedList where g.ExternalIdentifier > 0 select g;
            foreach (Note note in deleted)
            {
                if (DeletedPendingSync.Contains(note)) { continue; }
                DeletedPendingSync.Add(note);
            }

            //The list remains dirty until the DeletedList is clear, so clear it now.
            DeletedList.Clear();

            //Set to true after sync, so set false during save.
            _lastServerNoteEditChanged = false;
        }

        #endregion
        
        /// <summary>
        /// Merge notes. Synchronisation occurs in a cloned NoteList, 
        /// so need to merge changes back into the primary NoteList. 
        /// </summary>        
        public void Merge(NoteList syncNotes)
        {            
            foreach (Note note in syncNotes)
            {                        
                Note target = GetNote(this, note.NoteID);
                if (target == null)
                {
                    //Add
                    this.BeginEdit();
                    this.Add(note);
                    this.EndNew(this.IndexOf(note));
                }
                else
                {
                    //Edit
                    if (note.IsDirty) //Dirty means changes occured during sync.
                    {
                        this.BeginEdit();
                        target.MapFields(note);                        
                    }

                    //Set SyncRequired flag (so client edits only get pushed to server once).
                    target.SyncRequired = note.SyncRequired;
                }
            }
            
            foreach (Note note in syncNotes.DeletedList)
            {
                //Remove
                Note target = GetNote(this, note.NoteID);
                if (target != null)
                {
                    this.Remove(target);
                }
            }

            //Clear Deleted. Sync of Deleted items is complete.
            if (this.DeletedList.Count > 0) { this.DeletedList.Clear(); }

            this.DeletedPendingSync.Clear();
            foreach (Note note in syncNotes.DeletedPendingSync)
            {
                //Notes remaining in syncNotes.DeletedPendingSync were not
                //successfully deleted during the client changes synchronisation.
                //These need to be retained so can attempt delete again.
                this.DeletedPendingSync.Add(note);
            }
            
            //Map LastServerNoteEdit.
            if (syncNotes.LastServerNoteEdit > this.LastServerNoteEdit)
            {
                this.LastServerNoteEdit = syncNotes.LastServerNoteEdit;
            }            
        }
        #endregion

        #region Event Handlers
        #endregion

        #region Base Class Overrides
        /// <summary>
        /// AddNewCore. Initialises and adds a new Note item to the List.
        /// </summary>        
        protected override object AddNewCore()
        {
            Guid guid = Guid.NewGuid();
            Note note = new Note(guid);
            Add(note);

            return note;
        }

        /// <summary>
        /// IsSavable. 
        /// </summary>
        public override bool IsSavable
        {
            get 
            {
                if (_lastServerNoteEditChanged)
                {
                    return base.IsValid;
                }

                return base.IsSavable;
            }
        }        
        #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
Web02 | 2.8.150123.1 | Last Updated 25 Sep 2009
Article Copyright 2009 by Mark Brownsword
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid