Пример #1
0
        public override IDisplayResult Edit(TransformalizeFormPart part)
        {
            var commands = new AdoFormCommands();
            var process  = _configurationContainer.CreateScope(part.Arrangement.Text, part.ContentItem, new Dictionary <string, string>(), false).Resolve <Process>();

            _settings.ApplyCommonSettings(process);

            using (var scope = _container.CreateScope(process, _logger)) {
                if (scope.IsRegistered <AdoFormCommandWriter>())
                {
                    commands = scope.Resolve <AdoFormCommandWriter>().Write();
                }
            }

            return(Initialize <EditTransformalizeFormPartViewModel>("TransformalizeFormPart_Edit", model => {
                model.TransformalizeFormPart = part;
                model.Arrangement = part.Arrangement;
                model.LocationEnableHighAccuracy = part.LocationEnableHighAccuracy;
                model.LocationMaximumAge = part.LocationMaximumAge;
                model.LocationTimeout = part.LocationTimeout;
                model.CreateCommand = commands.Create;
                model.InsertCommand = commands.Insert;
                model.UpdateCommand = commands.Update;
                model.SelectCommand = commands.Select;
            }).Location("Content:1"));
        }
Пример #2
0
        private Process LoadInternal(string arrangement, ContentItem item, IDictionary <string, string> parameters = null, ISerializer serializer = null)
        {
            Process process;

            if (parameters != null)
            {
                foreach (var kv in parameters)
                {
                    _parameters[kv.Key] = kv.Value;
                }
            }

            using (MiniProfiler.Current.Step("Load")) {
                _configurationContainer.Serializer = serializer;
                process = _configurationContainer.CreateScope(arrangement, item, _parameters).Resolve <Process>();
            }

            // in case common settings were not applied when transforming / validating parameters
            _settings.ApplyCommonSettings(process);

            // translates file content item id to full path
            if (process.Connections.Any())
            {
                var connection = process.Connections.FirstOrDefault(c => _fileBasedProviders.Contains(c.Provider) && c.File.Length == Common.IdLength && !c.File.Contains('.'));
                if (connection != null)
                {
                    var filePart = _fileService.GetFilePart(connection.File).Result;
                    if (filePart != null)
                    {
                        connection.File = System.IO.Path.Combine(_formFileStore.Path, filePart.FullPath.Text);
                    }
                }
            }

            if (process.Errors().Any() || process.Log.Any(l => l.LogLevel == LogLevel.Error))
            {
                process.Status  = 500;
                process.Message = "Process has errors.";
            }
            else
            {
                process.Status  = 200;
                process.Message = "Ok";
            }

            return(process);
        }
Пример #3
0
        private string ModifyInternal(string cfg, int id, IDictionary <string, string> parameters)
        {
            var process = new Process(cfg)
            {
                Id = id
            };

            // if there aren't any parameters, just leave
            if (!process.Parameters.Any())
            {
                return(cfg);
            }

            // if there isn't a primary key, just leave
            var key = process.Parameters.FirstOrDefault(p => p.PrimaryKey);

            if (key == null || parameters == null || !parameters.ContainsKey(key.Name))
            {
                return(cfg);
            }

            // if the value being passed in for the primary key is the type's default value, just leave because it's an insert
            if (parameters[key.Name] == Transformalize.Constants.TypeDefaults()[key.Type].ToString())
            {
                return(cfg);
            }

            _settings.ApplyCommonSettings(process);

            var fields = new List <Field>();

            // get all the fields that should be in the form's table and are not specific to insert or update scope
            foreach (var pr in process.Parameters.Where(p => p.Output && p.Scope == "[default]"))
            {
                var field = new Field {
                    Name      = pr.Name,
                    Alias     = pr.Name,
                    Type      = pr.Type,
                    Default   = pr.Value,
                    Length    = pr.Length,
                    Format    = pr.Format,
                    InputType = pr.InputType,
                    Precision = pr.Precision,
                    Scale     = pr.Scale
                };

                fields.Add(field);
            }

            var connection = process.Connections.First(c => c.Table != "[default]");

            // create entity
            var entity = new Entity {
                Name   = connection.Table,
                Fields = fields,
                Input  = connection.Name,
                Filter = new List <Filter>()
                {
                    new Filter {
                        Field = key.Name, Value = parameters[key.Name]
                    }
                }
            };

            // create process to load the form submission
            var modified = new Process {
                Id       = id,
                Name     = "Load Form",
                ReadOnly = true,
                Entities = new List <Entity> {
                    entity
                },
                Connections = new List <Connection> {
                    connection
                }
            };

            modified.Load();

            if (!modified.Errors().Any())
            {
                // run the process which should get a single row (the form submission) into output
                CfgRow output;
                using (var scope = _container.CreateScope(modified, _logger)) {
                    scope.Resolve <IProcessController>().Execute();
                    output = modified.Entities[0].Rows.FirstOrDefault();
                }

                if (output != null)
                {
                    foreach (var parameter in process.Parameters)
                    {
                        var field = modified.Entities[0].Fields.FirstOrDefault(f => f.Name == parameter.Name);
                        // put the form submission value in the parameters
                        if (field != null)
                        {
                            parameters[parameter.Name] = output[field.Name].ToString();
                        }
                    }
                }
            }

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

            return(cfg);
        }
Пример #4
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);
        }