From 34073844bcc49a582ae54e0661f58a6852c37446 Mon Sep 17 00:00:00 2001 From: Milan Hanajik Date: Thu, 30 Aug 2018 08:04:19 +0200 Subject: [PATCH] Config.FluentCommon cleanup : SaveToDb( ) and DeleteFromDb( ) removed. --- Config/FluentCommon.cs | 62 +---------------------------- TBF/ComponentsManagerDlg.cs | 39 ++++++++++--------- TBF/MetrologyDlg.cs | 34 +++++++++++----- TBF/UiControls/ProceduresCtrl.cs | 67 ++++++++++++++++++++++---------- 4 files changed, 92 insertions(+), 110 deletions(-) diff --git a/Config/FluentCommon.cs b/Config/FluentCommon.cs index ca8b42f6c..49e0ab41f 100644 --- a/Config/FluentCommon.cs +++ b/Config/FluentCommon.cs @@ -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(); - } - } - /// /// 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; } - - /// - /// Create an empty water meters database - /// - /// DBType.SQLite or DBType.MySql - /// Connection string - /// true=success, false=error - 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; - } } } diff --git a/TBF/ComponentsManagerDlg.cs b/TBF/ComponentsManagerDlg.cs index 5c55449b6..b215a69e5 100644 --- a/TBF/ComponentsManagerDlg.cs +++ b/TBF/ComponentsManagerDlg.cs @@ -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 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; } } diff --git a/TBF/MetrologyDlg.cs b/TBF/MetrologyDlg.cs index 1681b79ab..a4ad8f387 100644 --- a/TBF/MetrologyDlg.cs +++ b/TBF/MetrologyDlg.cs @@ -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; diff --git a/TBF/UiControls/ProceduresCtrl.cs b/TBF/UiControls/ProceduresCtrl.cs index fee6e353c..1aa73756a 100644 --- a/TBF/UiControls/ProceduresCtrl.cs +++ b/TBF/UiControls/ProceduresCtrl.cs @@ -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); + } } } }