Example #1
0
        private void buttonGenerate_Click(object sender, EventArgs eArgs)
        {
            var selectedUser = (UserViewModel)comboBoxUsers.SelectedItem;

            if (selectedUser == null)
            {
                MessageBox.Show($"Must select a user before generating the model.", "Error Generating XRM Model",
                                MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }

            toolBar.Enabled = false;

            WorkAsync(
                new WorkAsyncInfo
            {
                Message = "Retrieving data ...",
                Work    =
                    (w, e) =>
                {
                    w.ReportProgress(0, $"Generating XRM Model ...");

                    var groupedForms = ControlData.SelectedForms
                                       .GroupBy(f => f.ObjectTypeCode).ToList();
                    var modelGenerator = new ModelGenerator();
                    var models         = new List <XrmModel>();

                    for (var i = 0; i < groupedForms.Count; i++)
                    {
                        var group = groupedForms[i];
                        w.ReportProgress((i + 1) / groupedForms.Count * 90, $"Generating model for {group.Key} ...");

                        models.Add(
                            modelGenerator.Generate(
                                Service,
                                new ModelGeneratorParams
                        {
                            SelectedUserId = selectedUser.Id.GetValueOrDefault(),
                            EntityName     = group.Key,
                            SelectedForms  = group.Select(g => g.Id.GetValueOrDefault()).ToList()
                        }));
                    }

                    var templateModel =
                        new T4TemplateModel
                    {
                        IsGenerateOnlineCode = ControlData.IsGenerateOnlineCode,
                        Models = models
                    };

                    w.ReportProgress(95, $"Generating TypeScript code ...");
                    var page        = new XrmMockGeneratorTemplate(templateModel);
                    var pageContent = page.TransformText();

                    e.Result = pageContent;
                },
                ProgressChanged =
                    e =>
                {
                    // If progress has to be notified to user, use the following method:
                    SetWorkingMessage(e.UserState.ToString());

                    // If progress has to be notified to user, through the
                    // status bar, use the following method
                    SendMessageToStatusBar?.Invoke(this,
                                                   new StatusBarMessageEventArgs(e.ProgressPercentage, e.UserState.ToString()));
                },
                PostWorkCallBack =
                    e =>
                {
                    SendMessageToStatusBar?.Invoke(this, new StatusBarMessageEventArgs(""));

                    var result       = (string)e.Result;
                    var saveDialogue =
                        new SaveFileDialog
                    {
                        Title           = "Save XRM Model ...",
                        OverwritePrompt = true,
                        Filter          = "TypeScript files (*.ts)|*.ts",
                        FileName        = "xrm.model.ts"
                    };
                    saveDialogue.ShowDialog();

                    if (saveDialogue.FileName.IsNotEmpty())
                    {
                        using (var stream = (FileStream)saveDialogue.OpenFile())
                        {
                            var array = Encoding.UTF8.GetBytes(result);
                            stream.Write(array, 0, array.Length);
                        }
                    }

                    toolBar.Enabled = true;
                },
                AsyncArgument = null,
                IsCancelable  = false,
                MessageWidth  = 340,
                MessageHeight = 150
            });
        }
 public XrmMockGeneratorTemplate(T4TemplateModel templateModel)
 {
     this.templateModel = templateModel;
 }