private object FindLookupObject(string searchValue, ValueTransitContext ctx)
        {
            if (LookupPredicate != null)
            {
                var foundObject = Source
                                  .GetCachedData()
                                  .SingleOrDefault(i => LookupPredicate.Evaluate(new ValueTransitContext(i, ctx.TransitValue)));

                return(foundObject);
            }

            switch (Mode)
            {
            case LookupMode.Single:
                return(Source.GetObjectsByKey(searchValue).SingleOrDefault());

            case LookupMode.First:
                return(Source.GetObjectsByKey(searchValue).FirstOrDefault());

            case LookupMode.All:
                var result = Source.GetObjectsByKey(searchValue);
                return(result.Any() ? result : null);

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Exemplo n.º 2
0
        public override void ExecuteInternal(ValueTransitContext ctx)
        {
            ReturnValue = Expression.Evaluate(ctx);
            string valueType = ReturnValue?.GetType().Name.Truncate(30);

            ctx.TraceLine($"<- ({  valueType }){ReturnValue?.ToString().Truncate(30)}");
        }
Exemplo n.º 3
0
 public override void ExecuteInternal(ValueTransitContext ctx)
 {
     if (!Condition.Evaluate(ctx))
     {
         ctx.Flow = TransitionFlow.SkipValue;
     }
 }
        private bool ConditionIsTrue(ValueTransitContext ctx)
        {
            string transitValue = ctx.TransitValue?.ToString();

            switch (Condition)
            {
            case "@any":
                return(true);

            case "@empty":
                return(string.IsNullOrEmpty(transitValue));
            }

            if (Condition.StartsWith("@regexp:"))
            {
                var regex = new Regex(Condition.Replace("@regexp:", ""), RegexOptions.IgnoreCase);
                return(regex.IsMatch(transitValue));
            }

            //if (Condition.StartsWith("{"))
            //{
            //    return (bool)ExpressionEvaluator.Evaluate(Condition, ctx);
            //}

            return(transitValue?.Contains(Condition) ?? false);
        }
        public IEnumerable <IDataObject> GetData()
        {
            Migrator.Current.Tracer.TraceLine($"DataSource ({ this }) - Get data...");

            uint rowCounter = 0;

            foreach (var valuesObject in GetDataInternal())
            {
                rowCounter++;
                var ctx = new ValueTransitContext(valuesObject, valuesObject);

                if (Filter != null && ctx.Execute(Filter) == false)
                {
                    continue;
                }

                ctx.Execute(Key);
                var strKey = ctx.TransitValue?.ToString();

                if (strKey.IsEmpty())
                {
                    continue;
                }

                valuesObject.Key       = UnifyKey(strKey);
                valuesObject.RowNumber = rowCounter;
                if (PrepareData != null)
                {
                    ctx.Execute(PrepareData);
                }
                yield return(valuesObject);
            }
        }
Exemplo n.º 6
0
 public override void ExecuteInternal(ValueTransitContext ctx)
 {
     base.ExecuteInternal(ctx);
     if (ExpressionContext.IsEmpty(ctx.TransitValue))
     {
         ctx.Flow = TransitionFlow.SkipValue;
     }
 }
 public override void ExecuteInternal(ValueTransitContext ctx)
 {
     if (ConditionIsTrue(ctx))
     {
         var value = Replace(ctx);
         ctx.SetCurrentValue(value);
         ctx.Flow = Important ? TransitionFlow.SkipValue : TransitionFlow.Continue;
     }
 }
 public MigrationEventTraceEntry(MigrationEvent eventType, ValueTransitContext ctx, string message)
 {
     EventType   = eventType;
     Message     = message;
     ObjectKey   = ctx.Source.Key;
     DataSetName = ctx.DataPipeline?.Name;
     RowNumber   = ctx.Source.RowNumber;
     Query       = ctx.DataPipeline?.Source.ToString();
 }
        public void TraceEvent(MigrationEvent eventType, ValueTransitContext ctx, string message)
        {
            message = FormatMessage(message);

            ctx.AddTraceEntry(message, ConsoleColor.Yellow);

            _migrationEvents.Add(new MigrationEventTraceEntry(eventType, ctx, message));

            SendTraceMessage(message, ConsoleColor.Yellow);
        }
        public void TraceLine(string message, ValueTransitContext ctx = null, ConsoleColor color = ConsoleColor.White)
        {
            message = FormatMessage(message);

            ctx?.AddTraceEntry(message, color);

            if (ctx == null || ctx.Trace)
            {
                SendTraceMessage(message, color);
            }
        }
Exemplo n.º 11
0
 public override void ExecuteInternal(ValueTransitContext ctx)
 {
     if (ToField.IsNotEmpty())
     {
         ctx.Target.SetValue(ToField, ctx.TransitValue);
     }
     else
     {
         Expression.Evaluate(ctx);
     }
 }
Exemplo n.º 12
0
        public override void ExecuteInternal(ValueTransitContext ctx)
        {
            foreach (var childTransition in Commands)
            {
                TransitChild(childTransition, ctx);

                if (ctx.Flow != TransitionFlow.Continue)
                {
                    ctx.TraceLine($"Breaking {this.GetType().Name}");
                    break;
                }
            }
        }
Exemplo n.º 13
0
        public override void ExecuteInternal(ValueTransitContext ctx)
        {
            var target = Target.GetObjectByKeyOrCreate(ctx.Source.Key);

            //target can be empty when using TransitMode = OnlyExitedObjects
            if (target == null)
            {
                ctx.Flow = TransitionFlow.SkipObject;
                return;
            }

            ctx.Target = target;
            TraceColor = ConsoleColor.Magenta;
            ctx.TraceLine($"PIPELINE '{ctx.DataPipeline.Name}' OBJECT, Row {ctx.Source.RowNumber}, Key [{ctx.Source.Key}], IsNew:  {target.IsNew}");
        }
Exemplo n.º 14
0
        public override void ExecuteInternal(ValueTransitContext ctx)
        {
            if (Flow == TransitionFlow.Debug)
            {
                Debugger.Break();
                Flow = TransitionFlow.Continue;
            }

            if (Flow == TransitionFlow.RiseError)
            {
                throw new Exception(Message.IsEmpty() ? "Error raised by Flow command!" : Message);
            }

            ctx.Flow = Flow;
        }
Exemplo n.º 15
0
        public void Run()
        {
            var stopwatch = new Stopwatch();

            stopwatch.Start();

            Tracer.TraceLine("====== Migration start ======");

            try
            {
                foreach (string name in _mapConfig.Variables.Keys.ToList())
                {
                    if (_mapConfig.Variables[name] is CommandBase command)
                    {
                        var ctx = new ValueTransitContext(null, null);
                        ctx.Execute(command);
                        _mapConfig.Variables[name] = ctx.TransitValue;
                    }
                }

                foreach (var pipeline in _mapConfig.Pipeline.Where(i => i.Enabled))
                {
                    pipeline.Run();
                }
            }
            catch (DataMigrationException e)
            {
                Tracer.TraceMigrationException("Error occured while pipeline processing", e);
                if (ThrowExeptionOnError)
                {
                    throw;
                }
            }
            catch (Exception e)
            {
                Tracer.TraceLine(e.ToString());
                if (ThrowExeptionOnError)
                {
                    throw;
                }
            }

            stopwatch.Stop();
            Tracer.SaveLogs();
            Tracer.TraceLine($"====== END {stopwatch.Elapsed.TotalMinutes} mins ======");
        }
        private string Replace(ValueTransitContext ctx)
        {
            // if (ReplaceValue.Contains("{"))
            // {
            //     return ExpressionEvaluator.EvaluateString(ReplaceValue, ctx);
            // }
            if (Condition.StartsWith("@regexp:"))
            {
                var regex = new Regex(Condition.Replace("@regexp:", ""), RegexOptions.IgnoreCase);
                return(regex.Replace(ctx.TransitValue.ToString(), ReplaceValue));
            }

            if (Condition == "@empty" || Condition == "@any")
            {
                return(ReplaceValue);
            }

            return(ctx.TransitValue?.ToString().Replace(Condition, ReplaceValue));
        }
        public override void ExecuteInternal(ValueTransitContext ctx)
        {
            Init();
            var  value            = ctx.TransitValue;
            char decimalSeparator = DecimalSeparator == 0 ? MapConfig.Current.DefaultDecimalSeparator : DecimalSeparator;

            try
            {
                var typedValue = TypeConverter.GetTypedValue(_typeCode, value, decimalSeparator, Format);
                ctx.SetCurrentValue(typedValue);
            }
            catch (Exception e)
            {
                if (OnError == null)
                {
                    throw;
                }

                ctx.Execute(OnError);
            }
        }
        public override void ExecuteInternal(ValueTransitContext ctx)
        {
            foreach (var childTransition in Commands)
            {
                ctx.Execute(childTransition);

                if (ctx.Flow == TransitionFlow.SkipValue)
                {
                    //if ReplaceUnit returned SkipValue then need to stop ONLY replacing sequence (hack, need to refactor to do
                    //it in more convenient way
                    ctx.Flow = TransitionFlow.Continue;
                    break;
                }

                if (ctx.Flow != TransitionFlow.Continue)
                {
                    ctx.TraceLine($"Breaking {this.GetType().Name}");
                    break;
                }
            }
        }
        public override void ExecuteInternal(ValueTransitContext ctx)
        {
            object lookupObject = null;

            var valueToFind = ctx.TransitValue?.ToString();

            if (valueToFind.IsNotEmpty())
            {
                lookupObject = FindLookupObject(valueToFind, ctx);

                if (lookupObject == null)
                {
                    if (TraceNotFound)
                    {
                        string message = $"Lookup ({Source}) object not found by value '{valueToFind}'\nSource row: { ctx.Source.RowNumber}, Source key: {ctx.Source.Key}";
                        Migrator.Current.Tracer.TraceEvent(MigrationEvent.LookupFailed, ctx, message);
                    }

                    ctx.Execute(OnNotFound);
                }
            }

            ctx.SetCurrentValue(lookupObject);
        }
Exemplo n.º 20
0
 protected virtual void TransitChild(T childCommand, ValueTransitContext ctx)
 {
     ctx.Execute(childCommand);
 }
Exemplo n.º 21
0
        public override void ExecuteInternal(ValueTransitContext ctx)
        {
            var returnValue = Expression.Evaluate(ctx);

            ctx.SetCurrentValue(returnValue);
        }
Exemplo n.º 22
0
 public override void ExecuteInternal(ValueTransitContext ctx)
 {
     ctx.TraceLine(Message.Evaluate(ctx));
 }
 public ExpressionContext(ValueTransitContext ctx)
 {
     _ctx = ctx;
 }
Exemplo n.º 24
0
 /// Method to override in client's code for custom commands. Allow to use custom logic.
 /// inherited from CommandBase class
 /// Don't call this method directly. Use Execute method of ValueTransitContext instead
 public abstract void ExecuteInternal(ValueTransitContext ctx);
        public override void ExecuteInternal(ValueTransitContext ctx)
        {
            string result = Commands.Select(i => ctx.Execute(i)?.ToString()).Join("/");

            ctx.SetCurrentValue(result);
        }
 public override void ExecuteInternal(ValueTransitContext ctx)
 {
     ctx.Trace = this.Trace;
 }