<%-- 
Author: yubaolee
Description: 用于生成OpenAuth.Core开源版前端Web界面，包括mvc controller/csthml/js
--%>
<%@ Template Language="C#" TargetLanguage="Text" Debug="True" OutputType="None"  %>

<%@ Assembly Name="SchemaExplorer" %>
<%@ Assembly Name="CodeSmith.CustomProperties" %>

<%@ Assembly Name="Mono.Cecil" Path="..\Common" %>
<%@ Assembly Name="ICSharpCode.NRefactory" Path="..\Common" %>
<%@ Assembly Name="ICSharpCode.NRefactory.CSharp" Path="..\Common" %>

<%@ Assembly Src="Internal\Model.cs" %>
<%@ Assembly Src="Internal\Extensions.cs" %>
<%@ Assembly Src="Internal\Generator.cs" %>
<%@ Assembly Src="Internal\Parser.cs" %>

<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Linq" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.Text.RegularExpressions" %>

<%@ Import Namespace="SchemaMapper" %>

<%@ Property Name="SourceTable" 
Type="SchemaExplorer.TableSchema" 
Category="Context"
Description="连接的数据库" 
OnChanged="OnSourceDatabaseChanged"%>

<%@ Property Name="ModuleName"
             Type="System.String"
             Description="模块名称，如：User"%>
<%@ Property Name="directory"
             Type="System.String"
             Default=".\"
             Optional="True"
             Description="代码生成路径"
             Editor="System.Windows.Forms.Design.FolderNameEditor, System.Design, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" %>

<%@ Register Name="HtmlGenerateClass"
             Template="Web\Index.cshtml.cst"
             MergeProperties="False" %>
<%@ Register Name="JSGenerateClass"
             Template="Web\index.js.cst"
             MergeProperties="False" %>
<%@ Register Name="ControllerGenerateClass"
             Template="Web\Controller.cst"
             MergeProperties="False" %>
Generating Entities ...
<% Generate(); %>

<script runat="template">
    public void Generate()
    {
        Stopwatch watch = Stopwatch.StartNew();
        string outputDirectory = Path.GetFullPath(directory);

        CreateControllerClass();
        CreateHtmlClass();
        CreateJSClass();
        
        this.RegisterReference("System.Configuration");
        this.RegisterReference("System.Data");
        this.RegisterReference("System.Data.Entity");
        this.RegisterReference("System.Runtime.Serialization");
        this.RegisterReference("EntityFramework");

        watch.Stop();
        Response.WriteLine("Generate Time: " + watch.ElapsedMilliseconds + " ms");
    }

    //创建控制器,如UserManagerController.cs
    public void CreateControllerClass()
    {
        ControllerGenerateClass generatedClass = this.Create<ControllerGenerateClass>();
        this.CopyPropertiesTo(generatedClass);

        string rootDirectory = Path.GetFullPath(directory);
      
          string generatedFile = Path.GetFullPath(directory) + "/Controllers/"+  ModuleName + "sController.cs";

        generatedClass.ModuleName = ModuleName;

        Response.WriteLine(generatedFile);
        generatedClass.RenderToFile(generatedFile, generatedFile, true);
    }
    

     //创建视图,如views/Users/index.html
     public void CreateHtmlClass()
    {
        HtmlGenerateClass generatedClass = this.Create<HtmlGenerateClass>();
        this.CopyPropertiesTo(generatedClass);

        string generatedFile = Path.GetFullPath(directory) + "/Views/"+ModuleName+"s/index.cshtml";

        generatedClass.ModuleName = ModuleName;
        generatedClass.SourceTable = SourceTable;

        Response.WriteLine(generatedFile);
        generatedClass.RenderToFile(generatedFile, generatedFile, true);
    }

    //创建视图,如js/userManager.js
     public void CreateJSClass()
    {
        JSGenerateClass generatedClass = this.Create<JSGenerateClass>();
        this.CopyPropertiesTo(generatedClass);

        string generatedFile = Path.GetFullPath(directory) + "/userJs/"+ModuleName.ToLower()+"s.js";

        generatedClass.ModuleName = ModuleName;

        Response.WriteLine(generatedFile);
        generatedClass.RenderToFile(generatedFile, generatedFile, true);
    }


    //更换数据源时，改变ModuleName
    private void OnSourceDatabaseChanged(object sender, EventArgs e)
    {
         if (SourceTable == null)
            return;
            ModuleName = SourceTable.Name;
    }

</script>