public static IPersistentTypeHasAssociations CreatePersistentTypesAssociation(ModelElement sourceElement,
                                                                                      ModelElement targetElement, IAssociationInfo sourceInfo, IAssociationInfo targetInfo, string associationName,
                                                                                      bool createPropertyEnd1, bool createPropertyEnd2)
        {
            PersistentType sourceEntity = (PersistentType)sourceElement;
            PersistentType targetEntity = (PersistentType)targetElement;
            PersistentTypeHasAssociations typeAssociations = new PersistentTypeHasAssociations(sourceEntity, targetEntity);

            typeAssociations.End1.Multiplicity            = sourceInfo.Multiplicity;
            typeAssociations.End1.OnOwnerRemove           = sourceInfo.OnOwnerRemove;
            typeAssociations.End1.OnTargetRemove          = sourceInfo.OnTargetRemove;
            typeAssociations.End1.UseAssociationAttribute = sourceInfo.UseAssociationAttribute;

            // association End2 values
            typeAssociations.End2.Multiplicity            = targetInfo.Multiplicity;
            typeAssociations.End2.OnOwnerRemove           = targetInfo.OnOwnerRemove;
            typeAssociations.End2.OnTargetRemove          = targetInfo.OnTargetRemove;
            typeAssociations.End2.UseAssociationAttribute = targetInfo.UseAssociationAttribute;

            typeAssociations.Name = associationName;


            NavigationProperty sourceNavigationProperty = createPropertyEnd1 ? new NavigationProperty(sourceElement.Partition) : null;

            //bool isToSelfLookup = sourceInfo == targetInfo;

            //NavigationProperty targetNavigationProperty = isToSelfLookup ? null : new NavigationProperty(targetElement.Partition);
            NavigationProperty targetNavigationProperty = null;

            //if (!isToSelfLookup || !createPropertyEnd1)
            if (createPropertyEnd2)
            {
                targetNavigationProperty = new NavigationProperty(targetElement.Partition);
            }


            if (sourceNavigationProperty != null)
            {
                sourceNavigationProperty.PersistentTypeOfNavigationProperty = sourceEntity;
                sourceNavigationProperty.Name         = sourceInfo.PairTo.Value;
                sourceNavigationProperty.Multiplicity = targetInfo.Multiplicity;
                sourceNavigationProperty.PersistentTypeHasAssociations = typeAssociations;
            }

            if (targetNavigationProperty != null)
            {
                targetNavigationProperty.PersistentTypeOfNavigationProperty = targetEntity;
                targetNavigationProperty.Name         = targetInfo.PairTo.Value;
                targetNavigationProperty.Multiplicity = sourceInfo.Multiplicity;
                targetNavigationProperty.PersistentTypeHasAssociations = typeAssociations;
            }

            typeAssociations.End1.PairTo.SetAsCustom(createPropertyEnd1 ? sourceNavigationProperty.Name : targetNavigationProperty.Name);
            typeAssociations.End2.PairTo.SetAsCustom(createPropertyEnd2 ? targetNavigationProperty.Name : sourceNavigationProperty.Name);

            return(typeAssociations);
        }
Beispiel #2
0
        internal static void ValidateNavigationPropertyAssociation(NavigationProperty navigationProperty, ValidationContext context)
        {
            if (navigationProperty.PersistentTypeHasAssociations == null)
            {
                INavigationProperty navProp = navigationProperty;

                context.LogError(
                    string.Format(ERROR_NO_ASSOCIATION_ASSIGNED,
                                  navigationProperty.Name, navProp.Owner.Name),
                    CODE_NO_ASSOCIATION_ASSIGNED, new ModelElement[] { navigationProperty });
            }
        }
Beispiel #3
0
        private PropertyDescriptorCollection GetCustomProperties(Attribute[] attributes)
        {
            var attributeBuilder = this.CreateBuilder(attributes, base.GetProperties(attributes),
                                                      modelElement =>
            {
                NavigationProperty navigationProperty = modelElement as NavigationProperty;
                return(navigationProperty == null
                                        ? false
                                        : navigationProperty.Multiplicity != MultiplicityKind.Many);
            });

            Guid typedEntitySetPropertyId = NavigationPropertyHasTypedEntitySet.TypedEntitySetDomainRoleId;

            attributeBuilder.ReplaceRolePlayerPropertyAtribute(typedEntitySetPropertyId,
                                                               descriptor => new BrowsableAttribute(false));

            return(attributeBuilder.Build());
        }
