示例#1
0
 public GeoRegionViewModel(GeoRegionViewModel parentNode)
 {
     Level = parentNode.Level + 1;
     Parent = parentNode.Id;
     ParentNode = parentNode;
     Children = new ObservableCollection<GeoRegionViewModel>();
 }
        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 AddGeoRegions(IEnumerable<t030_geo_region> data)
        {
            _geoRegions.Clear();

            var treeData = new Dictionary<int, List<GeoRegionViewModel>>();
            var rootData = new List<GeoRegionViewModel>();
            GeoRegionViewModel rootNode = null;

            // To save on nested LINQ searching, populate a hierachy bucket model first
            foreach (var region in data)
            {
                if (!region.t030_parent_id.HasValue)
                {
                    // This is the top level root node, only one of these
                    rootNode = new GeoRegionViewModel(region);
                    continue;
                }

                if (region.t030_level_no == 0)
                {
                    // This is one of the first level regions directly under the root, use these as our tree seeds
                    rootData.Add(new GeoRegionViewModel(region));
                    continue;
                }

                // Otherwise it is a lower level node, record which parent bucket it is under
                var parentId = region.t030_parent_id.Value;
                if (!treeData.ContainsKey(parentId))
                    treeData.Add(parentId, new List<GeoRegionViewModel>());

                treeData[parentId].Add(new GeoRegionViewModel(region));
            }

            // If we have not found a root node, this is not valid
            if (rootNode == null) throw new InvalidDataException("You must have a root level node defined");

            // Now populate the actual tree
            foreach (var topLevel in rootData)
            {
                foreach (var child in GetChildren(topLevel.Id, treeData))
                {
                    child.ParentNode = topLevel;
                    topLevel.Children.Add(child);
                }

                topLevel.ParentNode = rootNode;
                rootNode.Children.Add(topLevel);
            }

            _geoRegions.Add(rootNode);
        }
        private static IList<GeoRegionViewModel> FindGeoCodeTree(GeoRegionViewModel region)
        {
            var codes = new List<GeoRegionViewModel> {region};

            if (region.Level != MaintenanceConstants.GeoRegionMappingLevel)
            {
                foreach (var nodes in region.Children.Select(FindGeoCodeTree))
                {
                    codes.AddRange(nodes);
                }
            }

            return codes;
        }
        private void SaveGeoRegion()
        {
            try
            {
                if (!ValidateFields()) return;

                if (!_editMode)
                {
                    // This is a new Meta-Region creation
                    var geoRegion = SelectedGeoRegion.GetNewRegionObject();
                    _repository.InsertItem(geoRegion);

                    // This is a leaf level node so we need a t031 entry too
                    if (geoRegion.t030_level_no.HasValue && geoRegion.t030_level_no.Value == MaintenanceConstants.GeoRegionMappingLevel)
                    {
                        var geoCode = new t031_geo_code
                                          {
                                              t030_subarea_id = geoRegion.t030_id,
                                              t030_subarea_code = geoRegion.t030_code,
                                              t030_subarea_name = geoRegion.t030_name,
                                              t030_area_id = SelectedGeoRegion.ParentNode.Id,
                                              t030_area_code = SelectedGeoRegion.ParentNode.Code,
                                              t030_area_name = SelectedGeoRegion.ParentNode.Name,
                                              t030_country_id = SelectedGeoRegion.ParentNode.ParentNode.Id,
                                              t030_country_code = SelectedGeoRegion.ParentNode.ParentNode.Code,
                                              t030_country_name = SelectedGeoRegion.ParentNode.ParentNode.Name,
                                              t030_region_id = SelectedGeoRegion.ParentNode.ParentNode.ParentNode.Id,
                                              t030_region_code = SelectedGeoRegion.ParentNode.ParentNode.ParentNode.Code,
                                              t030_region_name = SelectedGeoRegion.ParentNode.ParentNode.ParentNode.Name,
                                              t031_active_flag = "Y"
                                          };

                        _repository.InsertItem(geoCode);
                    }

                    _repository.Save();
                    SelectedGeoRegion.SetSaved();

                    var newNode = new GeoRegionViewModel(geoRegion) {ParentNode = SelectedGeoRegion.ParentNode};
                    SelectedGeoRegion.ParentNode.Children.Add(newNode);

                    _eventAggregator.GetEvent<UserNotificationEvent>().Publish(new DialogParams("Save successful", "Geo Region created successfully"));
                }
                else
                {
                    var geoRegion = SelectedGeoRegion.GetUpdatedRegionObject();
                    _repository.UpdateItem(geoRegion, geoRegion.t030_id);

                    // This is a leaf level node so we need to update the t031 entry too
                    if (geoRegion.t030_level_no.HasValue && geoRegion.t030_level_no.Value == MaintenanceConstants.GeoRegionMappingLevel)
                    {
                        var geoCode =
                            _repository.GetItems<t031_geo_code>().FirstOrDefault(
                                g => g.t030_subarea_id == geoRegion.t030_id);

                        if (geoCode != null)
                        {
                            geoCode.t030_subarea_code = geoRegion.t030_code;
                            geoCode.t030_subarea_name = geoRegion.t030_name;
                            _repository.UpdateItem(geoCode, geoCode.t031_id);
                        }
                    }

                    _repository.Save();
                    SelectedGeoRegion.SetSaved();

                    _eventAggregator.GetEvent<UserNotificationEvent>().Publish(new DialogParams("Update successful", "Geo Region updated successfully"));
                }

                _moduleController.SwitchPanel(MaintenanceRegionNames.GeoRegionRegion, MaintenanceViewNames.GeoRegionView);
            }
            catch (Exception ex)
            {
                _eventAggregator.GetEvent<UserNotificationEvent>().Publish(new DialogParams("Save unsuccessful", "There was an error saving this Meta-Region. " + ex.Message));
            }
        }
 private void EditGeoRegion(GeoRegionViewModel region)
 {
     EditMode = true;
     region.ResetEditValues();
     SelectedGeoRegion = region;
 }
 private void CreateNewGeoRegion(GeoRegionViewModel region)
 {
     EditMode = false;
     region.ResetEditValues();
     SelectedGeoRegion = new GeoRegionViewModel(region);
 }