Пример #1
0
        private static string TransformParameters(IComponentContext ctx, string cfg)
        {
            var preProcess = new Transformalize.ConfigurationFacade.Process(
                cfg,
                new Dictionary <string, string>(),
                new List <IDependency> {
                ctx.Resolve <IReader>(),
                new DateMathModifier(),
                new ParameterModifier(new NullPlaceHolderReplacer()),
                ctx.ResolveNamed <IDependency>(TransformModule.ParametersName)
            }.ToArray());

            if (!preProcess.Parameters.Any(pr => pr.Transforms.Any()))
            {
                return(null);
            }

            var fields = preProcess.Parameters.Select(pr => new Field {
                Name       = pr.Name,
                Alias      = pr.Name,
                Default    = pr.Value,
                Type       = pr.Type,
                Transforms = pr.Transforms.Select(o => o.ToOperation()).ToList()
            }).ToList();
            var len    = fields.Count;
            var entity = new Entity {
                Name = "Parameters", Alias = "Parameters", Fields = fields
            };
            var mini = new Process {
                Name     = "ParameterTransform",
                ReadOnly = true,
                Entities = new List <Entity> {
                    entity
                },
                Maps    = preProcess.Maps.Select(m => m.ToMap()).ToList(),      // for map transforms
                Scripts = preProcess.Scripts.Select(s => s.ToScript()).ToList() // for transforms that use scripts (e.g. js)
            };

            mini.Check(); // very important to check after creating, as it runs validation and even modifies!

            if (!mini.Errors().Any())
            {
                // modification in Check() do not make it out to local variables so overwrite them
                fields = mini.Entities.First().Fields;
                entity = mini.Entities.First();

                var c          = new PipelineContext(ctx.Resolve <IPipelineLogger>(), mini, entity);
                var transforms = new List <ITransform> {
                    new Transformalize.Transforms.System.DefaultTransform(c, fields)
                };
                transforms.AddRange(TransformFactory.GetTransforms(ctx, c, fields));

                // make an input out of the parameters
                var input = new List <IRow>();
                var row   = new MasterRow(len);
                for (var i = 0; i < len; i++)
                {
                    row[fields[i]] = preProcess.Parameters[i].Value;
                }

                input.Add(row);

                var output = transforms.Aggregate(input.AsEnumerable(), (rows, t) => t.Operate(rows)).ToList().First();

                for (var i = 0; i < len; i++)
                {
                    var parameter = preProcess.Parameters[i];
                    parameter.Value = output[fields[i]].ToString();
                    parameter.T     = string.Empty;
                    parameter.Transforms.Clear();
                }

                return(preProcess.Serialize());
            }

            var context = new PipelineContext(ctx.Resolve <IPipelineLogger>(), mini, entity);

            foreach (var error in mini.Errors())
            {
                context.Error(error);
            }

            return(null);
        }
