public override void ExecCommand(MenuCommand menuCommand)
        {
            CurrentModelSelection modelSelection = GetCurrentSelectedPersistentType();
            EntityDiagram         entityDiagram  = modelSelection.GetFromSelection <EntityDiagram>(false).SingleOrDefault();
            PersistentType        persistentType = modelSelection.IsPersistentTypeSelected
                                                ? modelSelection.CurrentPersistentType
                                                : null;


            Store store = persistentType != null ? persistentType.Store : entityDiagram.Store;

            IEnumerable <PersistentTypeItem> existingTypeNames =
                store.ElementDirectory.FindElements <PersistentType>()
                .Where(item => item.TypeKind.In(PersistentTypeKind.Entity, PersistentTypeKind.Interface, PersistentTypeKind.Structure))
                .Select(item => new PersistentTypeItem(item.Name, ModelUtil.ConvertTo(item.TypeKind),
                                                       item.AllProperties.Select(prop => prop.Name).ToArray()));

            PersistentTypeItem persistentTypeEnd1 = persistentType == null
                                                        ? null
                                                        : new PersistentTypeItem(
                persistentType.Name,
                ModelUtil.ConvertTo(persistentType.TypeKind),
                persistentType.AllProperties.Select(prop => prop.Name).
                ToArray());

            Func <PersistentTypeItem, PersistentType> findTypeFunc =
                (typeItemd) => store.ElementDirectory
                .FindElements <PersistentType>()
                .Where(item => item.Name == typeItemd.Name && ModelUtil.ConvertTo(item.TypeKind) == typeItemd.Kind)
                .SingleOrDefault();

            IEnumerable <string> existingAssociations =
                store.ElementDirectory.FindElements <PersistentTypeHasAssociations>().Select(assoc => assoc.Name).ToArray();

            if (existingTypeNames.Count() < 2)
            {
                MessageBox.Show("There must be at least 2 persistent types to make association.", "Add Association...",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            //FormAddAssociation.ResultData resultData;
            //if (FormAddAssociation.DialogShow(existingTypeNames, existingAssociations, persistentTypeEnd1, out resultData))

            FormAddAssociation.ResultData resultData;
            if (FormAddAssociation.DialogShow(existingTypeNames, existingAssociations, persistentTypeEnd1, out resultData))
            {
                modelSelection.MakeActionWithinTransaction(string.Format("Add new association '{0}'", resultData.AssociationName),
                                                           delegate
                {
                    PersistentType sourcePersistentType = findTypeFunc(resultData.PersistentItem1.TypeItem);
                    PersistentType targetPersistentType = findTypeFunc(resultData.PersistentItem2.TypeItem);

                    IAssociationInfo sourceInfo = new AssociationInfo
                    {
                        Multiplicity            = resultData.PersistentItem2.Multiplicity,
                        OnOwnerRemove           = AssociationOnRemoveAction.Default,
                        OnTargetRemove          = AssociationOnRemoveAction.Default,
                        UseAssociationAttribute = resultData.PersistentItem2.UseAssociationAttribute
                    };
                    sourceInfo.PairTo.SetAsCustom(resultData.PersistentItem1.PropertyName);

                    IAssociationInfo targetInfo = new AssociationInfo
                    {
                        Multiplicity            = resultData.PersistentItem1.Multiplicity,
                        OnOwnerRemove           = AssociationOnRemoveAction.Default,
                        OnTargetRemove          = AssociationOnRemoveAction.Default,
                        UseAssociationAttribute = resultData.PersistentItem1.UseAssociationAttribute
                    };
                    targetInfo.PairTo.SetAsCustom(resultData.PersistentItem2.PropertyName);

                    if (resultData.SimpleMode && sourcePersistentType == targetPersistentType)
                    {
                        targetInfo           = sourceInfo;
                        targetPersistentType = sourcePersistentType;
                    }

                    var persistentTypesAssociation = AssociationConnectorBuilder.CreatePersistentTypesAssociation(sourcePersistentType,
                                                                                                                  targetPersistentType, sourceInfo, targetInfo, resultData.AssociationName,
                                                                                                                  true, !resultData.SimpleMode);

                    Owner.SelectModelElement((ModelElement)persistentTypesAssociation);
                });
            }
        }
        public override void ExecCommand(MenuCommand menuCommand)
        {
            CurrentModelSelection modelSelection = GetCurrentSelectedPersistentType();
            EntityDiagram         entityDiagram  = modelSelection.GetFromSelection <EntityDiagram>(false).Single();

            EntityModel entityModel = entityDiagram.Store.ElementDirectory.FindElements <EntityModel>().Single();

            IEnumerable <Tuple <string, EntityKind> > existingTypeNames =
                entityDiagram.Store.ElementDirectory.FindElements <PersistentType>()
                .Where(item => item.TypeKind.In(PersistentTypeKind.Entity, PersistentTypeKind.Interface, PersistentTypeKind.Structure))
                .Select(item => new Tuple <string, EntityKind>(item.Name, ConvertTypeKind(item.TypeKind)));

            Func <string, EntityKind, PersistentType> findTypeFunc =
                (typeName, typeKind) => entityDiagram.Store.ElementDirectory
                .FindElements <PersistentType>()
                .Where(item => item.Name == typeName && ConvertTypeKind(item.TypeKind) == typeKind)
                .SingleOrDefault();

            FormAddPersistentType.ResultData resultData;
            if (FormAddPersistentType.DialogShow(existingTypeNames, out resultData))
            {
                modelSelection.MakeActionWithinTransaction(string.Format("Add new {0} with name '{1}'", resultData.TypeKind, resultData.TypeName),
                                                           delegate
                {
                    PersistentType newEntity = null;

                    switch (resultData.TypeKind)
                    {
                    case EntityKind.Entity:
                        {
                            newEntity = new Entity(entityModel.Partition);
                            if (resultData.BaseTypeInfo.Item1.HasValue)
                            {
                                Entity entity   = (Entity)newEntity;
                                entity.BaseType = findTypeFunc(resultData.BaseTypeInfo.Item2, resultData.BaseTypeInfo.Item1.Value) as EntityBase;
                            }
                            break;
                        }

                    case EntityKind.Structure:
                        {
                            newEntity = new Structure(entityModel.Partition);
                            if (resultData.BaseTypeInfo.Item1.HasValue)
                            {
                                Structure structure = (Structure)newEntity;
                                structure.BaseType  = findTypeFunc(resultData.BaseTypeInfo.Item2, resultData.BaseTypeInfo.Item1.Value) as EntityBase;
                            }
                            break;
                        }

                    case EntityKind.Interface:
                        {
                            newEntity = new Interface(entityModel.Partition);
                            if (resultData.BaseTypeInfo.Item1.HasValue)
                            {
                                Interface @interface    = (Interface)newEntity;
                                Interface baseInterface = findTypeFunc(resultData.BaseTypeInfo.Item2, resultData.BaseTypeInfo.Item1.Value) as Interface;
                                @interface.InheritedInterfaces.Add(baseInterface);
                            }
                            break;
                        }
                    }

                    newEntity.Name = resultData.TypeName;
                    entityModel.PersistentTypes.Add(newEntity);

                    // create key property
                    if (resultData.KeyPropertyInfo.Item1)
                    {
                        var newProperty  = new ScalarProperty(newEntity.Partition);
                        newProperty.Name = resultData.KeyPropertyInfo.Item2;
                        newProperty.KeyAttribute.Enabled = true;
                        Guid typeId      = SystemPrimitiveTypesConverter.GetTypeId(resultData.KeyPropertyInfo.Item3);
                        newProperty.Type = (DomainType)entityModel.GetDomainType(typeId);
                        newEntity.Properties.Add(newProperty);
                    }

                    Owner.SelectModelElement((ModelElement)newEntity);
                });
            }
        }
Example #3
0
 public CustomDesignSurfaceElementOperations(IServiceProvider serviceProvider, EntityDiagram diagram) : base(serviceProvider, diagram)
 {
 }