#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.Objectoriented
{
	/// <summary>
	/// This class is the abstraction of a physical objectoriented association.
	/// </summary>
    [Serializable]
    public abstract class PhysicalObjectorientedAssociation : PhysicalElement
    {
        #region Constants
        /// <summary>
        /// Name of the Rolename1 property
        /// </summary>
        private const string Rolename1PropertyName = "Rolename1";

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

        #region Members
        /// <summary>
        /// First class.
        /// </summary>
        private PhysicalObjectorientedClass _class1;
        
        /// <summary>
        /// Second class.
        /// </summary>
        private PhysicalObjectorientedClass _class2;
        
        /// <summary>
        /// Rolename of the first class.
        /// </summary>
        private string _rolename1;
        
        /// <summary>
        /// Rolename of the second class.
        /// </summary>
        private string _rolename2;
        #endregion

        #region Constructors / Destructors
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="schema">Physical objectoriented schema.</param>
        /// <param name="id">Unique id of the object.</param>
        /// <param name="origin">Origin element the association is based on.</param>
        /// <param name="name">Name of the association.</param>
        /// <param name="class1">First class.</param>
        /// <param name="class2">Second class.</param>
        /// <param name="rolename1">Rolename of the first class.</param>
        /// <param name="rolename2">Rolename of the second class.</param>
        public PhysicalObjectorientedAssociation(IPhysicalSchema schema, string id, IElement origin, string name,
            PhysicalObjectorientedClass class1, PhysicalObjectorientedClass class2,
            string rolename1, string rolename2)
            : base(schema, id, origin, name)
        {
            this._class1 = class1;
            this._class2 = class2;
            this._rolename1 = rolename1;
            this._rolename2 = rolename2;

            // Restore physical changes if available
            object rolename1ToRestore = DataContainer.Instance.PhysicalData.GetRestoreDataItem(base._schema, this.Id, PhysicalObjectorientedAssociation.Rolename1PropertyName);
            if (null != rolename1ToRestore)
            {
                this._rolename1 = (string)rolename1ToRestore;
            }
            object rolename2ToRestore = DataContainer.Instance.PhysicalData.GetRestoreDataItem(base._schema, this.Id, PhysicalObjectorientedAssociation.Rolename2PropertyName);
            if (null != rolename2ToRestore)
            {
                this._rolename2 = (string)rolename2ToRestore;
            }
        }
        #endregion

        #region Properties
        /// <summary>
        /// First class.
        /// </summary>
		[ReadOnly(true)]
        public PhysicalObjectorientedClass Class1
        {
            get { return this._class1; }
        }

		/// <summary>
		/// Second class.
		/// </summary>
		[ReadOnly(true)]
        public PhysicalObjectorientedClass Class2
        {
            get { return this._class2; }
        }

		/// <summary>
		/// Indicates if the association is a self relation.
		/// </summary>
		public bool IsSelfAssociation
		{
			get { return this._class1 == this._class2; }
		}
        
		/// <summary>
		/// Rolename of first class.
		/// </summary>
        public string Rolename1
        {
            get { return this._rolename1; }
            set
            {
                if (this._rolename1 != value)
                {
                    this.EnsureUpdateOriginalRestoreDataItem(base._schema, this, PhysicalObjectorientedAssociation.Rolename1PropertyName, value);
                    this._rolename1 = value;
                    this.FireElementChangedEvent(this, new ElementEventArgs(this, ElementChangedAction.ChangedProperty));
                }
            }
        }

		/// <summary>
		/// Rolename of second class.
		/// </summary>
        public string Rolename2
        {
            get { return this._rolename2; }
            set
            {
                if (this._rolename2 != value)
                {
                    this.EnsureUpdateOriginalRestoreDataItem(base._schema, this, PhysicalObjectorientedAssociation.Rolename2PropertyName, value);
                    this._rolename2 = 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)
        {
            base.WriteXml(writer);

            writer.WriteAttributeString("class1", this.Class1.Id);
            writer.WriteAttributeString("class2", this.Class2.Id);
            writer.WriteAttributeString("rolename1", this.Rolename1);
            writer.WriteAttributeString("rolename2", this.Rolename2);
        }
        #endregion
    }
}
