318 lines
12 KiB
C#
318 lines
12 KiB
C#
using System;
|
|
using FluentNHibernate.Cfg;
|
|
using FluentNHibernate.Cfg.Db;
|
|
using NHibernate;
|
|
using NHibernate.Cfg;
|
|
using NHibernate.Tool.hbm2ddl;
|
|
using TBF.Resources;
|
|
|
|
namespace TBF
|
|
{
|
|
/// <summary>
|
|
/// Identifies the content of a database
|
|
/// </summary>
|
|
public enum Database
|
|
{
|
|
Users,
|
|
Bench,
|
|
Procedures,
|
|
WaterMeters,
|
|
Count,
|
|
}
|
|
|
|
/// <summary>
|
|
/// Identifies the type of a database
|
|
/// </summary>
|
|
public enum DBType
|
|
{
|
|
SQLite, /// SQLite
|
|
MySql, /// MySQL database
|
|
DbTypesCount,
|
|
}
|
|
|
|
|
|
public static class FluentCommon
|
|
{
|
|
/// <summary>
|
|
/// Session factories for regular sessions.
|
|
/// </summary>
|
|
public static ISessionFactory[] SessionFactories = new ISessionFactory[(int)Database.Count];
|
|
|
|
/// <summary>
|
|
/// NHibernate session factory (to create the database session 'SessionFactory')
|
|
/// </summary>
|
|
/// <returns>A database session</returns>
|
|
static ISessionFactory CreateSessionFactory(Database database)
|
|
{
|
|
DBType dbType;
|
|
string connectionString;
|
|
|
|
switch (database)
|
|
{
|
|
default:
|
|
case Database.Users:
|
|
dbType = Program.CurrentBench.UsersDBSettings.DbType;
|
|
connectionString = Program.CurrentBench.UsersDBSettings.ConnectionString;
|
|
break;
|
|
case Database.Bench:
|
|
dbType = Program.CurrentBench.BenchDBSettings.DbType;
|
|
connectionString = Program.CurrentBench.BenchDBSettings.ConnectionString;
|
|
break;
|
|
case Database.Procedures:
|
|
dbType = Program.CurrentBench.ProceduresDBSettings.DbType;
|
|
connectionString = Program.CurrentBench.ProceduresDBSettings.ConnectionString;
|
|
break;
|
|
case Database.WaterMeters:
|
|
dbType = Program.CurrentBench.WaterMetersDBSettings.DbType;
|
|
connectionString = Program.CurrentBench.WaterMetersDBSettings.ConnectionString;
|
|
break;
|
|
}
|
|
|
|
return CreateSessionFactory(database, new DBSettings(dbType, connectionString), false);
|
|
}
|
|
|
|
/// <summary>
|
|
/// NHibernate session factory (to create the database session 'SessionFactory')
|
|
/// </summary>
|
|
/// <returns>A database session</returns>
|
|
public static ISessionFactory CreateSessionFactory(Database database, DBSettings dbSettings, bool createDB)
|
|
{
|
|
try
|
|
{
|
|
FluentConfiguration cfg = Fluently.Configure();
|
|
|
|
switch (dbSettings.DbType)
|
|
{
|
|
default:
|
|
case DBType.SQLite:
|
|
cfg = cfg.Database(SQLiteConfiguration.Standard.UsingFile(dbSettings.ConnectionString));
|
|
break;
|
|
case DBType.MySql:
|
|
cfg = cfg.Database(MySQLConfiguration.Standard.ConnectionString(dbSettings.ConnectionString));
|
|
break;
|
|
}
|
|
|
|
switch (database)
|
|
{
|
|
default:
|
|
case Database.Users:
|
|
cfg = cfg.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Program>());
|
|
break;
|
|
case Database.Bench:
|
|
cfg = cfg.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Program>());
|
|
break;
|
|
case Database.Procedures:
|
|
cfg = cfg.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Program>());
|
|
break;
|
|
case Database.WaterMeters:
|
|
cfg = cfg.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Program>());
|
|
break;
|
|
}
|
|
|
|
if (createDB)
|
|
{
|
|
return cfg.ExposeConfiguration(BuildSchemaCreate).BuildSessionFactory();
|
|
}
|
|
else
|
|
{
|
|
return cfg.ExposeConfiguration(BuildSchema).BuildSessionFactory();
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public delegate void BuildSchemaDlgt(Configuration config);
|
|
|
|
static void BuildSchema(Configuration config)
|
|
{
|
|
/// This NHibernate tool takes a configuration (with mapping info in)
|
|
/// and exports a database schema from it
|
|
new SchemaExport(config).SetOutputFile("db_schema");
|
|
}
|
|
|
|
static void BuildSchemaCreate(Configuration config)
|
|
{
|
|
/// This NHibernate tool takes a configuration (with mapping info in)
|
|
/// and exports a database schema from it
|
|
new SchemaExport(config).Create(true, true);
|
|
}
|
|
|
|
/// Create a NHibernate session for the given database
|
|
public static ISession CreateSession(Database database)
|
|
{
|
|
int ix = (int)database;
|
|
if (ix < 0 || ix >= (int)Database.Count) return null;
|
|
|
|
if (SessionFactories[ix] == null) SessionFactories[ix] = CreateSessionFactory(database);
|
|
|
|
return SessionFactories[ix].OpenSession();
|
|
}
|
|
|
|
public static void SaveToDb(Database database, object obj)
|
|
{
|
|
ISession session = FluentCommon.CreateSession(database);
|
|
SaveToDb(session, obj);
|
|
}
|
|
|
|
public static void SaveToDb(ISession session, object obj)
|
|
{
|
|
using (var transaction = session.BeginTransaction())
|
|
{
|
|
session.SaveOrUpdate(obj);
|
|
try { transaction.Commit(); }
|
|
catch { }
|
|
}
|
|
}
|
|
|
|
public static void DeleteFromDb(Database database, object obj)
|
|
{
|
|
ISession session = FluentCommon.CreateSession(database);
|
|
SaveToDb(session, obj);
|
|
}
|
|
|
|
public static void DeleteFromDb(ISession session, object obj)
|
|
{
|
|
using (var transaction = session.BeginTransaction())
|
|
{
|
|
session.Delete(obj);
|
|
transaction.Commit();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create an empty users database.
|
|
/// Database contains only the user 'admin' and the control board component 'CB'.
|
|
/// </summary>
|
|
/// <param name="dbType">DBType.SQLite or DBType.MySql</param>
|
|
/// <param name="connectionString">Connection string</param>
|
|
/// <returns>true=success, false=error</returns>
|
|
public static bool CreateEmptyUsersDB(DBSettings dbSettings)
|
|
{
|
|
ISessionFactory sessionFactory = CreateSessionFactory(Database.Users, dbSettings, true);
|
|
if (sessionFactory == null) return false;
|
|
|
|
/// Populate the database
|
|
using (var session = sessionFactory.OpenSession())
|
|
{
|
|
using (var transaction = session.BeginTransaction())
|
|
{
|
|
///
|
|
/// Prepare all groups
|
|
///
|
|
var testers = new Entities.Group { Gid = (int)TBF.Grp.GID.Testers, Name = Strings.Tester };
|
|
var testingSpecialists = new Entities.Group { Gid = (int)TBF.Grp.GID.TestingSpecialists, Name = Strings.Testing_Specialist };
|
|
var headOfLab = new Entities.Group { Gid = (int)TBF.Grp.GID.HeadOfLab, Name = Strings.Head_of_Lab };
|
|
var maintenanceSpecialists = new Entities.Group { Gid = (int)TBF.Grp.GID.MaintenanceSpecialists, Name = Strings.Maintenance_Specialist };
|
|
var metrologists = new Entities.Group { Gid = (int)TBF.Grp.GID.Metrologists, Name = Strings.Metrologist };
|
|
var calibrationSpecialists = new Entities.Group { Gid = (int)TBF.Grp.GID.CalibrationSpecialists, Name = Strings.Calibration_Specialist };
|
|
var administrators = new Entities.Group { Gid = (int)TBF.Grp.GID.Administrators, Name = Strings.Administrator };
|
|
|
|
/// Create the user 'admin' add his groups
|
|
var admin = new Entities.User
|
|
{
|
|
Name = Program.AdminUsername,
|
|
Description = "Administrator",
|
|
LastPwChange = DateTime.Now
|
|
};
|
|
admin.SetPassword(Program.AdminPassword);
|
|
admin.AddGroup(testers);
|
|
admin.AddGroup(testingSpecialists);
|
|
admin.AddGroup(headOfLab);
|
|
admin.AddGroup(maintenanceSpecialists);
|
|
admin.AddGroup(metrologists);
|
|
admin.AddGroup(calibrationSpecialists);
|
|
admin.AddGroup(administrators);
|
|
|
|
session.SaveOrUpdate(admin);
|
|
|
|
/// Create the control board component
|
|
var cmpnt = (new BenchControl.Elde.ControlBoardCfg(new BenchControl.Elde.ControlBoardFactory())).CreateDbEntity();
|
|
session.SaveOrUpdate(cmpnt);
|
|
|
|
transaction.Commit();
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create an empty bench database
|
|
/// </summary>
|
|
/// <param name="dbType">DBType.SQLite or DBType.MySql</param>
|
|
/// <param name="connectionString">Connection string</param>
|
|
/// <returns>true=success, false=error</returns>
|
|
public static bool CreateEmptyBenchDB(DBSettings dbSettings)
|
|
{
|
|
ISessionFactory sessionFactory = CreateSessionFactory(Database.Users, dbSettings, true);
|
|
if (sessionFactory == null) return false;
|
|
|
|
/// Populate the database
|
|
//using (var session = sessionFactory.OpenSession())
|
|
//{
|
|
// using (var transaction = session.BeginTransaction())
|
|
// {
|
|
// /// Create one component
|
|
// var cmpnt = (new BenchControl.Elde.ControlBoardCfg("CB", 1)).CreateDbEntity();
|
|
// session.SaveOrUpdate(cmpnt);
|
|
// transaction.Commit();
|
|
// }
|
|
//}
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create an empty procedures database
|
|
/// </summary>
|
|
/// <param name="dbType">DBType.SQLite or DBType.MySql</param>
|
|
/// <param name="connectionString">Connection string</param>
|
|
/// <returns>true=success, false=error</returns>
|
|
public static bool CreateEmptyProceduresDB(DBSettings dbSettings)
|
|
{
|
|
ISessionFactory sessionFactory = CreateSessionFactory(Database.Users, dbSettings, true);
|
|
if (sessionFactory == null) return false;
|
|
|
|
/// Populate the database
|
|
//using (var session = sessionFactory.OpenSession())
|
|
//{
|
|
// using (var transaction = session.BeginTransaction())
|
|
// {
|
|
// /// Create one component
|
|
// var cmpnt = (new BenchControl.Elde.ControlBoardCfg("CB", 1)).CreateDbEntity();
|
|
// session.SaveOrUpdate(cmpnt);
|
|
// transaction.Commit();
|
|
// }
|
|
//}
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create an empty water meters database
|
|
/// </summary>
|
|
/// <param name="dbType">DBType.SQLite or DBType.MySql</param>
|
|
/// <param name="connectionString">Connection string</param>
|
|
/// <returns>true=success, false=error</returns>
|
|
public static bool CreateEmptyWaterMetersDB(DBSettings dbSettings)
|
|
{
|
|
ISessionFactory sessionFactory = CreateSessionFactory(Database.Users, dbSettings, true);
|
|
if (sessionFactory == null) return false;
|
|
|
|
/// Populate the database
|
|
//using (var session = sessionFactory.OpenSession())
|
|
//{
|
|
// using (var transaction = session.BeginTransaction())
|
|
// {
|
|
// /// Create one component
|
|
// var cmpnt = (new BenchControl.Elde.ControlBoardCfg("CB", 1)).CreateDbEntity();
|
|
// session.SaveOrUpdate(cmpnt);
|
|
// transaction.Commit();
|
|
// }
|
|
//}
|
|
return true;
|
|
}
|
|
}
|
|
}
|