#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.Collections.Generic;
using System.Text;
using dataMagus.Common.Logical;
#endregion

namespace dataMagus.Common.Physical.Relational
{
	/// <summary>
	/// This class extends a physical relational relationship for 1:1 implementation.
	/// </summary>
    [Serializable]
    public class OneToOnePhysicalRelationalRelationship : PhysicalRelationalRelationship
    {
        #region Constants
        /// <summary>
        /// Name of the Cardinality1 property
        /// </summary>
        private const string Cardinality1PropertyName = "Cardinality1";

        /// <summary>
        /// Name of the Cardinality2 property
        /// </summary>
        private const string Cardinality2PropertyName = "Cardinality2";
        #endregion

        #region Enumerations
		/// <summary>
		/// Possible cardinalities.
		/// </summary>
        public enum Cardinality
        {
        	/// <summary>
        	/// Zero or one.
        	/// </summary>
            ZeroOrOne,
            
            /// <summary>
            /// Only one.
            /// </summary>
            OnlyOne
        }
        #endregion

        #region Members
		/// <summary>
		/// Cardinality to the first table.
		/// </summary>
        private Cardinality _cardinality1 = Cardinality.ZeroOrOne;

		/// <summary>
		/// Cardinality to the second table.
		/// </summary>
        private Cardinality _cardinality2 = Cardinality.ZeroOrOne;
        #endregion

        #region Constructors / Destructors
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="schema">Physical relational schema.</param>
        /// <param name="id">Unique id of the object.</param>
        /// <param name="origin">Origin element the relationship is based on.</param>
        /// <param name="name">Name of the relationship.</param>
        /// <param name="table1">First table.</param>
        /// <param name="table2">Second table.</param>
        /// <param name="rolename1">Rolename of the first table.</param>
        /// <param name="rolename2">Rolename of the second table.</param>
		/// <param name="cardinality1">Cardinality to the first table.</param>
		/// <param name="cardinality2">Cardinality to the second table.</param>
        public OneToOnePhysicalRelationalRelationship(PhysicalRelationalSchema schema, string id, IElement origin, string name,
            PhysicalRelationalTable table1, PhysicalRelationalTable table2,
            string rolename1, string rolename2, Cardinality cardinality1, Cardinality cardinality2)
            : base(schema, id, origin, name, table1, table2, rolename1, rolename2)
        {
            this._cardinality1 = cardinality1;
            this._cardinality2 = cardinality2;

            // Restore physical changes if available
            object cardinality1ToRestore = DataContainer.Instance.PhysicalData.GetRestoreDataItem(base._schema, this.Id, OneToOnePhysicalRelationalRelationship.Cardinality1PropertyName);
            if (null != cardinality1ToRestore)
            {
                this._cardinality1 = (Cardinality)cardinality1ToRestore;
            }

            object cardinality2ToRestore = DataContainer.Instance.PhysicalData.GetRestoreDataItem(base._schema, this.Id, OneToOnePhysicalRelationalRelationship.Cardinality2PropertyName);
            if (null != cardinality2ToRestore)
            {
                this._cardinality2 = (Cardinality)cardinality2ToRestore;
            }
        }
        #endregion

        #region Properties
		/// <summary>
		/// Cardinality to the first table.
		/// </summary>
		[ReadOnly(true)]
        public Cardinality Cardinality1
        {
            get { return this._cardinality1; }
            set
            {
                if (this._cardinality1 != value)
                {
                    this.EnsureUpdateOriginalRestoreDataItem(base._schema, this, OneToOnePhysicalRelationalRelationship.Cardinality1PropertyName, value);
                    this._cardinality1 = value;
                    this.FireElementChangedEvent(this, new ElementEventArgs(this, ElementChangedAction.ChangedProperty));
                }
            }
        }

		/// <summary>
		/// Cardinality to the second table.
		/// </summary>
		[ReadOnly(true)]
        public Cardinality Cardinality2
        {
            get { return this._cardinality2; }
            set
            {
                if (this._cardinality2 != value)
                {
                    this.EnsureUpdateOriginalRestoreDataItem(base._schema, this, OneToOnePhysicalRelationalRelationship.Cardinality2PropertyName, value);
                    this._cardinality2 = value;
                    this.FireElementChangedEvent(this, new ElementEventArgs(this, ElementChangedAction.ChangedProperty));
                }
            }
        }
        #endregion

        #region Methodes
        /// <summary>
        /// Writes a the instace of this class to a writer.
        /// </summary>
        /// <param name="writer">Writer object to write into.</param>
        public override void WriteXml(System.Xml.XmlWriter writer)
        {
            writer.WriteStartElement("OneToOneRelationship");

            // write first all attributes
            writer.WriteAttributeString("cardinality1", this.Cardinality1.ToString());
            writer.WriteAttributeString("cardinality2", this.Cardinality2.ToString());

            // write base elements
            base.WriteXml(writer);

            writer.WriteEndElement();
        }
        #endregion
    }
}
