/// <summary>
        ///     This method reads the .diagram file and makes sure that shapes exist for every item
        ///     in the .diagram file.
        /// </summary>
        internal static void ReloadDiagram(EntityDesignerViewModel viewModel)
        {
            var diagram = viewModel.GetDiagram();
            if (diagram == null)
            {
                // Empty DSL diagram
                return;
            }

            diagram.ResetWatermark(diagram.ActiveDiagramView);

            // get our artifact
            var artifact = EditingContextManager.GetArtifact(viewModel.EditingContext);
            Debug.Assert(artifact != null);
            if (!artifact.IsDesignerSafe)
            {
                return;
            }

            var diagramModel = viewModel.ModelXRef.GetExisting(diagram) as DesignerModel.Diagram;

            if (diagramModel != null)
            {
                // ensure that we still have all of the parts we need to re-translate from the Model
                EntityModelToDslModelTranslatorStrategy.TranslateDiagram(diagram, diagramModel);
            }
            else
            {
                // this path will usually only happen if an UMFDB extension has deleted the diagram node
                // since we don't have a diagram anymore, lay it all out and create a new one
                if (diagram.ModelElement.EntityTypes.Count < EntityDesignerDiagram.IMPLICIT_AUTO_LAYOUT_CEILING)
                {
                    diagram.AutoLayoutDiagram();
                }
                EntityModelToDslModelTranslatorStrategy.CreateDefaultDiagram(viewModel.EditingContext, diagram);
            }

            // remove "Select All" selection
            if (diagram.ActiveDiagramView != null)
            {
                diagram.ActiveDiagramView.Selection.Set(new DiagramItem(diagram));
            }
        }
        /// <summary>
        ///     This method loads the DSL view model with the items in the artifact's C-Model.
        /// </summary>
        internal void ReloadModel(EntityDesignerViewModel viewModel)
        {
            var diagram = viewModel.GetDiagram();
            if (diagram == null)
            {
                // empty DSL diagram
                return;
            }

            // get our artifact
            var artifact = EditingContextManager.GetArtifact(viewModel.EditingContext) as EntityDesignArtifact;
            Debug.Assert(artifact != null);

            var serializationResult = new SerializationResult();

            var serializationContext = new SerializationContext(GetDirectory(viewModel.Store), artifact.Uri.LocalPath, serializationResult);
            var transactionContext = new TransactionContext();
            transactionContext.Add(SerializationContext.TransactionContextKey, serializationContext);

            var workaroundFixSerializationTransactionValue = false;
            if (viewModel.Store.PropertyBag.ContainsKey("WorkaroundFixSerializationTransaction"))
            {
                workaroundFixSerializationTransactionValue = (bool)viewModel.Store.PropertyBag["WorkaroundFixSerializationTransaction"];
            }

            try
            {
                // To fix performance issue during reload, we turn-off layout during "serialization".
                viewModel.Store.PropertyBag["WorkaroundFixSerializationTransaction"] = true;

                using (var t = viewModel.Store.TransactionManager.BeginTransaction("ReloadModel", true, transactionContext))
                {
                    if (artifact.ConceptualModel() == null)
                    {
                        return;
                    }

                    DesignerModel.Diagram diagramModel = null;

                    // If DiagramId is not string empty, try to get the diagram from the artifact. 
                    // There is a situation where we could not find the diagram given an ID (for example: EDMX Model's Diagram that is created by VS before SQL 11; 
                    // In that case, we assign temporary ID to the diagram and a new ID will be generated every time the model is reloaded.)
                    // We could safely choose the first diagram since multiple diagrams feature didn't exist in VS prior to SQL11 release.
                    if (!string.IsNullOrEmpty(diagram.DiagramId))
                    {
                        diagramModel = artifact.DesignerInfo.Diagrams.GetDiagram(diagram.DiagramId);
                    }

                    if (diagramModel == null)
                    {
                        diagramModel = artifact.DesignerInfo.Diagrams.FirstDiagram;
                    }

                    if (diagramModel != null)
                    {
                        // Re-establish the xref between Escher conceptual model and DSL root model.
                        // and between Escher Diagram model and DSL diagram model.

                        Debug.Assert(viewModel.ModelXRef != null, "Why ModelXRef is null?");
                        if (viewModel.ModelXRef != null)
                        {
                            viewModel.ModelXRef.Add(artifact.ConceptualModel(), viewModel, viewModel.EditingContext);
                            viewModel.ModelXRef.Add(diagramModel, diagram, viewModel.EditingContext);
                            ModelTranslatorContextItem.GetEntityModelTranslator(viewModel.EditingContext)
                                .TranslateModelToDslModel(diagramModel, viewModel.Partition);
                        }
                    }

                    if (t.IsActive)
                    {
                        t.Commit();
                    }
                }
            }
            finally
            {
                viewModel.Store.PropertyBag["WorkaroundFixSerializationTransaction"] = workaroundFixSerializationTransactionValue;
            }
        }
        /// <summary>
        ///     This method will remove all child shapes of the EntityDesignerDiagram.
        /// </summary>
        internal static void ClearDiagram(EntityDesignerViewModel viewModel)
        {
            var diagram = viewModel.GetDiagram();
            if (diagram != null)
            {
                using (var t = viewModel.Store.TransactionManager.BeginTransaction("ClearDiagram", true))
                {
                    // make copy of AllElements so that we don't modify the collection while we are iterating over it.
                    // We are only interested to the model elements that belong to the viewModel; so look at the Partition's ElementDirectory
                    // instead of Store's ElementDirectory which is shared across diagram.
                    Debug.Assert(viewModel.Partition != null, "ViewModel's Partition should never be null.");
                    if (viewModel.Partition != null)
                    {
                        var elementDirectory = viewModel.Partition.ElementDirectory;
                        Debug.Assert(
                            elementDirectory != null, "ElementDirectory in partition for view model :" + diagram.Title + " is null.");
                        if (elementDirectory != null)
                        {
                            var allElements = new List<ModelElement>(elementDirectory.AllElements.Count);
                            allElements.AddRange(elementDirectory.AllElements);

                            foreach (var melem in allElements)
                            {
                                // don't delete our diagram, but remove every other presentation element
                                if (melem is PresentationElement
                                    && (melem is EntityDesignerDiagram) == false)
                                {
                                    melem.Delete();
                                }
                            }
                        }
                    }

                    if (t.IsActive)
                    {
                        t.Commit();
                    }
                }
            }
        }
        /// <summary>
        ///     This method removes all of the child elements of the EntityDesignerViewModel.
        /// </summary>
        internal static void ClearModel(EntityDesignerViewModel viewModel)
        {
            // The assumption is that if ModelXRef is null or we cannot get the diagram, DSL Model is empty
            if (viewModel.ModelXRef == null
                || viewModel.GetDiagram() == null)
            {
                return;
            }

            using (var t = viewModel.Store.TransactionManager.BeginTransaction("ClearModel", true))
            {
                // delete inheritance first
                foreach (var melem in viewModel.ModelXRef.ReferencedViewElements)
                {
                    if (melem is Inheritance)
                    {
                        melem.Delete();
                    }
                }

                // delete associations next
                foreach (var melem in viewModel.ModelXRef.ReferencedViewElements)
                {
                    if (melem is Association)
                    {
                        melem.Delete();
                    }
                }

                // delete entities last
                foreach (var melem in viewModel.ModelXRef.ReferencedViewElements)
                {
                    if (melem is EntityType)
                    {
                        melem.Delete();
                    }
                }

                // clear out the XRef
                viewModel.ModelXRef.Clear();

                if (t.IsActive)
                {
                    t.Commit();
                }
            }
        }
        private static void TranslateDiagramObjectHelper(
            EntityDesignerViewModel viewModel, ModelDiagram.BaseDiagramObject modelDiagramEFObject,
            EFObject modelObjectToFindViewModel, bool updateShapeElements, UpdateShapeInfoCallback updateShapeInfoCallback)
        {
            var diagram = viewModel.GetDiagram();
            EFObject diagramEFObject = modelDiagramEFObject;
            Debug.Assert(diagram != null, "Where is the DSL diagram?");
            Debug.Assert(diagramEFObject != null, "Where is the EFObject corresponding to the diagram?");

            if (diagram != null
                && diagramEFObject != null)
            {
                var shapeElement = viewModel.ModelXRef.GetExisting(diagramEFObject) as ViewModelDiagram.ShapeElement;
                if (shapeElement == null)
                {
                    // find the view model associated with the model EFObject
                    var viewModelElement = viewModel.ModelXRef.GetExisting(modelObjectToFindViewModel);
                    Debug.Assert(viewModelElement != null, "Where is the view model for the model object?");

                    if (viewModelElement != null)
                    {
                        // get the shape element fromm the view model
                        shapeElement = diagram.FindShape(viewModelElement);
                        Debug.Assert(shapeElement != null, "Where is the DSL ShapeElement for the view model?");

                        if (shapeElement != null)
                        {
                            // associate the designer model EFObject with the shape element
                            if ((viewModel.ModelXRef.GetExisting(diagramEFObject) == null)
                                && (viewModel.ModelXRef.GetExisting(shapeElement) == null))
                            {
                                viewModel.ModelXRef.Add(diagramEFObject, shapeElement, viewModel.EditingContext);
                            }
                        }
                    }
                }

                // update the shape information for the element
                if (updateShapeElements && shapeElement != null)
                {
                    updateShapeInfoCallback(shapeElement);
                }
            }
        }
        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;
        }