// ReSharper restore RedundantOverridenMember

        /// <summary>
        /// Executes the logic of the activity and calls the backend code to do the work
        /// Also responsible for adding the results to the data list
        /// </summary>
        /// <param name="context"></param>
        protected override void OnExecute(NativeActivityContext context)
        {
            _debugInputs  = new List <DebugItem>();
            _debugOutputs = new List <DebugItem>();
            IDataListCompiler compiler   = DataListFactory.CreateDataListCompiler();
            IDSFDataObject    dataObject = context.GetExtension <IDSFDataObject>();
            ErrorResultTO     errors;
            ErrorResultTO     allErrors = new ErrorResultTO();
            Guid executionId            = dataObject.DataListID;

            InitializeDebug(dataObject);
            try
            {
                IsSingleValueRule.ApplyIsSingleValueRule(Result, allErrors);
                // Fetch all fields to search....
                IList <string> toSearch = FieldsToSearch.Split(',');
                // now process each field for entire evaluated Where expression....
                IBinaryDataListEntry bdle = compiler.Evaluate(executionId, enActionType.User, SearchCriteria, false, out errors);
                if (dataObject.IsDebugMode())
                {
                    var itemToAdd = new DebugItem();
                    itemToAdd.Add(new DebugItemResult {
                        Type = DebugItemResultType.Label, Value = "Fields To Search"
                    });
                    _debugInputs.Add(itemToAdd);
                }
                allErrors.MergeErrors(errors);

                if (bdle != null)
                {
                    IDev2DataListEvaluateIterator itr = Dev2ValueObjectFactory.CreateEvaluateIterator(bdle);
                    IDev2DataListUpsertPayloadBuilder <string> toUpsert = Dev2DataListBuilderFactory.CreateStringDataListUpsertBuilder(true);
                    int idx;
                    if (!Int32.TryParse(StartIndex, out idx))
                    {
                        idx = 1;
                    }
                    IBinaryDataList toSearchList = compiler.FetchBinaryDataList(executionId, out errors);
                    allErrors.MergeErrors(errors);
                    int iterationIndex = 0;
                    foreach (string s in toSearch)
                    {
                        if (dataObject.IsDebugMode())
                        {
                            IBinaryDataListEntry tmpEntry = compiler.Evaluate(executionId, enActionType.User, s, false, out errors);
                            AddDebugInputItem(s, string.Empty, tmpEntry, executionId);
                        }
                        // each entry in the recordset
                        while (itr.HasMoreRecords())
                        {
                            IList <IBinaryDataListItem> cols = itr.FetchNextRowData();
                            foreach (IBinaryDataListItem c in cols)
                            {
                                IRecsetSearch  searchTo = ConvertToSearchTo(c.TheValue, idx.ToString(CultureInfo.InvariantCulture));
                                IList <string> results  = RecordsetInterrogator.FindRecords(toSearchList, searchTo, out errors);
                                allErrors.MergeErrors(errors);
                                string concatRes = string.Empty;

                                // ReSharper disable LoopCanBeConvertedToQuery
                                foreach (string r in results)
                                // ReSharper restore LoopCanBeConvertedToQuery
                                {
                                    concatRes = string.Concat(concatRes, r, ",");
                                }

                                if (concatRes.EndsWith(","))
                                {
                                    concatRes = concatRes.Remove(concatRes.Length - 1);
                                }
                                //2013.06.03: Ashley Lewis for bug 9498 - handle multiple regions in result
                                DataListCleaningUtils.SplitIntoRegions(Result);
                                //2013.06.07: Massimo Guerrera for BUG 9497 - To handle putting out to a scalar as a CSV

                                if (!DataListUtil.IsValueRecordset(Result))
                                {
                                    toUpsert.Add(Result, concatRes);
                                    toUpsert.FlushIterationFrame();
                                    if (dataObject.IsDebugMode())
                                    {
                                        AddDebugOutputItem(new DebugOutputParams(Result, concatRes, executionId, iterationIndex));
                                    }
                                }
                                else
                                {
                                    iterationIndex = 0;

                                    foreach (string r in results)
                                    {
                                        toUpsert.Add(Result, r);
                                        toUpsert.FlushIterationFrame();
                                        if (dataObject.IsDebugMode())
                                        {
                                            AddDebugOutputItem(new DebugOutputParams(Result, r, executionId, iterationIndex));
                                        }

                                        iterationIndex++;
                                    }
                                }
                            }
                        }
                    }

                    if (dataObject.IsDebugMode())
                    {
                        AddDebugInputItem(SearchCriteria, "Where", bdle, executionId);
                    }

                    // now push the result to the server
                    compiler.Upsert(executionId, toUpsert, out errors);
                    allErrors.MergeErrors(errors);
                }
            }
            finally
            {
                var hasErrors = allErrors.HasErrors();
                if (hasErrors)
                {
                    DisplayAndWriteError("DsfFindRecordsActivity", allErrors);
                    compiler.UpsertSystemTag(dataObject.DataListID, enSystemTag.Dev2Error, allErrors.MakeDataListReady(), out errors);
                    compiler.Upsert(executionId, Result, (string)null, out errors);
                }

                if (dataObject.IsDebugMode())
                {
                    if (hasErrors)
                    {
                        AddDebugOutputItem(new DebugItemStaticDataParams("", Result, ""));
                    }
                    DispatchDebugState(context, StateType.Before);
                    DispatchDebugState(context, StateType.After);
                }
            }
        }
