public ActionResult <ResponseStatus <NameIdPair[]> > CreateForm(
            [SwaggerWcfParameter(Description = "Contains the definition of one or more forms, plus optional reference data", Required = true)]
            [FromBody] FormCreateRequest request)
        {
            try
            {
                Logger.Info($"CreateForm({request}) called");
                var response = this.Service.CreateForm(request, true);
                Logger.Info($"CreateForm({request}) returned {Magmasystems.Framework.Serialization.Json.Serialize(response)}");

                this.Response.StatusCode = (int)HttpStatusCode.Created;
                return(response);
            }
            catch (Exception exc)
            {
                Logger.Error(exc);
                this.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(null);
            }
        }
        private void CreateAndRunForm(FormCreateRequest request)
        {
            using (FormManagerService service = new FormManagerService(new FormManagerServiceSettings {
                AutomatedInput = true
            }))
            {
                var response = service.CreateForm(request);
                Assert.IsNotNull(response);
                Assert.AreEqual(response.StatusCode, ResponseStatusCodes.OK);

                var formDefId = response.Value[0].Id;

                var newformResponse = service.NewForm(formDefId);
                Assert.IsNotNull(newformResponse);
                Assert.AreEqual(newformResponse.StatusCode, ResponseStatusCodes.OK);

                var idFormInstance = newformResponse.Value;
                var formInstance   = SBSFormModel.Instance.GetFormInstance(idFormInstance);
                Assert.IsNotNull(formInstance);

                ManualResetEvent eventStop = new ManualResetEvent(false);
                formInstance.Submitted += form => { eventStop.Set(); };
                formInstance.Cancelled += form => { eventStop.Set(); };
                service.RunFormEnded   += form =>
                {
                    if (form.Id == idFormInstance)
                    {
                        eventStop.Set();
                    }
                };

                var runformResponse = service.RunForm(idFormInstance);
                Assert.IsNotNull(runformResponse);
                Assert.AreEqual(runformResponse.StatusCode, ResponseStatusCodes.OK);

                bool rc = eventStop.WaitOne(5 * 1000);
                Assert.IsTrue(rc, "The test timed out");
            }
        }
        public void TestRepeaterGroup()
        {
            FormCreateRequest request = new FormCreateRequest
            {
                Forms = new List <FormTemplateFormDefinition>
                {
                    new FormTemplateFormDefinition
                    {
                        Title       = "Test Repeater Marc Form",
                        Name        = "RepeaterGroupForm",
                        Description = "This is a form for testing a repeater group",
                        Fields      = new List <FormTemplateFieldDefinition>
                        {
                            new FormTemplateFieldDefinition {
                                Name = "fieldOne", FieldType = "edit", Prompt = "Hit anything", DefaultValue = "foo"
                            },
                            new FormTemplateFieldDefinition
                            {
                                Name       = "employeeDetailsGroup",
                                FieldType  = "repeater",
                                Properties = new Properties
                                {
                                    { "groupname", "Employees" },
                                    { "suffix", "${index}" },
                                    { "end", "cbAddAnother" },
                                    { "continuevalue", true }      // if the value of cbAddAnother is true, then loop back
                                }
                            },
                            new FormTemplateFieldDefinition {
                                Name = "a", FieldType = "edit", Prompt = "a"
                            },
                            new FormTemplateFieldDefinition {
                                Name = "b", FieldType = "edit", Prompt = "b"
                            },
                            new FormTemplateFieldDefinition {
                                Name = "c", FieldType = "edit", Prompt = "c"
                            },
                            new FormTemplateFieldDefinition
                            {
                                Name = "btnSave", FieldType = "button", Prompt = "Save Employee", SubmissionFunctions = new List <FormSubmissionFunction>
                                {
                                    new FormSubmissionFunction
                                    {
                                        Workflow   = "rest://localhost:8089/DIYOnboardingService/form/${var:RepeaterGroupForm.Id}/employee/add",
                                        Properties = new Properties
                                        {
                                            { "method", "post" },
                                            { "body", "${form:RepeaterGroupForm}" },
                                            { "bodyFormat", "json" }
                                        }
                                    }
                                }
                            },
                            new FormTemplateFieldDefinition {
                                Name = "cbAddAnother", FieldType = "checkbox", Prompt = "Add another employee"
                            },
                            new FormTemplateFieldDefinition {
                                Name = "fieldTwo", FieldType = "edit", Prompt = "Hit anything", DefaultValue = "baz"
                            },
                        }
                    }
                }
            };

            // Make sure that this request is serializable into Json
            Json.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
            var json = Json.Serialize(request);

            Assert.IsNotNull(json);

            using (FormManagerService service = new FormManagerService(new FormManagerServiceSettings {
                NoCreateRestService = true, NoMessaging = true, AutomatedInput = true
            }))
            {
                // Create the form definition
                var responseCreate = service.CreateForm(request);
                Assert.IsNotNull(responseCreate);
                Assert.AreEqual(responseCreate.StatusCode, ResponseStatusCodes.OK);

                // Create an instance of the form.
                // This will exercise the logic that creates the SBSRepeaterField
                var formDefId       = responseCreate.Value[0].Id;
                var responseNewForm = service.NewForm(formDefId);
                Assert.IsNotNull(responseNewForm);
                Assert.AreEqual(responseNewForm.StatusCode, ResponseStatusCodes.OK);

                var idFormInstance = responseNewForm.Value;

                // Make sure that we can find the repeater field within the form
                var responseField = service.GetField(idFormInstance, "employeeDetailsGroup");
                Assert.IsNotNull(responseField);
                Assert.AreEqual(responseField.StatusCode, ResponseStatusCodes.OK);
                Assert.IsNotNull(responseField.Value);

                var field = responseField.Value;
                Assert.IsTrue(field is SBSRepeaterField);

                // Make sure that the properties took hold
                SBSRepeaterField repeaterField = (SBSRepeaterField)field;
                Assert.AreEqual(repeaterField.EndingFieldName, "cbAddAnother");

                // Get the instance of the form
                var formInstance = SBSFormModel.Instance.GetFormInstance(idFormInstance);
                Assert.IsNotNull(formInstance);

                // Prepare for the form to run asynchronously
                ManualResetEvent eventStop = new ManualResetEvent(false);
                formInstance.Submitted += form => { eventStop.Set(); };
                formInstance.Cancelled += form => { eventStop.Set(); };
                service.RunFormEnded   += form =>
                {
                    if (form.Id == idFormInstance)
                    {
                        eventStop.Set();
                    }
                };

                // Run the form asynchronously
                var runformResponse = service.RunForm(idFormInstance);
                Assert.IsNotNull(runformResponse);
                Assert.AreEqual(runformResponse.StatusCode, ResponseStatusCodes.OK);

                // Wait for the form to be finished
                bool rc = eventStop.WaitOne();
                Assert.IsTrue(rc, "The test failed");
            }
        }