2015-04-15 18:32:56 +00:00
|
|
|
|
///
|
|
|
|
|
|
/// Copyright (c) 2013-2015 Sensus Metering Systems
|
|
|
|
|
|
///
|
|
|
|
|
|
using System;
|
2015-12-06 19:24:22 +00:00
|
|
|
|
using System.Windows.Forms;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
using FluentNHibernate.Cfg;
|
|
|
|
|
|
using FluentNHibernate.Cfg.Db;
|
|
|
|
|
|
using NHibernate;
|
|
|
|
|
|
using NHibernate.Cfg;
|
|
|
|
|
|
using NHibernate.Tool.hbm2ddl;
|
2015-12-04 16:17:20 +00:00
|
|
|
|
using Config.Resources;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
2015-12-04 16:17:20 +00:00
|
|
|
|
namespace Config
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
|
|
|
|
|
public static class FluentCommon
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Session factories for regular sessions.
|
|
|
|
|
|
/// </summary>
|
2017-03-16 20:53:12 +00:00
|
|
|
|
public static ISessionFactory[] SessionFactories = new ISessionFactory[(int)Users.Entities.DBKind.Count];
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// NHibernate session factory (to create the database session 'SessionFactory')
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>A database session</returns>
|
2017-03-16 20:53:12 +00:00
|
|
|
|
static ISessionFactory CreateSessionFactory(Users.Entities.DBKind database)
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
2017-03-16 20:53:12 +00:00
|
|
|
|
Users.Entities.DBType dbType;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
string connectionString;
|
|
|
|
|
|
|
|
|
|
|
|
switch (database)
|
|
|
|
|
|
{
|
|
|
|
|
|
default:
|
2017-03-16 20:53:12 +00:00
|
|
|
|
case Users.Entities.DBKind.Config:
|
2015-12-04 16:17:20 +00:00
|
|
|
|
dbType = Data.CurrentBench.ProceduresDBSettings.DbType;
|
|
|
|
|
|
connectionString = Data.CurrentBench.ProceduresDBSettings.ConnectionString;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
break;
|
2017-03-16 20:53:12 +00:00
|
|
|
|
case Users.Entities.DBKind.Results:
|
2015-12-04 16:17:20 +00:00
|
|
|
|
dbType = Data.CurrentBench.WaterMetersDBSettings.DbType;
|
|
|
|
|
|
connectionString = Data.CurrentBench.WaterMetersDBSettings.ConnectionString;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-16 20:53:12 +00:00
|
|
|
|
return CreateSessionFactory(database, dbType, connectionString, false);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// NHibernate session factory (to create the database session 'SessionFactory')
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>A database session</returns>
|
2017-03-16 20:53:12 +00:00
|
|
|
|
public static ISessionFactory CreateSessionFactory(Users.Entities.DBKind database, Users.Entities.DBType dbType, string connectionString, bool createDB)
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
FluentConfiguration cfg = Fluently.Configure();
|
|
|
|
|
|
|
2017-03-16 20:53:12 +00:00
|
|
|
|
switch (dbType)
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
|
|
|
|
|
default:
|
2017-03-16 20:53:12 +00:00
|
|
|
|
case Users.Entities.DBType.SQLite:
|
|
|
|
|
|
cfg = cfg.Database(SQLiteConfiguration.Standard.UsingFile(connectionString));
|
2014-07-28 07:54:35 +00:00
|
|
|
|
break;
|
2017-03-16 20:53:12 +00:00
|
|
|
|
case Users.Entities.DBType.MySql:
|
|
|
|
|
|
cfg = cfg.Database(MySQLConfiguration.Standard.ConnectionString(connectionString));
|
2014-07-28 07:54:35 +00:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
switch (database)
|
|
|
|
|
|
{
|
|
|
|
|
|
default:
|
2017-03-16 20:53:12 +00:00
|
|
|
|
case Users.Entities.DBKind.Config:
|
2015-12-04 16:17:20 +00:00
|
|
|
|
cfg = cfg.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Data>());
|
2014-07-28 07:54:35 +00:00
|
|
|
|
break;
|
2017-03-16 20:53:12 +00:00
|
|
|
|
case Users.Entities.DBKind.Results:
|
2015-12-04 16:17:20 +00:00
|
|
|
|
cfg = cfg.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Data>());
|
2014-07-28 07:54:35 +00:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (createDB)
|
|
|
|
|
|
{
|
|
|
|
|
|
return cfg.ExposeConfiguration(BuildSchemaCreate).BuildSessionFactory();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
return cfg.ExposeConfiguration(BuildSchema).BuildSessionFactory();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2015-12-06 19:24:22 +00:00
|
|
|
|
catch (Exception exc)
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
2016-05-28 11:40:58 +00:00
|
|
|
|
MessageBox.Show(string.Format(Strings.Cannot_open_DB_Cause_0, exc.Message),
|
|
|
|
|
|
Strings.Error,
|
|
|
|
|
|
MessageBoxButtons.OK,
|
|
|
|
|
|
MessageBoxIcon.Error);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
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
|
2017-03-16 20:53:12 +00:00
|
|
|
|
public static ISession CreateSession(Users.Entities.DBKind database)
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
|
|
|
|
|
int ix = (int)database;
|
2017-03-16 20:53:12 +00:00
|
|
|
|
if (ix < 0 || ix >= (int)Users.Entities.DBKind.Count) return null;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
|
|
if (SessionFactories[ix] == null) SessionFactories[ix] = CreateSessionFactory(database);
|
|
|
|
|
|
|
|
|
|
|
|
return SessionFactories[ix].OpenSession();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-16 20:53:12 +00:00
|
|
|
|
public static void SaveToDb(Users.Entities.DBKind database, object obj)
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
2015-12-31 15:42:49 +00:00
|
|
|
|
SaveToDb(CreateSession(database), obj);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void SaveToDb(ISession session, object obj)
|
|
|
|
|
|
{
|
|
|
|
|
|
using (var transaction = session.BeginTransaction())
|
|
|
|
|
|
{
|
2017-06-08 20:54:13 +00:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
session.SaveOrUpdate(obj);
|
|
|
|
|
|
transaction.Commit();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
|
}
|
2014-07-28 07:54:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-16 20:53:12 +00:00
|
|
|
|
public static void DeleteFromDb(Users.Entities.DBKind database, object obj)
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
2015-12-31 15:42:49 +00:00
|
|
|
|
DeleteFromDb(CreateSession(database), obj);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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>
|
2017-03-16 20:53:12 +00:00
|
|
|
|
public static bool CreateEmptyConfigDB(Users.Entities.DBType dbType, string connectionString)
|
2015-12-31 21:48:22 +00:00
|
|
|
|
{
|
2017-03-16 20:53:12 +00:00
|
|
|
|
ISessionFactory sessionFactory = CreateSessionFactory(Users.Entities.DBKind.Config, dbType, connectionString, true);
|
2015-12-31 21:48:22 +00:00
|
|
|
|
if (sessionFactory == null) return false;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
|
|
/// Populate the database
|
|
|
|
|
|
using (var session = sessionFactory.OpenSession())
|
|
|
|
|
|
{
|
|
|
|
|
|
using (var transaction = session.BeginTransaction())
|
|
|
|
|
|
{
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Prepare all groups
|
|
|
|
|
|
///
|
2017-05-01 09:16:23 +00:00
|
|
|
|
var testers = new Config.Entities.Group((int)Users.Grp.GID.Testers, Strings.Tester);
|
|
|
|
|
|
var testingSpecialists = new Config.Entities.Group((int)Users.Grp.GID.TestingSpecialists, Strings.Testing_Specialist);
|
|
|
|
|
|
var headOfLab = new Config.Entities.Group((int)Users.Grp.GID.HeadOfLab, Strings.Head_of_Lab);
|
|
|
|
|
|
var maintenanceSpecialists = new Config.Entities.Group((int)Users.Grp.GID.MaintenanceSpecialists, Strings.Maintenance_Specialist);
|
|
|
|
|
|
var metrologists = new Config.Entities.Group((int)Users.Grp.GID.Metrologists, Strings.Metrologist);
|
|
|
|
|
|
var calibrationSpecialists = new Config.Entities.Group((int)Users.Grp.GID.CalibrationSpecialists, Strings.Calibration_Specialist);
|
|
|
|
|
|
var administrators = new Config.Entities.Group((int)Users.Grp.GID.Administrators, Strings.Administrator);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
|
|
/// Create the user 'admin' add his groups
|
2017-05-01 09:16:23 +00:00
|
|
|
|
var admin = new Config.Entities.User
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
2016-12-21 08:22:30 +00:00
|
|
|
|
UserName = Data.AdminUsername,
|
|
|
|
|
|
FullName = Strings.Administrator,
|
2014-07-28 07:54:35 +00:00
|
|
|
|
LastPwChange = DateTime.Now
|
|
|
|
|
|
};
|
2015-12-04 16:17:20 +00:00
|
|
|
|
admin.SetPassword(Data.AdminPassword);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
admin.AddGroup(testers);
|
|
|
|
|
|
admin.AddGroup(testingSpecialists);
|
|
|
|
|
|
admin.AddGroup(headOfLab);
|
|
|
|
|
|
admin.AddGroup(maintenanceSpecialists);
|
|
|
|
|
|
admin.AddGroup(metrologists);
|
|
|
|
|
|
admin.AddGroup(calibrationSpecialists);
|
|
|
|
|
|
admin.AddGroup(administrators);
|
|
|
|
|
|
|
|
|
|
|
|
session.SaveOrUpdate(admin);
|
|
|
|
|
|
|
2015-12-04 16:17:20 +00:00
|
|
|
|
///// Create the control board component
|
|
|
|
|
|
//var cmpnt = (new BenchControl.Elde.ControlBoardCfg(new BenchControl.Elde.ControlBoardFactory())).CreateDbEntity();
|
|
|
|
|
|
//session.SaveOrUpdate(cmpnt);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
|
|
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>
|
2017-03-16 20:53:12 +00:00
|
|
|
|
public static bool CreateEmptyResultsDB(Users.Entities.DBType dbType, string connectionString)
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
2017-03-16 20:53:12 +00:00
|
|
|
|
ISessionFactory sessionFactory = CreateSessionFactory(Users.Entities.DBKind.Results, dbType, connectionString, true);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|