#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.IO;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Xsl;
using System.Reflection;
using System.Windows.Forms;
using dataMagus.Common;
using dataMagus.Common.Physical;
using dataMagus.Common.Physical.Objectoriented;
using dataMagus.Common.PlugIn;
using dataMagus.Common.PlugIn.Generator;
#endregion

namespace dataMagus.PlugIns.Generators.Odl
{
	/// <summary>
	/// Implementation of the ODL code generator.
	/// </summary>
    public class OdlGenerator : IObjectorientedGenerator
    {
        #region Members
        /// <summary>
        /// Name of the generator.
        /// </summary>
        private string _name = "ODL";

        /// <summary>
        /// Clone of the schema to be generated.
        /// </summary>
        private PhysicalObjectorientedSchema _schema;
        #endregion

        #region Properties
        /// <summary>
        /// Extended data from the generator.
        /// </summary>
        public IExtendedData ExtendedData 
        { 
        	get { return GeneratorData.Instance;  }
        }

        /// <summary>
        /// Clone of the schema to be generated.
        /// </summary>
        public ISchema Schema
        {
            set
            {
                if (value is PhysicalObjectorientedSchema)
                {
                    this._schema = (PhysicalObjectorientedSchema)value;
                }
                else
                {
                    throw new Exception("Schema must be type of PhysicalObjectorientedSchema");
                }
            }
        }

        /// <summary>
        /// Name of the generator.
        /// </summary>
        public string Name 
        { 
        	get { return this._name; }
        }
        #endregion

        #region Methodes
        /// <summary>
        /// Update a datatype on the schema.
        /// </summary>
        /// <param name="newDatatype">New datatype.</param>
        /// <param name="oldDatatype">Old datatype.</param>
        /// <param name="action">Action happend.</param>
        public void UpdateDataType(DataType newDatatype, DataType oldDatatype, dataMagus.Common.Physical.PhysicalItemContainer.DataTypeAction action)
        {
            GeneratorData.Instance.UpdateDataType(newDatatype, oldDatatype, action);
        }

        /// <summary>
        /// Get the generator control for the extended class data.
        /// </summary>
        /// <param name="element">Element to store the extended data for.</param>
        /// <returns>Always null.</returns>
        public GeneratorControl GetExtendedClassDataControl(PhysicalObjectorientedClass element)
        {
        	return null;
        }

        /// <summary>
        /// Get the generator control for the extended property data.
        /// </summary>
        /// <param name="element">Element to store the extended data for.</param>
        /// <returns>Always null.</returns>
        public GeneratorControl GetExtendedPropertyDataControl(PhysicalObjectorientedProperty element)
        {
        	return null;
        }

        /// <summary>
        /// Get the generator control for the extended association data.
        /// </summary>
        /// <param name="element">Element to store the extended data for.</param>
        /// <returns>Always null.</returns>
        public GeneratorControl GetExtendedAssociationDataControl(PhysicalObjectorientedAssociation element)
        {
        	return null;
        }

        /// <summary>
        /// Get the generator control for the datatype mapping.
        /// </summary>
        /// <returns>Instance of a generator control.</returns>
        public GeneratorControl GetGeneratorDataTypeMappingsControl()
        {
            return new DataTypeMappingControl();
        }

        /// <summary>
        /// Generate source code into a stream.
        /// </summary>
        /// <param name="stream">Stream to generate in.</param>
        public void Generate(Stream stream)
        {
            // Check Schema
            if (null == this._schema)
            {
                throw new Exception("Schema must not be null.");
            }

            Stream xsltStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("dataMagus.PlugIns.Generators.Odl.Odl.xslt");
            XmlReader xsltXmlReader = XmlReader.Create(xsltStream);
            XsltSettings xslSettings = new XsltSettings();
            XsltSettings xsltSettings = new XsltSettings(true, true);
            XslCompiledTransform xslTransform = new XslCompiledTransform();
            xslTransform.Load(xsltXmlReader, xslSettings, null);
            
            using (MemoryStream ms = new MemoryStream())
            {
                using (XmlWriter writer = new XmlTextWriter(ms, Encoding.UTF8))
                {
                    writer.WriteStartElement("OdlGenerator");
                    this.ExtendedData.WriteXml(writer);
                    this._schema.WriteXml(writer);
                    writer.WriteEndElement();

                    // Important: writer must be flushed, memory stream must be reset to the begin
                    // and must be flushed
                    writer.Flush();
                    ms.Seek(0, SeekOrigin.Begin);
                    ms.Flush();
                      
                    XmlDocument doc = new XmlDocument();
                    doc.Load(ms);
                    xslTransform.Transform(doc, null, stream);
                }
            }
        }

        /// <summary>
        /// Clean all extended generator data.
        /// </summary>
        /// <param name="schema">Schema to clean the data for.</param>
        public void CleanExtendedData(IPhysicalSchema schema)
		{
			// there is no work to do
		}
		
        /// <summary>
        /// Get the generator control for the options tab on the generator dialog.
        /// </summary>
        /// <returns>Instance of a generator control.</returns>
        public GeneratorControl GetGeneratorOptionDataControl()
        {
        	return new OptionsControl();
        }

        /// <summary>
        /// Get the generator control for the extended schema data.
        /// </summary>
        /// <param name="schema">Schema to store the extended data for.</param>
        /// <returns>Always null.</returns>
        public GeneratorControl GetExtendedSchemaDataControl(IPhysicalSchema schema)
        {
        	return null;
        }
        #endregion
    }
}
