protected override void InvokeInternal(CommandProcessorContext cpc) { if (_diagram != null) { // Add diagram id information in the transaction context. // This is to ensure the diagram objects are created correctly. if (cpc.EfiTransaction.GetContextValue<DiagramContextItem>(EfiTransactionOriginator.TransactionOriginatorDiagramId) == null) { cpc.EfiTransaction.AddContextValue( EfiTransactionOriginator.TransactionOriginatorDiagramId, new DiagramContextItem(_diagram.Id.Value)); } } var service = cpc.EditingContext.GetEFArtifactService(); var artifact = service.Artifact; // the model that we want to add the entity to var model = ModelHelper.GetEntityModel(artifact, _modelSpace); var entitiesMap = new Dictionary<EntityTypeClipboardFormat, EntityType>(_clipboardEntities.ClipboardEntities.Count); // create copies of EntityTypes foreach (var clipboardEntity in _clipboardEntities.ClipboardEntities) { var cmd = new CopyEntityCommand(_diagram, clipboardEntity, _modelSpace); CommandProcessor.InvokeSingleCommand(cpc, cmd); entitiesMap.Add(clipboardEntity, cmd.EntityType); } // create copies of associations foreach (var clipboardAssociation in _clipboardEntities.ClipboardAssociations) { // Check if the association is in the Artifact/model. if (_diagram != null) { // Get the association by name. var association = artifact.ArtifactSet.LookupSymbol(clipboardAssociation.NormalizedName) as Association; if (association != null) { var entityTypesInAssociation = association.AssociationEnds().Select(ae => ae.Type.Target).ToList(); // Check whether the associated entity-types are created in the previous step. // When the user copy and paste an association and the associated entities in the same diagram, // we need to create a new copy of the association in the model. Without the check below, the code will determine that there is no need // to create the copy since the association exist in the model. if (entityTypesInAssociation.Except(entitiesMap.Values).Count() == 0) { // At this point we know that the association that is referred in clipboard exists in the current model. // Next we will check whether this association is represented in diagram or not. // if not, create a new association connector in the diagram. if (association.GetAntiDependenciesOfType<AssociationConnector>() .Count(ac => ac.Diagram.Id == _diagram.Id.Value) == 0) { // AssociationConnector is created by creating EntityTypeShapes that associationEnd refer to. foreach (var associationEnd in association.AssociationEnds()) { var entityType = associationEnd.Type.SafeTarget as ConceptualEntityType; Debug.Assert( entityType != null, "In: CopyEntitiesCommand's InvokeInternal, associationEnd's Type property should be typeof ConceptualEntityType"); if (entityType != null) { // CreateEntityTypeShapeAndConnectorsInDiagram method will check if the shape for the entity-type has been created; // and it will not create one if the shape already exists in the diagram. // Also, VerifyDiagramModelIntegrityVisitor will assert if there are duplicate diagram shapes (shapes that point to the same model element) // every-time a command transaction is committed. So adding another check to do the same thing here is redundant. CreateEntityTypeShapeCommand.CreateEntityTypeShapeAndConnectorsInDiagram( cpc, _diagram, entityType, false); } } } continue; } } } CopyAssociation(cpc, model, clipboardAssociation, entitiesMap); } // create copies of inheritances foreach (var inheritance in _clipboardEntities.ClipboardInheritances) { if (_diagram != null) { // Check if the underlying entity types are in the artifact/model to do that we need to: // - Get the Entity-Types by name. // - Check whether the entity-types match what were created in copy entities steps. var derivedEntity = artifact.ArtifactSet.LookupSymbol(inheritance.Key.NormalizedName) as EntityType; var baseEntity = artifact.ArtifactSet.LookupSymbol(inheritance.Value.NormalizedName) as EntityType; if (derivedEntity != null && baseEntity != null) { if (entitiesMap.Values.Contains(derivedEntity) && entitiesMap.Values.Contains(baseEntity)) { // check if the underlying entity-types are not in the diagram. // InheritanceConnector are created by ensuring both EntityTypeShapes are created. if (derivedEntity.GetAntiDependenciesOfType<EntityTypeShape>().Count(ets => ets.Diagram.Id == _diagram.Id.Value) == 0 && baseEntity.GetAntiDependenciesOfType<EntityTypeShape>() .Count(ets2 => ets2.Diagram.Id == _diagram.Id.Value) == 0) { // CreateEntityTypeShapeAndConnectorsInDiagram method will check if the shape for the entity-type has been created; // and it will not create one if the shape already exists in the diagram. // Also, VerifyDiagramModelIntegrityVisitor will assert if there are duplicate diagram shapes (shapes that point to the same model element) // every-time a command transaction is committed. So adding another check to do the same thing here is redundant. CreateEntityTypeShapeCommand.CreateEntityTypeShapeAndConnectorsInDiagram( cpc, _diagram, derivedEntity as ConceptualEntityType, false); CreateEntityTypeShapeCommand.CreateEntityTypeShapeAndConnectorsInDiagram( cpc, _diagram, baseEntity as ConceptualEntityType, false); } continue; } } } CopyInheritance(cpc, inheritance, entitiesMap); } }
protected override void InvokeInternal(CommandProcessorContext cpc) { if (_diagram != null) { // Add diagram id information in the transaction context. // This is to ensure the diagram objects are created correctly. if (cpc.EfiTransaction.GetContextValue <DiagramContextItem>(EfiTransactionOriginator.TransactionOriginatorDiagramId) == null) { cpc.EfiTransaction.AddContextValue( EfiTransactionOriginator.TransactionOriginatorDiagramId, new DiagramContextItem(_diagram.Id.Value)); } } var service = cpc.EditingContext.GetEFArtifactService(); var artifact = service.Artifact; // the model that we want to add the entity to var model = ModelHelper.GetEntityModel(artifact, _modelSpace); var entitiesMap = new Dictionary <EntityTypeClipboardFormat, EntityType>(_clipboardEntities.ClipboardEntities.Count); // create copies of EntityTypes foreach (var clipboardEntity in _clipboardEntities.ClipboardEntities) { var cmd = new CopyEntityCommand(_diagram, clipboardEntity, _modelSpace); CommandProcessor.InvokeSingleCommand(cpc, cmd); entitiesMap.Add(clipboardEntity, cmd.EntityType); } // create copies of associations foreach (var clipboardAssociation in _clipboardEntities.ClipboardAssociations) { // Check if the association is in the Artifact/model. if (_diagram != null) { // Get the association by name. var association = artifact.ArtifactSet.LookupSymbol(clipboardAssociation.NormalizedName) as Association; if (association != null) { var entityTypesInAssociation = association.AssociationEnds().Select(ae => ae.Type.Target).ToList(); // Check whether the associated entity-types are created in the previous step. // When the user copy and paste an association and the associated entities in the same diagram, // we need to create a new copy of the association in the model. Without the check below, the code will determine that there is no need // to create the copy since the association exist in the model. if (entityTypesInAssociation.Except(entitiesMap.Values).Count() == 0) { // At this point we know that the association that is referred in clipboard exists in the current model. // Next we will check whether this association is represented in diagram or not. // if not, create a new association connector in the diagram. if (association.GetAntiDependenciesOfType <AssociationConnector>() .Count(ac => ac.Diagram.Id == _diagram.Id.Value) == 0) { // AssociationConnector is created by creating EntityTypeShapes that associationEnd refer to. foreach (var associationEnd in association.AssociationEnds()) { var entityType = associationEnd.Type.SafeTarget as ConceptualEntityType; Debug.Assert( entityType != null, "In: CopyEntitiesCommand's InvokeInternal, associationEnd's Type property should be typeof ConceptualEntityType"); if (entityType != null) { // CreateEntityTypeShapeAndConnectorsInDiagram method will check if the shape for the entity-type has been created; // and it will not create one if the shape already exists in the diagram. // Also, VerifyDiagramModelIntegrityVisitor will assert if there are duplicate diagram shapes (shapes that point to the same model element) // every-time a command transaction is committed. So adding another check to do the same thing here is redundant. CreateEntityTypeShapeCommand.CreateEntityTypeShapeAndConnectorsInDiagram( cpc, _diagram, entityType, false); } } } continue; } } } CopyAssociation(cpc, model, clipboardAssociation, entitiesMap); } // create copies of inheritances foreach (var inheritance in _clipboardEntities.ClipboardInheritances) { if (_diagram != null) { // Check if the underlying entity types are in the artifact/model to do that we need to: // - Get the Entity-Types by name. // - Check whether the entity-types match what were created in copy entities steps. var derivedEntity = artifact.ArtifactSet.LookupSymbol(inheritance.Key.NormalizedName) as EntityType; var baseEntity = artifact.ArtifactSet.LookupSymbol(inheritance.Value.NormalizedName) as EntityType; if (derivedEntity != null && baseEntity != null) { if (entitiesMap.Values.Contains(derivedEntity) && entitiesMap.Values.Contains(baseEntity)) { // check if the underlying entity-types are not in the diagram. // InheritanceConnector are created by ensuring both EntityTypeShapes are created. if (derivedEntity.GetAntiDependenciesOfType <EntityTypeShape>().Count(ets => ets.Diagram.Id == _diagram.Id.Value) == 0 && baseEntity.GetAntiDependenciesOfType <EntityTypeShape>() .Count(ets2 => ets2.Diagram.Id == _diagram.Id.Value) == 0) { // CreateEntityTypeShapeAndConnectorsInDiagram method will check if the shape for the entity-type has been created; // and it will not create one if the shape already exists in the diagram. // Also, VerifyDiagramModelIntegrityVisitor will assert if there are duplicate diagram shapes (shapes that point to the same model element) // every-time a command transaction is committed. So adding another check to do the same thing here is redundant. CreateEntityTypeShapeCommand.CreateEntityTypeShapeAndConnectorsInDiagram( cpc, _diagram, derivedEntity as ConceptualEntityType, false); CreateEntityTypeShapeCommand.CreateEntityTypeShapeAndConnectorsInDiagram( cpc, _diagram, baseEntity as ConceptualEntityType, false); } continue; } } } CopyInheritance(cpc, inheritance, entitiesMap); } }
private void AddNewConceptualEntityTypesFromArtifact( CommandProcessorContext cpc, HashSet<EntityType> entityTypesFromTempArtifactToBeAdded, out Dictionary<EntityType, EntityType> tempArtifactCEntityTypeToNewCEntityTypeInExistingArtifact) { tempArtifactCEntityTypeToNewCEntityTypeInExistingArtifact = new Dictionary<EntityType, EntityType>(); // add in all new C-side Entity Types (creating clones because the // EntityType objects in entityTypesToBeAdded are from the temporary DB artifact) foreach (var etFromTempArtifact in entityTypesFromTempArtifactToBeAdded) { var etcf = new EntityTypeClipboardFormat(etFromTempArtifact); var cmd = new CopyEntityCommand(etcf, ModelSpace.Conceptual); CommandProcessor.InvokeSingleCommand(cpc, cmd); var newEntityTypeInExistingArtifact = cmd.EntityType; // add an EntitySetMapping for the new EntityType if (null != newEntityTypeInExistingArtifact) { // add mapping from EntityType in the temp artifact to the newly-created // EntityType in the existing artifact tempArtifactCEntityTypeToNewCEntityTypeInExistingArtifact.Add( etFromTempArtifact, newEntityTypeInExistingArtifact); var esmInTempArtifact = ModelHelper.FindEntitySetMappingForEntityType(etFromTempArtifact); var newEntitySet = newEntityTypeInExistingArtifact.EntitySet as ConceptualEntitySet; if (null == esmInTempArtifact) { Debug.Fail("null esmInTempArtifact"); } else if (null == newEntitySet) { Debug.Fail("null newEntitySet"); } else { CloneEntitySetMapping( cpc, esmInTempArtifact, _preExistingModel.Artifact.MappingModel().FirstEntityContainerMapping, newEntitySet, tempArtifactCEntityTypeToNewCEntityTypeInExistingArtifact); } } else { throw new UpdateModelFromDatabaseException( string.Format( CultureInfo.CurrentCulture, Resources.UpdateFromDatabaseCannotCreateEntityType, etFromTempArtifact.ToPrettyString())); } } }