private void MarkRegionStructureForDeletion(GeoRegionViewModel region, RegionDeletionObjects deletionObjects) { // If it is a leaf level node, remove all mappings if (region.Level == MaintenanceConstants.GeoRegionMappingLevel) { // t031_geo_code is the central table that everything links to so we need to find the relevant entry and // one by one remove all dependencies on it first var geoCode = _repository.GetItems<t031_geo_code>().FirstOrDefault(g => g.t030_subarea_id == region.Id); if (geoCode != null) { // t032 table is a mapping between t031 and t033, remove these var metaregionmappings = _repository.GetItems<t032_meta_region_to_geo_code_xref>().Where(m => m.t031_id == geoCode.t031_id); deletionObjects.MetaRegionMappings.AddRange(metaregionmappings); // t013 table is a mpaping between t031 and t010, remove these var metariskregionperils = _repository.GetItems<t013_meta_risk_to_region_perils>().Where(m => m.t031_id == geoCode.t031_id); deletionObjects.MetaRiskPerilMappings.AddRange(metariskregionperils); // Now event loss footprint entries which depend on this table var eventlossfootprints1 = _repository.GetItems<t00001_event_loss_footprint>().Where(m => m.t031_id == geoCode.t031_id); deletionObjects.EventLossFootprintsOne.AddRange(eventlossfootprints1); // Second event set var eventlossfootprints2 = _repository.GetItems<t00002_event_loss_footprint>().Where(m => m.t031_id == geoCode.t031_id); deletionObjects.EventLossFootprintsTwo.AddRange(eventlossfootprints2); // Finally delete the t031 entry now all its dependences have been removed deletionObjects.GeoCodes.Add(geoCode); } } deletionObjects.GeoRegions.Add(region.GetOriginalRegionObject()); }
private void DeleteGeoRegionWorker() { var deletionObjects = new RegionDeletionObjects(); try { // Simple case, deleting a leaf level node if (SelectedGeoRegion.Level == MaintenanceConstants.GeoRegionMappingLevel) { MarkRegionStructureForDeletion(SelectedGeoRegion, deletionObjects); } else { // Deleting a sub tree, need to build up a list of leaf level nodes we need to delete first var geoRegionsToDelete = FindGeoCodeTree(SelectedGeoRegion).ToList(); foreach (var region in geoRegionsToDelete) { MarkRegionStructureForDeletion(region, deletionObjects); } } // Now delete everything in one go, much quicker to do it this way _repository.DeleteItems(deletionObjects.MetaRegionMappings); _repository.DeleteItems(deletionObjects.MetaRiskPerilMappings); _repository.DeleteItems(deletionObjects.EventLossFootprintsOne); _repository.DeleteItems(deletionObjects.EventLossFootprintsTwo); _repository.DeleteItems(deletionObjects.GeoCodes); _repository.DeleteItems(deletionObjects.GeoRegions); _repository.Save(); BusyHelper.RunGuiWork(() => SelectedGeoRegion.ParentNode.Children.Remove(SelectedGeoRegion)); BusyHelper.RunGuiWork( () => _eventAggregator.GetEvent<UserNotificationEvent>().Publish(new DialogParams("Delete successful", "Geo region deleted successfully"))); } catch (Exception ex) { BusyHelper.CleanUp(_eventAggregator); BusyHelper.RunGuiWork( () => _eventAggregator.GetEvent<UserNotificationEvent>().Publish(new DialogParams("Delete unsuccessful", "There was an error deleting this Geo region. " + ex.Message))); } }