/* $Id: DataSetController.cs,v 1.8 2004/10/27 14:09:20 larsbm Exp $
* Copyright (c) 2004 Engine EAR GmbH & Co. KG
* Developed by: Lars Behrmann, lb@engine.de
*/
using System;
using System.Data;
using System.IO;
using System.Windows.Forms;
using SQLToNeo.Model;
namespace SQLToNeo.Controller{
/// <summary>
/// Zusammenfassung fr DataSetController.
/// </summary>
public class DataSetController
{
public string _path;
private bool _generateJavaName;
#region Property get set CurrentDataSet
private DataSet _currentDataSet;
public DataSet CurrentDataSet
{
get { return this._currentDataSet; }
set { this._currentDataSet = value; }
}
#endregion
#region Property get set EmptyDataSet
private DataSet _emptyDataSet;
public DataSet EmptyDataSet
{
get { return this._emptyDataSet; }
set { this._emptyDataSet = value; }
}
#endregion
#region Property get set GenerateJavaName
public bool GenerateJavaName
{
get
{
return _generateJavaName;
}
set
{
_generateJavaName = value;
}
}
#endregion
#region Property get set Databasename
private string _databasename;
public string Databasename
{
get { return this._databasename; }
set { this._databasename = value; }
}
#endregion
#region Property get set IsNew
private bool _isnew;
public bool IsNew
{
get { return this._isnew; }
set { this._isnew = value; }
}
#endregion
public DataSetController()
{
CurrentDataSet = new DataSet("CurrentDataSet");
EmptyDataSet = new DataSet("OnlineDataSet");
_path = Application.StartupPath +@"\"+ System.Configuration.ConfigurationManager.AppSettings["projectfolder"] +@"\";
}
public DataSetController(bool generateJavaName)
{
this._generateJavaName = generateJavaName;
CurrentDataSet = new DataSet("CurrentDataSet");
EmptyDataSet = new DataSet("OnlineDataSet");
_path = Application.StartupPath +@"\"+ System.Configuration.ConfigurationManager.AppSettings["projectfolder"] +@"\";
}
public bool LoadData(string databasename)
{
CreateEmptyDataSet();
Databasename = _path + databasename+".xml";
if(File.Exists(Databasename))
{
if(LoadExisting())
return true;
else
return false;
}
else
CreateNewSchema();
return true;
}
public void SpecialMerge()
{
MergeSpecial ms = new MergeSpecial();
ms.GenerateJavaName = _generateJavaName;
DataSet dsTemp = ms.Merge(CurrentDataSet, EmptyDataSet);
CurrentDataSet = dsTemp;
}
private bool LoadExisting()
{
try
{
CurrentDataSet.ReadXml(Databasename,XmlReadMode.ReadSchema);
CreateNeoSpecificRow();
return true;
}
catch(Exception ex)
{
Console.WriteLine("Error while reading existing DataSet from xml file");
Console.WriteLine("Error Message:\n {0}", ex.Message);
return false;
}
}
public void SaveCurrentDataSet()
{
CurrentDataSet.WriteXml(Databasename,XmlWriteMode.WriteSchema);
//CurrentDataSet.WriteXml(@"D:\temp\nunit.xml",XmlWriteMode.WriteSchema);
}
private void CreateNewSchema()
{
CurrentDataSet.Tables.Add(TableBuilder.GetTableXML());
CurrentDataSet.Tables.Add(TableBuilder.GetTableForeignKeys("iforeignkey"));
CurrentDataSet.Tables.Add(TableBuilder.GetTableForeignKeys("foreignkey"));
CurrentDataSet.Tables.Add(TableBuilder.GetTableTable());
CurrentDataSet.Tables.Add(TableBuilder.GetTableColumns());
DataRelation dl = new DataRelation("cols",CurrentDataSet.Tables["table"].Columns["id"],
CurrentDataSet.Tables["column"].Columns["tableid"], true);
CurrentDataSet.Relations.Add(dl);
dl = new DataRelation("foreignkeys",CurrentDataSet.Tables["table"].Columns["id"],
CurrentDataSet.Tables["foreignkey"].Columns["tableid"], true);
CurrentDataSet.Relations.Add(dl);
dl = new DataRelation("iforeignkeys",CurrentDataSet.Tables["table"].Columns["id"],
CurrentDataSet.Tables["iforeignkey"].Columns["tableid"], true);
CurrentDataSet.Relations.Add(dl);
foreach(DataRelation drl in CurrentDataSet.Relations)
drl.Nested = true;
CreateNeoSpecificRow();
}
private void CreateEmptyDataSet()
{
EmptyDataSet.Tables.Add(TableBuilder.GetTableXML());
EmptyDataSet.Tables.Add(TableBuilder.GetTableForeignKeys("iforeignkey"));
EmptyDataSet.Tables.Add(TableBuilder.GetTableForeignKeys("foreignkey"));
EmptyDataSet.Tables.Add(TableBuilder.GetTableTable());
EmptyDataSet.Tables.Add(TableBuilder.GetTableColumns());
DataRelation dl = new DataRelation("cols",EmptyDataSet.Tables["table"].Columns["id"],
EmptyDataSet.Tables["column"].Columns["tableid"], true);
EmptyDataSet.Relations.Add(dl);
dl = new DataRelation("foreignkeys",EmptyDataSet.Tables["table"].Columns["id"],
EmptyDataSet.Tables["foreignkey"].Columns["tableid"], true);
EmptyDataSet.Relations.Add(dl);
dl = new DataRelation("iforeignkeys",EmptyDataSet.Tables["table"].Columns["id"],
EmptyDataSet.Tables["iforeignkey"].Columns["tableid"], true);
EmptyDataSet.Relations.Add(dl);
foreach(DataRelation drl in EmptyDataSet.Relations)
drl.Nested = true;
}
private void CreateNeoSpecificRow()
{
if(CurrentDataSet.Tables["xml"].Rows.Count==0)
{
DataRow dr = CurrentDataSet.Tables["xml"].NewRow();
CurrentDataSet.Tables["xml"].Rows.Add(dr);
}
}
}
}
/*
* $Log: DataSetController.cs,v $
* 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/25 13:47:15 larsbm
* - Add new classes MergeSpecial and MergSpecialTest
* - Merging the current local model with the current db schema on the server
*
* Revision 1.5 2004/10/21 15:39:26 larsbm
* - Add Test for method SpecialMerge(), don't run!
*
* 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
*
*
*/
|