##|TYPE Template
##|UNIQUEID 9fcabf5d-31fd-4546-8750-b6c1f7b89087
##|TITLE Learning Example
##|NAMESPACE 
##|SOURCE_TYPE Source
##|OUTPUT_LANGUAGE C#
##|GUI_ENGINE .Net Script
##|GUI_LANGUAGE C#
##|GUI_BEGIN

public class GeneratedGui : DotNetScriptGui
{
	public GeneratedGui(ZeusContext context) : base(context) {}

	//-----------------------------------------
	// The User Interface Entry Point
	//-----------------------------------------
	public override void Setup()
	{
		if ( !input.Contains( "chooseTables" ) || !input.Contains( "txtPath" ) ||
				( !input.Contains( "chkClass" ) && !input.Contains( "chkNaming" ) ) )
		{
			if(context.Objects.ContainsKey("DnpUtils"))DnpUtils.ReadInputFromCache(context);
			// UI Form Layout
			ui.Title = "CSLA Object Mapping";
			ui.Width = 600;
			ui.Height = 450;
			// Output Path Label, TextBox and Button
			GuiLabel label1 = ui.AddLabel( "label1", "Select the output path:", "Select the output path in the field below." );
			// label1.Width = 200;
			GuiTextBox outputPath = ui.AddTextBox( "outputPath", GetDefault("defaultOutputPath"), "Select the Output Path." );
			outputPath.Width = 450;
			GuiFilePicker selectPath = ui.AddFilePicker( "selectPath", "Select Path", "Select the Output Path.", "outputPath", true );
			selectPath.Top = outputPath.Top;
			selectPath.Width = 100;
			selectPath.Left = outputPath.Left + outputPath.Width + 20;
			// Namespace Label and TextBox
			GuiLabel label2 = ui.AddLabel( "label2", "Namespace: ",  "Provide your objects namespace." );
			label2.Width = 280;
			GuiTextBox classNamespace = ui.AddTextBox( "classNamespace", "Volian.Object.Library", "Provide your objects namespace." );				
			classNamespace.Width = 280;
			// Member Prefix Label and TextBox
			GuiLabel label3 = ui.AddLabel( "label3", "Member prefix: ", "Provide your member prefix." );
			label3.Width = 100;
			label3.Top = label2.Top;
			label3.Left = label2.Width + 20;
			GuiTextBox memberPrefix = ui.AddTextBox( "memberPrefix", "_", "Provide your member prefix." );
			memberPrefix.Width = 100;
			memberPrefix.Top = classNamespace.Top;
			memberPrefix.Left = classNamespace.Width + 20;
      // Database Connection Name Label and TextBox  
			GuiLabel label3A = ui.AddLabel( "label3A", "dbConnection: ", "Provide a Connection Name." );
			label3A.Width = 150;
			label3A.Top = label3.Top;
			label3A.Left = label3.Left+label3.Width + 10;
			GuiTextBox dbConnection = ui.AddTextBox( "dbConnection", "VEPROMS", "Provide a Connection Name." );
			dbConnection.Width = 150;
			dbConnection.Top = memberPrefix.Top;
			dbConnection.Left = memberPrefix.Left + memberPrefix.Width + 10;
             
			// Setup Database selection combobox.
			GuiLabel label4 = ui.AddLabel( "label4", "Select a database:", "Select a database in the dropdown below." );
			label4.Width = 280;
			GuiComboBox chooseDatabase = ui.AddComboBox( "chooseDatabase", "Select a database." );
			chooseDatabase.Width = 280;
			
			GuiCheckBox chkPrimary = MakeGuiCheckBox( "chkPrimary", "Primary Keys", true, "Show Primary Keys",100 );
			chkPrimary.Checked = false;
			GuiCheckBox chkColumns = MakeGuiCheckBox( "chkColumns", "Columns", true, "Show Columns" ,150,chkPrimary,200,0);
			chkColumns.Checked = false;
			GuiCheckBox chkFK = MakeGuiCheckBox( "chkFK", "Foreign Keys", true, "Show Foreign Keys" ,150,chkPrimary,400,0);
			chkFK.Checked = false;
			// Setup Tables selection multi-select listbox.
			GuiLabel label7 = ui.AddLabel( "label7", "Select tables:", "Select tables from the listbox below." );
			//label7.Top = chkInternal.Top + 20;
			GuiListBox chooseTables = ui.AddListBox( "chooseTables", "Select tables." );
			chooseTables.Height = 120;
	
			// Setup Views selection multi-select listbox.
			//GuiLabel label8 = ui.AddLabel( "label8", "Select views:", "Select views from the listbox below." );
			//GuiListBox chooseViews = ui.AddListBox( "chooseViews", "Select views." );
			//chooseViews.Height = 80;
			
			// Attach the onchange event to the cmbDatabases control.
			setupDatabaseDropdown( chooseDatabase );
			GuiLabel labelTest = ui.AddLabel( "labelTest", "Status: Select CheckBox to Run",  "Checkbox must be selected first" );
			chooseDatabase.AttachEvent( "onchange", "chooseDatabase_onchange" );
			chkPrimary.AttachEvent( "onclick", "checkbox_onclick" );
			chkColumns.AttachEvent( "onclick", "checkbox_onclick" );
			chkFK.AttachEvent( "onclick", "checkbox_onclick" );
			chooseTables.AttachEvent( "onchange", "chooseTable_onclick" );
			ui.ShowGui = true;
			GuiButton btnOK = ui.AddButton( "OK", "OK", "Generate Code" );
			btnOK.ClosesForm = true;
			btnOK.Enabled = false;
		}
		else 
		{
			ui.ShowGui = false;
		}
	}
	public void setupDatabaseDropdown( GuiComboBox Databases )
	{
		try 
		{	
			if( MyMeta.IsConnected )
			{
				Databases.BindData( MyMeta.Databases );
				if( MyMeta.DefaultDatabase != null ) 
				{
					Databases.SelectedValue = MyMeta.DefaultDatabase.Alias;
					bindTables( Databases.SelectedValue );
					bindViews( Databases.SelectedValue );
				}
			}
		}
		catch
		{
		}
	}
	
