private List <Application> ReadApplications() { Log.Info(typeof(Database), "Reading application data..."); try { using (var connection = new SqliteConnection(Settings.SQLiteConnectionString)) { PersistenceStorage.PersistentDataContext persistenceDb = new PersistenceStorage.PersistentDataContext(connection); List <Application> applications = new List <Application>(); foreach (PersistenceStorage.Application persistenceApp in persistenceDb.Application.ToList()) { Monoscape.Common.Model.Application app = new Monoscape.Common.Model.Application(); app.Id = persistenceApp.ID; app.Name = persistenceApp.Name; app.Version = persistenceApp.Version; app.State = ConvertApplicationState(persistenceApp.State.ToString()); app.FileName = persistenceApp.FilePath; app.Tenants = ConvertTenants(persistenceApp.Tenant); app.RowState = EntityState.Queried; applications.Add(app); } Log.Info(typeof(Database), applications.Count + " applications found"); return(applications); } } catch (Exception e) { Log.Error(typeof(Database), "Could not connect to the database", e); throw e; } }
private void WriteTenants(PersistenceStorage.PersistentDataContext persistenceDb, Application app) { if ((app.Tenants != null) && (app.Tenants.Count > 0)) { List <Tenant> removedTenants = new List <Tenant>(); foreach (Tenant tenant in app.Tenants) { if (tenant.RowState == EntityState.New) { PersistenceStorage.Tenant pTenant = new PersistenceStorage.Tenant(); pTenant.Name = tenant.Name; pTenant.ApplicationID = tenant.ApplicationId; pTenant.UpperScaleLimit = tenant.UpperScaleLimit; pTenant.ScalingFactor = tenant.ScalingFactor; persistenceDb.Tenant.InsertOnSubmit(pTenant); Log.Info(typeof(Database), "New application " + app.Name + " tenant " + tenant.Name + " added"); } else if (tenant.RowState == EntityState.Modified) { PersistenceStorage.Tenant pTenant = persistenceDb.Tenant.FirstOrDefault(x => x.ID == tenant.Id); pTenant.Name = tenant.Name; pTenant.UpperScaleLimit = tenant.UpperScaleLimit; pTenant.ScalingFactor = tenant.ScalingFactor; Log.Info(typeof(Database), "Application " + app.Name + " tenant " + tenant.Name + " updated"); } else if (tenant.RowState == EntityState.Removed) { PersistenceStorage.Tenant pTenant = persistenceDb.Tenant.FirstOrDefault(x => x.ID == tenant.Id); persistenceDb.Tenant.DeleteOnSubmit(pTenant); removedTenants.Add(tenant); } } foreach (Tenant removed in removedTenants) { app.Tenants.Remove(removed); Log.Info(typeof(Database), "Application " + app.Name + " tenant " + removed.Name + " removed"); } } }
private void WriteApplications() { Log.Info(typeof(Database), "Writing application data..."); try { var connection = new SqliteConnection(Settings.SQLiteConnectionString); PersistenceStorage.PersistentDataContext persistenceDb = new PersistenceStorage.PersistentDataContext(connection); List<Application> removedApps = new List<Application>(); foreach (Application app in Applications) { if (app.RowState == EntityState.New) { PersistenceStorage.Application persistenceApp = new PersistenceStorage.Application(); persistenceApp.Name = app.Name; persistenceApp.Version = app.Version; persistenceApp.State = app.State.ToString(); persistenceApp.FilePath = app.FileName; persistenceDb.Application.InsertOnSubmit(persistenceApp); Log.Info(typeof(Database), "New application " + app.Name + " added"); WriteTenants(persistenceDb, app); } else if (app.RowState == EntityState.Modified) { PersistenceStorage.Application persistenceApp = persistenceDb.Application.FirstOrDefault(a => a.ID == app.Id); persistenceApp.Name = app.Name; persistenceApp.Version = app.Version; persistenceApp.State = app.State.ToString(); persistenceApp.FilePath = app.FileName; Log.Info(typeof(Database), "Application " + app.Name + " updated"); WriteTenants(persistenceDb, app); } else if (app.RowState == EntityState.Removed) { string fileName = Path.GetFileName(app.FileName); string filePath = Path.Combine(Path.GetFullPath(Settings.ApplicationStoreFolder), fileName); PersistenceStorage.Application persistenceApp = persistenceDb.Application.FirstOrDefault(a => a.ID == app.Id); persistenceDb.Application.DeleteOnSubmit(persistenceApp); removedApps.Add(app); // Remove uploaded file if(File.Exists(filePath)) File.Delete(filePath); // Remove tenants foreach (Tenant t in app.Tenants) t.RowState = EntityState.Removed; WriteTenants(persistenceDb, app); } } foreach (Application removed in removedApps) { Database.GetInstance().Applications.Remove(removed); Log.Info(typeof(Database), "Application " + removed.Name + " removed"); } // Reload applications from the database on next call applicationsField = null; // Commit persistence storage persistenceDb.SubmitChanges(); } catch(Exception e) { Log.Error(typeof(Database), "Could not write application data", e); throw e; } }
private List<Application> ReadApplications() { Log.Info(typeof(Database), "Reading application data..."); try { using(var connection = new SqliteConnection(Settings.SQLiteConnectionString)) { PersistenceStorage.PersistentDataContext persistenceDb = new PersistenceStorage.PersistentDataContext(connection); List<Application> applications = new List<Application>(); foreach (PersistenceStorage.Application persistenceApp in persistenceDb.Application.ToList()) { Monoscape.Common.Model.Application app = new Monoscape.Common.Model.Application(); app.Id = persistenceApp.ID; app.Name = persistenceApp.Name; app.Version = persistenceApp.Version; app.State = ConvertApplicationState(persistenceApp.State.ToString()); app.FileName = persistenceApp.FilePath; app.Tenants = ConvertTenants(persistenceApp.Tenant); app.RowState = EntityState.Queried; applications.Add(app); } Log.Info(typeof(Database), applications.Count + " applications found"); return applications; } } catch(Exception e) { Log.Error(typeof(Database), "Could not connect to the database", e); throw e; } }
private void WriteApplications() { Log.Info(typeof(Database), "Writing application data..."); try { var connection = new SqliteConnection(Settings.SQLiteConnectionString); PersistenceStorage.PersistentDataContext persistenceDb = new PersistenceStorage.PersistentDataContext(connection); List <Application> removedApps = new List <Application>(); foreach (Application app in Applications) { if (app.RowState == EntityState.New) { PersistenceStorage.Application persistenceApp = new PersistenceStorage.Application(); persistenceApp.Name = app.Name; persistenceApp.Version = app.Version; persistenceApp.State = app.State.ToString(); persistenceApp.FilePath = app.FileName; persistenceDb.Application.InsertOnSubmit(persistenceApp); Log.Info(typeof(Database), "New application " + app.Name + " added"); WriteTenants(persistenceDb, app); } else if (app.RowState == EntityState.Modified) { PersistenceStorage.Application persistenceApp = persistenceDb.Application.FirstOrDefault(a => a.ID == app.Id); persistenceApp.Name = app.Name; persistenceApp.Version = app.Version; persistenceApp.State = app.State.ToString(); persistenceApp.FilePath = app.FileName; Log.Info(typeof(Database), "Application " + app.Name + " updated"); WriteTenants(persistenceDb, app); } else if (app.RowState == EntityState.Removed) { string fileName = Path.GetFileName(app.FileName); string filePath = Path.Combine(Path.GetFullPath(Settings.ApplicationStoreFolder), fileName); PersistenceStorage.Application persistenceApp = persistenceDb.Application.FirstOrDefault(a => a.ID == app.Id); persistenceDb.Application.DeleteOnSubmit(persistenceApp); removedApps.Add(app); // Remove uploaded file if (File.Exists(filePath)) { File.Delete(filePath); } // Remove tenants foreach (Tenant t in app.Tenants) { t.RowState = EntityState.Removed; } WriteTenants(persistenceDb, app); } } foreach (Application removed in removedApps) { Database.GetInstance().Applications.Remove(removed); Log.Info(typeof(Database), "Application " + removed.Name + " removed"); } // Reload applications from the database on next call applicationsField = null; // Commit persistence storage persistenceDb.SubmitChanges(); } catch (Exception e) { Log.Error(typeof(Database), "Could not write application data", e); throw e; } }