using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using TdoCodeGenerator.Generator;
using TdoCodeGenerator.TdoGeneratorDom;
namespace TdoCodeGenerator{
public partial class frmNewTdoProject : Form
{
#region Fields
private static string[] servers;
private static string[] databases;
private TdoProjectClass fTdoProject;
#endregion Fields
#region Constructors
public frmNewTdoProject()
{
InitializeComponent();
this.TdoProject = null;
}
#endregion Constructors
#region Properties
public TdoProjectClass TdoProject
{
get
{
return this.fTdoProject;
}
set
{
this.fTdoProject = value;
if (this.fTdoProject == null)
{
//New
this.fTdoProject = new TdoProjectClass();
this.fTdoProject.Settings.Source.Server = TdoCodeGenerator.Properties.Settings.Default.DefaultSQLServer;
this.fTdoProject.Settings.Source.DataBase = String.Empty;
this.fTdoProject.Settings.Source.IntegratedSecurity = true;
this.fTdoProject.Settings.Destination.BaseNameSpace = "Tdo.DatabaseName";
this.fTdoProject.Settings.Destination.CreateOneFileForClass = false;
this.fTdoProject.Settings.Destination.Language = XmlLanguage.CSharpNet;
this.fTdoProject.Name = "DatabaseName on ServerName" + " (" + (this.fTdoProject.Settings.Destination.Language==XmlLanguage.CSharpNet ? "C#": "Vb") + ")";
this.fTdoProject.Settings.Destination.OutputFolder = String.Empty;
this.fTdoProject.Settings.Destination.XmlRootNameSpace = "http://tempuri.org/Tdo/DatabaseName";
this.fTdoProject.Settings.Destination.GenerationMode = GenerationModeEnum.All;
}
this.cmbDataSources.Text = this.fTdoProject.Settings.Source.Server;
this.rbSQLServerAuthentication.Checked = !this.fTdoProject.Settings.Source.IntegratedSecurity;
this.rbWindowsAuthentication.Checked = this.fTdoProject.Settings.Source.IntegratedSecurity;
if (!this.fTdoProject.Settings.Source.IntegratedSecurity)
{
this.txtUsername.Text = this.fTdoProject.Settings.Source.UserName;
this.txtPassword.Text = this.fTdoProject.Settings.Source.Password;
}
try
{
//this.cmbDatabase_DropDown(this, EventArgs.Empty);
if (!String.IsNullOrEmpty(this.fTdoProject.Settings.Source.DataBase))
{
this.cmbDatabase.Items.Add(this.fTdoProject.Settings.Source.DataBase);
this.cmbDatabase.Text = this.fTdoProject.Settings.Source.DataBase;
}
}
catch
{
MessageBox.Show("Database not found", "Database not found", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
}
this.txtRootNamespace.Text = this.fTdoProject.Settings.Destination.BaseNameSpace;
this.rbOneFile.Checked = !this.fTdoProject.Settings.Destination.CreateOneFileForClass;
this.rbNFiles.Checked = this.fTdoProject.Settings.Destination.CreateOneFileForClass;
this.rbCSharp.Checked = (this.fTdoProject.Settings.Destination.Language == XmlLanguage.CSharpNet ? true : false);
this.rbVBNET.Checked = (this.fTdoProject.Settings.Destination.Language == XmlLanguage.VbNet ? true : false);
this.txtOutputFolder.Text = this.fTdoProject.Settings.Destination.OutputFolder;
this.txtXmlRootNamespace.Text = this.fTdoProject.Settings.Destination.XmlRootNameSpace;
this.rbAll.Checked= (this.fTdoProject.Settings.Destination.GenerationMode==GenerationModeEnum.All ? true : false);
this.rbSelectionOnly.Checked = (this.fTdoProject.Settings.Destination.GenerationMode == GenerationModeEnum.SelectionOnly ? true : false);
this.txtTdoProjectName.Text = this.fTdoProject.Name;
this.Enabled = true;
}
}
#endregion Properties
#region Methods
private void RefreshDataSources()
{
try
{
this.Cursor = Cursors.WaitCursor;
this.cmbDataSources.Items.Clear();
string[] servers = TdoDiscovery.GetSqlDataSources();
frmNewTdoProject.servers = servers;
foreach (string server in servers)
{
this.cmbDataSources.Items.Add(server);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
}
finally
{
this.Cursor = Cursors.Arrow;
}
}
private void RefreshDatabases()
{
try
{
string currentdb = this.cmbDatabase.Text;
this.Cursor = Cursors.WaitCursor;
this.cmbDatabase.Items.Clear();
this.fTdoProject.Settings.Source.DataBase = String.Empty;
string[] databases = TdoDiscovery.GetDatabases(this.fTdoProject.Settings.Source.ConnectionString,TdoCodeGenerator.Properties.Settings.Default.SQLServerConnectionTimeOut);
frmNewTdoProject.databases = servers;
foreach (string database in databases)
{
this.cmbDatabase.Items.Add(database);
}
this.cmbDatabase.Text = currentdb;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
}
finally
{
this.Cursor = Cursors.Arrow;
}
}
#endregion Methods
#region Event Handlers
private void btnRefresh_Click(object sender, EventArgs e)
{
this.RefreshDataSources();
}
private void cmdDataSources_DropDown(object sender, EventArgs e)
{
if (this.cmbDataSources.Items==null || this.cmbDataSources.Items.Count == 0)
{
if (frmNewTdoProject.servers==null)
this.RefreshDataSources();
}
}
private void frmDataSource_Load(object sender, EventArgs e)
{
if (frmNewTdoProject.servers != null)
{
foreach (string server in frmNewTdoProject.servers)
{
this.cmbDataSources.Items.Add(server);
}
}
}
private void cmbDatabase_DropDown(object sender, EventArgs e)
{
if (this.cmbDatabase.Items == null || this.cmbDatabase.Items.Count <= 1)
{
this.RefreshDatabases();
}
}
private void btnRefreshDatabases_Click(object sender, EventArgs e)
{
this.RefreshDatabases();
}
private void rbSQLServerAuthentication_CheckedChanged(object sender, EventArgs e)
{
if (this.rbSQLServerAuthentication.Checked)
{
this.TdoProject.Settings.Source.IntegratedSecurity = false;
this.cmbDatabase.Items.Clear();
this.txtUsername.Enabled = true;
this.txtPassword.Enabled = true;
}
else
{
this.TdoProject.Settings.Source.IntegratedSecurity = true;
this.cmbDatabase.Items.Clear();
this.txtUsername.Enabled = false;
this.txtPassword.Enabled = false;
}
}
private string RemoveInvalidCharacters(string filename)
{
string result = filename;
//Remove Invalid File Characters
if (this.txtTdoProjectName.Text.IndexOfAny(Path.GetInvalidFileNameChars()) != -1)
{
foreach (char c in Path.GetInvalidFileNameChars())
{
result.Replace(c.ToString(), String.Empty);
}
}
return result;
}
private void cmbDataSources_TextChanged(object sender, EventArgs e)
{
frmNewTdoProject.databases = null;
this.fTdoProject.Settings.Source.Server = this.cmbDataSources.Text;
this.cmbDatabase.Items.Clear();
if (!String.IsNullOrEmpty(this.cmbDataSources.Text))
{
this.btnRefreshDatabases.Enabled = true;
this.cmbDatabase.Enabled = true;
string lang = (this.rbCSharp.Checked ? "C#" : "Vb");
if (String.IsNullOrEmpty(this.fTdoProject.Path))
this.txtTdoProjectName.Text = this.RemoveInvalidCharacters("DatabaseName on " + this.cmbDataSources.Text.Replace('\\','-') + " (" + lang + ")");
}
else
{
this.btnRefreshDatabases.Enabled = false;
this.cmbDatabase.Enabled = false;
string lang = (this.rbCSharp.Checked ? "C#" : "Vb");
if (String.IsNullOrEmpty(this.fTdoProject.Path))
this.txtTdoProjectName.Text = this.RemoveInvalidCharacters("DatabaseName on ServerName" + " (" + lang + ")");
}
this.ValidateForm();
}
private void cmbDatabase_TextChanged(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(this.cmbDatabase.Text))
{
if (String.IsNullOrEmpty(this.fTdoProject.Path))
this.txtRootNamespace.Text = "Tdo." + TdoSourceCodeGenerator.TransformToVariable("",this.cmbDatabase.Text,false);
if (String.IsNullOrEmpty(this.fTdoProject.Path))
this.txtXmlRootNamespace.Text = "http://tempuri.org/Tdo/" + TdoSourceCodeGenerator.TransformToVariable("", this.cmbDatabase.Text, false);
string lang = (this.rbCSharp.Checked ? "C#" : "Vb");
if (String.IsNullOrEmpty(this.fTdoProject.Path))
this.txtTdoProjectName.Text = this.RemoveInvalidCharacters(this.cmbDatabase.Text + " on " + this.cmbDataSources.Text.Replace('\\', '-') + " (" + lang + ")");
}
else
{
if (String.IsNullOrEmpty(this.fTdoProject.Path))
this.txtRootNamespace.Text = "Tdo.DatabaseName";
if (String.IsNullOrEmpty(this.fTdoProject.Path))
this.txtXmlRootNamespace.Text = "http://tempuri.org/Tdo/DatabaseName";
string lang = (this.rbCSharp.Checked ? "C#" : "Vb");
if (String.IsNullOrEmpty(this.fTdoProject.Path))
this.txtTdoProjectName.Text = this.RemoveInvalidCharacters("DatabaseName on " + this.cmbDataSources.Text.Replace('\\', '-') + " (" + lang + ")");
}
this.ValidateForm();
}
private void btnOutputFolder_Click(object sender, EventArgs e)
{
if (Directory.Exists(this.txtOutputFolder.Text))
{
this.folderBrowserDialog1.SelectedPath = this.txtOutputFolder.Text;
}
if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
this.txtOutputFolder.Text = this.folderBrowserDialog1.SelectedPath;
}
}
#endregion Event Handlers
private void btnCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
private void btnOk_Click(object sender, EventArgs e)
{
if (this.fTdoProject == null)
this.fTdoProject = new TdoProjectClass();
this.fTdoProject.Name = this.txtTdoProjectName.Text;
this.fTdoProject.Settings.Source.Server = this.cmbDataSources.Text;
this.fTdoProject.Settings.Source.DataBase = this.cmbDatabase.Text;
this.fTdoProject.Settings.Source.IntegratedSecurity = this.rbWindowsAuthentication.Checked;
if (!this.fTdoProject.Settings.Source.IntegratedSecurity)
{
this.fTdoProject.Settings.Source.UserName = this.txtUsername.Text;
this.fTdoProject.Settings.Source.Password = this.txtPassword.Text;
}
this.fTdoProject.Settings.Destination.BaseNameSpace = this.txtRootNamespace.Text;
this.fTdoProject.Settings.Destination.CreateOneFileForClass = this.rbNFiles.Checked;
this.fTdoProject.Settings.Destination.Language = (this.rbCSharp.Checked ? XmlLanguage.CSharpNet : XmlLanguage.VbNet);
this.fTdoProject.Settings.Destination.OutputFolder = this.txtOutputFolder.Text;
this.fTdoProject.Settings.Destination.XmlRootNameSpace = this.txtXmlRootNamespace.Text;
this.fTdoProject.Settings.Destination.GenerationMode = (this.rbAll.Checked ? GenerationModeEnum.All : GenerationModeEnum.SelectionOnly);
this.DialogResult = DialogResult.OK;
this.Close();
}
private bool ValidateTdoRootNamespace(string ns)
{
return System.Text.RegularExpressions.Regex.Match(ns, @"^(?:(?:((?![0-9_])[a-zA-Z0-9_]+)\.?)+)(?<!\.)$").Success;
}
private bool ValidateFileName(string filename)
{
return new System.Text.RegularExpressions.Regex(@"^[^\\\./:\*\?\""<>\|]{1}[^\\/:\*\?\""<>\|]{0,254}$").Match(filename).Success;
}
private bool ValidateForm()
{
bool isValid=true;
this.errorProvider1.Clear();
if (String.IsNullOrEmpty(this.cmbDataSources.Text))
{
isValid = false;
this.errorProvider1.SetError(this.cmbDataSources, "Invalid SQL Server.");
}
if (String.IsNullOrEmpty(this.cmbDatabase.Text))
{
isValid = false;
this.errorProvider1.SetError(this.cmbDatabase, "Invalid SQL Server Database.");
}
if (String.IsNullOrEmpty(this.txtTdoProjectName.Text.Trim()) || !this.ValidateFileName(this.txtTdoProjectName.Text))
{
isValid = false;
this.errorProvider1.SetError(this.txtTdoProjectName, "Invalid Tdo Project Name.");
}
if (String.IsNullOrEmpty(this.txtRootNamespace.Text) || !this.ValidateTdoRootNamespace(this.txtRootNamespace.Text))
{
isValid = false;
this.errorProvider1.SetError(this.txtRootNamespace, "Invalid Tdo root namespace.");
}
if (String.IsNullOrEmpty(this.txtXmlRootNamespace.Text))
{
isValid = false;
this.errorProvider1.SetError(this.txtXmlRootNamespace, "Invalid Xml root namespace.");
}
if (String.IsNullOrEmpty(this.txtOutputFolder.Text))
{
this.errorProvider1.SetError(this.txtOutputFolder, "Output folder require.");
}
if (isValid)
{
isValid = Directory.Exists(this.txtOutputFolder.Text);
if (!isValid)
{
this.errorProvider1.SetError(this.txtOutputFolder, "Output folder not found.");
}
}
this.btnOk.Enabled = isValid;
return isValid;
}
private void txtTdoProjectName_TextChanged(object sender, EventArgs e)
{
this.ValidateForm();
}
private void txtRootNamespace_TextChanged(object sender, EventArgs e)
{
this.ValidateForm();
}
private void txtXmlRootNamespace_TextChanged(object sender, EventArgs e)
{
this.ValidateForm();
}
private void txtOutputFolder_TextChanged(object sender, EventArgs e)
{
this.toolTip1.SetToolTip(this.txtOutputFolder, this.txtOutputFolder.Text);
this.ValidateForm();
}
private void cmbLanguage_TextChanged(object sender, EventArgs e)
{
string server = "DataSource";
string database = "DatabaseName";
if (!String.IsNullOrEmpty(this.cmbDataSources.Text)) server = this.cmbDataSources.Text.Replace("\\","-");
if (!String.IsNullOrEmpty(this.cmbDatabase.Text)) database = this.cmbDatabase.Text;
string lang = (this.rbCSharp.Checked ? "C#" : "Vb");
this.txtTdoProjectName.Text = this.RemoveInvalidCharacters(database+ " on " + server + " (" + lang + ")");
}
private void txtUsername_TextChanged(object sender, EventArgs e)
{
this.TdoProject.Settings.Source.UserName = this.txtUsername.Text;
this.cmbDatabase.Items.Clear();
}
private void txtPassword_TextChanged(object sender, EventArgs e)
{
this.TdoProject.Settings.Source.Password = this.txtPassword.Text;
this.cmbDatabase.Items.Clear();
}
private void frmNewTdoProject_Activated(object sender, EventArgs e)
{
this.ValidateForm();
}
}
}
|