Пример #2
0
        private string ModifyInternal(string cfg, int id, IDictionary <string, string> parameters)
        {
            // using facade (which is all string properties) so things can be
            // transformed before types are checked or place-holders are replaced

            var builder = new ContainerBuilder();

            builder.RegisterModule(new ShorthandModule(_logger));

            Transformalize.ConfigurationFacade.Process facade;

            using (var scope = builder.Build().BeginLifetimeScope()) {
                facade = new Transformalize.ConfigurationFacade.Process(
                    cfg,
                    parameters: parameters,
                    dependencies: new List <IDependency> {
                    new TransferParameterModifier(), // consumes parameters
                    scope.ResolveNamed <IDependency>(TransformModule.ParametersName),
                    scope.ResolveNamed <IDependency>(ValidateModule.ParametersName)
                }.ToArray()
                    );
                facade.Id = id.ToString();
            }

            if (!facade.Parameters.Any())
            {
                return(cfg);
            }

            _settings.ApplyCommonSettings(facade);

            var fields = new List <Field>();

            foreach (var pr in facade.Parameters)
            {
                var field = new Field {
                    Name  = pr.Name,
                    Alias = pr.Name,
                    // Default = pr.Value,  (something has changed, this value didn't cause problems before but it is now in DefaultRowReader)
                    Label      = pr.Label,
                    PostBack   = pr.PostBack,
                    Type       = pr.Type,
                    Help       = pr.Help,
                    InputType  = pr.InputType, // used in ParameterRowReader to identify files
                    Transforms = pr.Transforms.Select(o => o.ToOperation()).ToList(),
                    Validators = pr.Validators.Select(o => o.ToOperation()).ToList()
                };
                if (!string.IsNullOrEmpty(pr.Length))
                {
                    field.Length = pr.Length;
                }
                if (!string.IsNullOrEmpty(pr.Precision) && int.TryParse(pr.Precision, out int precision))
                {
                    field.Precision = precision;
                }
                if (!string.IsNullOrEmpty(pr.Scale) && int.TryParse(pr.Scale, out int scale))
                {
                    field.Scale = scale;
                }
                fields.Add(field);
            }

            var validatorFields = new List <Field>();

            foreach (var field in fields.Where(f => f.Validators.Any()))
            {
                field.ValidField   = field.Name + "Valid";
                field.MessageField = field.Name + "Message";

                validatorFields.Add(new Field {
                    Name    = field.ValidField,
                    Input   = false,
                    Default = "true",
                    Type    = "bool"
                });
                validatorFields.Add(new Field {
                    Name    = field.MessageField,
                    Input   = false,
                    Default = string.Empty,
                    Type    = "string",
                    Length  = "255"
                });
            }

            // create an internal connection for input
            var connections = new List <Transformalize.ConfigurationFacade.Connection> {
                new Transformalize.ConfigurationFacade.Connection()
                {
                    Name = _tpInput, Provider = "internal"
                }
            };

            // add existing connections in case maps need to be loaded
            connections.AddRange(facade.Connections);

            //create an internal connection for output
            connections.Add(new Transformalize.ConfigurationFacade.Connection()
            {
                Name = _tpOutput, Provider = "internal"
            });

            // create entity
            var entity = new Entity {
                Name             = "Parameters",
                Fields           = fields,
                CalculatedFields = validatorFields,
                Input            = _tpInput
            };

            // disable checking for invalid characters unless set
            var arrangementParameters = new List <Parameter>();

            foreach (var parameter in facade.Parameters)
            {
                var add = parameter.ToParameter();
                if (parameter.InvalidCharacters == null)
                {
                    add.InvalidCharacters = string.Empty;
                }
                arrangementParameters.Add(add);
            }

            // create process to transform and validate the parameter values
            var process = new Process {
                Id         = id,
                Name       = "Transformalize Parameters",
                ReadOnly   = true,
                Mode       = "form", // causes auto post-back's to resolve to either true or false
                Output     = _tpOutput,
                Parameters = arrangementParameters,
                Maps       = facade.Maps.Select(m => m.ToMap()).ToList(),
                Scripts    = facade.Scripts.Select(m => m.ToScript()).ToList(),
                Entities   = new List <Entity> {
                    entity
                },
                Connections = connections.Select(c => c.ToConnection()).ToList()
            };

            process.Load(); // very important to check after creating, as it runs validation and even modifies!

            if (!process.Errors().Any())
            {
                // modification in Load() do not make it out to local variables so overwrite them
                entity          = process.Entities.First();
                fields          = entity.Fields;
                validatorFields = entity.CalculatedFields;

                CfgRow output;
                _container.GetReaderAlternate = (input, rowFactory) => new ParameterRowReader(input, new DefaultRowReader(input, rowFactory));
                using (var scope = _container.CreateScope(process, _logger)) {
                    scope.Resolve <IProcessController>().Execute();
                    output = process.Entities[0].Rows.FirstOrDefault();
                }

                for (int i = 0; i < process.Maps.Count; i++)
                {
                    var source = process.Maps[i];
                    var target = facade.Maps[i];
                    if (source.Items.Any() && !target.Items.Any())
                    {
                        foreach (var item in source.Items)
                        {
                            target.Items.Add(new Transformalize.ConfigurationFacade.MapItem()
                            {
                                From      = item.From.ToString(),
                                To        = item.To.ToString(),
                                Parameter = item.Parameter,
                                Value     = item.Value
                            });
                        }
                        target.Query = string.Empty; // remove the query so they are not queried again
                    }
                }

                if (output != null)
                {
                    JintVisibility jintVisibility = null;

                    foreach (var parameter in facade.Parameters)
                    {
                        var field = fields.First(f => f.Name == parameter.Name);

                        // set the transformed value
                        parameter.Value    = output[field.Name].ToString();
                        parameter.PostBack = field.PostBack; // auto is changed to true|false in transformalize

                        // set the validation results
                        if (parameter.Validators.Any())
                        {
                            if ((bool)output[field.ValidField])
                            {
                                parameter.Valid = "true";
                            }
                            else
                            {
                                parameter.Valid   = "false";
                                parameter.Message = ((string)output[field.MessageField]).TrimEnd('|');
                            }
                        }

                        // set the visibility
                        if (string.IsNullOrEmpty(parameter.Visible))
                        {
                            parameter.Visible = "true";
                        }
                        else
                        {
                            if (jintVisibility == null)
                            {
                                jintVisibility = new JintVisibility();
                            }
                            var response = jintVisibility.Visible(new JvRequest(output, parameter.Visible));
                            if (response.Faulted)
                            {
                                _logger.Error(() => $"Parameter {parameter.Name} has a visible script error: {response.Message}");
                                _notifier.Error(H[$"Parameter {parameter.Name} has a visible script error: {response.Message}"]);
                            }
                            parameter.Visible = response.Visible.ToString().ToLower();
                            if (parameter.Visible == "false")
                            {
                                parameter.Valid = "true"; // because they won't be able to fix it
                            }
                        }

                        // remove this stuff because all the transforming and validating is done at this point
                        parameter.T = null;
                        parameter.Transforms.Clear();
                        parameter.V = null;
                        parameter.Validators.Clear();
                    }
                }

                return(facade.Serialize());
            }

            foreach (var error in process.Errors())
            {
                _logger.Error(() => error);
            }

            return(cfg);
        }