Config.FluentCommon cleanup : SaveToDb( ) and DeleteFromDb( ) removed.

This commit is contained in:
Milan Hanajik 2018-08-30 08:04:19 +02:00
parent 6f3b9be77e
commit 34073844bc
4 changed files with 92 additions and 110 deletions

View File

@ -1,5 +1,5 @@
///
/// Copyright (c) 2013-2015 Sensus Metering Systems
/// Copyright (c) 2013-2018 Sensus Slovensko a.s.
///
using System;
using System.Windows.Forms;
@ -127,41 +127,6 @@ namespace Config
return SessionFactories[ix].OpenSession();
}
public static void SaveToDb(Users.Entities.DBKind database, object obj)
{
SaveToDb(CreateSession(database), obj);
}
public static void SaveToDb(ISession session, object obj)
{
using (var transaction = session.BeginTransaction())
{
try
{
session.SaveOrUpdate(obj);
transaction.Commit();
}
catch
{
transaction.Rollback();
}
}
}
public static void DeleteFromDb(Users.Entities.DBKind database, object obj)
{
DeleteFromDb(CreateSession(database), obj);
}
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'.
@ -218,30 +183,5 @@ namespace Config
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>
public static bool CreateEmptyResultsDB(Users.Entities.DBType dbType, string connectionString)
{
ISessionFactory sessionFactory = CreateSessionFactory(Users.Entities.DBKind.Results, dbType, connectionString, true);
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;
}
}
}

View File

@ -7,6 +7,7 @@ using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using log4net;
using NHibernate;
using Config.Entities;
using TBF.BenchControl;
using TBF.BenchControl.Generic;
@ -26,7 +27,7 @@ namespace TBF
IList<Component> toBeDeletedEntities;
NHibernate.ISession session;
ISession session;
SelectComponentClassDlg selectComponentTypeDlg; /// Constructed once, the selection is kept between dialog usages
@ -214,29 +215,31 @@ namespace TBF
///
/// Save DB changes, return true when DB changes saved OK
///
bool SaveDBChanges(NHibernate.ISession ses)
bool SaveDBChanges(ISession session)
{
try
using (ITransaction transaction = session.BeginTransaction())
{
foreach (var cmpnt in toBeDeletedEntities)
try
{
Config.FluentCommon.DeleteFromDb(ses, cmpnt);
}
foreach (var cmpnt in toBeDeletedEntities) session.Delete(cmpnt);
int itemNr = 1;
foreach (var cmpnt in cmpntEntities)
int itemNr = 1;
foreach (var cmpnt in cmpntEntities)
{
cmpnt.ItemNr = itemNr++;
session.SaveOrUpdate(cmpnt);
}
transaction.Commit();
session.Flush();
return true;
}
catch (Exception exc)
{
cmpnt.ItemNr = itemNr++;
Config.FluentCommon.SaveToDb(ses, cmpnt);
transaction.Rollback();
log.ErrorFormat("Exception when saving components : {1}", exc.Message);
return false;
}
ses.Flush();
return true;
}
catch (Exception exc)
{
log.ErrorFormat("Exception when saving components : {1}", exc.Message);
return false;
}
}

View File

@ -162,16 +162,30 @@ namespace TBF
private void okButton_Click(object sender, EventArgs e)
{
foreach (TabPage tabPage in metrologyTabControl.TabPages)
{
IMetrologyDlgTab tab = tabPage.Tag as IMetrologyDlgTab;
tab.OkBtnClicked();
if (!(tabPage.Tag is MetrologyDlgDensityTab))
{
Config.FluentCommon.SaveToDb(session, tab.MeterEntity);
foreach (var entity in tab.ToBeRemoved) Config.FluentCommon.DeleteFromDb(session, entity);
}
using (ITransaction transaction = session.BeginTransaction())
{
try
{
foreach (TabPage tabPage in metrologyTabControl.TabPages)
{
IMetrologyDlgTab tab = tabPage.Tag as IMetrologyDlgTab;
tab.OkBtnClicked();
if (!(tabPage.Tag is MetrologyDlgDensityTab))
{
session.SaveOrUpdate(tab.MeterEntity);
foreach (var entity in tab.ToBeRemoved) session.Delete(entity);
}
}
transaction.Commit();
session.Flush();
}
catch (Exception exc)
{
transaction.Rollback();
log.ErrorFormat("Cannot save changes to DB: {0}", exc.Message);
MessageBox.Show(Strings.Cannot_save_changes, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
Program.LocalSettings.MetrologyDlgLeft = Location.X;

View File

@ -112,18 +112,30 @@ namespace TBF.UiControls
{
base.OkBtnClicked();
foreach (var entity in ToBeRemovedItems)
{
Config.FluentCommon.DeleteFromDb(session, entity);
}
ToBeRemovedItems.Clear();
int itemNr = 0;
foreach (var entity in MyItems)
{
(entity as Procedure).ItemNr = itemNr++;
Config.FluentCommon.SaveToDb(session, entity);
}
session.Flush();
using (ITransaction transaction = session.BeginTransaction())
{
try
{
foreach (var entity in ToBeRemovedItems) session.Delete(entity);
ToBeRemovedItems.Clear();
int itemNr = 0;
foreach (var entity in MyItems)
{
(entity as Procedure).ItemNr = itemNr++;
session.SaveOrUpdate(entity);
}
transaction.Commit();
session.Flush();
}
catch (Exception exc)
{
transaction.Rollback();
log.ErrorFormat("Cannot save changes to DB: {0}", exc.Message);
MessageBox.Show(Strings.Cannot_save_changes, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
/// Update the column widths
Program.LocalSettings.ProceduresColumnWidths = new int[Columns.Count];
@ -199,16 +211,29 @@ namespace TBF.UiControls
}
/// Update the database
foreach (var entity in ToBeRemovedItems)
using (ITransaction transaction = session.BeginTransaction())
{
Config.FluentCommon.DeleteFromDb(session, entity);
}
ToBeRemovedItems.Clear();
int itemNr = 0;
foreach (var entity in MyItems)
{
(entity as Procedure).ItemNr = itemNr++;
Config.FluentCommon.SaveToDb(session, entity);
try
{
foreach (var entity in ToBeRemovedItems) session.Delete(entity);
ToBeRemovedItems.Clear();
int itemNr = 0;
foreach (var entity in MyItems)
{
(entity as Procedure).ItemNr = itemNr++;
session.SaveOrUpdate(entity);
}
transaction.Commit();
session.Flush();
}
catch (Exception exc)
{
transaction.Rollback();
log.ErrorFormat("Cannot save changes to DB: {0}", exc.Message);
MessageBox.Show(Strings.Cannot_save_changes, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
}