#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 represents a physical objectoriented property.
	/// </summary>
    [Serializable]
    public class PhysicalObjectorientedProperty : PhysicalElement
    {
        #region Constants
        /// <summary>
        /// Name of the DataType property
        /// </summary>
        private const string DataTypePropertyName = "DataType";
        #endregion

        #region Members
		/// <summary>
		/// Parent element the property belongs to.
		/// </summary>
        private PhysicalObjectorientedClass _parent;

        /// <summary>
        /// Datatype of the property.
        /// </summary>
        private DataType _dataType = DataType.Unknown;
        #endregion

        #region Constructors / Destructors
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="schema">Physical objectoriented schema.</param>
        /// <param name="parent">Parent element the property belongs to.</param>
        /// <param name="id">Unique id of the object.</param>
        /// <param name="origin">Origin element the property is based on.</param>
        /// <param name="name">Name of the property.</param>
        /// <param name="dataType">Datatype of the property.</param>
        public PhysicalObjectorientedProperty(PhysicalObjectorientedSchema schema, PhysicalObjectorientedClass parent, string id, IElement origin, string name, DataType dataType)
			: base(schema, id, origin, name)
		{
            this._parent = parent;
			this._dataType = dataType;

            // Restore physical changes if available
            object dataTypeToRestore = DataContainer.Instance.PhysicalData.GetRestoreDataItem(base._schema, this.Id, PhysicalObjectorientedProperty.DataTypePropertyName);
            if (null != dataTypeToRestore)
            {
                this._dataType = (DataType)dataTypeToRestore;
            }
		}

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="schema">Physical objectoriented schema.</param>
        /// <param name="parent">Parent element the property belongs to.</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>
        public PhysicalObjectorientedProperty(PhysicalObjectorientedSchema schema, PhysicalObjectorientedClass parent, string id, IElement origin, string name)
            : this(schema, parent, id, origin, name, DataType.Unknown)
		{
		}
        #endregion

        #region Properties
		/// <summary>
		/// Parent element the property belongs to.
		/// </summary>
		[Browsable(false)]
        public PhysicalObjectorientedClass Parent
        {
            get { return this._parent; }
            set { this._parent = value; }
        }
        
        /// <summary>
        /// Indicator if the property is part of the key on its class.
        /// </summary>
        public bool IsPartOfKey
        {
            get
            {
                if (null == this._parent)
                {
                    return false;
                }
                return this._parent.ContainsKeyProperty(this);
            }
        }
        
        /// <summary>
        /// Datatype of the property.
        /// </summary>
        [ReadOnly(true)]
        public DataType DataType
        {
            get { return this._dataType; }
            set
            {
                if (this._dataType != value)
                {
                    this.EnsureUpdateOriginalRestoreDataItem(base._schema, this, PhysicalObjectorientedProperty.DataTypePropertyName, value);
                    this._dataType = value;
                    this.FireElementChangedEvent(this, new ElementEventArgs(this, ElementChangedAction.ChangedProperty));
                }
            }
        }
        #endregion

        #region Methodes
		/// <summary>
		/// Gets the parent element.
		/// </summary>
		/// <returns>Parent element.</returns>
        public override IElement GetParent()
        {
        	return this._parent;
        }

        /// <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("Property");
            base.WriteXml(writer);

            writer.WriteStartElement("DataType");
            writer.WriteAttributeString("baseType", this._dataType.BaseType.ToString());
            writer.WriteAttributeString("name", this._dataType.Name);
            writer.WriteEndElement();

            writer.WriteEndElement();
        }
        #endregion
    }
}
