private void MetadataTree_BeforeCheck(object sender, TreeNodeAdvBeforeCheckEventArgs e)
        {
            if (e.Node.Tag is RelationshipMetadataBase)
            {
                if (!options.CoupledRelationships)
                {
                    return;
                }
                string entity1    = "";
                string entity2    = "";
                string schemaName = "";
                if (e.Node.Tag is OneToManyRelationshipMetadata oneToMany)
                {
                    entity1    = oneToMany.ReferencingEntity;
                    entity2    = oneToMany.ReferencedEntity;
                    schemaName = oneToMany.SchemaName;
                }
                else if (e.Node.Tag is ManyToManyRelationshipMetadata manyToMany)
                {
                    entity1    = manyToMany.Entity1LogicalName;
                    entity2    = manyToMany.Entity2LogicalName;
                    schemaName = manyToMany.SchemaName;
                }

                foreach (TreeNodeAdv entity in metadataTree.Nodes)
                {
                    var entityName = ((EntityMetadata)entity.Tag).LogicalName;
                    if (entityName == entity1 || entityName == entity2)
                    {
                        foreach (TreeNodeAdv item in entity.Nodes)
                        {
                            if (item.Text == "Relationships")
                            {
                                if (!item.ExpandedOnce)
                                {
                                    StartWorkAsync(new WorkAsyncInfo
                                    {
                                        Message = $"Getting relationships for entity {entityName}",
                                        Work    = (worker, args) =>
                                        {
                                            args.Result = Service.Execute(new RetrieveEntityRequest()
                                            {
                                                EntityFilters = EntityFilters.Relationships,
                                                LogicalName   = entityName
                                            });
                                        },
                                        PostWorkCallBack = (args) =>
                                        {
                                            try
                                            {
                                                if (args.Error != null)
                                                {
                                                    MessageBox.Show(args.Error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                                }
                                                if (args.Result is RetrieveEntityResponse result)
                                                {
                                                    item.ExpandedOnce  = true;
                                                    var entityMetadata = entityMetadatas.FirstOrDefault(x => x.LogicalName == entityName);
                                                    typeof(EntityMetadata).GetProperty(nameof(entityMetadata.ManyToManyRelationships)).SetValue(entityMetadata, result.EntityMetadata.ManyToManyRelationships);
                                                    typeof(EntityMetadata).GetProperty(nameof(entityMetadata.OneToManyRelationships)).SetValue(entityMetadata, result.EntityMetadata.OneToManyRelationships);
                                                    typeof(EntityMetadata).GetProperty(nameof(entityMetadata.ManyToOneRelationships)).SetValue(entityMetadata, result.EntityMetadata.ManyToOneRelationships);

                                                    foreach (var relationship in result.EntityMetadata.ManyToManyRelationships.Union <RelationshipMetadataBase>(
                                                                 result.EntityMetadata.OneToManyRelationships).Union(
                                                                 result.EntityMetadata.ManyToOneRelationships).OrderBy(x => x.SchemaName))
                                                    {
                                                        TreeNodeAdv node = new TreeNodeAdv($"{relationship.SchemaName}")
                                                        {
                                                            ExpandedOnce = true,
                                                            ShowCheckBox = true,
                                                            Tag          = relationship
                                                        };
                                                        if (schemaName == relationship.SchemaName)
                                                        {
                                                            node.CheckState = e.NewCheckState;
                                                        }
                                                        item.Nodes.Add(node);
                                                    }
                                                }
                                            }
                                            catch (Exception ex)
                                            {
                                                MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                            }
                                            finally
                                            {
                                                WorkAsyncEnded();
                                            }
                                        }
                                    });
Beispiel #2
0
        private void MetadataTree_BeforeCheck(object sender, TreeNodeAdvBeforeCheckEventArgs e)
        {
            if (e.Node.Tag is RelationshipMetadataBase)
            {
                string entity1;
                string entity2;
                string schemaName;
                MessageBox.Show($"{sender}");
                if (e.Node.Tag is OneToManyRelationshipMetadata)
                {
                    var metadata = (OneToManyRelationshipMetadata)e.Node.Tag;
                    entity1    = metadata.ReferencingEntity;
                    entity2    = metadata.ReferencedEntity;
                    schemaName = metadata.SchemaName;
                }
                else
                {
                    var metadata = (ManyToManyRelationshipMetadata)e.Node.Tag;
                    entity1    = metadata.Entity1LogicalName;
                    entity2    = metadata.Entity2LogicalName;
                    schemaName = metadata.SchemaName;
                }

                MessageBox.Show($"{schemaName} {entity1} {entity2}");

                foreach (TreeNodeAdv entity in metadataTree.Nodes)
                {
                    var entityName = ((EntityMetadata)entity.Tag).LogicalName;
                    if (entityName == entity1 || entityName == entity2)
                    {
                        foreach (TreeNodeAdv item in entity.Nodes)
                        {
                            if (item.Text == "Relationships")
                            {
                                if (!item.ExpandedOnce)
                                {
                                    WorkAsync(new WorkAsyncInfo
                                    {
                                        Message = $"Getting relationships for entity {entityName}",
                                        Work    = (worker, args) =>
                                        {
                                            args.Result = Service.Execute(new RetrieveEntityRequest()
                                            {
                                                EntityFilters = EntityFilters.Relationships,
                                                LogicalName   = entityName
                                            });
                                        },
                                        PostWorkCallBack = (args) =>
                                        {
                                            if (args.Error != null)
                                            {
                                                MessageBox.Show(args.Error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                            }
                                            var result = args.Result as RetrieveEntityResponse;
                                            if (result != null)
                                            {
                                                item.ExpandedOnce = true;
                                                foreach (var relationship in result.EntityMetadata.ManyToManyRelationships.Union <RelationshipMetadataBase>(
                                                             result.EntityMetadata.OneToManyRelationships).Union(
                                                             result.EntityMetadata.ManyToOneRelationships).OrderBy(x => x.SchemaName))
                                                {
                                                    TreeNodeAdv node = new TreeNodeAdv($"{relationship.SchemaName}")
                                                    {
                                                        ExpandedOnce = true,
                                                        ShowCheckBox = true,
                                                        Tag          = relationship
                                                    };
                                                    if (schemaName == relationship.SchemaName)
                                                    {
                                                        node.CheckState = e.NewCheckState;
                                                    }
                                                    item.Nodes.Add(node);
                                                }
                                            }
                                        }
                                    });
                                }
                                else
                                {
                                    foreach (TreeNodeAdv relationship in item.Nodes)
                                    {
                                        if (relationship == e.Node)
                                        {
                                            continue;
                                        }
                                        if (((RelationshipMetadataBase)relationship.Tag).SchemaName == schemaName)
                                        {
                                            MessageBox.Show($"{schemaName} {entityName}");
                                            this.metadataTree.BeforeCheck -= treeEventHandler;
                                            relationship.CheckState        = e.NewCheckState;
                                            treeEventHandler = new TreeViewAdvBeforeCheckEventHandler(this.MetadataTree_BeforeCheck);
                                            this.metadataTree.BeforeCheck += treeEventHandler;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        private void MetadataTree_BeforeCheck(object sender, TreeNodeAdvBeforeCheckEventArgs e)
        {
            if (e.Node.Tag as RelationshipMetadataBase != null)
            {
                if (!options.CoupledRelationships)
                {
                    return;
                }
                string entity1    = "";
                string entity2    = "";
                string schemaName = "";
                if (e.Node.Tag is OneToManyRelationshipMetadata oneToMany)
                {
                    entity1    = oneToMany.ReferencingEntity;
                    entity2    = oneToMany.ReferencedEntity;
                    schemaName = oneToMany.SchemaName;
                }
                else if (e.Node.Tag is ManyToManyRelationshipMetadata manyToMany)
                {
                    entity1    = manyToMany.Entity1LogicalName;
                    entity2    = manyToMany.Entity2LogicalName;
                    schemaName = manyToMany.SchemaName;
                }

                foreach (TreeNodeAdv entity in metadataTree.Nodes)
                {
                    var entityName = ((EntityMetadata)entity.Tag).LogicalName;
                    if (entityName == entity1 || entityName == entity2)
                    {
                        foreach (TreeNodeAdv item in entity.Nodes)
                        {
                            if (item.Text == "Relationships")
                            {
                                if (!item.ExpandedOnce)
                                {
                                    BackgroundWorkHandler.EnqueueBackgroundWork(
                                        AlBackgroundWorkerFactory.NewWorker <string, RetrieveEntityResponse>(
                                            work: RetrieveRelationships,
                                            argument: entityName,
                                            workFinished: (string argument, RetrieveEntityResponse value, Exception exception) =>
                                    {
                                        try
                                        {
                                            if (exception != null)
                                            {
                                                MessageBox.Show(exception.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                            }
                                            if (value is RetrieveEntityResponse result)
                                            {
                                                item.ExpandedOnce  = true;
                                                var entityMetadata = entityMetadatas.FirstOrDefault(x => x.LogicalName == entityName);
                                                entityMetadata.SetPrivateValue(x => x.ManyToManyRelationships, result.EntityMetadata.ManyToManyRelationships);
                                                entityMetadata.SetPrivateValue(x => x.OneToManyRelationships, result.EntityMetadata.OneToManyRelationships);
                                                entityMetadata.SetPrivateValue(x => x.ManyToOneRelationships, result.EntityMetadata.ManyToOneRelationships);

                                                foreach (var relationship in result.EntityMetadata.ManyToManyRelationships.Union <RelationshipMetadataBase>(
                                                             result.EntityMetadata.OneToManyRelationships).Union(
                                                             result.EntityMetadata.ManyToOneRelationships).OrderBy(x => x.SchemaName))
                                                {
                                                    TreeNodeAdv node = new TreeNodeAdv($"{relationship.SchemaName}")
                                                    {
                                                        ExpandedOnce = true,
                                                        ShowCheckBox = true,
                                                        Tag          = relationship
                                                    };
                                                    if (schemaName == relationship.SchemaName)
                                                    {
                                                        node.CheckState = e.NewCheckState;
                                                    }
                                                    item.Nodes.Add(node);
                                                }
                                            }
                                        }
                                        catch (Exception ex)
                                        {
                                            MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                        }
                                    }).WithViewModel(pluginViewModel).WithMessage(this, string.Format(CultureInfo.CurrentCulture, Resources.GETTING_RELATIONSHIPS, entityName)));
                                }
                                else
                                {
                                    foreach (TreeNodeAdv relationship in item.Nodes)
                                    {
                                        if (relationship == e.Node)
                                        {
                                            continue;
                                        }
                                        if (((RelationshipMetadataBase)relationship.Tag).SchemaName == schemaName)
                                        {
                                            this.metadataTree.BeforeCheck -= treeEventHandler;
                                            relationship.CheckState        = e.NewCheckState;
                                            treeEventHandler = new TreeViewAdvBeforeCheckEventHandler(this.MetadataTree_BeforeCheck);
                                            this.metadataTree.BeforeCheck += treeEventHandler;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else if (e.Node.Text == "Attributes" && e.NewCheckState == CheckState.Checked && !e.Node.ExpandedOnce)
            {
                AttributeMetadataHandler.GetAttributes(((EntityMetadata)e.Node.Parent.Tag).LogicalName, e.Node, checkedState: true);
            }
            else if (e.Node.Text == "Relationships" && e.NewCheckState == CheckState.Checked && !e.Node.ExpandedOnce)
            {
                RelationshipMetadataHandler.GetRelationships(((EntityMetadata)e.Node.Parent.Tag).LogicalName, e.Node, checkedState: true);
            }
        }