Ejemplo n.º 1
0
        private void tsbCreateRecords_Click(object sender, EventArgs e)
        {
            if (lvOptionSets.CheckedItems.Count == 0)
            {
                return;
            }

            var mm           = new MetadataManager(Service);
            var entityExists = mm.EntityExists("gap_powerbioptionsetref");

            if (!entityExists)
            {
                var message = "'Power BI Option-Set Xref' entity does not exist on your organization. Would you like to create it? If you answer 'no', the process will be aborted";
                if (MessageBox.Show(this, message, "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
                {
                    return;
                }
            }

            var settings = new Settings
            {
                CreateEntity = !entityExists,
                OptionSets   = lvOptionSets.CheckedItems.Cast <ListViewItem>().Select(o => (AttributeMetadata)o.Tag).ToList(),
                AllMetadata  = emc
            };

            tsbCreateRecords.Enabled = false;
            tsbDeleteEntity.Enabled  = false;
            tsbLoadEntities.Enabled  = false;

            WorkAsync(new WorkAsyncInfo
            {
                Message       = "Initializing...",
                AsyncArgument = settings,
                Work          = (bw, evt) =>
                {
                    if (((Settings)evt.Argument).CreateEntity)
                    {
                        var organization = Service.RetrieveMultiple(new QueryExpression("organization")
                        {
                            ColumnSet = new ColumnSet("languagecode")
                        }).Entities.First();

                        bw.ReportProgress(0, "Creating entity...");
                        mm.CreatePowerBiOptionSetRefEntity(organization.GetAttributeValue <int>("languagecode"), bw);
                    }

                    bw.ReportProgress(0, "Processing Option Sets ...");

                    CreateRecords(settings);
                },
                ProgressChanged = evt =>
                {
                    SetWorkingMessage(evt.UserState.ToString());
                },
                PostWorkCallBack = evt =>
                {
                    if (evt.Error != null)
                    {
                        MessageBox.Show(this, "An error ocurred while creating record(s): " + evt.Error.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }

                    tsbCreateRecords.Enabled = true;
                    tsbDeleteEntity.Enabled  = true;
                    tsbLoadEntities.Enabled  = true;
                }
            });
        }