213 lines
6.2 KiB
C#
213 lines
6.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using FluentNHibernate.Cfg;
|
|
using FluentNHibernate.Cfg.Db;
|
|
using NHibernate;
|
|
using NHibernate.Cfg;
|
|
using NHibernate.Tool.hbm2ddl;
|
|
|
|
namespace MonitoringDB
|
|
{
|
|
public static class FluentNH
|
|
{
|
|
/// <summary>
|
|
/// Current session factory for the last used connection string or null
|
|
/// </summary>
|
|
public static ISessionFactory SessionFactory;
|
|
|
|
|
|
/// <summary> Connection string for all sessions </summary>
|
|
static string connectionString;
|
|
///
|
|
public static string ConnectionString
|
|
{
|
|
get { return connectionString; }
|
|
set
|
|
{
|
|
if (value != connectionString)
|
|
{
|
|
connectionString = value;
|
|
SessionFactory = null; /// Clear SessionFactory on connection string change
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// NHibernate session factory (to create the database session 'SessionFactory')
|
|
/// </summary>
|
|
/// <returns>A database session</returns>
|
|
static ISessionFactory CreateSessionFactory()
|
|
{
|
|
return CreateSessionFactory(false);
|
|
}
|
|
|
|
/// <summary>
|
|
/// NHibernate session factory (to create the database session 'SessionFactory')
|
|
/// </summary>
|
|
/// <returns>A database session</returns>
|
|
public static ISessionFactory CreateSessionFactory(bool createDB)
|
|
{
|
|
FluentConfiguration cfg = Fluently
|
|
.Configure()
|
|
.Database(MySQLConfiguration.Standard.ConnectionString(connectionString))
|
|
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Entities.Process>());
|
|
|
|
if (createDB)
|
|
{
|
|
return cfg.ExposeConfiguration(BuildSchemaCreate).BuildSessionFactory();
|
|
}
|
|
else
|
|
{
|
|
return cfg.ExposeConfiguration(BuildSchema).BuildSessionFactory();
|
|
}
|
|
}
|
|
|
|
public delegate void BuildSchemaDlgt(Configuration config);
|
|
|
|
public 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(string connectionString)
|
|
{
|
|
ConnectionString = connectionString; /// Clears session factory on connction string change
|
|
|
|
if (SessionFactory == null)
|
|
SessionFactory = CreateSessionFactory();
|
|
|
|
return SessionFactory.OpenSession();
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Create an empty 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 CreateEmptyDB(string connectionString)
|
|
{
|
|
ConnectionString = connectionString;
|
|
|
|
ISessionFactory sessionFactory = CreateSessionFactory(true);
|
|
if (sessionFactory == null) return false;
|
|
|
|
/// Populate the database
|
|
using (var session = sessionFactory.OpenSession())
|
|
{
|
|
//using (var transaction = session.BeginTransaction())
|
|
//{
|
|
// var part1 = new MonitoringDB.Entities.Part("flow tube", CodeType.UniqueNr, CodeForm.QRCode, 0);
|
|
// session.SaveOrUpdate(part1);
|
|
// transaction.Commit();
|
|
//}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
public static IList<WMPart> FindWMParts(string pcbNumber, string connString)
|
|
{
|
|
IList<WMPart> foundParts = new List<WMPart>(); /// initially empty
|
|
|
|
if (string.IsNullOrEmpty(pcbNumber)) return foundParts; /// return an empty list
|
|
|
|
try
|
|
{
|
|
ISession session = CreateSession(connString);
|
|
|
|
IList<Entities.ReferenceRecord> referenceRecords = session
|
|
.QueryOver<Entities.ReferenceRecord>()
|
|
.Where(x => (x.Code == pcbNumber))
|
|
.OrderBy(x => x.TimeStamp).Desc
|
|
.List<Entities.ReferenceRecord>();
|
|
|
|
if (referenceRecords.Count == 0) return foundParts;
|
|
|
|
foundParts.Add(new WMPart(referenceRecords[0]));
|
|
Entities.Process process = referenceRecords[0].Process;
|
|
///
|
|
foreach (var refR in referenceRecords)
|
|
{
|
|
if (process != refR.Process)
|
|
{
|
|
continue; /// skip records obtained by another process
|
|
}
|
|
|
|
IList<Entities.Record> relatedRecords = session
|
|
.QueryOver<Entities.Record>()
|
|
.Where(x => (x.ReferenceRecord == refR))
|
|
.List<Entities.Record>();
|
|
|
|
|
|
foreach (var relR in relatedRecords)
|
|
{
|
|
foundParts.Add(new WMPart(relR));
|
|
foreach (var ws in process.Worksteps)
|
|
{
|
|
if (ws.ReferencePart == relR.Part)
|
|
{
|
|
FindWMPartsRecursively(process, ws.ReferencePart, relR.Code, session, ref foundParts);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
|
|
return foundParts;
|
|
}
|
|
|
|
|
|
static void FindWMPartsRecursively(Entities.Process process, Entities.Part refPart, string code, ISession session, ref IList<WMPart> foundParts)
|
|
{
|
|
IList<Entities.ReferenceRecord> referenceRecords = session
|
|
.QueryOver<Entities.ReferenceRecord>()
|
|
.Where(x => (x.Workstep.ReferencePart == refPart))
|
|
.Where(x => (x.Code == code))
|
|
.OrderBy(x => x.TimeStamp).Desc
|
|
.List<Entities.ReferenceRecord>();
|
|
|
|
if (referenceRecords.Count == 0) return;
|
|
|
|
foreach (var refR in referenceRecords)
|
|
{
|
|
IList<Entities.Record> relatedRecords = session
|
|
.QueryOver<Entities.Record>()
|
|
.Where(x => (x.ReferenceRecord == refR))
|
|
.List<Entities.Record>();
|
|
|
|
|
|
foreach (var relR in relatedRecords)
|
|
{
|
|
foundParts.Add(new WMPart(relR));
|
|
|
|
foreach (var ws in process.Worksteps)
|
|
{
|
|
if (ws.ReferencePart == relR.Part)
|
|
{
|
|
FindWMPartsRecursively(process, ws.ReferencePart, relR.Code, session, ref foundParts);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|