Copying users from a remote database fixed (UI) and done as a database transaction, ver. 2.18.982

This commit is contained in:
Milan Hanajik 2018-08-30 12:57:12 +02:00
parent 49ae2a84c4
commit 9dbca87429
5 changed files with 38 additions and 61 deletions

View File

@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.18.928.0")]
[assembly: AssemblyFileVersion("2.18.928.0")]
[assembly: AssemblyVersion("2.18.982.0")]
[assembly: AssemblyFileVersion("2.18.982.0")]

View File

@ -29,5 +29,5 @@ using System.Runtime.InteropServices;
// Build Number
// Revision
//
[assembly: AssemblyVersion("2.18.981.0")]
[assembly: AssemblyFileVersion("2.18.981.0")]
[assembly: AssemblyVersion("2.18.982.0")]
[assembly: AssemblyFileVersion("2.18.982.0")]

View File

@ -105,37 +105,6 @@ namespace Users
}
public static void SaveObject(object obj)
{
SaveObject(CreateSession(), obj);
}
///
public static void SaveObject(ISession session, object obj)
{
using (var transaction = session.BeginTransaction())
{
session.SaveOrUpdate(obj);
try { transaction.Commit(); }
catch { }
}
}
public static void DeleteObject(object obj)
{
DeleteObject(CreateSession(), obj);
}
///
public static void DeleteObject(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'.

View File

@ -70,6 +70,7 @@ namespace Users.Forms
{
string msg = exc.Message;
}
ListUsers();
}
@ -217,7 +218,8 @@ namespace Users.Forms
if (listViewUsers.SelectedItems.Count == 1 && listViewUsers.SelectedItems[0].Tag is User)
{
User Selecteduser = listViewUsers.SelectedItems[0].Tag as User;
Users.DB.DeleteObject(session, Selecteduser);
session.Delete(Selecteduser);
session.Flush();
listOfUsers.Remove(Selecteduser);
ListUsers();
}
@ -228,52 +230,51 @@ namespace Users.Forms
if (GlobalData.RemoteUsersDB == null || string.IsNullOrEmpty(GlobalData.RemoteUsersDB.ConnectionString) ||
GlobalData.LocalUsersDB == null || string.IsNullOrEmpty(GlobalData.LocalUsersDB.ConnectionString))
{
MessageBox.Show(Strings.No_remote_database_of_users,
Strings.Error,
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
MessageBox.Show(Strings.No_remote_database_of_users, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
if (MessageBox.Show(Strings.Are_you_sure,
Strings.Warning,
MessageBoxButtons.YesNo,
MessageBoxIcon.Exclamation) != DialogResult.Yes)
if (MessageBox.Show(Strings.Are_you_sure, Strings.Warning, MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) != DialogResult.Yes)
{
return;
}
IList<User> remoteUsers;
IList<Group> remoteGroups;
ITransaction transaction = null;
try
{
///
/// Remote DB session, read remote users
/// Create remote DB session and read remote users
///
DB.DbType = GlobalData.RemoteUsersDB.DbType;
DB.ConnectionString = GlobalData.RemoteUsersDB.ConnectionString;
ISession session = DB.CreateSession();
IList<User> remoteUsers = session.QueryOver<User>().List();
IList<Group> remoteGroups = session.QueryOver<Group>().List();
ISession remoteSession = DB.CreateSession();
remoteUsers = remoteSession.QueryOver<User>().List();
remoteGroups = remoteSession.QueryOver<Group>().List();
///
/// Local DB session
/// Prepare local DB session
///
DB.DbType = GlobalData.LocalUsersDB.DbType;
DB.ConnectionString = GlobalData.LocalUsersDB.ConnectionString;
session = DB.CreateSession();
ISession localSession = DB.CreateSession();
transaction = localSession.BeginTransaction();
///
/// Delete all local users
///
IList<User> users = session.QueryOver<User>().List();
foreach (var user in users) session.Delete(user);
IList<Group> groups = session.QueryOver<Group>().List();
foreach (var group in groups) session.Delete(group);
IList<User> users = localSession.QueryOver<User>().List();
foreach (var user in users) localSession.Delete(user);
IList<Group> groups = localSession.QueryOver<Group>().List();
foreach (var group in groups) localSession.Delete(group);
///
/// Save all remote users to the local DB
///
IList<User> newUsers = session.QueryOver<User>().List();
IList<Group> newGroups = session.QueryOver<Group>().List();
IList<User> newUsers = new List<User>();
IList<Group> newGroups = new List<Group>();
foreach (var user in remoteUsers)
{
User newUser = (User)user.Clone();
@ -283,9 +284,14 @@ namespace Users.Forms
Group newGroup = new Group(group.Gid, group.Name);
newUser.AddGroup(newGroup);
}
DB.SaveObject(session, newUser);
localSession.SaveOrUpdate(newUser);
}
session.Flush();
transaction.Commit();
localSession.Flush();
listOfUsers = newUsers;
ListUsers();
MessageBox.Show(string.Format(Strings.N_users_copied, remoteUsers.Count),
Strings.Confirmation,
@ -294,11 +300,13 @@ namespace Users.Forms
}
catch (Exception)
{
if (transaction != null) transaction.Rollback();
MessageBox.Show(Strings.Copying_remote_users_failed,
Strings.Confirmation,
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}
}
}
}

View File

@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.18.981.0")]
[assembly: AssemblyFileVersion("2.18.981.0")]
[assembly: AssemblyVersion("2.18.982.0")]
[assembly: AssemblyFileVersion("2.18.982.0")]