Exemplo n.º 1
0
        /// <summary>
        /// Load diagram from DB
        /// </summary>
        /// <param name="canvas">Canvas for diagram</param>
        /// <param name="data">XML attribute from DB</param>
        public async Task LoadDiagram(DesignerCanvas canvas, XDocument data)
        {
            var root = data.Root;

            ViewModel.LoadFromElement(data.Root);

            if (ViewModel.CanvasWidth != DatabaseModelDesignerViewModel.DefaultWidth || ViewModel.CanvasHeight != DatabaseModelDesignerViewModel.DefaultWidth)
            {
                StreamGeometry geometry = DesignerCanvas.CreateGridWithStreamGeometry(ViewModel.CanvasHeight, ViewModel.CanvasWidth,
                                                                                      DesignerCanvas.GridCellWidth);
                canvas.RefreshGuideLines(geometry);
            }

            var labels = root?.XPathSelectElements("LabelViewModels/LabelViewModel")
                         .Select(t =>
            {
                var vm = new LabelViewModel();
                vm.LoadFromElement(t);
                return(vm);
            }).ToList();

            if (labels != null && labels.Any())
            {
                foreach (LabelViewModel label in labels)
                {
                    ViewModel.LabelViewModels.Add(label);
                }
            }

            var ctx        = new DatabaseContext(SessionProvider.Instance.ConnectionType);
            var tablesInDb = await Task.Run(() => ctx.ListTables());

            var tableElements = root?.XPathSelectElements("TableViewModels/TableViewModel")
                                .Select(t =>
            {
                var vm = new TableViewModel();
                vm.LoadFromElement(t);
                return(vm);
            })
                                .Where(s => tablesInDb.Any(t => t.Id.Equals(s.Model.Id)))
                                .ToList();

            Output.WriteLine(DiagramLoaded);

            if (tableElements == null || !tableElements.Any())
            {
                return;
            }

            var relationShipsInDb = await Task.Run(() => ctx.ListAllForeignKeys());

            var allRelationshipsDetailsPom = new List <RelationshipModel>();

            foreach (TableViewModel model in tableElements)
            {
                TableModel tab = ctx.ReadTableDetails(model.Model.Id, model.Model.Title);
                model.Model.RefreshModel(tab);
                ViewModel.TableViewModels.Add(model);
            }

            foreach (TableViewModel model in tableElements)
            {
                IEnumerable <RelationshipModel> models = await Task.Run(() => ctx.ListRelationshipsForTable(model.Model.Title, tableElements.Select(s => s.Model)));

                allRelationshipsDetailsPom.AddRange(models);
            }

            var allRelationshipsDetails = allRelationshipsDetailsPom
                                          .GroupBy(t => t.Name)
                                          .Select(t => t.FirstOrDefault())
                                          .ToList();

            var relationElements = root?.XPathSelectElements("ConnectionInfoViewModels/ConnectionInfoViewModel")
                                   .Select(t =>
            {
                var vm = new ConnectionInfoViewModel {
                    DesignerCanvas = canvas
                };
                vm.LoadFromElement(t);
                return(vm);
            })
                                   .Where(t => relationShipsInDb.Any(s => s.Equals(t.RelationshipModel.Name)))
                                   .Where(t => allRelationshipsDetails.Any(s => ctx.AreRelationshipModelsTheSame(t.RelationshipModel, s)))
                                   .ToList();


            relationElements.ForEach(t =>
            {
                t.SourceViewModel      = tableElements.FirstOrDefault(s => s.Model.Id.Equals(t.RelationshipModel.Source.Id));
                t.DestinationViewModel = tableElements.FirstOrDefault(s => s.Model.Id.Equals(t.RelationshipModel.Destination.Id));
                t.RelationshipModel.RefreshModel(allRelationshipsDetails.FirstOrDefault(s => ctx.AreRelationshipModelsTheSame(t.RelationshipModel, s)));
                ViewModel.ConnectionInfoViewModels.Add(t);
                t.BuildLoadedConnection();
            });

            var newRelations =
                allRelationshipsDetails
                .Where(t => !relationElements.Any(s => s.RelationshipModel.Name.Equals(t.Name)))
                .Select(t =>
            {
                var vm = new ConnectionInfoViewModel
                {
                    DesignerCanvas       = canvas,
                    SourceViewModel      = tableElements.FirstOrDefault(s => s.Model.Id.Equals(t.Source.Id)),
                    DestinationViewModel = tableElements.FirstOrDefault(s => s.Model.Id.Equals(t.Destination.Id))
                };
                vm.RelationshipModel.RefreshModel(t);
                return(vm);
            })
                .ToList();

            newRelations.ForEach(async t =>
            {
                ViewModel.ConnectionInfoViewModels.Add(t);
                await t.BuildConnection3(ViewModel);
            });
        }