//This file is part of ORM.NET.
//
//ORM.NET is free software; you can redistribute it and/or
//modify it under the terms of the GNU Lesser General Public
//License as published by the Free Software Foundation; either
//version 2.1 of the License, or (at your option) any later version.
//
//ORM.NET is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//Lesser General Public License for more details.
//
//You should have received a copy of the GNU Lesser General Public
//License along with ORM.NET; if not, write to the Free Software
//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Collections;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;
namespace OrmLib{
/// <summary>
/// Privides the base implimentation of each business object.
/// </summary>
/// <remarks>
/// This class is abstract an should not be instantiated directly.
/// </remarks>
[Serializable()]
public abstract class BusinessBase : IEditableObject, ICustomTypeDescriptor
{
/// <summary>
/// This is an internal field and should not be used.
/// </summary>
/// <remarks>
/// The underlying row of the object wrapper.
/// </remarks>
protected internal DataRow dataRow = null;
/// <summary>
/// Represents an array of the objects' parents.
/// That is, all the collections that it is in.
/// </summary>
/// <remarks>
/// Removing from this List will stop events being fired on the removed collection.
/// Add to this list will cause events to be fired on the added collection.
/// </remarks>
internal protected ArrayList Parents = new ArrayList();
/// <summary>
/// Used for IEditableObject
/// </summary>
private bool editMode = false;
/// <summary>
/// Used for IEditableObject to delete if CancelEdit() is called.
/// </summary>
internal bool isNew = false;
/// <summary>
/// When CommitAll() is called, the underlying row will be deleted from the database
/// </summary>
public virtual void Delete()
{
while ( this.Parents.Count > 0 )
{
((IList)this.Parents[0]).Remove(this);
}
dataRow.Delete();
}
/// <summary>
/// Method to fire the onListItemChanged event of all parent Collections
/// </summary>
protected virtual void OnChanged()
{
foreach ( CollectionTemplate ct in this.Parents )
{
ct.OnPropertyChanged(this);
}
}
/// <summary>
/// Builds the propertydescriptor collection for data binding
/// </summary>
/// <remarks>
/// This method makes it possible to edit SqlTypes in the grid,
/// and traverse child collections.
/// </remarks>
/// <param name="attributes"></param>
/// <returns></returns>
private PropertyDescriptorCollection GetPropertyDescriptorCollection( Attribute[] attributes )
{
ArrayList output = new ArrayList();
foreach ( PropertyInfo pi in this.GetType().GetProperties() )
{
// dont do any indexers
if ( pi.MemberType == MemberTypes.Property && pi.GetIndexParameters().Length > 0 ) continue;
object[] atts = pi.GetCustomAttributes(typeof(BindableAttribute),true);
if ( atts.Length == 1 && atts[0] as BindableAttribute != null && ((BindableAttribute)atts[0]).Bindable == true ) continue;
if ( pi.PropertyType.Namespace == "System.Data.SqlTypes" )
{
// create the base type property descriptor
output.Add(OrmLib.SqlPropertyDescriptor.GetProperty( pi.Name, pi.PropertyType ) );
}
else
{
output.Add(GenericPropertyDescriptor.GetProperty(pi));
}
}
return new PropertyDescriptorCollection((PropertyDescriptor[])output.ToArray(typeof(PropertyDescriptor)));
}
#region IEditableObject explicit interface definitions
/// <summary>
/// <see cref="IEditableObject.BeginEdit()"/>
/// </summary>
void IEditableObject.BeginEdit()
{
if ( ! editMode )
{
this.dataRow.BeginEdit();
editMode = true;
}
}
/// <summary>
/// <see cref="IEditableObject.CancelEdit()"/>
/// </summary>
void IEditableObject.CancelEdit()
{
if ( editMode )
{
if ( isNew && !DesignMode )// this.Delete();
{
while ( this.Parents.Count > 0 )
{
((IList)this.Parents[0]).Remove(this);
}
}
this.dataRow.CancelEdit();
editMode = false;
}
}
private bool DesignMode
{
get
{
foreach ( CollectionTemplate collection in this.Parents )
{
if ( collection.Site != null && collection.Site.DesignMode )
return true;
}
return false;
}
}
/// <summary>
/// <see cref="IEditableObject.EndEdit()"/>
/// </summary>
void IEditableObject.EndEdit()
{
if ( editMode )
{
this.dataRow.EndEdit();
editMode = false;
isNew = false;
}
}
#endregion
#region ICustomTypeDescriptor explicit interface definitions
/// <summary>
/// <see cref="ICustomTypeDescriptor.GetAttributes()"/>
/// </summary>
AttributeCollection ICustomTypeDescriptor.GetAttributes()
{
return TypeDescriptor.GetAttributes(this, true);
}
/// <summary>
/// <see cref="ICustomTypeDescriptor.GetClassName()"/>
/// </summary>
string ICustomTypeDescriptor.GetClassName()
{
return TypeDescriptor.GetClassName(this, true);
}
/// <summary>
/// <see cref="ICustomTypeDescriptor.GetComponentName()"/>
/// </summary>
string ICustomTypeDescriptor.GetComponentName()
{
return TypeDescriptor.GetComponentName(this, true);
}
/// <summary>
/// <see cref="ICustomTypeDescriptor.GetConverter()"/>
/// </summary>
TypeConverter ICustomTypeDescriptor.GetConverter()
{
return TypeDescriptor.GetConverter(this, true);
}
/// <summary>
/// <see cref="ICustomTypeDescriptor.GetDefaultEvent()"/>
/// </summary>
EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
{
return TypeDescriptor.GetDefaultEvent(this, true);
}
/// <summary>
/// <see cref="ICustomTypeDescriptor.GetDefaultProperty()"/>
/// </summary>
PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
{
return null;
}
/// <summary>
/// <see cref="ICustomTypeDescriptor.GetEditor(System.Type)"/>
/// </summary>
object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
{
return TypeDescriptor.GetEditor(this, editorBaseType, true);
}
/// <summary>
/// <see cref="ICustomTypeDescriptor.GetEvents()"/>
/// </summary>
EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
{
return TypeDescriptor.GetEvents(this, true);
}
/// <summary>
/// <see cref="ICustomTypeDescriptor.GetEvents()"/>
/// </summary>
EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
{
return TypeDescriptor.GetEvents(this, attributes, true);
}
/// <summary>
/// <see cref="ICustomTypeDescriptor.GetProperties()"/>
/// </summary>
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
{
return ((ICustomTypeDescriptor)this).GetProperties(new Attribute[0]);
}
/// <summary>
/// <see cref="ICustomTypeDescriptor.GetProperties()"/>
/// </summary>
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
{
return GetPropertyDescriptorCollection(attributes);
}
/// <summary>
/// <see cref="ICustomTypeDescriptor.GetPropertyOwner(System.ComponentModel.PropertyDescriptor)"/>
/// </summary>
object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
{
return this;
}
#endregion
}
}
|