/// <summary> /// Translate model EntityType into view EntityType (creates a view EntityType if not yet created) /// </summary> /// <param name="viewModel"></param> /// <param name="entityType"></param> /// <param name="processChildren"></param> /// <returns></returns> private static ViewModelEntityType TranslateEntityType(EntityDesignerViewModel viewModel, ConceptualEntityType entityType) { var viewET = ModelToDesignerModelXRef.GetNewOrExisting(viewModel.EditingContext, entityType, viewModel.Partition) as ViewModelEntityType; viewET.Name = entityType.LocalName.Value; return(viewET); }
internal static ModelToDesignerModelXRef GetModelToDesignerModelXRef(EditingContext context) { // Update EFObject to ModelElement cross reference so that Search Results can later access it var xref = context.Items.GetValue <ModelToDesignerModelXRef>(); if (xref == null) { xref = new ModelToDesignerModelXRef(); context.Items.SetValue(xref); } return(xref); }
/// <summary> /// Translate base type of a model EntityType into view Inheritance (creates an Inheritance if not yet created) /// </summary> /// <param name="viewModel"></param> /// <param name="entityType"></param> /// <returns></returns> private static Inheritance TranslateBaseType(EntityDesignerViewModel viewModel, ConceptualEntityType entityType) { if (entityType.BaseType.Status == BindingStatus.Known) { var baseType = ModelToDesignerModelXRef.GetExisting(viewModel.EditingContext, entityType.BaseType.Target, viewModel.Partition) as ViewModelEntityType; var derivedType = ModelToDesignerModelXRef.GetExisting(viewModel.EditingContext, entityType, viewModel.Partition) as ViewModelEntityType; // in Multiple diagram scenario, baseType and derivedType might not exist in the diagram. if (baseType != null && derivedType != null) { return (ModelToDesignerModelXRef.GetNewOrExisting(viewModel.EditingContext, entityType.BaseType, baseType, derivedType) as Inheritance); } } return(null); }
/// <summary> /// Translate model Property into view Property (creates a view Property if not yet created) /// </summary> /// <param name="viewEntityType"></param> /// <param name="property"></param> /// <returns></returns> private ViewModelProperty TranslateProperty(ViewModelEntityType viewEntityType, ModelProperty property) { var viewProperty = ModelToDesignerModelXRef.GetNewOrExisting(EditingContext, property, viewEntityType.Partition) as ViewModelProperty; var scalarProperty = viewProperty as ScalarProperty; if (scalarProperty != null) { // flag if we are part of the key scalarProperty.EntityKey = property.IsKeyProperty; } // set the other properties if they aren't null if (property.LocalName.Value != null) { viewProperty.Name = property.LocalName.Value; } viewProperty.Type = property.TypeName; return(viewProperty); }
private void TranslateEntityModel( IEnumerable <ModelEntityType> entityTypes, IEnumerable <ModelAssociation> associations, EntityDesignerViewModel entityViewModel) { // create each entity type and add its properties foreach (var et in entityTypes) { var cet = et as ConceptualEntityType; Debug.Assert(cet != null, "EntityType is not ConceptualEntityType"); var viewET = TranslateEntityType(entityViewModel, cet); entityViewModel.EntityTypes.Add(viewET); TranslatePropertiesOfEntityType(et, viewET); } // create any inheritance relationships foreach (var et in entityTypes) { var cet = et as ConceptualEntityType; Debug.Assert(cet != null, "EntityType is not ConceptualEntityType"); TranslateBaseType(entityViewModel, cet); } // create the associations foreach (var assoc in associations) { TranslateAssociation(entityViewModel, assoc); } // add navigation properties to the entities foreach (var et in entityTypes) { var viewET = ModelToDesignerModelXRef.GetNewOrExisting(entityViewModel.EditingContext, et, entityViewModel.Partition) as ViewModelEntityType; Debug.Assert(viewET != null, "Why wasn't the entity shape added already?"); TranslateNavigationPropertiesOfEntityType(et, viewET); } }
/// <summary> /// Translate model NavigationProperty into view NavigationProperty (creates a view NavigationProperty if not yet created) /// </summary> private ViewModelNavigationProperty TranslateNavigationProperty(ViewModelEntityType viewEntityType, ModelNavigationProperty navProp) { var viewNavProp = ModelToDesignerModelXRef.GetNewOrExisting(EditingContext, navProp, viewEntityType.Partition) as ViewModelNavigationProperty; Debug.Assert(viewNavProp != null, "Expected non-null navigation property"); viewNavProp.Name = navProp.LocalName.Value; if (navProp.Relationship.Status == BindingStatus.Known) { var association = ModelToDesignerModelXRef.GetExisting(EditingContext, navProp.Relationship.Target, viewEntityType.Partition) as ViewModelAssociation; // Association might be null here if the related entity does not exist in the current diagram. if (association != null) { viewNavProp.Association = association; // On a self-relationship case, we ensure that the Source and Target NavProps are not set with the same value. // The source is set first only if SourceEntity == TargetEntityType and the Source navprop has not been set yet. The other case would be // if this is not a self-relationship. if (viewEntityType == association.SourceEntityType && ((association.SourceEntityType == association.TargetEntityType && association.SourceNavigationProperty == null) || (association.SourceEntityType != association.TargetEntityType))) { association.SourceNavigationProperty = viewNavProp; } // SourceEntityType might be the same as TargetEntityType, so we need to check this as well if (viewEntityType == association.TargetEntityType) { association.TargetNavigationProperty = viewNavProp; } } } return(viewNavProp); }
internal override DslModeling.ModelElement TranslateModelToDslModel(EFObject modelElement, DslModeling.Partition partition) { DesignerModel.Diagram diagram = null; if (modelElement != null) { diagram = modelElement as DesignerModel.Diagram; if (diagram == null) { throw new ArgumentException("modelElement should be a diagram"); } } // get the service so that we can access the root of the entity model var service = _editingContext.GetEFArtifactService(); if (service == null) { throw new InvalidOperationException(EntityDesignerResources.Error_NoArtifactService); } EntityDesignerViewModel entityViewModel = null; var entityDesignArtifact = service.Artifact as EntityDesignArtifact; Debug.Assert(entityDesignArtifact != null, "Artifact is not type of EntityDesignArtifact"); if (entityDesignArtifact != null) { // Only translate the Escher Model to Dsl Model if the artifact is designer safe. if (entityDesignArtifact.IsDesignerSafe) { // now get the root of the model. var model = entityDesignArtifact.ConceptualModel; Debug.Assert(model != null, "Could not get ConceptualModel from the artifact."); if (model != null) { entityViewModel = ModelToDesignerModelXRef.GetNewOrExisting(_editingContext, model, partition) as EntityDesignerViewModel; entityViewModel.Namespace = model.Namespace.Value; // If the passed-in diagram is null, retrieve the first diagram if available. if (diagram == null && entityDesignArtifact.DesignerInfo() != null && entityDesignArtifact.DesignerInfo().Diagrams != null && entityDesignArtifact.DesignerInfo().Diagrams.FirstDiagram != null) { diagram = entityDesignArtifact.DesignerInfo().Diagrams.FirstDiagram; } IList <ModelEntityType> entities; IList <ModelAssociation> associations; if (diagram != null) { RetrieveModelElementsFromDiagram(diagram, out entities, out associations); } else { entities = model.EntityTypes().ToList(); associations = model.Associations().ToList(); } TranslateEntityModel(entities, associations, entityViewModel); } } else { // return empty view model if the artifact is not designer safe so the Diagram can show safe-mode watermark entityViewModel = new EntityDesignerViewModel(partition); entityViewModel.EditingContext = _editingContext; } } return(entityViewModel); }
/// <summary> /// Translate model Association into view Association (creates a view Association if not yet created) /// </summary> /// <param name="viewModel"></param> /// <param name="association"></param> /// <returns></returns> private static ViewModelAssociation TranslateAssociation( EntityDesignerViewModel viewModel, ModelAssociation association) { var ends = association.AssociationEnds(); var end1 = ends[0]; var end2 = ends[1]; if (end1.Type.Status == BindingStatus.Known && end2.Type.Status == BindingStatus.Known) { var viewEnd1 = ModelToDesignerModelXRef.GetExisting(viewModel.EditingContext, end1.Type.Target, viewModel.Partition) as ViewModelEntityType; var viewEnd2 = ModelToDesignerModelXRef.GetExisting(viewModel.EditingContext, end2.Type.Target, viewModel.Partition) as ViewModelEntityType; // Only create association if both entityType exist. if (viewEnd1 != null && viewEnd2 != null) { var viewAssoc = ModelToDesignerModelXRef.GetNewOrExisting(viewModel.EditingContext, association, viewEnd1, viewEnd2) as ViewModelAssociation; viewAssoc.Name = association.LocalName.Value; viewAssoc.SourceMultiplicity = end1.Multiplicity.Value; viewAssoc.TargetMultiplicity = end2.Multiplicity.Value; // There could be a situation where association is created after navigation property (for example: the user add an entity-type and then add related types ). // In that case we need to make sure that view's association and navigation property are linked. Debug.Assert( end1.Type.Target != null, "Association End: " + end1.DisplayName + " does not reference a valid entity-type."); if (end1.Type.Target != null) { var modelSourceNavigationProperty = ModelHelper.FindNavigationPropertyForAssociationEnd(end1.Type.Target as ConceptualEntityType, end1); if (modelSourceNavigationProperty != null) { var viewSourceNavigationProperty = ModelToDesignerModelXRef.GetExisting( viewModel.EditingContext, modelSourceNavigationProperty, viewModel.Partition) as ViewModelNavigationProperty; if (viewSourceNavigationProperty != null) { viewAssoc.SourceNavigationProperty = viewSourceNavigationProperty; viewSourceNavigationProperty.Association = viewAssoc; } } } Debug.Assert( end2.Type.Target != null, "Association End: " + end2.DisplayName + " does not reference a valid entity-type."); if (end2.Type.Target != null) { var modelTargetNavigatioNProperty = ModelHelper.FindNavigationPropertyForAssociationEnd(end2.Type.Target as ConceptualEntityType, end2); if (modelTargetNavigatioNProperty != null) { var viewTargetNavigationProperty = ModelToDesignerModelXRef.GetExisting( viewModel.EditingContext, modelTargetNavigatioNProperty, viewModel.Partition) as ViewModelNavigationProperty; if (viewTargetNavigationProperty != null) { viewAssoc.TargetNavigationProperty = viewTargetNavigationProperty; viewTargetNavigationProperty.Association = viewAssoc; } } } return(viewAssoc); } } return(null); }
internal static ModelToDesignerModelXRef GetModelToDesignerModelXRef(EditingContext context) { // Update EFObject to ModelElement cross reference so that Search Results can later access it var xref = context.Items.GetValue<ModelToDesignerModelXRef>(); if (xref == null) { xref = new ModelToDesignerModelXRef(); context.Items.SetValue(xref); } return xref; }