tbf/MergeResultsDBs/Program.cs

191 lines
8.0 KiB
C#
Raw Permalink Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using log4net;
using NHibernate;
using FluentNHibernate;
using Results;
using Results.Entities;
namespace MergeResultsDBs
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("A range of records from the 2nd database will be appended to the 1st database.");
Console.WriteLine("Enter the 1st (target) database name:");
string firstDB = Console.ReadLine();
Console.WriteLine("Enter the 2nd database name:");
string secondDB = Console.ReadLine();
Console.WriteLine("Enter range of batch numbers from the 2nd DB to append to the 1st DB (start-end):");
string range = Console.ReadLine();
ISession session1;
IList<Components> componentsList;
int componentsListInitialCount;
IList<TestData> testDataList;
int testDataListInitialCount;
IList<WaterMeterData> waterMeterDataList;
int waterMeterDataListInitialCount;
try
{
DB.ConnectionString = string.Format("SERVER=localhost; DATABASE={0}; UID=root; PASSWORD=kraken; CHARSET=utf8;", firstDB);
session1 = DB.CreateSession();
componentsList = session1.QueryOver<Components>().List();
componentsListInitialCount = componentsList.Count;
testDataList = session1.QueryOver<TestData>().List();
testDataListInitialCount = testDataList.Count();
waterMeterDataList = session1.QueryOver<WaterMeterData>().List();
waterMeterDataListInitialCount = waterMeterDataList.Count();
}
catch (Exception exc)
{
MessageBox.Show(string.Format("Cannot open the 1st database:\r\n{0}", exc.Message));
return;
}
ISession session2;
IList<Components> oriComponentsList;
IList<TestData> oriTestDataList;
IList<WaterMeterData> oriWMDataList;
try
{
DB.ConnectionString = string.Format("SERVER=localhost; DATABASE={0}; UID=root; PASSWORD=kraken; CHARSET=utf8;", secondDB);
session2 = DB.CreateSession();
oriComponentsList = session2.QueryOver<Components>().List();
oriTestDataList = session2.QueryOver<TestData>().List();
oriWMDataList = session2.QueryOver<WaterMeterData>().List();
}
catch (Exception exc)
{
MessageBox.Show(string.Format("Cannot open the 2nd database:\r\n{0}", exc.Message));
return;
}
int batchNrStart;
int batchNrEnd;
string[] fields = range.Split(new char[] { '-' });
if (fields.Length != 2 ||
!int.TryParse(fields[0], out batchNrStart) ||
!int.TryParse(fields[1], out batchNrEnd) ||
batchNrEnd < batchNrStart)
{
MessageBox.Show("Invalid range of batch numbers");
return;
}
Console.WriteLine(string.Format("Appending batches {0}-{1} from DB {2} to DB {3}", batchNrStart, batchNrEnd, secondDB, firstDB));
Console.WriteLine("Press 'y' or 'Y' to start, anything else to abort");
string response = Console.ReadLine();
if (response != "y" && response != "Y")
{
return;
}
Console.WriteLine("PROCESSING DATABASES ...");
for (int batchNr = batchNrStart; batchNr <= batchNrEnd; batchNr++)
{
Console.Write(string.Format("{0} ", batchNr));
/// Copy one batch from the 2nd to the 1st database
var oriBatches = session2.QueryOver<Batch>()
.Where(x => (x.BatchNr == batchNr))
.List();
if (oriBatches.Count != 1) continue;
///---------------------------------------------------------------
Batch oriBatch = oriBatches[0];
var transaction = session1.BeginTransaction();
try
{
Batch batch = new Batch(oriBatch);
oriBatch.TestRslts = session2.QueryOver<TestRslt>()
.Where(x => (x.Batch.Id == oriBatch.Id))
.List();
foreach (var oriTR in oriBatch.TestRslts)
{
/// Find appropriate Components
Components components = oriTR.Components == null
? null
: Components.UpdateList(componentsList, new Components(oriTR.Components));
if (components != null) session1.SaveOrUpdate(components);
/// Find appropriate TestData
TestData testData = TestData.UpdateList(testDataList, new TestData(oriTR.TestData));
session1.SaveOrUpdate(testData);
TestRslt tr = new TestRslt();
tr.CopyContentFrom(oriTR);
tr.Batch = batch;
tr.Components = components;
tr.TestData = testData;
batch.TestRslts.Add(tr);
session1.SaveOrUpdate(tr);
}
oriBatch.WaterMeters = session2.QueryOver<WaterMeter>()
.Where(x => (x.Batch.Id == oriBatch.Id))
.List();
foreach (var oriWM in oriBatch.WaterMeters)
{
/// Find appropriate WaterMeterData
WaterMeterData wmData = WaterMeterData.UpdateList(waterMeterDataList, new WaterMeterData(oriWM.WaterMeterData));
session1.SaveOrUpdate(wmData);
WaterMeter wm = new WaterMeter();
wm.CopyContentFrom(oriWM);
wm.Batch = batch;
wm.WaterMeterData = wmData;
foreach (var oriMTR in oriWM.MeterTestRslts)
{
MeterTestRslt mtr = new MeterTestRslt(oriMTR);
mtr.WaterMeter = wm;
mtr.TestRslt = batch.TestRslts.FirstOrDefault<TestRslt>(x => (x.Name() == oriMTR.TestRslt.Name() &&
x.Part == oriMTR.TestRslt.Part &&
x.RepetitionNr == oriMTR.TestRslt.RepetitionNr));
if (mtr.TestRslt == null)
{
throw new Exception("Something strange happened");
}
wm.MeterTestRslts.Add(mtr);
session1.SaveOrUpdate(mtr);
}
batch.WaterMeters.Add(wm);
session1.SaveOrUpdate(wm);
}
session1.SaveOrUpdate(batch);
transaction.Commit();
}
catch
{
transaction.Rollback();
throw new Exception("Commit failed - transaction reverted");
}
}
Console.WriteLine();
session1.Flush();
session1.Close();
session2.Close();
Console.WriteLine("Successfully completed");
Console.ReadLine();
}
}
}