private EntityDesignerDiagram GetNewOrExistingViewDiagram() { EntityDesignerDiagram diagram = null; // Check whether the EntityDesignerDiagram exists. var docData = DocData as MicrosoftDataEntityDesignDocDataBase; diagram = GetExistingViewDiagram(docData, _diagramId); // Create DSL diagram if the diagram has not been created yet. if (diagram == null && String.IsNullOrEmpty(_diagramId) == false) { var artifact = Context.GetEFArtifactService().Artifact as EntityDesignArtifact; Debug.Assert(artifact != null, "Could not find instance of EntityDesignArtifact in EditingContext."); if (artifact != null) { if (artifact.IsDesignerSafe && artifact.DesignerInfo != null && artifact.DesignerInfo.Diagrams != null) { // Get the corresponding model diagram. var modelDiagram = artifact.DesignerInfo.Diagrams.GetDiagram(_diagramId); if (modelDiagram != null) { using (var transaction = docData.Store.TransactionManager.BeginTransaction("OpenView", true)) { var evm = ModelTranslatorContextItem.GetEntityModelTranslator(Context) .TranslateModelToDslModel(modelDiagram, new DslModeling.Partition(docData.Store)) as EntityDesignerViewModel; diagram = CreateDslDiagram(evm); transaction.Commit(); } } } else { using (var transaction = docData.Store.TransactionManager.BeginTransaction("OpenView", true)) { var evm = ModelTranslatorContextItem.GetEntityModelTranslator(Context) .TranslateModelToDslModel(null, new DslModeling.Partition(docData.Store)) as EntityDesignerViewModel; diagram = CreateDslDiagram(evm); transaction.Commit(); } } } } return(diagram); }
internal override EntityDesignerViewModel LoadModel( SerializationResult serializationResult, Partition partition, string fileName, ISchemaResolver schemaResolver, ValidationController validationController, ISerializerLocator serializerLocator) { var docData = VSHelpers.GetDocData(PackageManager.Package, fileName) as IEntityDesignDocData; docData.CreateAndLoadBuffer(); EntityDesignerViewModel evm = null; var serializationContext = new SerializationContext(GetDirectory(partition.Store), fileName, serializationResult); var transactionContext = new TransactionContext(); transactionContext.Add(SerializationContext.TransactionContextKey, serializationContext); using (var t = partition.Store.TransactionManager.BeginTransaction("Load Model from " + fileName, true, transactionContext)) { var uri = Tools.XmlDesignerBase.Base.Util.Utils.FileName2Uri(fileName); var context = PackageManager.Package.DocumentFrameMgr.EditingContextManager.GetNewOrExistingContext(uri); evm = ModelTranslatorContextItem.GetEntityModelTranslator(context).TranslateModelToDslModel(null, partition) as EntityDesignerViewModel; if (evm == null) { serializationResult.Failed = true; } else { if (t.IsActive) { t.Commit(); } } } // Validate imported model if (!serializationResult.Failed && (validationController != null)) { validationController.Validate(partition, ValidationCategories.Load); } return(evm); }
public void EnsureDiagramIsCreated(EFArtifact artifact) { if (RootElement != null) { var modelRoot = RootElement as EntityDesignerViewModel; if (modelRoot != null) { var diagram = modelRoot.GetDiagram(); Debug.Assert(diagram != null, "DSL Diagram should have been created by now"); if (diagram != null) { Debug.Assert(artifact.DesignerInfo() != null, "artifact.DesignerInfo should not be null"); Debug.Assert(artifact.DesignerInfo().Diagrams != null, "artifact.DesignerInfo.Diagrams should not be null"); if (artifact.DesignerInfo() != null && artifact.DesignerInfo().Diagrams != null) { var saveDiagramDocument = false; if (artifact.DesignerInfo().Diagrams.FirstDiagram == null) { // layout is very slow. Only auto-layout if less than a max number of types if (diagram.ModelElement.EntityTypes.Count < EntityDesignerDiagram.IMPLICIT_AUTO_LAYOUT_CEILING) { diagram.AutoLayoutDiagram(); } try { EntityDesignerViewModel.RespondToModelChanges = false; EntityModelToDslModelTranslatorStrategy.CreateDefaultDiagram(modelRoot.EditingContext, diagram); // Ensure that DSL Diagram and Model Diagram are in sync: // - Update xref between 2 diagrams // - Propagetes model diagram info to DSL Diagram. var modelDiagram = artifact.DesignerInfo().Diagrams.FirstDiagram; Debug.Assert(modelDiagram != null, "modelDiagram should not be null"); if (modelDiagram != null) { ModelTranslatorContextItem.GetEntityModelTranslator(EditingContext) .SynchronizeSingleDslModelElement(modelRoot, modelDiagram); } } finally { EntityDesignerViewModel.RespondToModelChanges = true; } // save the file after adding Diagram element so it won't open as a dirty document saveDiagramDocument = true; } if (saveDiagramDocument) { var rdt = new RunningDocumentTable(ServiceProvider); rdt.SaveFileIfDirty(FileName); } _isDiagramLoaded = true; } } } } }
/// <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; } }