/* $Id: NewMainFormController.cs,v 1.18 2004/11/27 07:09:42 larsbm Exp $
* Copyright (c) 2004 Engine EAR GmbH & Co. KG
* Developed by: Lars Behrmann, lb@engine.de
*/
using System;
using System.IO;
using System.Data;
using System.Collections;
using System.Windows.Forms;
using SQLToNeo.Model;
using SQLToNeo.Gui;
using SQLToNeo.PlugIn;
namespace SQLToNeo.Controller{
/// <summary>
/// Zusammenfassung fr NewMainFormController.
/// </summary>
public class NewMainFormController
{
private bool ModelHasChanges;
private CurrencyManager cm;
private NewMainForm _form;
public NewMainForm Form
{
get { return this._form; }
set { this._form = value; }
}
private DataSetController _dscontroller;
public DataSetController DSController
{
get { return this._dscontroller; }
set { this._dscontroller = value; }
}
public NewMainFormController(NewMainForm frm)
{
Form = frm;
CreateProjectFolder();
}
public void LoadCatalogs()
{
OleDBServerConnector c = new OleDBServerConnector();
if(c.CreateConnectionString(Form.Server, Form.User, Form.Password, Form.NTAuthentication))
{
Cursor.Current = Cursors.WaitCursor;
Form.Catalogs.Items.Clear();
foreach(string s in c.GetExistingDatabaseNames())
Form.Catalogs.Items.Add(s);
Cursor.Current = Cursors.Default;
}
else
MessageBox.Show("Error on creating the connection string!");
}
public void SaveChanges()
{
if(ModelHasChanges)
if(Form.SaveChanges() == DialogResult.Yes)
GenerateModel();
}
public void LoadDatabase()
{
SaveChanges();
DSController = new DataSetController();
DSController.GenerateJavaName = Form.GenerateJavaName;
if(DSController.LoadData(Form.Catalogs.SelectedItem.ToString()))
{
Cursor.Current = Cursors.WaitCursor;
SetGridBinding();
Analyze();
SetCurrencyBinding();
NewRowIsForbidden();
DeleteRowIsForbidden();
CatchRowChanges();
FillNeoSpecificTextBoxes();
StatusBarText();
ModelHasChanges = false;
Cursor.Current = Cursors.Default;
}
else
MessageBox.Show("Error while loading data schema!");
}
public void Analyze()
{
Anaylzer a = new Anaylzer((IAnalyzer)Activator.CreateInstance(typeof(MSSQLAnalyzer)));
a.Instance.Catalog = Form.Catalogs.SelectedItem.ToString();
a.Instance.NTAuthentication = Form.NTAuthentication;
a.Instance.User = Form.User;
a.Instance.Password = Form.Password;
a.Instance.Server = Form.Server;
a.Instance.AnalyzeAndFill(DSController.EmptyDataSet);
//Do the special merge :)
DSController.SpecialMerge();
}
private void SetCurrencyBinding()
{
cm = (CurrencyManager)Form.Table.BindingContext[Form.Table.DataSource];
cm.PositionChanged +=new EventHandler(cm_PositionChanged);
cm_PositionChanged(null,null);
}
private void CatchRowChanges()
{
if(DSController != null)
{
((DataTable)Form.Table.DataSource).RowChanged +=new DataRowChangeEventHandler(NewMainFormController_RowChanged);
((DataTable)Form.Column.DataSource).RowChanged +=new DataRowChangeEventHandler(NewMainFormController_RowChanged1);
((DataTable)Form.IForeignkey.DataSource).RowChanged +=new DataRowChangeEventHandler(NewMainFormController_RowChanged2);
((DataTable)Form.ForeignKey.DataSource).RowChanged +=new DataRowChangeEventHandler(NewMainFormController_RowChanged3);
}
}
public void GenerateModel()
{
if(DSController != null)
{
ReadNeoSpecificTextBoxes();
DSController.SaveCurrentDataSet();
Writer writer = new Writer((IWriter)Activator.CreateInstance(typeof(NeoXMLModelWriter)));
writer.Instance.GenerateJavaName = Form.GenerateJavaName;
if(writer.Instance.Write(DSController.CurrentDataSet))
{
MessageBox.Show("Model built and saved!");
ModelHasChanges = false;
}
else
MessageBox.Show("Error while building and saving the model!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
Form.NoModelLoaded();
}
public void CheckforConflicts()
{
if(DSController != null)
{
DSController.CurrentDataSet.AcceptChanges();
ConflictForm cf = new ConflictForm();
RestrictionChecker rc = new RestrictionChecker();
rc.Validate(DSController.CurrentDataSet);
cf.AddConflicts(rc.Conflicts);
cf.ShowDialog();
}
else
Form.NoModelLoaded();
}
private void cm_PositionChanged(object sender, EventArgs e)
{
if (cm.Count == 0)
return;
DataRowView dr = (DataRowView)cm.Current;
int currentTableId = Convert.ToInt32(dr["id"].ToString());
((DataTable)Form.Column.DataSource).DefaultView.RowFilter = "tableid = '"+currentTableId.ToString()+"'";
((DataTable)Form.IForeignkey.DataSource).DefaultView.RowFilter = "tableid = '"+currentTableId.ToString()+"'";
((DataTable)Form.ForeignKey.DataSource).DefaultView.RowFilter = "tableid = '"+currentTableId.ToString()+"'";
SetGridCaptions(dr["javaName"].ToString());
}
private void NewRowIsForbidden()
{
((DataTable)Form.Table.DataSource).DefaultView.AllowNew = false;
((DataTable)Form.Column.DataSource).DefaultView.AllowNew = false;
((DataTable)Form.IForeignkey.DataSource).DefaultView.AllowNew = false;
((DataTable)Form.ForeignKey.DataSource).DefaultView.AllowNew = false;
}
private void DeleteRowIsForbidden()
{
((DataTable)Form.Table.DataSource).DefaultView.AllowDelete = false;
((DataTable)Form.Column.DataSource).DefaultView.AllowDelete = false;
((DataTable)Form.IForeignkey.DataSource).DefaultView.AllowDelete = false;
((DataTable)Form.ForeignKey.DataSource).DefaultView.AllowDelete = false;
}
private void SetGridCaptions(string tablename)
{
Form.Column.CaptionText = tablename + ": Columns";
Form.ForeignKey.CaptionText = tablename + ": Foreignkeys";
Form.IForeignkey.CaptionText = tablename + ": IForeignkeys";
}
private void SetGridBinding()
{
Form.Table.DataSource = DSController.CurrentDataSet.Tables["table"];
Form.Column.DataSource = DSController.CurrentDataSet.Tables["column"];
Form.IForeignkey.DataSource = DSController.CurrentDataSet.Tables["iforeignkey"];
Form.ForeignKey.DataSource = DSController.CurrentDataSet.Tables["foreignkey"];
}
private void FillNeoSpecificTextBoxes()
{
Console.WriteLine(DSController.CurrentDataSet.Tables["xml"].Rows[0].ItemArray);
Form.NorquePathFile.Text = DSController.CurrentDataSet.Tables["xml"].Rows[0]["norquedtd"].ToString();
Form.NeoDbName.Text = DSController.CurrentDataSet.Tables["xml"].Rows[0]["neodbname"].ToString();
Form.PackageName.Text = DSController.CurrentDataSet.Tables["xml"].Rows[0]["packagename"].ToString();
Form.NeoPath.Text = DSController.CurrentDataSet.Tables["xml"].Rows[0]["neopath"].ToString();
Form.ModelPathFile.Text = DSController.CurrentDataSet.Tables["xml"].Rows[0]["modelpathfile"].ToString();
Form.IDMethod.SelectedItem = DSController.CurrentDataSet.Tables["xml"].Rows[0]["idmethod"].ToString();
Form.JavaNamingMethod.SelectedItem = DSController.CurrentDataSet.Tables["xml"].Rows[0]["javaNamingMethod"].ToString();
Form.TablePrefix.Text = DSController.CurrentDataSet.Tables["xml"].Rows[0]["tablePrefix"].ToString();
}
private void ReadNeoSpecificTextBoxes()
{
DSController.CurrentDataSet.Tables["xml"].Rows[0]["norquedtd"] = Form.NorquePathFile.Text;
DSController.CurrentDataSet.Tables["xml"].Rows[0]["neodbname"] = Form.NeoDbName.Text;
DSController.CurrentDataSet.Tables["xml"].Rows[0]["packagename"] = Form.PackageName.Text;
DSController.CurrentDataSet.Tables["xml"].Rows[0]["neopath"] = Form.NeoPath.Text;
DSController.CurrentDataSet.Tables["xml"].Rows[0]["modelpathfile"] = Form.ModelPathFile.Text;
DSController.CurrentDataSet.Tables["xml"].Rows[0]["idmethod"] = Form.IDMethod.SelectedItem;
DSController.CurrentDataSet.Tables["xml"].Rows[0]["javaNamingMethod"] = Form.JavaNamingMethod.SelectedItem;
DSController.CurrentDataSet.Tables["xml"].Rows[0]["tablePrefix"] = Form.TablePrefix.Text;
}
private void StatusBarText()
{
Form.StatusBar.Panels[0].Text = "Current Database: "+Form.Catalogs.SelectedItem.ToString();
Form.StatusBar.Panels[1].Text = GetStatistics();
}
private string GetStatistics()
{
string s = "Statistics : ";
s += "Tables " + DSController.CurrentDataSet.Tables["table"].Rows.Count.ToString();
s += ", Columns " + DSController.CurrentDataSet.Tables["column"].Rows.Count.ToString();
s += ", FK " + DSController.CurrentDataSet.Tables["foreignkey"].Rows.Count.ToString();
s += ", IFK " + DSController.CurrentDataSet.Tables["iforeignkey"].Rows.Count.ToString();
return s;
}
private void CreateProjectFolder()
{
string projectfolder = Application.StartupPath +@"\"+ System.Configuration.ConfigurationManager.AppSettings["projectfolder"];
if(!Directory.Exists(projectfolder))
{
Form.CreateProjectFolder(projectfolder);
Directory.CreateDirectory(projectfolder);
}
}
public bool ValidateCommandline()
{
if(Form.Commandline.IndexOf(";") > -1)
{
string[] args = Form.Commandline.Split(';');
if(args.Length == 5)
{
if(args[4].Equals("0") || args[4].Equals("1"))
return true;
}
}
Form.CommandLineError();
return false;
}
#region RowChanged events
private void NewMainFormController_RowChanged(object sender, DataRowChangeEventArgs e)
{
ModelHasChanges = true;
}
private void NewMainFormController_RowChanged1(object sender, DataRowChangeEventArgs e)
{
ModelHasChanges = true;
}
private void NewMainFormController_RowChanged2(object sender, DataRowChangeEventArgs e)
{
ModelHasChanges = true;
}
private void NewMainFormController_RowChanged3(object sender, DataRowChangeEventArgs e)
{
ModelHasChanges = true;
}
#endregion
}
}
/*
* $Log: NewMainFormController.cs,v $
* Revision 1.18 2004/11/27 07:09:42 larsbm
* - Bugfix NewMainFormController
* - New version 2.0.2
*
* Revision 1.17 2004/11/24 13:57:04 larsbm
* - new project SQLToNeoPlugIn
* - switched interfaces from mainsource into plug in
*
* Revision 1.16 2004/11/22 09:13:13 larsbm
* - Add commandline possibility for QueryCommander
*
* Revision 1.15 2004/11/09 09:01:51 larsbm
* - Fixed bugs in NeoXmlWriter
* - Add waitcursor while loading data
*
* Revision 1.14 2004/11/09 07:59:37 larsbm
* - Add method : CreateProjectFolder()
*
* Revision 1.13 2004/11/09 07:39:25 larsbm
* - Forgot to the reset ModelHasChanges to false, ups
*
* Revision 1.12 2004/11/09 07:33:59 larsbm
* - Deny add and delete rows
* - rember changes did by the user to the xml model
*
* Revision 1.11 2004/11/08 12:46:51 larsbm
* - Add little validation
*
* Revision 1.10 2004/11/02 14:02:38 larsbm
* - Add new property to IDataBaseConnector
* - Form for displaying possible conflicts
* - Implement Conflictform
* - Add Mainmenu to mainform
*
* Revision 1.9 2004/10/29 13:49:36 larsbm
* - work on .. last todo realize "create = false"
*
* Revision 1.8 2004/10/27 14:09:20 larsbm
* - Fixed bugs in tablebuilder, TablestyleCreator
* - Working on the forms
* - Finishing DBTypes
* - Add IWriter & Writer, NeoXMLModelWriter as abstraction for writing the xml model
*
* Revision 1.7 2004/10/26 13:23:43 larsbm
* - Fixed bugs in tablebuilder
* - Working on the forms
* - Finishing MergeSpecial
* - Make app run with mergespecial
* - start converter for sqldatatypes
*
* Revision 1.6 2004/10/21 15:39:26 larsbm
* - Add Test for method SpecialMerge(), don't run!
*
* Revision 1.5 2004/10/20 14:03:15 larsbm
* - Added support for foreign and primary keys
* - Implement SQLServerConnector (half)
* - Running into trouble with merging datasets marked as todo
*
* Revision 1.4 2004/10/18 14:13:52 larsbm
* - Add columns, foreignkeys
* - Bring Anaylzer, NewMainformController and DataSetController together
* - Several bugfixes
*
* Revision 1.3 2004/10/13 08:53:54 larsbm
* - Start to implement new anaylzer via bridge pattern
* - Adding a test for the new analyzer
*
*
*/
|