Ejemplo n.º 1
0
        private bool CheckCondition(CleanupRule rule, SourceContext sourceContext, object capturedValue, string stringValue,
                                    EvaluatedValue evaluation)
        {
            switch (rule.Condition)
            {
            case CleanupCondition.Empty:
            {
                _matched[rule.ColumnName] = evaluation.Value;
                return(capturedValue == null || capturedValue as string == "");
            }

            case CleanupCondition.StartsWith:
            {
                _matched[rule.ColumnName] = rule.ConditionArgument;
                return(stringValue.StartsWith(rule.ConditionArgument));
            }

            case CleanupCondition.EndsWith:
            {
                _matched[rule.ColumnName] = rule.ConditionArgument;
                return(stringValue.EndsWith(rule.ConditionArgument));
            }

            case CleanupCondition.Equal:
            {
                _matched[rule.ColumnName] = rule.ConditionArgument;
                return(stringValue == rule.ConditionArgument);
            }

            case CleanupCondition.Regex:
            {
                var matched = Regex.Match(stringValue, rule.ConditionArgument);
                if (matched.Length > 0)
                {
                    _matched[rule.ColumnName] = matched.Value;
                    return(true);
                }

                return(false);
            }

            case CleanupCondition.Contains:
            {
                _matched[rule.ColumnName] = rule.ConditionArgument;
                return(stringValue.Contains(rule.ConditionArgument));
            }

            case CleanupCondition.Expression:
            {
                _matched[rule.ColumnName] = stringValue;
                return((bool)_expressionEvaluator.Evaluate(rule.ConditionArgument));
            }

            default:
                return(false);
            }
        }
Ejemplo n.º 2
0
 private void SafeRun(Action action, SourceContext sourceContext, CleanupRule rule)
 {
     try
     {
         action();
     }
     catch (Exception e)
     {
         sourceContext.AddErrorForSourceColumn(rule.ColumnName, e, SourceContext.ErrorTypeCleanup);
     }
 }
Ejemplo n.º 3
0
        private void ApplyConditionalCleanup(CleanupRule rule, SourceContext sourceContext, string stringValue,
                                             SingleCleanupRuleResult result, EvaluatedValue evaluation)
        {
            switch (rule.Action)
            {
            case CleanupAction.Replace:
            {
                sourceContext.Source[rule.ColumnName] = evaluation.Value;
            }
            break;

            case CleanupAction.ReplaceMatched:
            {
                if (_matched.ContainsKey(rule.ColumnName) && !string.IsNullOrEmpty(_matched[rule.ColumnName]))
                {
                    if (string.IsNullOrEmpty(stringValue))
                    {
                        sourceContext.Source[rule.ColumnName] = evaluation.Value;
                    }
                    else
                    {
                        sourceContext.Source[rule.ColumnName] = stringValue.Replace(_matched[rule.ColumnName], evaluation.Value);
                    }
                }
            }
            break;

            case CleanupAction.Remove:
            {
                result.IsDeleted = true;
            }
            break;

            case CleanupAction.StartLoad:
            {
                _frameState.StartLoad();
            }
            break;

            case CleanupAction.StartLoadExclude:
            {
                _frameState.StartLoadExclude();
            }
            break;

            case CleanupAction.StopLoad:
            {
                _frameState.StopLoad();
            }
            break;
            }
        }
Ejemplo n.º 4
0
        private void Apply(CleanupRule rule, SourceContext sourceContext, SingleCleanupRuleResult result)
        {
            var capturedValue = sourceContext.Source[rule.ColumnName];
            var stringValue   = capturedValue != null?capturedValue.ToString() : "";

            EnrichContextByRow(sourceContext);
            Lazy <string> evaluation = new Lazy <string>(() =>
            {
                var evaluatedResult = _expressionEvaluator.Evaluate(rule.Expression).ToString();


                return(evaluatedResult);
            });
            EvaluatedValue evaluatedValue = new EvaluatedValue(evaluation);

            if (CheckCondition(rule, sourceContext, capturedValue, stringValue, evaluatedValue))
            {
                ApplyConditionalCleanup(rule, sourceContext, stringValue, result, evaluatedValue);
            }
            ApplyNonConditionalCleanup(rule, sourceContext, stringValue, result, evaluatedValue);
        }
Ejemplo n.º 5
0
        private void ApplyNonConditionalCleanup(CleanupRule rule, SourceContext sourceContext, string stringValue,
                                                SingleCleanupRuleResult result, EvaluatedValue evaluation)
        {
            switch (rule.Action)
            {
            case CleanupAction.GetPrevious:
            {
                if (string.IsNullOrEmpty(stringValue) && _previousNonEmptyValue.ContainsKey(rule.ColumnName))
                {
                    sourceContext.Source[rule.ColumnName] = _previousNonEmptyValue[rule.ColumnName];
                }
                else
                {
                    _previousNonEmptyValue[rule.ColumnName] = stringValue;
                }
            }
            break;
            }

            if (_frameState.IsLocked())
            {
                result.IsDeleted = true;
            }
        }