Esempio n. 1
0
        // ReSharper restore RedundantOverridenMember

        /// <summary>
        /// The execute method that is called when the activity is executed at run time and will hold all the logic of the activity
        /// </summary>
        protected override void OnExecute(NativeActivityContext context)
        {
            _debugInputs  = new List <DebugItem>();
            _debugOutputs = new List <DebugItem>();
            IDSFDataObject dataObject = context.GetExtension <IDSFDataObject>();

            IDataListCompiler compiler    = DataListFactory.CreateDataListCompiler();
            IDev2IndexFinder  indexFinder = new Dev2IndexFinder();
            ErrorResultTO     allErrors   = new ErrorResultTO();
            ErrorResultTO     errors      = new ErrorResultTO();
            Guid executionId = DataListExecutionID.Get(context);

            InitializeDebug(dataObject);
            IDev2DataListUpsertPayloadBuilder <List <string> > toUpsert       = Dev2DataListBuilderFactory.CreateStringListDataListUpsertBuilder();
            IDev2DataListUpsertPayloadBuilder <string>         toUpsertScalar = Dev2DataListBuilderFactory.CreateStringDataListUpsertBuilder();

            toUpsert.IsDebug       = dataObject.IsDebugMode();
            toUpsertScalar.IsDebug = dataObject.IsDebugMode();
            try
            {
                IDev2IteratorCollection outerIteratorCollection = Dev2ValueObjectFactory.CreateIteratorCollection();
                IDev2IteratorCollection innerIteratorCollection = Dev2ValueObjectFactory.CreateIteratorCollection();

                allErrors.MergeErrors(errors);


                IBinaryDataListEntry expressionsEntry = compiler.Evaluate(executionId, enActionType.User, Characters, false, out errors);
                allErrors.MergeErrors(errors);
                IDev2DataListEvaluateIterator itrChar = Dev2ValueObjectFactory.CreateEvaluateIterator(expressionsEntry);

                outerIteratorCollection.AddIterator(itrChar);

                #region Iterate and Find Index

                expressionsEntry = compiler.Evaluate(executionId, enActionType.User, InField, false, out errors);

                if (dataObject.IsDebugMode())
                {
                    AddDebugInputItem(new DebugItemVariableParams(InField, "In Field", expressionsEntry, executionId));
                    AddDebugInputItem(new DebugItemStaticDataParams(Index, "Index"));
                    AddDebugInputItem(new DebugItemVariableParams(Characters, "Characters", itrChar.FetchEntry(), executionId));
                    AddDebugInputItem(new DebugItemStaticDataParams(Direction, "Direction"));
                }

                var completeResultList = new List <string>();

                while (outerIteratorCollection.HasMoreData())
                {
                    allErrors.MergeErrors(errors);
                    errors.ClearErrors();
                    IDev2DataListEvaluateIterator itrInField = Dev2ValueObjectFactory.CreateEvaluateIterator(expressionsEntry);
                    innerIteratorCollection.AddIterator(itrInField);

                    string chars = outerIteratorCollection.FetchNextRow(itrChar).TheValue;
                    while (innerIteratorCollection.HasMoreData())
                    {
                        if (!string.IsNullOrEmpty(InField) && !string.IsNullOrEmpty(Characters))
                        {
                            var val = innerIteratorCollection.FetchNextRow(itrInField);
                            if (val != null)
                            {
                                IEnumerable <int> returedData = indexFinder.FindIndex(val.TheValue, Index, chars, Direction, MatchCase, StartIndex);
                                completeResultList.AddRange(returedData.Select(value => value.ToString(CultureInfo.InvariantCulture)).ToList());
                                //2013.06.03: Ashley Lewis for bug 9498 - handle multiple regions in result
                            }
                        }
                    }
                }
                var rule   = new IsSingleValueRule(() => Result);
                var single = rule.Check();
                if (single != null)
                {
                    allErrors.AddError(single.Message);
                }
                else
                {
                    var rsType = DataListUtil.GetRecordsetIndexType(Result);
                    if (rsType == enRecordsetIndexType.Numeric)
                    {
                        toUpsertScalar.Add(Result, string.Join(",", completeResultList));
                        compiler.Upsert(executionId, toUpsertScalar, out errors);
                        allErrors.MergeErrors(errors);
                        if (!allErrors.HasErrors() && dataObject.IsDebugMode())
                        {
                            foreach (var debugOutputTo in toUpsertScalar.DebugOutputs)
                            {
                                AddDebugOutputItem(new DebugItemVariableParams(debugOutputTo));
                            }
                            toUpsert.DebugOutputs.Clear();
                        }
                    }
                    else
                    {
                        toUpsert.Add(Result, completeResultList);
                        compiler.Upsert(executionId, toUpsert, out errors);
                        allErrors.MergeErrors(errors);
                        if (!allErrors.HasErrors() && dataObject.IsDebugMode())
                        {
                            foreach (var debugOutputTo in toUpsert.DebugOutputs)
                            {
                                AddDebugOutputItem(new DebugItemVariableParams(debugOutputTo));
                            }
                            toUpsert.DebugOutputs.Clear();
                        }
                    }
                }
                #endregion
            }
            catch (Exception e)
            {
                Dev2Logger.Log.Error("DSFFindActivity", e);
                allErrors.AddError(e.Message);
            }
            finally
            {
                #region Handle Errors
                var hasErrors = allErrors.HasErrors();
                if (hasErrors)
                {
                    DisplayAndWriteError("DsfIndexActivity", allErrors);
                    compiler.UpsertSystemTag(dataObject.DataListID, enSystemTag.Dev2Error, allErrors.MakeDataListReady(), out errors);
                    compiler.Upsert(executionId, Result, (string)null, out errors);
                }

                #endregion

                if (dataObject.IsDebugMode())
                {
                    if (hasErrors)
                    {
                        foreach (var debugOutputTo in toUpsert.DebugOutputs)
                        {
                            AddDebugOutputItem(new DebugItemVariableParams(debugOutputTo));
                        }
                    }
                    DispatchDebugState(context, StateType.Before);
                    DispatchDebugState(context, StateType.After);
                }
            }
        }
        /// <summary>
        /// The execute method that is called when the activity is executed at run time and will hold all the logic of the activity
        /// </summary>
        protected override void OnExecute(NativeActivityContext context)
        {
            _debugInputs  = new List <DebugItem>();
            _debugOutputs = new List <DebugItem>();
            IDSFDataObject dataObject = context.GetExtension <IDSFDataObject>();

            IDataListCompiler compiler = DataListFactory.CreateDataListCompiler();

            ErrorResultTO allErrors = new ErrorResultTO();
            ErrorResultTO errors;
            Guid          executionId = DataListExecutionID.Get(context);

            InitializeDebug(dataObject);
            // Process if no errors
            try
            {
                IsSingleValueRule.ApplyIsSingleValueRule(Result, allErrors);
                IDev2DataListUpsertPayloadBuilder <string> toUpsert = Dev2DataListBuilderFactory.CreateStringDataListUpsertBuilder(true);
                toUpsert.IsDebug    = (dataObject.IsDebugMode());
                toUpsert.ResourceID = dataObject.ResourceID;
                IDev2IteratorCollection colItr = Dev2ValueObjectFactory.CreateIteratorCollection();

                IDev2DataListEvaluateIterator dtItr = CreateDataListEvaluateIterator(string.IsNullOrEmpty(DateTime) ? GlobalConstants.CalcExpressionNow : DateTime, executionId, compiler, colItr, allErrors);
                colItr.AddIterator(dtItr);
                IDev2DataListEvaluateIterator ifItr = CreateDataListEvaluateIterator(InputFormat, executionId, compiler, colItr, allErrors);
                colItr.AddIterator(ifItr);
                IDev2DataListEvaluateIterator ofItr = CreateDataListEvaluateIterator(OutputFormat, executionId, compiler, colItr, allErrors);
                colItr.AddIterator(ofItr);
                IDev2DataListEvaluateIterator tmaItr = CreateDataListEvaluateIterator(TimeModifierAmountDisplay, executionId, compiler, colItr, allErrors);
                colItr.AddIterator(tmaItr);

                if (dataObject.IsDebugMode())
                {
                    if (string.IsNullOrEmpty(DateTime))
                    {
                        var defaultDateTimeDebugItem = new DebugItem();
                        AddDebugItem(new DebugItemStaticDataParams("System Date Time", "Input"), defaultDateTimeDebugItem);
                        AddDebugItem(new DebugItemStaticDataParams(System.DateTime.Now.ToString(CultureInfo.CurrentCulture), "="), defaultDateTimeDebugItem);
                        _debugInputs.Add(defaultDateTimeDebugItem);
                    }
                    else
                    {
                        AddDebugInputItem(new DebugItemVariableParams(DateTime, "Input", dtItr.FetchEntry(), executionId));
                    }

                    var dateTimePattern = string.Format("{0} {1}", CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern, CultureInfo.CurrentCulture.DateTimeFormat.LongTimePattern);

                    if (string.IsNullOrEmpty(InputFormat))
                    {
                        var defaultDateTimeDebugItem = new DebugItem();
                        AddDebugItem(new DebugItemStaticDataParams("System Date Time Format", "Input Format"), defaultDateTimeDebugItem);
                        AddDebugItem(new DebugItemStaticDataParams(dateTimePattern, "="), defaultDateTimeDebugItem);
                        _debugInputs.Add(defaultDateTimeDebugItem);
                    }
                    else
                    {
                        AddDebugInputItem(new DebugItemVariableParams(InputFormat, "Input Format", ifItr.FetchEntry(), executionId));
                    }

                    var debugItem = new DebugItem();
                    AddDebugItem(new DebugItemStaticDataParams(TimeModifierType, "Add Time"), debugItem);
                    AddDebugItem(new DebugItemVariableParams(TimeModifierAmountDisplay, "", tmaItr.FetchEntry(), executionId, true), debugItem);
                    _debugInputs.Add(debugItem);

                    if (string.IsNullOrEmpty(OutputFormat))
                    {
                        var defaultDateTimeDebugItem = new DebugItem();
                        AddDebugItem(new DebugItemStaticDataParams("System Date Time Format", "Output Format"), defaultDateTimeDebugItem);
                        AddDebugItem(new DebugItemStaticDataParams(dateTimePattern, "="), defaultDateTimeDebugItem);
                        _debugInputs.Add(defaultDateTimeDebugItem);
                    }
                    else
                    {
                        AddDebugInputItem(new DebugItemVariableParams(OutputFormat, "Output Format", ofItr.FetchEntry(), executionId));
                    }
                }

                if (!allErrors.HasErrors())
                {
                    while (colItr.HasMoreData())
                    {
                        IDateTimeOperationTO transObj = ConvertToDateTimeTo(colItr.FetchNextRow(dtItr).TheValue,
                                                                            colItr.FetchNextRow(ifItr).TheValue,
                                                                            colItr.FetchNextRow(ofItr).TheValue,
                                                                            TimeModifierType,
                                                                            colItr.FetchNextRow(tmaItr).TheValue
                                                                            );

                        //Create a DateTimeFomatter using the DateTimeConverterFactory.DONE
                        IDateTimeFormatter format = DateTimeConverterFactory.CreateFormatter();
                        string             result;
                        string             error;
                        if (format.TryFormat(transObj, out result, out error))
                        {
                            string expression = Result;
                            //2013.06.03: Ashley Lewis for bug 9498 - handle multiple regions in result

                            toUpsert.Add(expression, result);

                            toUpsert.FlushIterationFrame();
                        }
                        else
                        {
                            allErrors.AddError(error);
                        }
                    }
                    compiler.Upsert(executionId, toUpsert, out errors);
                    allErrors.MergeErrors(errors);
                    if (dataObject.IsDebugMode() && !allErrors.HasErrors())
                    {
                        foreach (var debugOutputTo in toUpsert.DebugOutputs)
                        {
                            AddDebugOutputItem(new DebugItemVariableParams(debugOutputTo));
                        }
                    }
                    allErrors.MergeErrors(errors);
                }
            }
            catch (Exception e)
            {
                Dev2Logger.Log.Error("DSFDateTime", e);
                allErrors.AddError(e.Message);
            }
            finally
            {
                // Handle Errors
                var hasErrors = allErrors.HasErrors();
                if (hasErrors)
                {
                    DisplayAndWriteError("DsfDateTimeActivity", 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);
                }
            }
        }