Пример #1
0
        public override void ElementAdded(ElementAddedEventArgs e)
        {
            base.ElementAdded(e);

            Association          element = (Association)e.ModelElement;
            Store                store   = element.Store;
            Transaction          current = store.TransactionManager.CurrentTransaction;
            PluralizationService pluralizationService = ModelRoot.PluralizationService;

            if (current.IsSerializing)
            {
                return;
            }

            if (string.IsNullOrEmpty(element.TargetPropertyName))
            {
                string rootName = element.TargetMultiplicity == Multiplicity.ZeroMany &&
                                  pluralizationService?.IsSingular(element.Target.Name) == true
                                 ? pluralizationService.Pluralize(element.Target.Name)
                                 : element.Target.Name;

                string identifierName = rootName;
                int    index          = 0;

                while (element.Source.HasPropertyNamed(identifierName))
                {
                    identifierName = $"{rootName}_{++index}";
                }

                element.TargetPropertyName = identifierName;
            }

            if (element is BidirectionalAssociation bidirectionalAssociation)
            {
                if (string.IsNullOrEmpty(bidirectionalAssociation.SourcePropertyName))
                {
                    string rootName = element.SourceMultiplicity == Multiplicity.ZeroMany &&
                                      pluralizationService?.IsSingular(element.Source.Name) == true
                                    ? pluralizationService.Pluralize(element.Source.Name)
                                    : element.Source.Name;

                    string identifierName = rootName;
                    int    index          = 0;

                    while (element.Target.HasPropertyNamed(identifierName))
                    {
                        identifierName = $"{rootName}_{++index}";
                    }

                    bidirectionalAssociation.SourcePropertyName = identifierName;
                }
            }

            AssociationChangeRules.SetEndpointRoles(element);
        }
Пример #2
0
      private void ProcessUnidirectionalAssociations(ParsingModels.ModelClass modelClass)
      {
         List<ModelUnidirectionalAssociation> unidirectionalAssociations = modelClass.UnidirectionalAssociations;

         foreach (ModelUnidirectionalAssociation data in unidirectionalAssociations)
         {
            UnidirectionalAssociation existing = Store.Get<UnidirectionalAssociation>()
                                                      .FirstOrDefault(x => x.Target.Name == data.TargetClassName
                                                                        && x.Source.Name == data.SourceClassName
                                                                        && x.Source.Name == modelClass.Name // just to be sure
                                                                        && x.TargetPropertyName == data.TargetPropertyName);

            if (existing != null)
            {
               if (string.IsNullOrWhiteSpace(existing.FKPropertyName) && !string.IsNullOrWhiteSpace(data.ForeignKey))
               {
                  existing.FKPropertyName = data.ForeignKey;
                  existing.Source.ModelRoot.ExposeForeignKeys = true;
               }

               continue;
            }

            ModelClass source = Store.Get<ModelClass>().FirstOrDefault(c => c.FullName == data.SourceClassFullName);
            ModelClass target = Store.Get<ModelClass>().FirstOrDefault(c => c.FullName == data.TargetClassFullName);

            if (source == null || target == null || source.FullName != modelClass.FullName)
               continue;

            // ReSharper disable once UnusedVariable
            UnidirectionalAssociation element = new UnidirectionalAssociation(Store,
                                                    new[]
                                                    {
                                                       new RoleAssignment(UnidirectionalAssociation.UnidirectionalSourceDomainRoleId, source),
                                                       new RoleAssignment(UnidirectionalAssociation.UnidirectionalTargetDomainRoleId, target)
                                                    },
                                                    new[]
                                                    {
                                                       new PropertyAssignment(Association.SourceMultiplicityDomainPropertyId, ConvertMultiplicity(data.SourceMultiplicity)),
                                                       new PropertyAssignment(Association.TargetMultiplicityDomainPropertyId, ConvertMultiplicity(data.TargetMultiplicity)),
                                                       new PropertyAssignment(Association.TargetPropertyNameDomainPropertyId, data.TargetPropertyName),
                                                       new PropertyAssignment(Association.TargetSummaryDomainPropertyId, data.TargetSummary),
                                                       new PropertyAssignment(Association.TargetDescriptionDomainPropertyId, data.TargetDescription),
                                                       new PropertyAssignment(Association.FKPropertyNameDomainPropertyId, data.ForeignKey),
                                                       new PropertyAssignment(Association.SourceRoleDomainPropertyId, ConvertRole(data.SourceRole)), 
                                                       new PropertyAssignment(Association.TargetRoleDomainPropertyId, ConvertRole(data.TargetRole)), 
                                                    });

            AssociationChangeRules.SetEndpointRoles(element);
            element.EnsureForeignKeyAttributes();
         }
      }
