2015-04-15 18:32:56 +00:00
|
|
|
|
///
|
2021-05-27 15:30:04 +00:00
|
|
|
|
/// Copyright (c) 2013-2021 Sensus Slovensko a.s.
|
2015-04-15 18:32:56 +00:00
|
|
|
|
///
|
|
|
|
|
|
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;
|
2021-05-27 15:30:04 +00:00
|
|
|
|
using Common;
|
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
|
|
|
|
{
|
2021-05-27 15:30:04 +00:00
|
|
|
|
public class FluentCommon
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// NHibernate session factory (to create the database session 'SessionFactory')
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>A database session</returns>
|
2021-05-27 15:30:04 +00:00
|
|
|
|
public static ISessionFactory CreateSessionFactory(DBKind database, 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:
|
2021-05-27 15:30:04 +00:00
|
|
|
|
case DBType.SQLite:
|
2017-03-16 20:53:12 +00:00
|
|
|
|
cfg = cfg.Database(SQLiteConfiguration.Standard.UsingFile(connectionString));
|
2014-07-28 07:54:35 +00:00
|
|
|
|
break;
|
2021-05-27 15:30:04 +00:00
|
|
|
|
case DBType.MySql:
|
2017-03-16 20:53:12 +00:00
|
|
|
|
cfg = cfg.Database(MySQLConfiguration.Standard.ConnectionString(connectionString));
|
2014-07-28 07:54:35 +00:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
switch (database)
|
|
|
|
|
|
{
|
|
|
|
|
|
default:
|
2021-05-27 15:30:04 +00:00
|
|
|
|
case DBKind.Config:
|
|
|
|
|
|
case DBKind.RemoteConfig:
|
|
|
|
|
|
cfg = cfg.Mappings(m => m.FluentMappings.AddFromAssemblyOf<FluentCommon>());
|
2014-07-28 07:54:35 +00:00
|
|
|
|
break;
|
2021-05-27 15:30:04 +00:00
|
|
|
|
case DBKind.Results:
|
|
|
|
|
|
cfg = cfg.Mappings(m => m.FluentMappings.AddFromAssemblyOf<FluentCommon>());
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <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>
|
2021-05-27 15:30:04 +00:00
|
|
|
|
public static bool CreateEmptyConfigDB(DBType dbType, string connectionString)
|
2015-12-31 21:48:22 +00:00
|
|
|
|
{
|
2021-05-27 15:30:04 +00:00
|
|
|
|
ISessionFactory sessionFactory = CreateSessionFactory(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())
|
|
|
|
|
|
{
|
|
|
|
|
|
///
|
2020-01-08 12:04:51 +00:00
|
|
|
|
/// Create user 'admin'
|
2014-07-28 07:54:35 +00:00
|
|
|
|
///
|
2017-05-01 09:16:23 +00:00
|
|
|
|
var admin = new Config.Entities.User
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
2021-09-19 11:34:03 +00:00
|
|
|
|
UserName = GlobalData.AdminUsername,
|
2016-12-21 08:22:30 +00:00
|
|
|
|
FullName = Strings.Administrator,
|
2014-07-28 07:54:35 +00:00
|
|
|
|
LastPwChange = DateTime.Now
|
|
|
|
|
|
};
|
2021-09-19 11:34:03 +00:00
|
|
|
|
admin.SetPassword(GlobalData.AdminPassword);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
2020-01-08 12:04:51 +00:00
|
|
|
|
///
|
|
|
|
|
|
/// Prepare all groups, add some of them to admin
|
|
|
|
|
|
///
|
2021-05-27 15:30:04 +00:00
|
|
|
|
for (GID gid = 0; gid < GID.NrOfGroups; gid++)
|
2020-01-08 12:04:51 +00:00
|
|
|
|
{
|
|
|
|
|
|
Config.Entities.Group group = new Config.Entities.Group(gid);
|
|
|
|
|
|
switch (gid)
|
|
|
|
|
|
{
|
2021-05-27 15:30:04 +00:00
|
|
|
|
case GID.Testers:
|
|
|
|
|
|
case GID.TestingSpecialists:
|
|
|
|
|
|
case GID.HeadOfLab:
|
|
|
|
|
|
case GID.MaintenanceSpecialists:
|
|
|
|
|
|
case GID.Metrologists:
|
|
|
|
|
|
case GID.CalibrationSpecialists:
|
|
|
|
|
|
case GID.Administrators:
|
2020-01-08 12:04:51 +00:00
|
|
|
|
#if TURA_IPERL || TURA_IPERL_NEW || TURA_SPECIAL
|
2021-05-27 15:30:04 +00:00
|
|
|
|
case GID.TraceabilityManagement:
|
2020-01-08 12:04:51 +00:00
|
|
|
|
#elif KEMPNO_50 || KRAKOW_50 || TORUN_50 || WARSAW_END
|
2021-05-27 15:30:04 +00:00
|
|
|
|
case GID.MetrologicalAuthority:
|
|
|
|
|
|
case GID.WaterMeterAuthority:
|
2020-01-08 12:04:51 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
admin.AddGroup(group);
|
|
|
|
|
|
session.SaveOrUpdate(group); /// Save this group
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
session.SaveOrUpdate(admin); /// Save user 'admin'
|
2014-07-28 07:54:35 +00:00
|
|
|
|
transaction.Commit();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|