#region Copyright and GPL
/*************************************************************************
 * dataMagus - The open source database learning software
 * Copyright (C) 2007 by all authors
 * Authors: Monika Schwitter (monsy@gmx.ch)
 *          Jürg Zgraggen (j.zgraggen@ejaz.net)
 *          HSLU (Hochschule Luzern - Technik+Architektur)
 * 
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     This program 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 General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *************************************************************************/
#endregion
#region Using
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using dataMagus.Common.PlugIn;
using dataMagus.Common.PlugIn.Generator;
using System.Data;
using System.Collections;
using System.Collections.Generic;
#endregion

namespace dataMagus.PlugIns.Generators.SqlServer2000
{
	/// <summary>
	/// Control for doing the datatype mapping for SqlServer.
	/// </summary>
	public partial class DataTypeMappingControl : GeneratorControl
	{
		#region Members
		/// <summary>
		/// Dataview for the grid control.
		/// </summary>
        private DataView _mappingView = null;
		#endregion

		#region Constructors / Destructors
		/// <summary>
		/// Default constructor.
		/// </summary>
        public DataTypeMappingControl()
		{
			InitializeComponent();
		}
		#endregion

		#region Methodes
		/// <summary>
		/// Event handler.
		/// </summary>
		/// <param name="sender">Sender object.</param>
		/// <param name="e">EventArgs</param>
		private void DataTypeMappingControlLoad(object sender, EventArgs e)
		{
            this._mappingView = this.CreateDataMapping();
            this._mappingView.AllowNew = false;
            this._mappingView.AllowDelete = false;
            this._mappingView.AllowEdit = true;
            this.dataTypesDataGridView.DataSource = this._mappingView;
		}
		
		/// <summary>
		/// Event handler.
		/// </summary>
		/// <param name="sender">Sender object.</param>
		/// <param name="e">EventArgs</param>
		private void DataTypeMappingControlLeave(object sender, EventArgs e)
        {
            DataView dv = (DataView)this.dataTypesDataGridView.DataSource;

            DataTable modifiedTable = dv.Table.GetChanges();
            if (null != modifiedTable)
            {
                foreach (DataRow row in modifiedTable.Rows)
                {
                    if (DataRowState.Modified == row.RowState)
                    {
                        string physicalDataType = row["PhysicalDataType", DataRowVersion.Original].ToString();
                        string targetDataType = row["TargetDataType"].ToString();
                        GeneratorData.Instance.UpdateDataTypeMapping(physicalDataType, targetDataType);
                    }
                }
                dv.Table.AcceptChanges();
            }
		}

		/// <summary>
		/// Create the datatype mapping view.
		/// </summary>
		/// <returns>Dataview of the mapping.</returns>
        private DataView CreateDataMapping()
        {
            // fill types mapping
            DataTypeMappingDataSet dsx = new DataTypeMappingDataSet();
            IEnumerator datatypesEnumerator = GeneratorData.Instance.GetDataTypeMappings();
            while (datatypesEnumerator.MoveNext())
            {
                KeyValuePair<string, string> pair = (KeyValuePair<string, string>)datatypesEnumerator.Current;
                dsx.DataTypeMappingDataTable.AddDataTypeMappingDataTableRow(pair.Key, pair.Value);
            }
            dsx.AcceptChanges();
            return dsx.DataTypeMappingDataTable.DefaultView;
        }
    	#endregion		
	}
}
