using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Data;
using System.Data.OleDb;
using C1.Data;

namespace DataLibrary
{
    /// <summary>
    /// C1SchemaDef container class: main class of the data library.
    /// Contains the C1SchemaDef component managing the data schema. 
    /// </summary>
    public class DataClass : System.ComponentModel.Component
    {
        /// <summary>
        /// C1SchemaDef component managing the data schema.
        /// Needs to be public in order to allow access to the library.
        /// </summary>
        public C1.Data.C1SchemaDef schemaDef;
        private C1.Data.C1DataSetLogic c1DataSetLogic1;

        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public DataClass(System.ComponentModel.IContainer container)
        {
            /// <summary>
            /// Required for Windows.Forms Class Composition Designer support
            /// </summary>
            container.Add(this);
            InitializeComponent();
        }

        public DataClass()
        {
            /// <summary>
            /// Required for Windows.Forms Class Composition Designer support
            /// </summary>
            InitializeComponent();
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(DataClass));
            this.schemaDef = new C1.Data.C1SchemaDef();
            this.c1DataSetLogic1 = new C1.Data.C1DataSetLogic();
            ((System.ComponentModel.ISupportInitialize)(this.schemaDef)).BeginInit();
            // 
            // schemaDef
            // 
            this.schemaDef.DummyImage = ((System.Drawing.Bitmap)(resources.GetObject("schemaDef.DummyImage")));
            this.schemaDef.CreateSchema += new System.EventHandler(this.schemaDef_CreateSchema);
            this.schemaDef.SerializedSchemaXML = this.schemaDef.GetSchemaFromResources(resources, "schemaDef", this);
            this.schemaDef.SerializedDesignTimeOptionsXML = this.schemaDef.GetDesignTimeOptionsFromResources(resources, "schemaDef", this);
            // 
            // c1DataSetLogic1
            // 
            this.c1DataSetLogic1.DataSetDef = "DataSet";
            this.c1DataSetLogic1.SchemaComponent = this.schemaDef;
            this.c1DataSetLogic1.SchemaPersist = null;
            this.c1DataSetLogic1.AfterFetch += new C1.Data.FetchEventHandler(this.c1DataSetLogic1_AfterFetch);
            this.c1DataSetLogic1.AfterUpdate += new C1.Data.DataSetEventHandler(this.c1DataSetLogic1_AfterUpdate);
            this.c1DataSetLogic1.BeforeFetch += new C1.Data.FetchEventHandler(this.c1DataSetLogic1_BeforeFetch);
            this.c1DataSetLogic1.BeforeUpdate += new C1.Data.DataSetEventHandler(this.c1DataSetLogic1_BeforeUpdate);
            ((System.ComponentModel.ISupportInitialize)(this.schemaDef)).EndInit();

        }
        #endregion

        // This code demonstrates how you can set Connection.DbConnection/DbTransaction directly to
        // a pre-created connection/transaction object. If you set Connection.DbConnection before
        // C1DataObjects opens the connection to fetch data, C1DataObjects does not create a
        // new connection object, uses the one you supplied. Similarly, C1DataObjects opens a new
        // transaction only if you did not set Connection.DbTransaction.
        private void c1DataSetLogic1_BeforeFetch(object sender, C1.Data.FetchEventArgs e)
        {
            if (e.TableView == null)
            {
                OleDbConnection conn = CreateConnection();
                conn.Open();
                schemaDef.Schema.Connections[0].DbConnection = conn;
            }
        }

        private void c1DataSetLogic1_AfterFetch(object sender, C1.Data.FetchEventArgs e)
        {
            if (e.TableView == null)
            {
                schemaDef.Schema.Connections[0].DbConnection.Close();
            }
        }

        private void c1DataSetLogic1_BeforeUpdate(object sender, C1.Data.DataSetEventArgs e)
        {
            OleDbConnection conn = CreateConnection();
            conn.Open();
            schemaDef.Schema.Connections[0].DbConnection = conn;
            schemaDef.Schema.Connections[0].DbTransaction = conn.BeginTransaction();
        }

        private void c1DataSetLogic1_AfterUpdate(object sender, C1.Data.DataSetEventArgs e)
        {
            schemaDef.Schema.Connections[0].DbTransaction.Commit();
            schemaDef.Schema.Connections[0].DbConnection.Close();
        }

        private OleDbConnection CreateConnection()
        {
            OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\ComponentOne Studio.NET 2.0\common\Nwind.mdb;Persist Security Info=False");

            #region Switch to C1NWind.mdb database

            string dbPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal) +
                "\\ComponentOne Samples\\Common\\C1NWind.mdb";
            if (System.IO.File.Exists(dbPath))
            {
                conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbPath;
            }

            #endregion

            return conn;
        }

        #region Switch to C1NWind.mdb database

        private void schemaDef_CreateSchema(object sender, EventArgs e)
        {
            string dbPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal) +
                "\\ComponentOne Samples\\Common\\C1NWind.mdb";
            if (System.IO.File.Exists(dbPath))
            {
                C1.Data.SchemaObjects.Connection conn = schemaDef.Schema.Connections[0];
                conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbPath;
            }
        }

        #endregion
    }
}
