using System;
using System.Web.UI;
using System.Web.UI.Design;
using System.ComponentModel;
using System.Reflection;
using Csla.Properties;
namespace Csla.Web
{
  /// 
  /// A Web Forms data binding control designed to support
  /// CSLA .NET business objects as data sources.
  /// 
  [Designer(typeof(Csla.Web.Design.CslaDataSourceDesigner))]
  [DisplayName("CslaDataSource")]
  [Description("CSLA .NET Data Source Control")]
  [ToolboxData("<{0}:CslaDataSource runat=\"server\">{0}:CslaDataSource>")]
  public class CslaDataSource : DataSourceControl
  {
    private CslaDataSourceView _defaultView;
    /// 
    /// Event raised when an object is to be created and
    /// populated with data.
    /// 
    /// Handle this event in a page and set
    /// e.BusinessObject to the populated business object.
    /// 
    public event EventHandler SelectObject;
    /// 
    /// Event raised when an object is to be populated with data
    /// and inserted.
    /// 
    /// Handle this event in a page to create an
    /// instance of the object, load the object with data and
    /// insert the object into the database.
    public event EventHandler InsertObject;
    /// 
    /// Event raised when an object is to be updated.
    /// 
    /// Handle this event in a page to update an
    /// existing instance of an object with new data and then
    /// save the object into the database.
    public event EventHandler UpdateObject;
    /// 
    /// Event raised when an object is to be deleted.
    /// 
    /// Handle this event in a page to delete
    /// an object from the database.
    public event EventHandler DeleteObject;
    /// 
    /// Returns the default view for this data control.
    /// 
    /// Ignored.
    /// 
    /// This control only contains a "Default" view.
    protected override DataSourceView GetView(string viewName)
    {
      if (_defaultView == null)
        _defaultView = new CslaDataSourceView(this, "Default");
      return _defaultView;
    }
    /// 
    /// Get or set the name of the assembly (no longer used).
    /// 
    /// Obsolete - do not use.
    public string TypeAssemblyName
    {
      get { return ((CslaDataSourceView)this.GetView("Default")).TypeAssemblyName; }
      set { ((CslaDataSourceView)this.GetView("Default")).TypeAssemblyName = value; }
    }
    /// 
    /// Get or set the full type name of the business object
    /// class to be used as a data source.
    /// 
    /// Full type name of the business class,
    /// including assembly name.
    public string TypeName
    {
      get { return ((CslaDataSourceView)this.GetView("Default")).TypeName; }
      set { ((CslaDataSourceView)this.GetView("Default")).TypeName = value; }
    }
    /// 
    /// Get or set a value indicating whether the
    /// business object data source supports paging.
    /// 
    /// 
    /// To support paging, the business object
    /// (collection) must implement 
    /// .
    /// 
    public bool TypeSupportsPaging
    {
      get { return ((CslaDataSourceView)this.GetView("Default")).TypeSupportsPaging; }
      set { ((CslaDataSourceView)this.GetView("Default")).TypeSupportsPaging = value; }
    }
    /// 
    /// Get or set a value indicating whether the
    /// business object data source supports sorting.
    /// 
    public bool TypeSupportsSorting
    {
      get { return ((CslaDataSourceView)this.GetView("Default")).TypeSupportsSorting; }
      set { ((CslaDataSourceView)this.GetView("Default")).TypeSupportsSorting = value; }
    }
    private static System.Collections.Generic.Dictionary _typeCache = 
      new System.Collections.Generic.Dictionary();
    /// 
    /// Returns a Type object based on the
    /// assembly and type information provided.
    /// 
    /// Optional assembly name.
    /// Full type name of the class,
    /// including assembly name.
    /// 
    internal static Type GetType(
      string typeAssemblyName, string typeName)
    {
      Type result = null;
      if (!string.IsNullOrEmpty(typeAssemblyName))
      {
        // explicit assembly name provided
        result = Type.GetType(string.Format(
          "{0}, {1}", typeName, typeAssemblyName), true, true);
      }
      else if (typeName.IndexOf(",") > 0)
      {
        // assembly qualified type name provided
        result = Type.GetType(typeName, true, true);
      }
      else
      {
        // no assembly name provided
        result = _typeCache[typeName];
        if (result == null)
          foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
          {
            result = asm.GetType(typeName, false, true);
            if (result != null)
            {
              _typeCache.Add(typeName, result);
              break;
            }
          }
      }
      if (result == null)
        throw new TypeLoadException(String.Format(Resources.TypeLoadException, typeName));
      return result;
    }
    /// 
    /// Returns a list of views available for this control.
    /// 
    /// This control only provides the "Default" view.
    protected override System.Collections.ICollection GetViewNames()
    {
      return new string[] { "Default" };
    }
    /// 
    /// Raises the SelectObject event.
    /// 
    internal void OnSelectObject(SelectObjectArgs e)
    {
      if (SelectObject != null)
        SelectObject(this, e);
    }
    /// 
    /// Raises the InsertObject event.
    /// 
    internal void OnInsertObject(InsertObjectArgs e)
    {
      if (InsertObject != null)
        InsertObject(this, e);
    }
    /// 
    /// Raises the UpdateObject event.
    /// 
    internal void OnUpdateObject(UpdateObjectArgs e)
    {
      if (UpdateObject != null)
        UpdateObject(this, e);
    }
    /// 
    /// Raises the DeleteObject event.
    /// 
    internal void OnDeleteObject(DeleteObjectArgs e)
    {
      if (DeleteObject != null)
        DeleteObject(this, e);
    }
  }
}