//===============================================================================
// 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 Folder : BusinessBase<Folder>
{
#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 Folder()
{
MarkAsChild();
}
/// <summary>
/// Constructor. Use this constructor for new records.
/// </summary>
public Folder(Guid noteID) : this()
{
LoadProperty(idProperty, noteID);
//Execute business rules against each property.
ValidationRules.CheckRules();
}
/// <summary>
/// Constructor. Use this constructor for existing records.
/// </summary>
public Folder(Guid folderID, bool isPrivate, bool archived, int order, string title)
: this()
{
LoadProperty(idProperty, folderID);
LoadProperty(isPrivateProperty, isPrivate);
LoadProperty(archivedProperty, archived);
LoadProperty(orderProperty, order);
LoadProperty(titleProperty, title);
//Execute business rules against each property.
ValidationRules.CheckRules();
MarkOld();
}
/// <summary>
/// Constructor. Use this constructor for existing records.
/// </summary>
public Folder(Guid folderID, bool isPrivate, bool archived, int order, string title, int externalIdentifier)
: this()
{
LoadProperty(idProperty, folderID);
LoadProperty(isPrivateProperty, isPrivate);
LoadProperty(archivedProperty, archived);
LoadProperty(orderProperty, order);
LoadProperty(titleProperty, title);
LoadProperty(externalIdentifierProperty, externalIdentifier);
//Execute business rules against each property.
ValidationRules.CheckRules();
MarkOld();
}
#endregion
#region Properties
private static PropertyInfo<Guid> idProperty = RegisterProperty(typeof(Folder), new PropertyInfo<Guid>("FolderID"));
private static PropertyInfo<bool> isPrivateProperty = RegisterProperty(typeof(Folder), new PropertyInfo<bool>("IsPrivate"));
private static PropertyInfo<bool> archivedProperty = RegisterProperty(typeof(Folder), new PropertyInfo<bool>("Archived"));
private static PropertyInfo<int> orderProperty = RegisterProperty(typeof(Folder), new PropertyInfo<int>("Order"));
private static PropertyInfo<string> titleProperty = RegisterProperty(typeof(Folder), new PropertyInfo<string>("Title"));
private static PropertyInfo<int> externalIdentifierProperty = RegisterProperty(typeof(Folder), new PropertyInfo<int>("ExternalIdentifier"));
/// <summary>
/// Reference to FolderID field. This is the unique identifier for the Folder.
/// </summary>
public Guid FolderID
{
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 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 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 Order field.
/// </summary>
public int Order
{
get
{
CanReadProperty(orderProperty.Name, true);
return GetProperty(orderProperty);
}
set
{
CanWriteProperty(orderProperty.Name, true);
if (!GetProperty(orderProperty).Equals(value))
{
SetProperty(orderProperty, value);
PropertyHasChanged(orderProperty.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 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.FolderSerializationContants.SERIALIZATION_FOLDER,
new XAttribute(Constants.FolderSerializationContants.SERIALIZATION_FOLDERID, this.FolderID),
new XAttribute(Constants.FolderSerializationContants.SERIALIZATION_ISPRIVATE, this.IsPrivate),
new XAttribute(Constants.FolderSerializationContants.SERIALIZATION_ARCHIVED, this.Archived),
new XAttribute(Constants.FolderSerializationContants.SERIALIZATION_ORDER, this.Order),
new XAttribute(Constants.FolderSerializationContants.SERIALIZATION_TITLE, this.Title),
new XAttribute(Constants.FolderSerializationContants.SERIALIZATION_EXTERNALIDENTIFIER, this.ExternalIdentifier),
new XAttribute(Constants.FolderSerializationContants.SERIALIZATION_SYNCREQUIRED, this.SyncRequired)
);
return goalElement;
}
/// <summary>
/// Marks the Folder as Old.
/// </summary>
public Folder MarkFolderOld()
{
if (base.IsDirty) { base.MarkOld(); }
return this;
}
/// <summary>
/// Reference to EditLevel.
/// </summary>
public int GetEditLevel()
{
return base.EditLevel;
}
/// <summary>
/// MapFields.
/// </summary>
public void MapFields(Folder folder)
{
this.IsPrivate = folder.IsPrivate;
this.Archived = folder.Archived;
this.Order = folder.Order;
this.Title = folder.Title;
this.ExternalIdentifier = folder.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 : Folder
{
if (target.FolderID == 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))
{
SyncRequired = true;
}
}
/// <summary>
/// Add Business Rules. Defines basic validation rules to execute against specified fields.
/// </summary>
protected override void AddBusinessRules()
{
// Title
ValidationRules.AddRule(CommonRules.StringRequired, titleProperty);
ValidationRules.AddRule(CommonRules.StringMaxLength, new CommonRules.MaxLengthRuleArgs(titleProperty, 32));
//Id - Validate value is Guid.
ValidationRules.AddRule<Folder>(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
}
}