//===============================================================================
// 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>
/// Note Type.
/// </summary>
[Serializable] public class Note : BusinessBase<Note>
{
#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 Note()
{
MarkAsChild();
}
/// <summary>
/// Constructor. Use this constructor for new records.
/// </summary>
public Note(Guid noteID) : this()
{
LoadProperty(IdProperty, noteID);
//Execute business rules against each property.
ValidationRules.CheckRules();
}
/// <summary>
/// Constructor. Use this constructor for existing records.
/// </summary>
public Note(Guid noteID, string title, string text, Guid folderID,
DateTime added, DateTime modified, bool isPrivate) : this()
{
LoadProperty(IdProperty, noteID);
LoadProperty(TitleProperty, title);
LoadProperty(TextProperty, text);
LoadProperty(FolderIdProperty, folderID);
LoadProperty(AddedProperty, added);
LoadProperty(ModifiedProperty, modified);
LoadProperty(IsPrivateProperty, isPrivate);
//Execute business rules against each property.
ValidationRules.CheckRules();
MarkOld();
}
/// <summary>
/// Constructor. Use this constructor for existing records.
/// </summary>
public Note(Guid noteID, string title, string text, Guid folderID,
DateTime added, DateTime modified, bool isPrivate, int externalIdentifier)
: this()
{
LoadProperty(IdProperty, noteID);
LoadProperty(TitleProperty, title);
LoadProperty(TextProperty, text);
LoadProperty(FolderIdProperty, folderID);
LoadProperty(AddedProperty, added);
LoadProperty(ModifiedProperty, modified);
LoadProperty(IsPrivateProperty, isPrivate);
LoadProperty(ExternalIdentifierProperty, externalIdentifier);
//Execute business rules against each property.
ValidationRules.CheckRules();
MarkOld();
}
#endregion
#region Properties
public static PropertyInfo<Guid> IdProperty = RegisterProperty(typeof(Note), new PropertyInfo<Guid>("NoteID"));
public static PropertyInfo<string> TitleProperty = RegisterProperty(typeof(Note), new PropertyInfo<string>("Title"));
public static PropertyInfo<string> TextProperty = RegisterProperty(typeof(Note), new PropertyInfo<string>("Text"));
public static PropertyInfo<Guid> FolderIdProperty = RegisterProperty(typeof(Note), new PropertyInfo<Guid>("FolderID", "Folder"));
public static PropertyInfo<DateTime> AddedProperty = RegisterProperty(typeof(Note), new PropertyInfo<DateTime>("Added"));
public static PropertyInfo<DateTime> ModifiedProperty = RegisterProperty(typeof(Note), new PropertyInfo<DateTime>("Modified"));
public static PropertyInfo<bool> IsPrivateProperty = RegisterProperty(typeof(Note), new PropertyInfo<bool>("IsPrivate"));
public static PropertyInfo<int> ExternalIdentifierProperty = RegisterProperty(typeof(Note), new PropertyInfo<int>("ExternalIdentifier"));
/// <summary>
/// Reference to NoteID field. This is the unique identifier for the Note.
/// </summary>
public Guid NoteID
{
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 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 Text field.
/// </summary>
public string Text
{
get
{
CanReadProperty(TextProperty.Name, true);
return GetProperty(TextProperty);
}
set
{
CanWriteProperty(TextProperty.Name, true);
if (!GetProperty(TextProperty).Equals(value))
{
SetProperty(TextProperty, value);
PropertyHasChanged(TextProperty.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 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 IsPrivate field.
/// </summary>
public bool IsPrivate
{
get
{
CanReadProperty(IsPrivateProperty.Name, true);
return GetProperty(IsPrivateProperty);
}
set
{
CanWriteProperty(IsPrivateProperty.Name, true);
if (!GetProperty(IsPrivateProperty).Equals(value))
{
SetProperty(IsPrivateProperty, value);
PropertyHasChanged(IsPrivateProperty.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 goalElement = new XElement(Constants.NoteSerializationContants.SERIALIZATION_NOTE,
new XAttribute(Constants.NoteSerializationContants.SERIALIZATION_NOTEID, this.NoteID),
new XAttribute(Constants.NoteSerializationContants.SERIALIZATION_TITLE, this.Title),
new XAttribute(Constants.NoteSerializationContants.SERIALIZATION_TEXT, this.Text),
new XAttribute(Constants.NoteSerializationContants.SERIALIZATION_FOLDERID, this.FolderID),
new XAttribute(Constants.NoteSerializationContants.SERIALIZATION_ADDED, this.Added),
new XAttribute(Constants.NoteSerializationContants.SERIALIZATION_MODIFIED, this.Modified),
new XAttribute(Constants.NoteSerializationContants.SERIALIZATION_ISPRIVATE, this.IsPrivate),
new XAttribute(Constants.NoteSerializationContants.SERIALIZATION_EXTERNALIDENTIFIER, this.ExternalIdentifier),
new XAttribute(Constants.NoteSerializationContants.SERIALIZATION_SYNCREQUIRED, this.SyncRequired)
);
return goalElement;
}
/// <summary>
/// Marks the Note as Old.
/// </summary>
public Note MarkNoteOld()
{
if (base.IsDirty) { base.MarkOld(); }
return this;
}
/// <summary>
/// Reference to EditLevel.
/// </summary>
public int GetEditLevel()
{
return base.EditLevel;
}
/// <summary>
/// MapFields.
/// </summary>
public void MapFields(Note note)
{
this.Title = note.Title;
this.Text = note.Text;
this.FolderID = note.FolderID;
this.Added = note.Added;
this.Modified = note.Modified;
this.IsPrivate = note.IsPrivate;
this.ExternalIdentifier = note.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 : Note
{
if (target.NoteID == 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 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<Note>(IdValidator, IdProperty);
// Title
ValidationRules.AddRule(CommonRules.StringRequired, TitleProperty);
ValidationRules.AddRule(CommonRules.StringMaxLength, new CommonRules.MaxLengthRuleArgs(TitleProperty, 255));
}
/// <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
}
}