Пример #3
0
        public override void ElementAdded(ElementAddedEventArgs e)
        {
            base.ElementAdded(e);

            Association          element = (Association)e.ModelElement;
            Store                store   = element.Store;
            Transaction          current = store.TransactionManager.CurrentTransaction;
            PluralizationService pluralizationService = ModelRoot.PluralizationService;

            if (current.IsSerializing || ModelRoot.BatchUpdating)
            {
                return;
            }

            // add unidirectional
            //    source can't be dependent (connection builder handles this)
            // if target is dependent,
            //    source cardinality is 0..1
            //    target cardinality is 0..1
            //    source is principal
            if (element is UnidirectionalAssociation && element.Target.IsDependentType)
            {
                element.TargetMultiplicity = Multiplicity.ZeroOne;
                element.SourceRole         = EndpointRole.Principal;
                element.TargetRole         = EndpointRole.Dependent;
            }

            // add bidirectional
            //    neither can be dependent (connection builder handles this)

            if (string.IsNullOrEmpty(element.TargetPropertyName))
            {
                string rootName = element.TargetMultiplicity == Multiplicity.ZeroMany && pluralizationService?.IsSingular(element.Target.Name) == true
                                 ? pluralizationService.Pluralize(element.Target.Name)
                                 : element.Target.Name;

                string identifierName = rootName;
                int    index          = 0;

                while (element.Source.HasPropertyNamed(identifierName))
                {
                    identifierName = $"{rootName}_{++index}";
                }

                element.TargetPropertyName = identifierName;
            }

            if (element is BidirectionalAssociation bidirectionalAssociation)
            {
                if (string.IsNullOrEmpty(bidirectionalAssociation.SourcePropertyName))
                {
                    string rootName = element.SourceMultiplicity == Multiplicity.ZeroMany && pluralizationService?.IsSingular(element.Source.Name) == true
                                    ? pluralizationService.Pluralize(element.Source.Name)
                                    : element.Source.Name;

                    string identifierName = rootName;
                    int    index          = 0;

                    while (element.Target.HasPropertyNamed(identifierName))
                    {
                        identifierName = $"{rootName}_{++index}";
                    }

                    bidirectionalAssociation.SourcePropertyName = identifierName;
                }
            }

            AssociationChangeRules.SetEndpointRoles(element);
        }