Beispiel #4
0
        protected override void OnCopy(ModelElement sourceElement)
        {
            base.OnCopy(sourceElement);

            NavigationProperty sourceProperty = (NavigationProperty)sourceElement;

            this.TypedEntitySet = sourceProperty.TypedEntitySet;
            this.KeyAttribute   = Common.ExtensionMethods.Clone(sourceProperty.KeyAttribute);

            using (ValidationContextRegion.DisableAll())
            {
                // this will add delete which dispose validation region when transaction completes/rolled-back

                /*this.Store.RegisterActionOnTransactionEvent(TransactionEvent.Committed | TransactionEvent.RolledBack,
                 *  disabledValidationRegion, (args, disposable) => disposable.Dispose());*/

                if (sourceProperty.PersistentTypeHasAssociations != null)
                {
                    this.PersistentTypeHasAssociations =
                        (PersistentTypeHasAssociations)sourceProperty.PersistentTypeHasAssociations.Copy(
                            new[]
                    {
                        PersistentTypeHasAssociations.TargetPersistentTypeDomainRoleId,
                        PersistentTypeHasAssociations.SourcePersistentTypeDomainRoleId
                    });
                }
            }

            if (this.PersistentTypeHasAssociations != null)
            {
                this.PersistentTypeHasAssociations.TargetPersistentType =
                    sourceProperty.PersistentTypeHasAssociations.TargetPersistentType;

                this.PersistentTypeHasAssociations.Name =
                    AssociationConnectorBuilder.BuildAssociationName(
                        this.PersistentTypeHasAssociations.SourcePersistentType,
                        this.PersistentTypeHasAssociations.TargetPersistentType);
            }
        }
