private ForEachInnerActivityTO GetInnerActivity(out string error)
        {
            ForEachInnerActivityTO result = null;

            error = string.Empty;

            try
            {
                var dev2ActivityIOMapping = DataFunc.Handler as IDev2ActivityIOMapping;


                if (dev2ActivityIOMapping == null)
                {
                    error = ErrorResource.ForEachWithNoContentError;
                }
                else
                {
                    var tmp = dev2ActivityIOMapping;
                    result = new ForEachInnerActivityTO(tmp);
                }
            }
            catch (Exception e)
            {
                error = e.Message;
            }


            return(result);
        }
        private ForEachInnerActivityTO GetInnerActivity(out string error)
        {
            ForEachInnerActivityTO result = null;

            error = string.Empty;

            try
            {
                var tmp = DataFunc.Handler as IDev2ActivityIOMapping;

                if (tmp == null)
                {
                    error = "Can not execute a For Each with no content";
                }
                else
                {
                    result = new ForEachInnerActivityTO(tmp);
                }
            }
            catch (Exception e)
            {
                error = e.Message;
            }


            return(result);
        }
        private ForEachInnerActivityTO InnerActivity()
        {
            if (!(OverrideDataFunc.Handler is IDev2ActivityIOMapping ioMapping))
            {
                throw new Exception(ErrorResource.InnerActivityWithNoContentError);
            }

            //TODO: Refactor/Rename ForEachInnerActivityTO to not be specific to "ForEach"
            var innerActivity = new ForEachInnerActivityTO(ioMapping);

            return(innerActivity);
        }
        private string ExecuteOverrideDataFunc(ForEachInnerActivityTO innerActivity)
        {
            IDSFDataObject dataObject = new DsfDataObject(string.Empty, Guid.Empty);

            var origInnerInputMapping = innerActivity.OrigInnerInputMapping;
            var inputs = TranslateInputMappingToInputs(origInnerInputMapping);

            foreach (var serviceInput in inputs)
            {
                var isVariable = ExecutionEnvironment.IsValidVariableExpression(serviceInput.Value, out string errorMessage, 0);
                if (isVariable)
                {
                    var inputName  = _dataObject.Environment.EvalToExpression(serviceInput.Value, _update);
                    var inputValue = ExecutionEnvironment.WarewolfEvalResultToString(_dataObject.Environment.Eval(inputName, _update, false, true));
                    dataObject.Environment.AssignWithFrame(new AssignValue(inputName, inputValue), _update);
                }
                else
                {
                    dataObject.Environment.AssignWithFrame(new AssignValue("[[" + serviceInput.Name + "]]", serviceInput.Value), _update);
                }
            }
            return(dataObject.Environment.ToJson());
        }
        protected override void ExecuteTool(IDSFDataObject dataObject, int update)
        {
            _previousParentId = dataObject.ParentInstanceID;
            _debugInputs      = new List <DebugItem>();
            _debugOutputs     = new List <DebugItem>();

            ErrorResultTO  allErrors = new ErrorResultTO();
            IIndexIterator itr       = null;

            InitializeDebug(dataObject);
            dataObject.ForEachNestingLevel++;
            try
            {
                ErrorResultTO      errors;
                ForEachBootstrapTO exePayload = FetchExecutionType(dataObject, dataObject.Environment, out errors, update);

                foreach (var err in errors.FetchErrors())
                {
                    dataObject.Environment.AddError(err);
                }
                itr = exePayload.IndexIterator;

                string error;
                ForEachInnerActivityTO innerA = GetInnerActivity(out error);
                var exeAct = innerA?.InnerActivity;
                allErrors.AddError(error);
                if (dataObject.IsDebugMode())
                {
                    DispatchDebugState(dataObject, StateType.Before, update);
                }
                dataObject.ParentInstanceID = UniqueID;
                dataObject.IsDebugNested    = true;
                if (dataObject.IsDebugMode())
                {
                    DispatchDebugState(dataObject, StateType.After, update);
                }
                exePayload.InnerActivity = innerA;

                while (itr?.HasMore() ?? false)
                {
                    operationalData = exePayload;
                    int idx         = exePayload.IndexIterator.FetchNextIndex();
                    int innerupdate = 0;
                    if (exePayload.ForEachType != enForEachType.NumOfExecution)
                    {
                        innerupdate = idx;
                    }
                    _childUniqueID = exeAct?.UniqueID;
                    exeAct?.Execute(dataObject, innerupdate);

                    operationalData.IncIterationCount();
                }
                if (errors.HasErrors())
                {
                    allErrors.MergeErrors(errors);
                }
            }
            catch (Exception e)
            {
                Dev2Logger.Error("DSFForEach", e);
                allErrors.AddError(e.Message);
            }
            finally
            {
                if (itr != null)
                {
                    if (ForEachType != enForEachType.NumOfExecution)
                    {
                        RestoreHandlerFn();
                    }
                }
                var serviceTestStep = dataObject.ServiceTest?.TestSteps?.Flatten(step => step.Children)?.FirstOrDefault(step => step.UniqueId == _originalUniqueID);
                if (dataObject.IsServiceTestExecution)
                {
                    var serviceTestSteps = serviceTestStep?.Children;
                    UpdateDebugStateWithAssertions(dataObject, serviceTestSteps?.ToList());
                    if (serviceTestStep != null)
                    {
                        var testRunResult = new TestRunResult();
                        GetFinalTestRunResult(serviceTestStep, testRunResult);
                        serviceTestStep.Result = testRunResult;
                    }
                }
                dataObject.ParentInstanceID = _previousParentId;
                dataObject.ForEachNestingLevel--;
                dataObject.IsDebugNested = false;
                if (dataObject.IsDebugMode())
                {
                    if (dataObject.IsServiceTestExecution && serviceTestStep != null)
                    {
                        var debugItems = TestDebugMessageRepo.Instance.GetDebugItems(dataObject.ResourceID, dataObject.TestName);
                        debugItems = debugItems.Where(state => state.WorkSurfaceMappingId == serviceTestStep.UniqueId).ToList();
                        var debugStates = debugItems.LastOrDefault();

                        var       debugItemStaticDataParams = new DebugItemServiceTestStaticDataParams(serviceTestStep.Result.Message, serviceTestStep.Result.RunTestResult == RunResult.TestFailed);
                        DebugItem itemToAdd = new DebugItem();
                        itemToAdd.AddRange(debugItemStaticDataParams.GetDebugItemResult());
                        debugStates?.AssertResultList?.Add(itemToAdd);
                    }
                    DispatchDebugState(dataObject, StateType.Duration, 0);
                }
                // Handle Errors
                if (allErrors.HasErrors())
                {
                    dataObject.ParentInstanceID = _previousParentId;
                    dataObject.ForEachNestingLevel--;
                    dataObject.IsDebugNested = false;
                    // Handle Errors
                    if (allErrors.HasErrors())
                    {
                        DisplayAndWriteError("DsfForEachActivity", allErrors);
                        foreach (var fetchError in allErrors.FetchErrors())
                        {
                            dataObject.Environment.AddError(fetchError);
                        }

                        dataObject.ParentInstanceID = _previousParentId;
                    }
                }
            }
        }
        protected override void OnExecute(NativeActivityContext context)
        {
            lock (_forEachExecutionObject)
            {
                IDSFDataObject dataObject = context.GetExtension <IDSFDataObject>();

                _debugInputs  = new List <DebugItem>();
                _debugOutputs = new List <DebugItem>();



                dataObject.ForEachNestingLevel++;
                ErrorResultTO allErrors = new ErrorResultTO();

                InitializeDebug(dataObject);
                try
                {
                    ErrorResultTO      errors;
                    ForEachBootstrapTO exePayload = FetchExecutionType(dataObject, dataObject.Environment, out errors, 0);

                    if (errors.HasErrors())
                    {
                        allErrors.MergeErrors(errors);
                        return;
                    }

                    if (dataObject.IsDebugMode())
                    {
                        DispatchDebugState(dataObject, StateType.Before, 0);
                    }

                    dataObject.ParentInstanceID = UniqueID;

                    allErrors.MergeErrors(errors);
                    string error;
                    ForEachInnerActivityTO innerA = GetInnerActivity(out error);
                    allErrors.AddError(error);

                    exePayload.InnerActivity = innerA;

                    operationalData = exePayload;
                    // flag it as scoped so we can use a single DataList
                    dataObject.IsDataListScoped = true;
                    dataObject.IsDebugNested    = true;

                    if (exePayload.InnerActivity != null && exePayload.IndexIterator.HasMore())
                    {
                        int idx = exePayload.IndexIterator.FetchNextIndex();
                        if (exePayload.ForEachType != enForEachType.NumOfExecution)
                        {
                            IterateIOMapping(idx);
                        }
                        else
                        {
                            dataObject.IsDataListScoped = false;
                        }

                        // schedule the func to execute ;)
                        dataObject.ParentInstanceID = UniqueID;

                        context.ScheduleFunc(DataFunc, string.Empty, ActivityCompleted);
                    }
                }
                catch (Exception e)
                {
                    Dev2Logger.Error("DSFForEach", e);
                    allErrors.AddError(e.Message);
                }
                finally
                {
                    // Handle Errors
                    if (allErrors.HasErrors())
                    {
                        DisplayAndWriteError("DsfForEachActivity", allErrors);
                        foreach (var fetchError in allErrors.FetchErrors())
                        {
                            dataObject.Environment.AddError(fetchError);
                        }

                        dataObject.ParentInstanceID = _previousParentId;
                    }
                    if (dataObject.IsDebugMode())
                    {
                        DispatchDebugState(dataObject, StateType.After, 0);
                    }
                }
            }
        }
        protected override void ExecuteTool(IDSFDataObject dataObject)
        {
            _previousParentId = dataObject.ParentInstanceID;
            _debugInputs      = new List <DebugItem>();
            _debugOutputs     = new List <DebugItem>();



            dataObject.ForEachNestingLevel++;
            ErrorResultTO allErrors = new ErrorResultTO();

            InitializeDebug(dataObject);
            try
            {
                ErrorResultTO      errors;
                ForEachBootstrapTO exePayload = FetchExecutionType(dataObject, dataObject.Environment, out errors);

                var    itr = exePayload.IndexIterator;
                string error;
                ForEachInnerActivityTO innerA = GetInnerActivity(out error);
                var exeAct = innerA.InnerActivity;
                allErrors.AddError(error);
                if (dataObject.IsDebugMode())
                {
                    DispatchDebugState(dataObject, StateType.Before);
                }
                dataObject.ParentInstanceID = UniqueID;
                dataObject.IsDebugNested    = true;
                if (dataObject.IsDebugMode())
                {
                    DispatchDebugState(dataObject, StateType.After);
                }
                exePayload.InnerActivity = innerA;
                var ind   = itr.MaxIndex();
                var count = 0;
                while (itr.HasMore() && count < ind)
                {
                    operationalData = exePayload;
                    int idx = exePayload.IndexIterator.FetchNextIndex();
                    if (exePayload.ForEachType != enForEachType.NumOfExecution)
                    {
                        IterateIOMapping(idx);
                    }

                    exeAct.Execute(dataObject);

                    count++;
                }
                if (errors.HasErrors())
                {
                    allErrors.MergeErrors(errors);
                }
            }
            catch (Exception e)
            {
                Dev2Logger.Log.Error("DSFForEach", e);
                allErrors.AddError(e.Message);
            }
            finally
            {
                if (ForEachType != enForEachType.NumOfExecution)
                {
                    RestoreHandlerFn();
                }

                dataObject.ParentInstanceID = _previousParentId;
                dataObject.ForEachNestingLevel--;
                dataObject.IsDebugNested = false;
                // Handle Errors
                if (allErrors.HasErrors())
                {
                    DisplayAndWriteError("DsfForEachActivity", allErrors);
                    foreach (var fetchError in allErrors.FetchErrors())
                    {
                        dataObject.Environment.AddError(fetchError);
                    }

                    dataObject.ParentInstanceID = _previousParentId;
                }
            }
        }