// ========================================================================
// Copyright 2006 - Volian Enterprises, Inc. All rights reserved.          
// Volian Enterprises - Proprietary Information - DO NOT COPY OR DISTRIBUTE
// ------------------------------------------------------------------------
// $Workfile: $     $Revision: $                                           
// $Author: $   $Date: $                                                   
//                                                                         
// $History: $                                                             
// ========================================================================
using System;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;
namespace Volian.Object.Library
{
	/// 
	///	ConnectionFolder Generated by MyGeneration using the CSLA Object Mapping template
	/// 
	[Serializable()]
	public partial class ConnectionFolder : BusinessBase
	{
	#region Business Methods
	
		private int _folderid;
		[System.ComponentModel.DataObjectField(true, true)]
		public int Folderid
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty(true);
				return _folderid;
			}
		}
		private int _parentid;
		[System.ComponentModel.DataObjectField(true, true)]
		public int Parentid
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty(true);
				return _parentid;
			}
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			set
			{
				CanWriteProperty(true);
				if (_parentid != value)
				{
					_parentid = value;
					PropertyHasChanged();
				}
			}
		}
		private string _name=string.Empty;
		[System.ComponentModel.DataObjectField(true, true)]
		public string Name
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty(true);
				return _name;
			}
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			set
			{
				CanWriteProperty(true);
				if (value == null) value = string.Empty;
				if (_name != value)
				{
					_name = value;
					PropertyHasChanged();
				}
			}
		}
		private string _title=string.Empty;
		[System.ComponentModel.DataObjectField(true, true)]
		public string Title
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty(true);
				return _title;
			}
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			set
			{
				CanWriteProperty(true);
				if (value == null) value = string.Empty;
				if (_title != value)
				{
					_title = value;
					PropertyHasChanged();
				}
			}
		}
		private string _config=string.Empty;
		[System.ComponentModel.DataObjectField(true, true)]
		public string Config
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty(true);
				return _config;
			}
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			set
			{
				CanWriteProperty(true);
				if (value == null) value = string.Empty;
				if (_config != value)
				{
					_config = value;
					PropertyHasChanged();
				}
			}
		}
		private DateTime _dts=new DateTime();
		[System.ComponentModel.DataObjectField(true, true)]
		public DateTime Dts
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty(true);
				return _dts;
			}
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			set
			{
				CanWriteProperty(true);
				if (_dts != value)
				{
					_dts = value;
					PropertyHasChanged();
				}
			}
		}
		private string _usrid=string.Empty;
		[System.ComponentModel.DataObjectField(true, true)]
		public string Usrid
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			get
			{
				CanReadProperty(true);
				return _usrid;
			}
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
			set
			{
				CanWriteProperty(true);
				if (value == null) value = string.Empty;
				if (_usrid != value)
				{
					_usrid = value;
					PropertyHasChanged();
				}
			}
		}
		private byte[] _lastchanged=new byte[8];//timestamp
		// TODO: Check ConnectionFolder.GetIdValue to assure that the ID returned is unique
		/// 
		/// Overrides Base GetIdValue - Used internally by CSLA to determine equality
		/// 
		/// A Unique ID for the current ConnectionFolder
		protected override object GetIdValue()
		{
			return _folderid;
		}
		// TODO: Replace base ConnectionFolder.ToString function as necessary
		/// 
		/// Overrides Base ToString
		/// 
		/// A string representation of current ConnectionFolder
		//public override string ToString()
		//{
		//  return base.ToString();
		//}
	
	#endregion
		#region ValidationRules
		protected override void AddBusinessRules()
		{
			ValidationRules.AddRule(
				Csla.Validation.CommonRules.StringRequired, "Name");
			ValidationRules.AddRule(
				Csla.Validation.CommonRules.StringMaxLength, 
				new Csla.Validation.CommonRules.MaxLengthRuleArgs("Name", 100));
			ValidationRules.AddRule(
				Csla.Validation.CommonRules.StringMaxLength, 
				new Csla.Validation.CommonRules.MaxLengthRuleArgs("Title", 510));
			ValidationRules.AddRule(
				Csla.Validation.CommonRules.StringMaxLength, 
				new Csla.Validation.CommonRules.MaxLengthRuleArgs("Config", 1073741823));
			ValidationRules.AddRule(
				Csla.Validation.CommonRules.StringMaxLength, 
				new Csla.Validation.CommonRules.MaxLengthRuleArgs("Usrid", 100));
		// TODO:  Add other validation rules
		}
		// Sample data comparison validation rule
		//private bool StartDateGTEndDate(object target, Csla.Validation.RuleArgs e)
		//{
		//	if (_started > _ended)
		//	{
		//		e.Description = "Start date can't be after end date";
		//		return false;
		//	}
		//	else
		//		return true;
		//}
		#endregion
	#region Authorization Rules
		protected override void AddAuthorizationRules()
		{
			//TODO: Who can read/write which fields
			//AuthorizationRules.AllowRead(Folderid, "");
			//AuthorizationRules.AllowRead(Parentid, "");
			//AuthorizationRules.AllowWrite(Parentid, "");
			//AuthorizationRules.AllowRead(Name, "");
			//AuthorizationRules.AllowWrite(Name, "");
			//AuthorizationRules.AllowRead(Title, "");
			//AuthorizationRules.AllowWrite(Title, "");
			//AuthorizationRules.AllowRead(Config, "");
			//AuthorizationRules.AllowWrite(Config, "");
			//AuthorizationRules.AllowRead(Dts, "");
			//AuthorizationRules.AllowWrite(Dts, "");
			//AuthorizationRules.AllowRead(Usrid, "");
			//AuthorizationRules.AllowWrite(Usrid, "");
		}
	
		public static bool CanAddObject()
		{
			// TODO: Can Add Authorization
			//return Csla.ApplicationContext.User.IsInRole("ProjectManager");
			return true;
		}
	
		public static bool CanGetObject()
		{
			// TODO: CanGet Authorization
			return true;
		}
	
		public static bool CanDeleteObject()
		{
			// TODO: CanDelete Authorization
			//bool result = false;
			//if (Csla.ApplicationContext.User.IsInRole("ProjectManager"))result = true;
			//if (Csla.ApplicationContext.User.IsInRole("Administrator"))result = true;
			//return result;
			return true;
		}
	
		public static bool CanEditObject()
		{
			// TODO: CanEdit Authorization
			//return Csla.ApplicationContext.User.IsInRole("ProjectManager");
			return true;
		}
		#endregion
	#region Factory Methods
	
    internal static ConnectionFolder New(string name)
    {
      return new ConnectionFolder(name);
    }
    internal static ConnectionFolder Get(SafeDataReader dr)
    {
      return new ConnectionFolder(dr);
    }
    private ConnectionFolder()
    {
      MarkAsChild();
				_parentid = ext.DefaultParentid;
				_dts = ext.DefaultDts;
				_usrid = ext.DefaultUsrid;
    }
    private ConnectionFolder(string name)
    {
      MarkAsChild();
      // TODO: Add any initialization & defaults 
				_parentid = ext.DefaultParentid;
				_dts = ext.DefaultDts;
				_usrid = ext.DefaultUsrid;
		_name  = name;
    }
    private ConnectionFolder(SafeDataReader dr)
    {
      MarkAsChild();
      Fetch(dr);
    }
	
	#endregion
	#region Data Access Portal
    private void Fetch(SafeDataReader dr)
    {
		try{
		_folderid = dr.GetInt32("FolderID");
		_parentid = dr.GetInt32("ParentID");
		_name = dr.GetString("Name");
		_title = dr.GetString("Title");
		_config = dr.GetString("Config");
		_dts = dr.GetDateTime("DTS");
		_usrid = dr.GetString("UsrID");
		dr.GetBytes("LastChanged", 0, _lastchanged, 0, 8);
		}
		catch(Exception ex)
		{
			Database.LogException("ConnectionFolder",ex);
		}
      MarkOld();
    }
    internal void Insert(Connection connection,SqlConnection cn)
    {
		// if we're not dirty then don't update the database
		if (!this.IsDirty) return;
		_lastchanged=Folder.Add(cn, ref _folderid, _parentid, connection.Dbid, _name, _title, _config, _dts, _usrid);
		MarkOld();
    }
    internal void Update(Connection connection,SqlConnection cn)
    {
		// if we're not dirty then don't update the database
		if (!this.IsDirty) return;
		_lastchanged=Folder.Update(cn, ref _folderid, _parentid, connection.Dbid, _name, _title, _config, _dts, _usrid, ref _lastchanged);
    }
    internal void DeleteSelf(Connection connection,SqlConnection cn)
    {
		// if we're not dirty then don't update the database
		if (!this.IsDirty) return;
		// if we're new then don't update the database
		if (this.IsNew) return;
		Folder.Remove(cn,_folderid);
		MarkNew();
    }
	
	#endregion
