private static void TranslateAssociationConnectors(
            AssociationConnector dslAssociationConnector, DesignerModel.AssociationConnector modelAssociationConnector,
            IList<ViewModelDiagram.ShapeElement> shapesToAutoLayout)
        {
            dslAssociationConnector.ManuallyRouted = modelAssociationConnector.ManuallyRouted.Value;
            // if the EdgePoint values is an empty collection or connectors are not manually routed, add the shape element to the collections that will be autolayout.
            if (modelAssociationConnector.ManuallyRouted.Value == false
                || modelAssociationConnector.ConnectorPoints.Count == 0)
            {
                if (shapesToAutoLayout.Contains(dslAssociationConnector) == false)
                {
                    shapesToAutoLayout.Add(dslAssociationConnector);
                }
            }
            else
            {
                var collection = new ViewModelDiagram.EdgePointCollection();
                foreach (var connectorPoint in modelAssociationConnector.ConnectorPoints)
                {
                    collection.Add(
                        new ViewModelDiagram.EdgePoint(connectorPoint.PointX.Value, connectorPoint.PointY.Value, VGPointType.Normal));
                }

                if (AreEdgePointsIdentical(dslAssociationConnector.EdgePoints, collection) == false)
                {
                    dslAssociationConnector.EdgePoints = collection;
                }
            }
        }
        internal static void TranslateDiagram(EntityDesignerDiagram diagram, DesignerModel.Diagram modelDiagram)
        {
            var viewModel = diagram.ModelElement;
            viewModel.ModelXRef.Add(modelDiagram, diagram, viewModel.EditingContext);

            using (var t = diagram.Store.TransactionManager.BeginTransaction("Translate diagram", true))
            {
                // list of shapes that don't have corresponding element in model and require auto-layout
                var shapesToAutoLayout = new List<ViewModelDiagram.ShapeElement>();

                // try to find object in model for each shape on a diagram
                foreach (var shapeElement in diagram.NestedChildShapes)
                {
                    var entityShape = shapeElement as EntityTypeShape;
                    if (entityShape != null
                        && entityShape.ModelElement != null)
                    {
                        var modelEntity = viewModel.ModelXRef.GetExisting(entityShape.ModelElement) as ModelEntityType;
                        if (modelEntity != null)
                        {
                            var modelEntityTypeShape =
                                modelDiagram.EntityTypeShapes.FirstOrDefault(ets => ets.EntityType.Target == modelEntity);
                            if (modelEntityTypeShape != null)
                            {
                                viewModel.ModelXRef.Add(modelEntityTypeShape, entityShape, viewModel.EditingContext);
                                var rectangle = new ViewModelDiagram.RectangleD(
                                    modelEntityTypeShape.PointX.Value, modelEntityTypeShape.PointY.Value
                                    , modelEntityTypeShape.Width.Value, 0.0);
                                entityShape.AbsoluteBounds = rectangle;
                                entityShape.IsExpanded = modelEntityTypeShape.IsExpanded.Value;
                                entityShape.FillColor = modelEntityTypeShape.FillColor.Value;
                            }
                        }
                        if (viewModel.ModelXRef.GetExisting(entityShape) == null)
                        {
                            shapesToAutoLayout.Add(entityShape);
                        }
                        continue;
                    }

                    var associationConnector = shapeElement as AssociationConnector;
                    if (associationConnector != null
                        && associationConnector.ModelElement != null)
                    {
                        var modelAssociation = viewModel.ModelXRef.GetExisting(associationConnector.ModelElement) as ModelAssociation;
                        if (modelAssociation != null)
                        {
                            var modelAssociationConnector =
                                modelDiagram.AssociationConnectors.FirstOrDefault(ac => ac.Association.Target == modelAssociation);
                            if (modelAssociationConnector != null)
                            {
                                viewModel.ModelXRef.Add(modelAssociationConnector, associationConnector, viewModel.EditingContext);
                                TranslateAssociationConnectors(associationConnector, modelAssociationConnector, shapesToAutoLayout);
                            }
                        }
                        continue;
                    }

                    var inheritanceConnector = shapeElement as InheritanceConnector;
                    if (inheritanceConnector != null
                        && inheritanceConnector.ModelElement != null)
                    {
                        var entityTypeBase = viewModel.ModelXRef.GetExisting(inheritanceConnector.ModelElement) as EntityTypeBaseType;
                        var modelEntity = entityTypeBase.Parent as ModelEntityType;
                        if (modelEntity != null)
                        {
                            var modelInheritanceConnector =
                                modelDiagram.InheritanceConnectors.FirstOrDefault(ic => ic.EntityType.Target == modelEntity);
                            if (modelInheritanceConnector != null)
                            {
                                viewModel.ModelXRef.Add(modelInheritanceConnector, inheritanceConnector, viewModel.EditingContext);
                                TranslateInheritanceConnectors(inheritanceConnector, modelInheritanceConnector, shapesToAutoLayout);
                            }
                        }
                        continue;
                    }
                }

                diagram.AutoLayoutDiagram(shapesToAutoLayout);

                // initiate zoom level, grid and scalar property options
                diagram.ZoomLevel = modelDiagram.ZoomLevel.Value;
                diagram.ShowGrid = modelDiagram.ShowGrid.Value;
                diagram.SnapToGrid = modelDiagram.SnapToGrid.Value;
                diagram.DisplayNameAndType = modelDiagram.DisplayType.Value;
                diagram.DiagramId = modelDiagram.Id.Value;
                diagram.Title = modelDiagram.Name.Value;

                t.Commit();
            }
        }
        private static void RetrieveModelElementsFromDiagram(
            DesignerModel.Diagram diagram,
            out IList<ModelEntityType> entityTypes,
            out IList<ModelAssociation> associations)
        {
            entityTypes = new List<ModelEntityType>();
            associations = new List<ModelAssociation>();
            var elementsToDelete = new List<EFElement>();

            foreach (var entityTypeShape in diagram.EntityTypeShapes)
            {
                if (entityTypeShape.EntityType != null
                    && entityTypeShape.EntityType.Status == BindingStatus.Known)
                {
                    if (entityTypes.Contains(entityTypeShape.EntityType.Target))
                    {
                        elementsToDelete.Add(entityTypeShape);
                    }
                    else
                    {
                        entityTypes.Add(entityTypeShape.EntityType.Target);
                    }
                }
            }

            foreach (var associationConnector in diagram.AssociationConnectors)
            {
                if (associationConnector.Association != null
                    && associationConnector.Association.Status == BindingStatus.Known)
                {
                    if (associations.Contains(associationConnector.Association.Target))
                    {
                        elementsToDelete.Add(associationConnector);
                    }
                    else
                    {
                        associations.Add(associationConnector.Association.Target);
                    }
                }
            }

            if (elementsToDelete.Any())
            {
                Debug.Fail(
                    string.Format(
                        CultureInfo.CurrentCulture, "Found {0} duplicate items in diagram, which will be deleted as part of loading: {1}",
                        elementsToDelete.Count, string.Join(",", elementsToDelete.Select(e => e.DisplayName))));

                if (diagram.Artifact.CanEditArtifact())
                {
                    // Don't use an EfiTransaction here, since we just want to clean up the XML and avoid triggering diagram ui deletes
                    using (var tx = diagram.Artifact.XmlModelProvider.BeginTransaction("LoadDiagram", "Remove duplicate diagram elements"))
                    {
                        foreach (var element in elementsToDelete)
                        {
                            element.Delete();
                        }

                        tx.Commit();
                    }
                }
                else
                {
                    Debug.Fail(
                        string.Format(
                            CultureInfo.CurrentCulture,
                            "Could not delete duplicate diagram items, proceeding with load and ignoring duplicates"));
                }
            }
        }
        private static EntityDesignerDiagram TranslateDiagramValues(EntityDesignerViewModel viewModel, DesignerModel.Diagram modelDiagram)
        {
            var diagram = viewModel.GetDiagram();

            Debug.Assert(diagram != null, "Why diagram is null?");
            if (diagram != null)
            {
                if (viewModel.ModelXRef.ContainsKey(modelDiagram) == false)
                {
                    viewModel.ModelXRef.Add(modelDiagram, diagram, viewModel.EditingContext);
                }

                using (var t = diagram.Store.TransactionManager.BeginTransaction("Translate diagram values", true))
                {
                    // set zoom level, grid and scalar property options
                    diagram.ZoomLevel = modelDiagram.ZoomLevel.Value;
                    diagram.ShowGrid = modelDiagram.ShowGrid.Value;
                    diagram.SnapToGrid = modelDiagram.SnapToGrid.Value;
                    diagram.DisplayNameAndType = modelDiagram.DisplayType.Value;
                    diagram.Title = modelDiagram.Name.Value;
                    diagram.DiagramId = modelDiagram.Id.Value;
                    t.Commit();
                }
            }
            return diagram;
        }