Beispiel #5
0
        private void InternalImplementToType(IEntityBase targetType, ImplementTypeOptions options)
        {
            EntityBase entityBase = (EntityBase)targetType;

            List <PropertyBase> addedProperties = new List <PropertyBase>();

            foreach (PropertyBase property in this.Properties)
            {
                if (!entityBase.ContainsProperty(property.PropertyKind, property.Name))
                {
                    PropertyBase copiedProperty =
                        (PropertyBase)property.Copy(new[] { PersistentTypeHasProperties.PersistentTypeDomainRoleId });
                    entityBase.Properties.Add(copiedProperty);
                    addedProperties.Add(copiedProperty);
                }
            }

            foreach (NavigationProperty navigationProperty in this.NavigationProperties)
            {
                if (!entityBase.ContainsProperty(PropertyKind.Navigation, navigationProperty.Name))
                {
                    NavigationProperty copiedNavigationProperty = (NavigationProperty)navigationProperty.Copy(new[]
                    {
                        PersistentTypeHasNavigationProperties.PersistentTypeOfNavigationPropertyDomainRoleId,
                    });
                    entityBase.NavigationProperties.Add(copiedNavigationProperty);
                    copiedNavigationProperty.PersistentTypeHasAssociations.SourcePersistentType = entityBase;
                    addedProperties.Add(copiedNavigationProperty);
                }
            }

            foreach (EntityIndex entityIndex in this.Indexes)
            {
                if (!entityBase.ContainsIndex(entityIndex))
                {
                    EntityIndex copiedIndex =
                        (EntityIndex)entityIndex.Copy(new[] { InterfaceHasIndexes.InterfaceOfIndexDomainRoleId });
                    entityBase.Indexes.Add(copiedIndex);
                }
            }

            if (Util.IsFlagSet(ImplementTypeOptions.CopyInheritedInterfaces, options))
            {
                foreach (Interface inheritedInterface in this.InheritedInterfaces)
                {
                    if (!entityBase.InheritedInterfaces.Contains(inheritedInterface))
                    {
                        entityBase.InheritedInterfaces.Add(inheritedInterface);
                    }
                }
            }

            if (!targetType.InheritedInterfaces.Contains(this))
            {
                entityBase.InheritedInterfaces.Add(this);
            }

            var duplicatedInfo = PersistentTypeValidation.FindDuplicatedPropertieInInheritanceTree(targetType, null);

            foreach (var addedProperty in addedProperties)
            {
                Func <IPropertyBase, bool> foundPropertyFunc =
                    item => item.PropertyKind == addedProperty.PropertyKind &&
                    Util.StringEqual(item.Name, addedProperty.Name, true);

                addedProperty.IsInherited =
                    duplicatedInfo.PropertiesWithDifferentType.Any(foundPropertyFunc) ||
                    duplicatedInfo.PropertiesWithSameType.Any(foundPropertyFunc);
            }
        }
        public void HandleEvent(ElementEventArgs args)
        {
            if (args.EventType == ElementEventType.CustomEvent)
            {
                object            customEventArgs   = args.CustomEventArgs[0];
                OrmAssociationEnd eventFromAssocEnd = customEventArgs as OrmAssociationEnd;
                string            changedProperty   = (string)args.CustomEventArgs[1];
                string            calledFromEndId   = (string)args.CustomEventArgs[2];

                if (eventFromAssocEnd != null)
                {
                    bool changeAll             = string.IsNullOrEmpty(changedProperty);
                    bool canChangeMultiplicity = changeAll || Util.StringEqual(changedProperty, "Multiplicity", true);
                    bool canChangePairTo       = changeAll || Util.StringEqual(changedProperty, "PairTo", true);

                    bool eventFromEnd1 = Util.StringEqual(calledFromEndId, "End1", true);

                    Func <bool, Tuple <NavigationProperty, OrmAssociationEnd> > getDefinitions = delegate(bool revert)
                    {
                        PersistentType persistentToFind = eventFromEnd1
                                                              ? (revert
                                                                     ? this.SourcePersistentType
                                                                     : this.TargetPersistentType)
                                                              : (revert
                                                                     ? this.TargetPersistentType
                                                                     : this.SourcePersistentType);

                        OrmAssociationEnd otherAssocEnd = eventFromEnd1
                                                              ? (revert
                                                                    ? this.End2
                                                                    : eventFromAssocEnd)
                                                              : (revert
                                                                    ? eventFromAssocEnd
                                                                    : this.End2);

                        NavigationProperty navigationProperty = this.NavigationProperties.SingleOrDefault(
                            navProperty => navProperty.PersistentTypeOfNavigationProperty == persistentToFind);

                        return(new Tuple <NavigationProperty, OrmAssociationEnd>(navigationProperty, otherAssocEnd));
                    };


                    {
                        if (canChangeMultiplicity)
                        {
                            bool revert             = false;
                            var  definitions        = getDefinitions(revert);
                            var  navigationProperty = definitions.Item1;
                            var  otherAssocEnd      = definitions.Item2;

                            if (navigationProperty != null)
                            {
                                this.Store.MakeActionWithinTransaction(
                                    string.Format("Updating multiplicity on navigation property '{0}' to value '{1}'",
                                                  navigationProperty.Name, eventFromAssocEnd.Multiplicity),
                                    () =>
                                {
                                    navigationProperty.Multiplicity = otherAssocEnd.Multiplicity;
                                });
                            }
                        }

                        if (canChangePairTo && !eventFromAssocEnd.PairTo.IsDefault())
                        {
                            string newName = eventFromAssocEnd.PairTo.Value;

                            bool revert             = true;
                            var  definitions        = getDefinitions(revert);
                            var  navigationProperty = definitions.Item1;

                            if (navigationProperty != null)
                            {
                                this.Store.MakeActionWithinTransaction(
                                    string.Format("Updating name on navigation property '{0}' to value '{1}'",
                                                  navigationProperty.Name, newName),
                                    () =>
                                {
                                    navigationProperty.Name = newName;
                                });
                            }
                        }
                    }
                }
            }
        }
        private static void CustomReadPropertiesFromElements(SerializationContext serializationContext, NavigationProperty element, XmlReader reader)
        {
            while (!serializationContext.Result.Failed && !reader.EOF && reader.NodeType == global::System.Xml.XmlNodeType.Element)
            {
                switch (reader.LocalName)
                {
                case "key":                                  // key
                    if (reader.IsEmptyElement)
                    {                                        // No serialized value, must be default one.
                        SerializationUtilities.Skip(reader); // Skip this tag.
                    }
                    else
                    {
                        OrmKeyAttribute keyAttribute = new OrmKeyAttribute();
                        keyAttribute.DeserializeFromXml(reader);
                        element.KeyAttribute = keyAttribute;

                        SerializationUtilities.SkipToNextElement(reader);
                        reader.SkipToNextElementFix();
                    }
                    break;

                case "multiplicity":                         // Multiplicity
                    if (reader.IsEmptyElement)
                    {                                        // No serialized value, must be default one.
                        SerializationUtilities.Skip(reader); // Skip this tag.
                    }
                    else
                    {
                        string           strMultiplicity = DONetEntityModelDesignerSerializationHelper.Instance.ReadElementContentAsString(serializationContext, element, reader);
                        MultiplicityKind valueOfMultiplicity;
                        if (Enum.TryParse(strMultiplicity, true, out valueOfMultiplicity))
                        {
                            element.Multiplicity = valueOfMultiplicity;
                        }
                        else
                        {       // Invalid property value, ignored.
                            EntityModelDesignerSerializationBehaviorSerializationMessages.IgnoredPropertyValue(serializationContext, reader, "multiplicity", typeof(global::TXSoftware.DataObjectsNetEntityModel.Common.MultiplicityKind), strMultiplicity);
                        }

                        SerializationUtilities.SkipToNextElement(reader);
                    }
                    break;

                default:
                    return;      // Don't know this element.
                }
            }
        }
        private static void CustomWritePropertiesAsElements(SerializationContext context, NavigationProperty element, XmlWriter writer)
        {
            if (!context.Result.Failed)
            {
                var multiplicity = element.Multiplicity.ToString();
                DONetEntityModelDesignerSerializationHelper.Instance.WriteElementString(context, element, writer, "multiplicity", multiplicity);
            }

            if (!context.Result.Failed)
            {
                element.KeyAttribute.SerializeToXml(writer);
            }
        }