#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 dataMagus.Common.Physical.Relational;
#endregion

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

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

        /// <summary>
        /// Runtime constructor.
        /// </summary>
        /// <param name="origin">Origin relationship the control is based on.</param>
        public ExtendedRelationshipControl(PhysicalRelationalRelationship 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>
        protected void ExtendedRelationshipControlLoad(object sender, EventArgs e)
		{
            this.onUpdateComboBox.Items.Add(RelationshipGeneratorData.ReferentialIntegrityAction.NoAction);
            this.onUpdateComboBox.Items.Add(RelationshipGeneratorData.ReferentialIntegrityAction.Restrict);
            this.onUpdateComboBox.Items.Add(RelationshipGeneratorData.ReferentialIntegrityAction.Cascade);
            this.onUpdateComboBox.Items.Add(RelationshipGeneratorData.ReferentialIntegrityAction.SetNull);
            this.onUpdateComboBox.Items.Add(RelationshipGeneratorData.ReferentialIntegrityAction.SetDefault);

            this.onDeleteComboBox.Items.Add(RelationshipGeneratorData.ReferentialIntegrityAction.NoAction);
            this.onDeleteComboBox.Items.Add(RelationshipGeneratorData.ReferentialIntegrityAction.Restrict);
            this.onDeleteComboBox.Items.Add(RelationshipGeneratorData.ReferentialIntegrityAction.Cascade);
            this.onDeleteComboBox.Items.Add(RelationshipGeneratorData.ReferentialIntegrityAction.SetNull);
            this.onDeleteComboBox.Items.Add(RelationshipGeneratorData.ReferentialIntegrityAction.SetDefault);

            RelationshipGeneratorData relData = GeneratorData.Instance.GetRelationshipData(this._origin.Id);
            if (null != relData)
            {
                this.onUpdateComboBox.SelectedItem = relData.OnUpdate;
                this.onDeleteComboBox.SelectedItem = relData.OnDelete;
            }
            else
            {
                this.onUpdateComboBox.SelectedItem = RelationshipGeneratorData.ReferentialIntegrityAction.NoAction;
                this.onDeleteComboBox.SelectedItem = RelationshipGeneratorData.ReferentialIntegrityAction.NoAction;
            }
		}

        /// <summary>
        /// Event handler.
        /// </summary>
        /// <param name="sender">Sender object.</param>
        /// <param name="e">EventArgs</param>
        protected void ExtendedRelationshipControlLeave(object sender, EventArgs e)
		{
            RelationshipGeneratorData.ReferentialIntegrityAction onUpdate = (RelationshipGeneratorData.ReferentialIntegrityAction)this.onUpdateComboBox.SelectedItem;
            RelationshipGeneratorData.ReferentialIntegrityAction onDelete = (RelationshipGeneratorData.ReferentialIntegrityAction)this.onDeleteComboBox.SelectedItem;
            
            // Get the saved extended relationship data
            RelationshipGeneratorData relData = GeneratorData.Instance.GetRelationshipData(this._origin.Id);
            if (null != relData)
            {
                if (RelationshipGeneratorData.ReferentialIntegrityAction.NoAction == onUpdate &&
                    RelationshipGeneratorData.ReferentialIntegrityAction.NoAction == onDelete)
                {
                    //if we set the default action (double NoAction), we remove the settings
                    //--> it's not necessary to keep this
                    GeneratorData.Instance.RemoveRelationshipData(this._origin.Id);
                }
                else if (relData.OnUpdate != onUpdate || relData.OnDelete != onDelete)
                {
                    //we only save the changes if they are different
                    relData.OnUpdate = onUpdate;
                    relData.OnDelete = onDelete;
                    GeneratorData.Instance.UpdateRelationshipData(this._origin.Id, relData);
                }
            }
            else
            { 
                // we only create and save the data if they are different to the
                // default actions (double NoAction)
                if (RelationshipGeneratorData.ReferentialIntegrityAction.NoAction != onUpdate ||
                    RelationshipGeneratorData.ReferentialIntegrityAction.NoAction != onDelete)
                {
                    relData = new RelationshipGeneratorData();
                    relData.OnUpdate = onUpdate;
                    relData.OnDelete = onDelete;
                    GeneratorData.Instance.UpdateRelationshipData(this._origin.Id, relData);
                }
            }
        }
        #endregion
    }
}