예제 #2
0
        /// <summary>
        ///     Executes the logic of the activity and calls the backend code to do the work
        ///     Also responsible for adding the results to the data list
        /// </summary>
        /// <param name="context"></param>
        protected override void OnExecute(NativeActivityContext context)
        {
            _debugInputs  = new List <DebugItem>();
            _debugOutputs = new List <DebugItem>();
            var compiler      = DataListFactory.CreateDataListCompiler();
            var dataObject    = context.GetExtension <IDSFDataObject>();
            var errorResultTo = new ErrorResultTO();
            var allErrors     = new ErrorResultTO();
            var executionId   = dataObject.DataListID;

            InitializeDebug(dataObject);


            int iterationIndex;

            try
            {
                IList <string> toSearch = FieldsToSearch.Split(',');
                if (dataObject.IsDebugMode())
                {
                    AddDebugInputValues(dataObject, toSearch, compiler, executionId, ref errorResultTo);
                }

                allErrors.MergeErrors(errorResultTo);
                IEnumerable <string> results = new List <string>();
                var toUpsert = Dev2DataListBuilderFactory.CreateStringDataListUpsertBuilder(true);
                toUpsert.IsDebug = dataObject.IsDebugMode();
                bool isFirstIteration = true;
                // ReSharper disable ForCanBeConvertedToForeach
                for (var i = 0; i < ResultsCollection.Count; i++)
                // ReSharper restore ForCanBeConvertedToForeach
                {
                    IEnumerable <string> resultsDuringSearch = new List <string>();
                    var currenSearchResults = new List <string>();
                    IDev2IteratorCollection itrCollection = Dev2ValueObjectFactory.CreateIteratorCollection();

                    IBinaryDataListEntry          binaryDataListEntrySearchCrit = compiler.Evaluate(executionId, enActionType.User, ResultsCollection[i].SearchCriteria, false, out errorResultTo);
                    IDev2DataListEvaluateIterator searchCritItr = Dev2ValueObjectFactory.CreateEvaluateIterator(binaryDataListEntrySearchCrit);
                    itrCollection.AddIterator(searchCritItr);
                    allErrors.MergeErrors(errorResultTo);

                    IBinaryDataListEntry          binaryDataListEntryFrom = compiler.Evaluate(executionId, enActionType.User, ResultsCollection[i].From, false, out errorResultTo);
                    IDev2DataListEvaluateIterator fromItr = Dev2ValueObjectFactory.CreateEvaluateIterator(binaryDataListEntryFrom);
                    itrCollection.AddIterator(fromItr);
                    allErrors.MergeErrors(errorResultTo);

                    IBinaryDataListEntry          binaryDataListEntryTo = compiler.Evaluate(executionId, enActionType.User, ResultsCollection[i].To, false, out errorResultTo);
                    IDev2DataListEvaluateIterator toItr = Dev2ValueObjectFactory.CreateEvaluateIterator(binaryDataListEntryTo);
                    itrCollection.AddIterator(toItr);
                    allErrors.MergeErrors(errorResultTo);

                    int idx;
                    if (!Int32.TryParse(StartIndex, out idx))
                    {
                        idx = 1;
                    }
                    var toSearchList = compiler.FetchBinaryDataList(executionId, out errorResultTo);
                    allErrors.MergeErrors(errorResultTo);



                    var searchType = ResultsCollection[i].SearchType;
                    if (string.IsNullOrEmpty(searchType))
                    {
                        continue;
                    }
                    // ReSharper disable PossibleMultipleEnumeration
                    while (itrCollection.HasMoreData())
                    {
                        var currentResults = results as IList <string> ?? results.ToList();

                        var splitOn        = new[] { "," };
                        var fieldsToSearch = FieldsToSearch.Split(splitOn, StringSplitOptions.RemoveEmptyEntries);

                        SearchTO searchTo;

                        if (fieldsToSearch.Length > 0)
                        {
                            bool isFirstFieldIteration = true;
                            foreach (var field in fieldsToSearch)
                            {
                                IList <string> iterationResults = new List <string>();
                                searchTo = DataListFactory.CreateSearchTO(field, searchType,
                                                                          itrCollection.FetchNextRow(searchCritItr)
                                                                          .TheValue,
                                                                          idx.ToString(CultureInfo.InvariantCulture),
                                                                          Result, MatchCase,
                                                                          false,
                                                                          itrCollection.FetchNextRow(fromItr).TheValue,
                                                                          itrCollection.FetchNextRow(toItr).TheValue);
                                ValidateRequiredFields(searchTo, out errorResultTo);
                                allErrors.MergeErrors(errorResultTo);
                                // ReSharper disable ConvertClosureToMethodGroup
                                (RecordsetInterrogator.FindRecords(toSearchList, searchTo, out errorResultTo)).ToList().ForEach(it => iterationResults.Add(it));
                                // ReSharper restore ConvertClosureToMethodGroup

                                if (RequireAllFieldsToMatch)
                                {
                                    resultsDuringSearch = isFirstFieldIteration ? iterationResults : currenSearchResults.Intersect(iterationResults);
                                    currenSearchResults = resultsDuringSearch.ToList();
                                }
                                else
                                {
                                    resultsDuringSearch = currenSearchResults.Union(iterationResults);
                                    currenSearchResults = RequireAllTrue ? new List <string>() : resultsDuringSearch.ToList();
                                }
                                isFirstFieldIteration = false;
                            }
                        }
                        else
                        {
                            searchTo = (SearchTO)ConvertToSearchTO(itrCollection.FetchNextRow(searchCritItr).TheValue,
                                                                   searchType, idx.ToString(CultureInfo.InvariantCulture),
                                                                   itrCollection.FetchNextRow(fromItr).TheValue,
                                                                   itrCollection.FetchNextRow(toItr).TheValue);

                            ValidateRequiredFields(searchTo, out errorResultTo);
                            allErrors.MergeErrors(errorResultTo);
                            resultsDuringSearch = RecordsetInterrogator.FindRecords(toSearchList, searchTo, out errorResultTo);
                        }

                        allErrors.MergeErrors(errorResultTo);

                        if (RequireAllTrue)
                        {
                            results = isFirstIteration ? resultsDuringSearch : currentResults.Intersect(resultsDuringSearch);
                        }
                        else
                        {
                            results = currentResults.Union(resultsDuringSearch);
                        }
                        isFirstIteration = false;
                    }
                }

                DataListCleaningUtils.SplitIntoRegions(Result);
                var rule           = new IsSingleValueRule(() => Result);
                var singleresError = rule.Check();
                if (singleresError != null)
                {
                    allErrors.AddError(singleresError.Message);
                }
                else
                {
                    string concatRes  = String.Empty;
                    var    allResults = results as IList <string> ?? results.ToList();
                    // ReSharper restore PossibleMultipleEnumeration
                    if (allResults.Count == 0)
                    {
                        allResults.Add("-1");
                    }

                    if (!DataListUtil.IsValueRecordset(Result))
                    {
                        // ReSharper disable LoopCanBeConvertedToQuery
                        foreach (var r in allResults)
                        // ReSharper restore LoopCanBeConvertedToQuery
                        {
                            concatRes = string.Concat(concatRes, r, ",");
                        }

                        if (concatRes.EndsWith(","))
                        {
                            concatRes = concatRes.Remove(concatRes.Length - 1);
                        }
                        toUpsert.Add(Result, concatRes);
                        toUpsert.FlushIterationFrame();
                    }
                    else
                    {
                        iterationIndex = 0;

                        foreach (var r in allResults)
                        {
                            toUpsert.Add(Result, r);
                            toUpsert.FlushIterationFrame();
                            iterationIndex++;
                        }
                    }
                    compiler.Upsert(executionId, toUpsert, out errorResultTo);
                    allErrors.MergeErrors(errorResultTo);

                    if (dataObject.IsDebugMode() && !allErrors.HasErrors())
                    {
                        foreach (var debugTo in toUpsert.DebugOutputs)
                        {
                            AddDebugOutputItem(new DebugItemVariableParams(debugTo));
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                Dev2Logger.Log.Error("DSFRecordsMultipleCriteria", exception);
                allErrors.AddError(exception.Message);
            }
            finally
            {
                var hasErrors = allErrors.HasErrors();
                if (hasErrors)
                {
                    DisplayAndWriteError("DsfFindRecordsMultipleCriteriaActivity", allErrors);
                    compiler.UpsertSystemTag(dataObject.DataListID, enSystemTag.Dev2Error, allErrors.MakeDataListReady(), out errorResultTo);
                    compiler.Upsert(executionId, Result, (string)null, out errorResultTo);
                }

                if (dataObject.IsDebugMode())
                {
                    if (hasErrors)
                    {
                        iterationIndex = 0;
                        var regions = DataListCleaningUtils.SplitIntoRegions(Result);
                        foreach (var region in regions)
                        {
                            AddDebugOutputItem(new DebugOutputParams(region, "", executionId, iterationIndex));
                            iterationIndex++;
                        }
                    }

                    DispatchDebugState(context, StateType.Before);
                    DispatchDebugState(context, StateType.After);
                }
            }
        }