// Standard Default Code
		//	#region extension
		Extension ext = new Extension();
		[Serializable()]
		partial class Extension : extensionBase
		{
		}
		[Serializable()]
		class extensionBase
		{
			// Default Values
			public virtual int DefaultParentid
			{
				get { return 0; }
			}
			public virtual int DefaultDbid
			{
				get { return 0; }
			}
			public virtual DateTime DefaultDts
			{
				get { return DateTime.Now; }
			}
			public virtual string DefaultUsrid
			{
				get { return Environment.UserName.ToUpper(); }
			}
			// Authorization Rules
			public virtual void AddAuthorizationRules(Csla.Security.AuthorizationRules rules)
			{
				// Needs to be overriden to add new authorization rules
			}
			// Validation Rules
			public virtual void AddValidationRules(Csla.Validation.ValidationRules rules)
			{
				// Needs to be overriden to add new validation rules
			}
		}
		//#endregion
// The follwing is a sample Extension File.  You can use it to create ConnectionFolderExt.cs
//using System;
//using System.Collections.Generic;
//using System.Text;
//using Csla;
//namespace PatrialClass
//{
//  public partial class ConnectionFolder
//  {
//    partial class Extension : extensionBase
//    {
// TODO: Override automatic defaults
//			public virtual int DefaultParentid
//			{
//				get { return 0; }
//			}
//			public virtual int DefaultDbid
//			{
//				get { return 0; }
//			}
//			public virtual DateTime DefaultDts
//			{
//				get { return DateTime.Now; }
//			}
//			public virtual string DefaultUsrid
//			{
//				get { return Environment.UserName.ToUpper(); }
//			}
//      public new void AddAuthorizationRules(Csla.Security.AuthorizationRules rules)
//      {
//        //rules.AllowRead(Dbid, "");
//      }
//      public new void AddValidationRules(Csla.Validation.ValidationRules rules)
//      {
//			rules.AddRule(
//				Csla.Validation.CommonRules.StringMaxLength, 
//				new Csla.Validation.CommonRules.MaxLengthRuleArgs("Name", 100));
//      }
//    }
//  }
//}
	} // Class
} // Namespace