Ejemplo n.º 1
0
        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;

            // check if entity is in the model
            _createdEntity = artifact.ArtifactSet.LookupSymbol(_clipboardEntity.NormalizedName) as EntityType;
            if (_diagram != null &&
                _createdEntity != null &&
                _createdEntity is ConceptualEntityType)
            {
                if (_createdEntity.GetAntiDependenciesOfType <EntityTypeShape>().Count(ets => ets.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, _createdEntity as ConceptualEntityType, _clipboardEntity.EntityTypeShapeFillColor, false);
                    return;
                }
            }
            CreateEntityCopyInModel(cpc);
        }
Ejemplo n.º 2
0
        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 (_efElements != null)
            {
                foreach (var efElement in _efElements)
                {
                    // --------------------------------------------------------------------------------
                    // | EF Element Type                   | Diagram shape type                       |
                    // --------------------------------------------------------------------------------
                    // | Conceptual Entity Type              Entity Type Shape                        |
                    // | Association                         Association Connector                    |
                    // | Association Set                     Association Connector                    |
                    // | Entity Set                          Entity Type Shapes for all ET in the set |
                    // | Property                            Property's entity type shape             |
                    // --------------------------------------------------------------------------------

                    var entityType     = efElement as ConceptualEntityType;
                    var association    = efElement as Association;
                    var entitySet      = efElement as EntitySet;
                    var associationSet = efElement as AssociationSet;

                    if (efElement is Property)
                    {
                        entityType = efElement.GetParentOfType(typeof(ConceptualEntityType)) as ConceptualEntityType;
                    }

                    if (associationSet != null &&
                        associationSet.Association.Status == BindingStatus.Known)
                    {
                        association = associationSet.Association.Target;
                    }

                    if (entityType != null)
                    {
                        CreateEntityTypeShapeCommand.CreateEntityTypeShapeAndConnectorsInDiagram(
                            cpc, _diagram, entityType, _createRelatedEntities);
                    }
                    else if (association != null)
                    {
                        Debug.Assert(
                            association.AssociationEnds().Count == 2,
                            "Received incorrect number of AssociationEnds (" + association.AssociationEnds().Count + ") for Association "
                            + association.ToPrettyString() + " should be 2.");

                        var assocEnds = association.AssociationEnds();
                        var assocEnd1 = assocEnds[0];
                        var assocEnd2 = assocEnds[1];

                        if (assocEnd1.Type.Status == BindingStatus.Known &&
                            assocEnd2.Type.Status == BindingStatus.Known)
                        {
                            CreateEntityTypeShapeCommand.CreateEntityTypeShapeAndConnectorsInDiagram(
                                cpc, _diagram, assocEnd1.Type.Target as ConceptualEntityType, _createRelatedEntities);
                            // Check whether the association is self association or not.
                            // If it is a self association, then we can skip creating the shape for the second associationEnd's entity type
                            // since both association-ends point to the same entity type.
                            if (assocEnd1.Type.Target != assocEnd2.Type.Target)
                            {
                                CreateEntityTypeShapeCommand.CreateEntityTypeShapeAndConnectorsInDiagram(
                                    cpc, _diagram, assocEnd2.Type.Target as ConceptualEntityType, _createRelatedEntities);
                            }
                        }
                    }
                    else if (entitySet != null)
                    {
                        foreach (var et in entitySet.GetEntityTypesInTheSet())
                        {
                            CreateEntityTypeShapeCommand.CreateEntityTypeShapeAndConnectorsInDiagram(
                                cpc, _diagram, et as ConceptualEntityType, _createRelatedEntities);
                        }
                    }
                    else
                    {
                        Debug.Fail("Unable to create diagram shape for EFElement with type:" + efElement.GetType().Name);
                    }
                }
            }
        }