protected override Task Validate()
        {
            var conditions = ConfigurationControls.FindByName <FilterPane>("Selected_Filter");

            if (conditions.Selected)
            {
                return(Task.FromResult(0));
            }
            var sd = JsonConvert.DeserializeObject(conditions.Value);

            if (conditions.Value != "[]")
            {
                var condition = JsonConvert.DeserializeObject <FilterDataDTO>(conditions.Value);
                foreach (var c in condition.Conditions)
                {
                    if (!string.IsNullOrEmpty(c.Field))
                    {
                        var error         = " cannot be empty";
                        var operatorError = "";
                        var valueError    = "";
                        if (string.IsNullOrEmpty(c.Operator))
                        {
                            operatorError = "Operator ";
                        }
                        if (string.IsNullOrEmpty(c.Value))
                        {
                            valueError = "Value";
                        }
                        if (!string.IsNullOrEmpty(operatorError) && !string.IsNullOrEmpty(valueError))
                        {
                            ValidationManager.SetError(operatorError + " " + "and " + valueError + error, "Selected_Filter");
                        }
                        else if (string.IsNullOrEmpty(operatorError) && !string.IsNullOrEmpty(valueError))
                        {
                            ValidationManager.SetError(valueError + error, "Selected_Filter");
                        }
                        else if (!string.IsNullOrEmpty(operatorError) && string.IsNullOrEmpty(valueError))
                        {
                            ValidationManager.SetError(operatorError + error, "Selected_Filter");
                        }
                    }
                }
            }
            return(Task.FromResult(0));
        }
        public override async Task FollowUp()
        {
            var specificRecipientOption = ((RadioButtonGroup)ConfigurationControls.Controls[0]).Radios[0];
            var specificTemplateOption  = ((RadioButtonGroup)ConfigurationControls.Controls[0]).Radios[1];
            var howToBeNotifiedDdl      = (DropDownList)ConfigurationControls.FindByName("NotificationHandler");

            //let's don't add child actions to solution until how to be notified option is selected
            //FR-1873
            if (string.IsNullOrEmpty(howToBeNotifiedDdl.Value))
            {
                return;
            }
            var specificRecipient = specificRecipientOption.Controls.Single();

            if (specificRecipientOption.Selected && string.IsNullOrEmpty(specificRecipient.Value))
            {
                return;
            }

            var specificTemplate = specificTemplateOption.Controls.Single();

            if (specificTemplateOption.Selected && string.IsNullOrEmpty(specificTemplate.Value))
            {
                return;
            }

            bool hasChildren = false;

            if (ActivityPayload.ChildrenActivities.Any())
            {
                hasChildren = true;
                ActivityPayload.ChildrenActivities.Clear();
            }

            var monitorDocusignAT = await HubCommunicator.GetActivityTemplate("terminalDocuSign", "Monitor_DocuSign_Envelope_Activity");

            var setDelayAT = await HubCommunicator.GetActivityTemplate("terminalFr8Core", "Set_Delay");

            var queryFr8WareHouseAT = await HubCommunicator.GetActivityTemplate("terminalFr8Core", "Query_Fr8_Warehouse");

            var testIncomingDataAT = await HubCommunicator.GetActivityTemplate("terminalFr8Core", "Test_Incoming_Data");

            var buildMessageAT = await HubCommunicator.GetActivityTemplate("terminalFr8Core", "Build_Message");

            //DocuSign
            var tasks = new List <Task>();
            var monitorDocuSignActionTask = HubCommunicator.AddAndConfigureChildActivity(ActivityPayload, monitorDocusignAT, "Monitor Docusign Envelope Activity", "Monitor Docusign Envelope Activity", 1);

            tasks.Add(monitorDocuSignActionTask);
            var setDelayActionTask = HubCommunicator.AddAndConfigureChildActivity(ActivityPayload, setDelayAT, "Set Delay", "Set Delay", 2);

            tasks.Add(setDelayActionTask);
            var queryFr8WarehouseActionTask = HubCommunicator.AddAndConfigureChildActivity(ActivityPayload, queryFr8WareHouseAT, "Query Fr8 Warehouse", "Query Fr8 Warehouse", 3);

            tasks.Add(queryFr8WarehouseActionTask);
            var filterActionTask = HubCommunicator.AddAndConfigureChildActivity(ActivityPayload, testIncomingDataAT, "Test Incoming Data", "Test Incoming Data", 4);

            tasks.Add(filterActionTask);
            Task <ActivityPayload> buildMessageActivityTask = null;

            if (!hasChildren)
            {
                buildMessageActivityTask = HubCommunicator.AddAndConfigureChildActivity((Guid)ActivityPayload.ParentPlanNodeId, buildMessageAT, "Build a Message", "Build a Message", 2);
                tasks.Add(buildMessageActivityTask);
            }

            await Task.WhenAll(tasks);

            var monitorDocuSignAction   = monitorDocuSignActionTask.Result;
            var setDelayAction          = setDelayActionTask.Result;
            var queryFr8WarehouseAction = queryFr8WarehouseActionTask.Result;
            var filterAction            = filterActionTask.Result;

            // var notifierActivity = notifierActivityTask.Result;
            if (specificRecipientOption.Selected)
            {
                ActivityConfigurator.SetControlValue(monitorDocuSignAction, "TemplateRecipientPicker.recipient.RecipientValue", specificRecipientOption.Controls[0].Value);
            }
            else if (specificTemplateOption.Selected)
            {
                var ddlbTemplate = (specificTemplateOption.Controls[0] as DropDownList);
                ActivityConfigurator.SetControlValue(monitorDocuSignAction, "TemplateRecipientPicker.template.UpstreamCrate",
                                                     ddlbTemplate.ListItems.Single(a => a.Key == ddlbTemplate.selectedKey));
            }

            if (buildMessageActivityTask != null)
            {
                var buildMessageActivity = buildMessageActivityTask.Result;

                ActivityConfigurator.SetControlValue(buildMessageActivity, "Body", MessageBody);
                ActivityConfigurator.SetControlValue(buildMessageActivity, "Name", "NotificationMessage");

                buildMessageActivity = await HubCommunicator.ConfigureActivity(buildMessageActivity);
            }

            if (!hasChildren)
            {
                var notifierAT = await HubCommunicator.GetActivityTemplate(Guid.Parse(howToBeNotifiedDdl.Value));

                var notifierActivity = await HubCommunicator.AddAndConfigureChildActivity((Guid)ActivityPayload.ParentPlanNodeId, notifierAT, howToBeNotifiedDdl.selectedKey, howToBeNotifiedDdl.selectedKey, 3);

                SetNotifierActivityBody(notifierActivity);
                await HubCommunicator.ConfigureActivity(notifierActivity);
            }

            ActivityConfigurator.SetControlValue(monitorDocuSignAction, "EnvelopeSent", "true");
            //let's make followup configuration for monitorDocuSignEventAction
            //followup call places EventSubscription crate in storage
            var configureMonitorDocusignTask = HubCommunicator.ConfigureActivity(monitorDocuSignAction);


            var durationControl = (Duration)ConfigurationControls.FindByName("TimePeriod");

            ActivityConfigurator.SetControlValue(setDelayAction, "Delay_Duration", durationControl.Value);
            await SetQueryFr8WarehouseActivityFields(queryFr8WarehouseAction, specificRecipientOption.Controls[0].Value);

            //let's make a followup configuration to fill criteria fields
            var configureQueryMTTask = HubCommunicator.ConfigureActivity(queryFr8WarehouseAction);
            var recipientEventStatus = (DropDownList)ConfigurationControls.FindByName("RecipientEvent");

            SetFilterUsingRunTimeActivityFields(filterAction, recipientEventStatus.Value);

            await Task.WhenAll(configureMonitorDocusignTask, configureQueryMTTask);

            monitorDocuSignAction = configureMonitorDocusignTask.Result;
            ActivityPayload.ChildrenActivities    = ActivityPayload.ChildrenActivities.OrderBy(a => a.Ordering).ToList();
            ActivityPayload.ChildrenActivities[0] = monitorDocuSignAction;
        }
        protected async Task HandleFollowUpConfiguration()
        {
            if (Storage.Count == 0)
            {
                return;
            }


            //update docusign templates list to get if new templates were provided by DS
            FillDocuSignTemplateSource("target_docusign_template");
            // Try to find DocuSignTemplate drop-down.

            var dropdownControlDTO = ConfigurationControls.FindByName("target_docusign_template");

            if (dropdownControlDTO == null)
            {
                return;
            }
            // Get DocuSign Template Id
            var docusignTemplateId = dropdownControlDTO.Value;

            //Abort configuration if templateId is the same that before
            if (!IsNewTemplateIdChoosen(Storage, docusignTemplateId))
            {
                return;
            }

            var conf          = DocuSignManager.SetUp(AuthorizationToken);
            var tabsandfields = DocuSignManager.GetTemplateRecipientsTabsAndDocuSignTabs(conf, docusignTemplateId);

            var roles = tabsandfields.Item1.Where(a => a.Tags.Contains(DocuSignConstants.DocuSignSignerTag));

            Storage.RemoveByLabel("DocuSignTemplateRolesFields");
            Storage.Add("DocuSignTemplateRolesFields", new KeyValueListCM(roles));


            var envelopeDataDTO = tabsandfields.Item2;

            var userDefinedFields = tabsandfields.Item1.Where(a => a.Tags.Contains(DocuSignConstants.DocuSignTabTag));

            //check for DocuSign default template names and add advisory json
            var hasDefaultNames = DocuSignManager.DocuSignTemplateDefaultNames(tabsandfields.Item2);

            if (hasDefaultNames)
            {
                var advisoryCrate          = Storage.CratesOfType <AdvisoryMessagesCM>().FirstOrDefault();
                var currentAdvisoryResults = advisoryCrate == null ? new AdvisoryMessagesCM() : advisoryCrate.Content;

                var advisory = currentAdvisoryResults.Advisories.FirstOrDefault(x => x.Name == advisoryName);

                if (advisory == null)
                {
                    currentAdvisoryResults.Advisories.Add(new AdvisoryMessageDTO {
                        Name = advisoryName, Content = advisoryContent
                    });
                }
                else
                {
                    advisory.Content = advisoryContent;
                }

                Storage.Add(Crate.FromContent("Advisories", currentAdvisoryResults));
            }

            Storage.RemoveByLabel("DocuSignTemplateUserDefinedFields");
            Storage.Add("DocuSignTemplateUserDefinedFields", new KeyValueListCM(userDefinedFields.Concat(roles)));

            //Create TextSource controls for ROLES
            var rolesMappingBehavior = new TextSourceMappingBehavior(Storage, "RolesMapping", true);

            rolesMappingBehavior.Clear();
            rolesMappingBehavior.Append(roles.Select(x => x.Key).ToList(), "Upstream Terminal-Provided Fields", AvailabilityType.RunTime);

            //Create Text Source controls for TABS
            var textSourceFields = new List <string>();

            textSourceFields = envelopeDataDTO.Where(x => x.Fr8DisplayType == ControlTypes.TextBox).Select(x => x.Name).ToList();
            var mappingBehavior = new TextSourceMappingBehavior(
                Storage,
                "Mapping",
                true
                );

            mappingBehavior.Clear();
            mappingBehavior.Append(textSourceFields, "Upstream Terminal-Provided Fields", AvailabilityType.RunTime);
            //Create TextSource controls for ROLES

            //Create radio Button Groups
            var radioButtonGroupBehavior = new RadioButtonGroupMappingBehavior(Storage, "RadioGroupMapping");

            radioButtonGroupBehavior.Clear();
            foreach (var item in envelopeDataDTO.Where(x => x.Fr8DisplayType == ControlTypes.RadioButtonGroup).ToList())
            {
                var radioButtonGroupDTO = item as DocuSignMultipleOptionsTabDTO;
                if (radioButtonGroupDTO == null)
                {
                    continue;
                }
                //todo: migrate the string format for label into template
                radioButtonGroupBehavior.Append(radioButtonGroupDTO.Name,
                                                $"For the <strong>{radioButtonGroupDTO.Name}</strong>, use:", radioButtonGroupDTO.Items.Select(x => new RadioButtonOption()
                {
                    Name     = x.Value,
                    Value    = x.Value,
                    Selected = x.Selected
                }).ToList());
            }

            //create checkbox controls
            var checkBoxMappingBehavior = new CheckBoxMappingBehavior(Storage, "CheckBoxMapping");

            checkBoxMappingBehavior.Clear();
            foreach (var item in envelopeDataDTO.Where(x => x.Fr8DisplayType == ControlTypes.CheckBox).ToList())
            {
                checkBoxMappingBehavior.Append(item.Name, item.Name);
            }

            //create dropdown controls
            var dropdownListMappingBehavior = new DropDownListMappingBehavior(Storage, "DropDownMapping");

            dropdownListMappingBehavior.Clear();
            foreach (var item in envelopeDataDTO.Where(x => x.Fr8DisplayType == ControlTypes.DropDownList).ToList())
            {
                var dropDownListDTO = item as DocuSignMultipleOptionsTabDTO;
                if (dropDownListDTO == null)
                {
                    continue;
                }

                dropdownListMappingBehavior.Append(dropDownListDTO.Name, $"For the <strong>{item.Name}</strong>, use:", dropDownListDTO.Items.Where(x => x.Text != string.Empty || x.Value != string.Empty).Select(x => new ListItem()
                {
                    Key      = string.IsNullOrEmpty(x.Value) ? x.Text : x.Value,
                    Value    = string.IsNullOrEmpty(x.Text) ? x.Value : x.Text,
                    Selected = x.Selected,
                }).ToList());
            }
        }