Esempio n. 1
0
        public void Test_AddAssocation_New()
        {
            PersistentTypeItem[] existingTypes =
                new PersistentTypeItem[]
            {
                new PersistentTypeItem("User", EntityKind.Entity, new [] { "Id", "Name" }),
                new PersistentTypeItem("Car", EntityKind.Entity, new [] { "Id", "Name", "Brand" }),
                new PersistentTypeItem("Address", EntityKind.Structure, new [] { "City", "Street" }),
            };
            string[] existingAssociations = new[]
            {
                "User1_Association"
            };

            FormAddAssociation.ResultData resultData;
            bool dialogShow = FormAddAssociation.DialogShow(existingTypes, existingAssociations, null, out resultData);
        }
Esempio n. 2
0
        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);
                });
            }
        }