89 lines
3.3 KiB
C#
89 lines
3.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Collections.ObjectModel;
|
|
using System.Threading;
|
|
using System.Xml.Linq;
|
|
|
|
namespace BeWoAdmin {
|
|
delegate void ProgressCallback(int value);
|
|
delegate void ProgressFinishedCallback();
|
|
class ServerCollection : ObservableCollection<Server>{
|
|
private ProgressCallback progressCallback;
|
|
private ProgressFinishedCallback progressFinishedCallback;
|
|
private int numberOfCheckedDBs;
|
|
private int sqlExecutionProgress;
|
|
private string lastSqlCommand = "";
|
|
|
|
public bool CancelationPending { set; private get; }
|
|
public bool TestConnectionsOnly { set; private get; }
|
|
public bool ExecutionInProgress { set; get; }
|
|
public List<Tenant> AllTenants {
|
|
get { return this.SelectMany(s => s.Tenants).ToList(); }
|
|
}
|
|
|
|
public List<Tenant> CheckedTenants {
|
|
get { return AllTenants.Where(t => t.IsChecked).ToList(); }
|
|
}
|
|
|
|
public Window1 Parent { get; set; }
|
|
public string LastSqlCommand {
|
|
get { return lastSqlCommand; }
|
|
set { lastSqlCommand = value; }
|
|
}
|
|
|
|
public void SetDelegates(ProgressCallback progressCallback, ProgressFinishedCallback progressFinishedCallback) {
|
|
this.progressCallback = progressCallback;
|
|
this.progressFinishedCallback = progressFinishedCallback;
|
|
}
|
|
|
|
// SQL-Execution
|
|
|
|
public void ExecuteSqlCommandOnCheckedDatabases() {
|
|
foreach (Tenant t in AllTenants)
|
|
t.ResetErrorMessage();
|
|
numberOfCheckedDBs = CheckedTenants.Count;
|
|
sqlExecutionProgress = 0;
|
|
CancelationPending = false;
|
|
foreach (var group in CheckedTenants.GroupBy(t => t.Server.UriHost))
|
|
new Thread(ExecuteSqlCommandOnDatabases).Start(group);
|
|
}
|
|
|
|
private void ExecuteSqlCommandOnDatabases(object tenantList) {
|
|
foreach (Tenant t in tenantList as IEnumerable<Tenant>) {
|
|
if (!CancelationPending)
|
|
if (TestConnectionsOnly)
|
|
t.TestConnection();
|
|
else
|
|
{
|
|
t.parent = this.Parent;
|
|
t.ExecuteSqlCommand(LastSqlCommand);
|
|
}
|
|
Interlocked.Increment(ref sqlExecutionProgress);
|
|
progressCallback(100 * sqlExecutionProgress / numberOfCheckedDBs);
|
|
}
|
|
|
|
if (sqlExecutionProgress == numberOfCheckedDBs) {
|
|
ExecutionInProgress = false;
|
|
TestConnectionsOnly = false;
|
|
progressFinishedCallback();
|
|
}
|
|
}
|
|
|
|
// File Handling
|
|
|
|
public void SaveToXml(string filename, string pw) {
|
|
var content = this.Select(s => s.ToXElement(pw));
|
|
new XDocument(new XElement("DatabaseCollection", content)).Save(filename);
|
|
}
|
|
|
|
public void LoadFromXml(string filename, string pw) {
|
|
XDocument xdoc = XDocument.Load(filename);
|
|
this.Clear();
|
|
foreach (XElement xe in xdoc.Element("DatabaseCollection").Elements("Server")) {
|
|
this.Add(Server.CreateFromXElement(xe, pw));
|
|
}
|
|
}
|
|
}
|
|
} |