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 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; } }