2021-09-19 20:04:16 +00:00
|
|
|
|
///
|
2023-03-28 11:03:38 +00:00
|
|
|
|
/// Copyright (c) 2021-2023 Sensus Slovensko a.s.
|
2021-09-19 20:04:16 +00:00
|
|
|
|
///
|
|
|
|
|
|
using System;
|
2023-03-28 11:03:38 +00:00
|
|
|
|
using System.Windows.Forms;
|
2021-09-19 20:04:16 +00:00
|
|
|
|
using NHibernate;
|
2023-03-28 11:03:38 +00:00
|
|
|
|
using NHibernate.Cfg;
|
|
|
|
|
|
using NHibernate.Tool.hbm2ddl;
|
|
|
|
|
|
using FluentNHibernate.Cfg;
|
|
|
|
|
|
using FluentNHibernate.Cfg.Db;
|
2021-09-23 09:46:40 +00:00
|
|
|
|
using Common;
|
2023-03-28 11:03:38 +00:00
|
|
|
|
using Events;
|
|
|
|
|
|
using TBF.Resources;
|
2023-05-22 10:38:55 +00:00
|
|
|
|
using System.Collections.Generic;
|
2021-09-19 20:04:16 +00:00
|
|
|
|
|
|
|
|
|
|
namespace TBF
|
|
|
|
|
|
{
|
|
|
|
|
|
public class DB
|
|
|
|
|
|
{
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Database related: This object reference is set after a successful user login
|
|
|
|
|
|
///
|
|
|
|
|
|
public static DatabaseSettings CurrentBench;
|
|
|
|
|
|
|
2023-03-28 11:03:38 +00:00
|
|
|
|
public static ISessionFactory ConfigDBSessionFactory = null; /// Configuration DB session factory (always != null)
|
|
|
|
|
|
public static ISessionFactory SharedDBSessionFactory = null; /// Shared configuration DB session factory or null
|
|
|
|
|
|
public static ISessionFactory ResultsDBSessionFactory = null; /// Results DB session factory (always != null)
|
|
|
|
|
|
public static ISessionFactory EventsDBSessionFactory = null; /// Events DB session factory or null
|
|
|
|
|
|
|
|
|
|
|
|
public static ISessionFactory LocalUsersDBSessionFactory = null; /// Local users DB session factory (always != null)
|
|
|
|
|
|
public static ISessionFactory SharedUsersDBSessionFactory = null; /// Shared users DB session factory or null
|
|
|
|
|
|
|
|
|
|
|
|
public static ISessionFactory[] UserSessionFactories
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
return (SharedUsersDBSessionFactory != null)
|
|
|
|
|
|
? new ISessionFactory[] { SharedUsersDBSessionFactory, LocalUsersDBSessionFactory }
|
|
|
|
|
|
: new ISessionFactory[] { LocalUsersDBSessionFactory };
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-09-19 20:04:16 +00:00
|
|
|
|
/// <summary>
|
2023-03-28 11:03:38 +00:00
|
|
|
|
/// Create all database session factories
|
2021-09-19 20:04:16 +00:00
|
|
|
|
/// </summary>
|
2023-03-28 11:03:38 +00:00
|
|
|
|
public static void CreateSessionFactories(DatabaseSettings dbSettings)
|
2021-09-19 20:04:16 +00:00
|
|
|
|
{
|
2023-03-28 11:03:38 +00:00
|
|
|
|
/// Configuration database (local, always non-null)
|
|
|
|
|
|
if (dbSettings.ProceduresDBSettings != null && dbSettings.ProceduresDBSettings.IsValid)
|
|
|
|
|
|
{
|
|
|
|
|
|
ConfigDBSessionFactory =
|
|
|
|
|
|
CreateSessionFactory(m => m.FluentMappings.AddFromAssemblyOf<Config.Entities.Procedure>(),
|
|
|
|
|
|
dbSettings.ProceduresDBSettings.DbType,
|
|
|
|
|
|
dbSettings.ProceduresDBSettings.ConnectionString);
|
2021-09-19 20:04:16 +00:00
|
|
|
|
|
2023-03-28 11:03:38 +00:00
|
|
|
|
LocalUsersDBSessionFactory =
|
|
|
|
|
|
CreateSessionFactory(m => m.FluentMappings.AddFromAssemblyOf<Users.Entities.User>(),
|
|
|
|
|
|
dbSettings.ProceduresDBSettings.DbType,
|
|
|
|
|
|
dbSettings.ProceduresDBSettings.ConnectionString);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Shared configuration database (remote)
|
|
|
|
|
|
if (dbSettings.UsersDBSettings != null && dbSettings.UsersDBSettings.IsValid)
|
|
|
|
|
|
{
|
|
|
|
|
|
SharedDBSessionFactory =
|
|
|
|
|
|
CreateSessionFactory(m => m.FluentMappings.AddFromAssemblyOf<Config.Entities.Procedure>(),
|
|
|
|
|
|
dbSettings.UsersDBSettings.DbType,
|
|
|
|
|
|
dbSettings.UsersDBSettings.ConnectionString);
|
|
|
|
|
|
|
|
|
|
|
|
SharedUsersDBSessionFactory =
|
|
|
|
|
|
CreateSessionFactory(m => m.FluentMappings.AddFromAssemblyOf<Users.Entities.User>(),
|
|
|
|
|
|
dbSettings.UsersDBSettings.DbType,
|
|
|
|
|
|
dbSettings.UsersDBSettings.ConnectionString);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Results database (local, always non-null)
|
|
|
|
|
|
if (dbSettings.WaterMetersDBSettings != null && dbSettings.WaterMetersDBSettings.IsValid)
|
2021-09-19 20:04:16 +00:00
|
|
|
|
{
|
2023-03-28 11:03:38 +00:00
|
|
|
|
Results.DB.SessionFactory = ResultsDBSessionFactory =
|
|
|
|
|
|
CreateSessionFactory(m => m.FluentMappings.AddFromAssemblyOf<Results.Entities.Batch>(),
|
|
|
|
|
|
dbSettings.WaterMetersDBSettings.DbType,
|
|
|
|
|
|
dbSettings.WaterMetersDBSettings.ConnectionString);
|
2021-09-19 20:04:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-28 11:03:38 +00:00
|
|
|
|
/// Events database (remote or local)
|
|
|
|
|
|
if (dbSettings.EventsDBSettings != null && dbSettings.EventsDBSettings.IsValid)
|
|
|
|
|
|
{
|
|
|
|
|
|
EventsDBSessionFactory =
|
|
|
|
|
|
CreateSessionFactory(m => m.FluentMappings.AddFromAssemblyOf<Events.Entities.Event>(),
|
|
|
|
|
|
dbSettings.EventsDBSettings.DbType,
|
|
|
|
|
|
dbSettings.EventsDBSettings.ConnectionString);
|
|
|
|
|
|
}
|
2021-09-19 20:04:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// NHibernate session factory (to create the database session 'SessionFactory')
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>A database session</returns>
|
2023-05-22 10:38:55 +00:00
|
|
|
|
public static ISessionFactory CreateSessionFactory(Action<MappingConfiguration> mappings, DBType dbType,
|
2023-03-28 11:03:38 +00:00
|
|
|
|
string connectionString, bool createDB = false)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2023-05-22 10:38:55 +00:00
|
|
|
|
var buildSchema = createDB ? (Action<Configuration>)BuildSchemaCreate : (Action<Configuration>)BuildSchema;
|
2023-03-28 11:03:38 +00:00
|
|
|
|
|
|
|
|
|
|
switch (dbType)
|
|
|
|
|
|
{
|
|
|
|
|
|
default:
|
2023-05-22 10:38:55 +00:00
|
|
|
|
case DBType.MySql:
|
|
|
|
|
|
|
|
|
|
|
|
return Fluently.Configure()
|
|
|
|
|
|
.Database(MySQLConfiguration.Standard.ConnectionString(connectionString))
|
|
|
|
|
|
.Mappings(mappings)
|
|
|
|
|
|
.ExposeConfiguration(buildSchema)
|
|
|
|
|
|
.BuildConfiguration()
|
|
|
|
|
|
.SetProperty("hibernate.connection.connect_timeout", Const.MySqlConnectTimeoutSec)
|
|
|
|
|
|
.BuildSessionFactory();
|
2023-03-28 11:03:38 +00:00
|
|
|
|
|
2023-05-22 10:38:55 +00:00
|
|
|
|
case DBType.SQLite:
|
2023-03-28 11:03:38 +00:00
|
|
|
|
|
2023-05-22 10:38:55 +00:00
|
|
|
|
return Fluently.Configure()
|
|
|
|
|
|
.Database(SQLiteConfiguration.Standard.UsingFile(connectionString))
|
|
|
|
|
|
.Mappings(mappings)
|
|
|
|
|
|
.ExposeConfiguration(buildSchema)
|
|
|
|
|
|
.BuildSessionFactory();
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2023-03-28 11:03:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception exc)
|
|
|
|
|
|
{
|
|
|
|
|
|
MessageBox.Show(string.Format("{0}: {1}", Strings.Cannot_connect_to_the_database, exc.Message),
|
|
|
|
|
|
Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void BuildSchema(Configuration config)
|
2021-09-19 20:04:16 +00:00
|
|
|
|
{
|
2023-03-28 11:03:38 +00:00
|
|
|
|
/// This NHibernate tool takes a configuration (with mapping info in)
|
|
|
|
|
|
/// and exports a database schema from it
|
|
|
|
|
|
new SchemaExport(config).SetOutputFile("db_schema");
|
|
|
|
|
|
}
|
2021-09-19 20:04:16 +00:00
|
|
|
|
|
2023-03-28 11:03:38 +00:00
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <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 CreateEmptyConfigDB(Common.DBType dbType, string connectionString)
|
|
|
|
|
|
{
|
|
|
|
|
|
ISessionFactory sessionFactory =
|
|
|
|
|
|
CreateSessionFactory(m => m.FluentMappings.AddFromAssemblyOf<Config.Entities.Procedure>(),
|
|
|
|
|
|
dbType, connectionString, true);
|
|
|
|
|
|
|
|
|
|
|
|
if (sessionFactory == null) return false;
|
|
|
|
|
|
|
|
|
|
|
|
/// Populate the database
|
|
|
|
|
|
using (var session = sessionFactory.OpenSession())
|
2021-09-19 20:04:16 +00:00
|
|
|
|
{
|
2023-03-28 11:03:38 +00:00
|
|
|
|
using (var transaction = session.BeginTransaction())
|
|
|
|
|
|
{
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Create user 'admin'
|
|
|
|
|
|
///
|
|
|
|
|
|
var admin = new Config.Entities.User
|
|
|
|
|
|
{
|
|
|
|
|
|
UserName = Config.Data.AdminUsername,
|
|
|
|
|
|
FullName = Strings.Administrator,
|
|
|
|
|
|
LastPwChange = DateTime.Now
|
|
|
|
|
|
};
|
|
|
|
|
|
admin.SetPassword(Config.Data.AdminPassword);
|
|
|
|
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Prepare all groups, add some of them to admin
|
|
|
|
|
|
///
|
|
|
|
|
|
for (Common.GID gid = 0; gid < Common.GID.NrOfGroups; gid++)
|
|
|
|
|
|
{
|
|
|
|
|
|
Config.Entities.Group group = new Config.Entities.Group(gid);
|
|
|
|
|
|
switch (gid)
|
|
|
|
|
|
{
|
|
|
|
|
|
case Common.GID.Testers:
|
|
|
|
|
|
case Common.GID.TestingSpecialists:
|
|
|
|
|
|
case Common.GID.HeadOfLab:
|
|
|
|
|
|
case Common.GID.MaintenanceSpecialists:
|
|
|
|
|
|
case Common.GID.Metrologists:
|
|
|
|
|
|
case Common.GID.CalibrationSpecialists:
|
|
|
|
|
|
case Common.GID.Administrators:
|
|
|
|
|
|
#if TURA_IPERL || TURA_IPERL_NEW || TURA_SPECIAL
|
|
|
|
|
|
case Common.GID.TraceabilityManagement:
|
|
|
|
|
|
#elif KEMPNO_50 || KRAKOW_50 || TORUN_50 || WARSAW_50 || WARSAW_END
|
|
|
|
|
|
case Common.GID.MetrologicalAuthority:
|
|
|
|
|
|
case Common.GID.WaterMeterAuthority:
|
|
|
|
|
|
#endif
|
|
|
|
|
|
admin.AddGroup(group);
|
|
|
|
|
|
session.SaveOrUpdate(group); /// Save this group
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
session.SaveOrUpdate(admin); /// Save user 'admin'
|
|
|
|
|
|
transaction.Commit();
|
|
|
|
|
|
}
|
2021-09-19 20:04:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-28 11:03:38 +00:00
|
|
|
|
return true;
|
2021-09-19 20:04:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|