	public void bindTables( string sDatabase )
	{
		//int count = 0;
	
		GuiListBox lstTables = ui["chooseTables"] as GuiListBox;
		
		try 
		{	
			IDatabase db = MyMeta.Databases[sDatabase];
			lstTables.BindData(db.Tables);
		}
		catch
		{
		}
	}
	
	public void bindViews( string sDatabase )
	{
		//int count = 0;
	
		GuiListBox lstViews = ui["chooseViews"] as GuiListBox;
		
		try 
		{	
			IDatabase db = MyMeta.Databases[sDatabase];
			lstViews.BindData( db.Views );
		}
		catch
		{
		}
	}
	
	public void chooseDatabase_onchange( GuiComboBox control )
	{
		//int count = 0;

		GuiComboBox cmbDatabases = ui["chooseDatabase"] as GuiComboBox;
	
		bindTables( cmbDatabases.SelectedText );
		bindViews( cmbDatabases.SelectedText );
		GuiTextBox dbConnection = ui["dbConnection"] as GuiTextBox;
		dbConnection.Text=cmbDatabases.SelectedText;
		GuiTextBox classNamespace = ui["classNamespace"] as GuiTextBox;
		classNamespace.Text=cmbDatabases.SelectedText + ".Object.Library";
		Status = string.Format("chooseDatabase_onchange");
	}
	private string Status
	{
		set
		{
			GuiLabel labelTest = ui["labelTest"] as GuiLabel;
			labelTest.Text = value;
		}
	}
	private GuiButton btnOK
	{
		get
		{
			foreach(IGuiControl ctrl in ui)
			{
				if(ctrl.GetType().Name == "GuiButton"){
					return ctrl as GuiButton;
				}
			}
			return null;
		}
	}
	public void SetupOkButton()
	{
		Status = string.Format("checkBox_onchange");
		GuiCheckBox chkPrimary= ui["chkPrimary"] as GuiCheckBox;
		GuiCheckBox chkColumns= ui["chkColumns"] as GuiCheckBox;
		GuiCheckBox chkFK= ui["chkFK"] as GuiCheckBox;
		GuiLabel labelTest = ui["labelTest"] as GuiLabel;
		string status = "";
		if(chkPrimary.Checked)status+=", " + chkPrimary.Text;
		if(chkColumns.Checked)status+=", " + chkColumns.Text;
		if(chkFK.Checked)status+=", " + chkFK.Text;
		if(status == "")
		{
			btnOK.Enabled = false;
			Status="Status: Select CheckBox to Run";
		}
		else
		{
			GuiListBox lb = ui["chooseTables"] as GuiListBox;
			if(lb.SelectedItems.Count > 0)
			{
				btnOK.Enabled = true;
				Status = string.Format("Will process: {0} {1}",status.Substring(2),lb.SelectedItems.Count);
			}
			else
			{
				Status = "Select one or more tables to process.";
				btnOK.Enabled = false;
			}
		}
	}
	public void checkbox_onclick( GuiCheckBox control )
	{
		SetupOkButton();
	}
	public void chooseTable_onclick( GuiListBox control )
	{
		SetupOkButton();
	}
	private string GetDefault(string sName)
	{
		if( input.Contains( sName ) ) 
		{
			return input[sName].ToString();
		}
		return "";
	}
	<%#FILE MakeGuiCheckBox.cs %>

}
##|GUI_END
##|BODY_MODE Markup
##|BODY_ENGINE .Net Script
##|BODY_LANGUAGE C#
##|BODY_TAG_START <%
##|BODY_TAG_END %>
##|BODY_BEGIN
<%
public class GeneratedTemplate : DotNetScriptTemplate
{
	public GeneratedTemplate(ZeusContext context) : base(context) {}
	//---------------------------------------------------
	// Render() is where you want to write your logic    
	//---------------------------------------------------
	public override void Render()
	{
		string dbName = input["chooseDatabase"].ToString();
		ArrayList selectedTables = input["chooseTables"] as ArrayList;
		foreach(string tblName in selectedTables)
		{
			ITable tbl = MyMeta.Databases[dbName].Tables[tblName];
			if((bool)input["chkPrimary"])PrimaryKeys(tbl);
			if((bool)input["chkColumns"])Details(tbl);
			if((bool)input["chkFK"])ForeignKeys(tbl);
		}
	}
	private void PrimaryKeys(ITable tbl)
	{
		output.writeln("Table - " + tbl.Name);
		foreach(IColumn col in tbl.PrimaryKeys)
		{
			output.writeln("  Column - " + col.Name);
		}
	}
	private void Details(ITable tbl)
	{
		Details(tbl,0);
	}
	private void Details(ITable tbl, int indent)
	{
		string sPad = "".PadRight(indent * 2);
%>		<%=sPad%> Table - <%=tbl.Name%><%
		foreach(IColumn col in tbl.Columns)
		{
			%>
			<%=sPad%> Column - <%=col.Name%> <%=col.DataTypeNameComplete%><%
		}
	}
	private void ForeignKeys(ITable tbl)
	{
		output.writeln("Table - " + tbl.Name);
		foreach(IForeignKey fk in tbl.ForeignKeys)
		{
			%>
	ForeignKey - <%=fk.Name%>
		Primary
<%Details(fk.PrimaryTable,6);%>
		Foreign
<%Details(fk.ForeignTable,6);%><%
		}
	}
}
%>
##|BODY_END