Пример #4
0
        public override void ElementAdded(ElementAddedEventArgs e)
        {
            base.ElementAdded(e);

            Association          element = (Association)e.ModelElement;
            Store                store   = element.Store;
            Transaction          current = store.TransactionManager.CurrentTransaction;
            PluralizationService pluralizationService = ModelRoot.PluralizationService;

            if (current.IsSerializing)
            {
                return;
            }

            // TODO: Error if 1..N to an owned type (EFCore) or to a complex type (EF6)
            if (element.TargetMultiplicity == Multiplicity.ZeroMany && element.Target.IsDependentType)
            {
                current.Rollback();
                ErrorDisplay.Show($"Can't have a 0..* association from {element.Source.Name} to dependent type {element.Target.Name}");

                return;
            }

            if (string.IsNullOrEmpty(element.TargetPropertyName))
            {
                string rootName = element.TargetMultiplicity == Multiplicity.ZeroMany &&
                                  pluralizationService?.IsSingular(element.Target.Name) == true
                                 ? pluralizationService.Pluralize(element.Target.Name)
                                 : element.Target.Name;

                string identifierName = rootName;
                int    index          = 0;

                while (element.Source.HasPropertyNamed(identifierName))
                {
                    identifierName = $"{rootName}_{++index}";
                }

                element.TargetPropertyName = identifierName;
            }

            if (element is BidirectionalAssociation bidirectionalAssociation)
            {
                if (string.IsNullOrEmpty(bidirectionalAssociation.SourcePropertyName))
                {
                    string rootName = element.SourceMultiplicity == Multiplicity.ZeroMany &&
                                      pluralizationService?.IsSingular(element.Source.Name) == true
                                    ? pluralizationService.Pluralize(element.Source.Name)
                                    : element.Source.Name;

                    string identifierName = rootName;
                    int    index          = 0;

                    while (element.Target.HasPropertyNamed(identifierName))
                    {
                        identifierName = $"{rootName}_{++index}";
                    }

                    bidirectionalAssociation.SourcePropertyName = identifierName;
                }
            }

            AssociationChangeRules.SetEndpointRoles(element);
        }
Пример #5
0
        protected override void OnDocumentLoaded()
        {
            base.OnDocumentLoaded();
            if (!(RootElement is ModelRoot modelRoot))
            {
                return;
            }

            // set to the project's namespace if no namespace set
            if (string.IsNullOrEmpty(modelRoot.Namespace))
            {
                using (Transaction tx = modelRoot.Store.TransactionManager.BeginTransaction("SetDefaultNamespace"))
                {
                    DTE     dte            = Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(DTE)) as DTE;
                    Project currentProject = GetActiveProject(dte);
                    modelRoot.Namespace = currentProject.Properties.Item("DefaultNamespace")?.Value as string;
                    tx.Commit();
                }
            }

            ReadOnlyCollection <Association> associations = modelRoot.Store.ElementDirectory.FindElements <Association>();

            if (associations.Any())
            {
                using (Transaction tx = modelRoot.Store.TransactionManager.BeginTransaction("StyleConnectors"))
                {
                    // style association connectors if needed
                    foreach (Association element in associations)
                    {
                        AssociationChangeRules.UpdateDisplayForPersistence(element);
                        AssociationChangeRules.UpdateDisplayForCascadeDelete(element);

                        // for older diagrams that didn't calculate this initially
                        AssociationChangeRules.SetEndpointRoles(element);
                    }

                    tx.Commit();
                }
            }

            List <GeneralizationConnector> generalizationConnectors = modelRoot.Store
                                                                      .ElementDirectory
                                                                      .FindElements <GeneralizationConnector>()
                                                                      .Where(x => !x.FromShape.IsVisible || !x.ToShape.IsVisible)
                                                                      .ToList();
            List <AssociationConnector> associationConnectors = modelRoot.Store
                                                                .ElementDirectory
                                                                .FindElements <AssociationConnector>()
                                                                .Where(x => !x.FromShape.IsVisible || !x.ToShape.IsVisible)
                                                                .ToList();

            if (generalizationConnectors.Any() || associationConnectors.Any())
            {
                using (Transaction tx = modelRoot.Store.TransactionManager.BeginTransaction("HideConnectors"))
                {
                    // hide any connectors that may have been hidden due to hidden shapes
                    foreach (GeneralizationConnector connector in generalizationConnectors)
                    {
                        connector.Hide();
                    }
                    foreach (AssociationConnector connector in associationConnectors)
                    {
                        connector.Hide();
                    }
                    tx.Commit();
                }
            }
        }