#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.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using dataMagus.Common.PlugIn;
using dataMagus.Common.PlugIn.Generator;
using dataMagus.Common.Physical.Relational;
using dataMagus.Common.Physical;
#endregion

namespace dataMagus.PlugIns.Generators.SqlServer2000
{
	/// <summary>
	/// This control handels the extended data for a column.
	/// </summary>
	public partial class ExtendedColumnControl : GeneratorControl
	{
        #region Members
        /// <summary>
        /// Origin relationship the control is based on.
        /// </summary>
        private PhysicalRelationalColumn _origin;
        #endregion

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

        /// <summary>
        /// Runtime constructor.
        /// </summary>
        /// <param name="origin">Origin column the control is based on.</param>
        public ExtendedColumnControl(PhysicalRelationalColumn origin)
        {
			InitializeComponent();
            if (null == origin)
            {
                throw new Exception("Parameter origin must not be null.");
            }
            this._origin = origin;
        }
        #endregion
        
        #region Methods
        /// <summary>
        /// Event handler.
        /// </summary>
        /// <param name="sender">Sender object.</param>
        /// <param name="e">EventArgs</param>
		private void ExtendedColumnControlLoad(object sender, EventArgs e)
		{
            if (!this._origin.IsPartOfForeignKey)
            {
                // there is only an ColumnGeneratorData available for the identity column so far
                ColumnGeneratorData colData = GeneratorData.Instance.GetColumnData(this._origin.Id);
                if (null != colData)
                {
                    // check if identity is still valid
                    if (colData.Identity && (!this._origin.DataType.BaseType.ToString().Equals(DataType.Number.ToString()) || this._origin.DataType.Equals(DataType.Float.ToString())) || this._origin.AllowNulls)
                    {
                        GeneratorData.Instance.RemoveColumnData(this._origin.Id);
                        this.identityCheckbox.Enabled = false;
                        this.identityCheckbox.Checked = false;
                    }
                    else
                    {
                        this.identityCheckbox.Checked = colData.Identity;
                        this.identityCheckbox.Enabled = true;
                        this.seedNumericUpDown.Value = colData.Seed;
                        this.incrementNumericUpDown.Value = colData.Increment;
                    }
                }
                else
                {
                    // check if column can be an identity column
                    // this is only allowed if no other column has been defined as an identity column
                    // only DataType.Number or DataType.Float is allowed
                    // if we are not consistent the generator will clean his data before the code generation
                    if (this._origin.DataType.BaseType.ToString().Equals(DataType.Number.BaseType.ToString()) || this._origin.DataType.BaseType.ToString().Equals(DataType.Float.BaseType.ToString()) && !this._origin.AllowNulls)
                    {
                        bool hasIdentity = false;
                        IEnumerator columnsEnumerator = this._origin.Parent.GetColumns();
                        while (columnsEnumerator.MoveNext())
                        {
                            PhysicalRelationalColumn siblingColumn = (PhysicalRelationalColumn)columnsEnumerator.Current;
                            if (siblingColumn.Id != this._origin.Id &&
                                GeneratorData.Instance.GetColumnData(siblingColumn.Id) != null)
                            {
                                hasIdentity = true;
                                break;
                            }
                        }
                        if (!hasIdentity)
                        {
                            this.identityCheckbox.Enabled = true;
                            this.identityCheckbox.Checked = false;
                        }
                    }
                    else
                    {
                        this.identityCheckbox.Enabled = false;
                    }
                }

                this.seedNumericUpDown.Enabled = this.identityCheckbox.Checked;
                this.incrementNumericUpDown.Enabled = this.identityCheckbox.Checked;
            }
		}
		
        /// <summary>
        /// Event handler.
        /// </summary>
        /// <param name="sender">Sender object.</param>
        /// <param name="e">EventArgs</param>
		private void ExtendedColumnControlLeave(object sender, EventArgs e)
		{
            if (!this._origin.IsPartOfForeignKey)
            {
                // Get the saved extended relationship data
                ColumnGeneratorData colData = GeneratorData.Instance.GetColumnData(this._origin.Id);
                if (null != colData)
                {
                    // if we have saved data and the identity box is not checked
                    // then we remove the saved data
                    if (!this.identityCheckbox.Checked)
                    {
                        GeneratorData.Instance.RemoveColumnData(this._origin.Id);
                        return;
                    }
                }
                else
                {
                    colData = new ColumnGeneratorData();
                }

                // We save the data if the identity box is checked
                if (this.identityCheckbox.Checked)
                {
                    colData.Identity = this.identityCheckbox.Checked;
                    colData.Seed = (int)this.seedNumericUpDown.Value;
                    colData.Increment = (int)this.incrementNumericUpDown.Value;
                    GeneratorData.Instance.UpdateColumnData(this._origin.Id, colData);
                }
            }
		}

        /// <summary>
        /// Event handler.
        /// </summary>
        /// <param name="sender">Sender object.</param>
        /// <param name="e">EventArgs</param>
        private void identityCheckbox_CheckedChanged(object sender, EventArgs e)
        {
            this.seedNumericUpDown.Enabled = this.identityCheckbox.Checked;
            this.incrementNumericUpDown.Enabled = this.identityCheckbox.Checked;
        }
        #endregion
	}
}
