Пример #1
0
        public void TestMethod1()
        {
            var process = new Process {
                Name        = "Test",
                ReadOnly    = true,
                Connections = new List <Connection> {
                    new Connection {
                        Name = "input", Provider = "activedirectory"
                    },
                    new Connection {
                        Name = "output", Provider = "internal"
                    }
                },
                Entities = new List <Entity> {
                    new Entity {
                        Name   = "Users",
                        Page   = 1,
                        Size   = 20,
                        Filter = new List <Filter> {
                            new Filter {
                                Expression = "(&(mail=*)(objectClass=user)(objectCategory=person))"
                            }
                        },
                        Fields = new List <Field> {
                            new Field {
                                Name = "mail"
                            },
                            new Field {
                                Name = "title"
                            },
                            new Field {
                                Name = "whenChanged", Type = "datetime"
                            }
                        }
                    }
                }
            };

            process.Load();

            Assert.AreEqual(0, process.Errors().Length);

            var logger  = new ConsoleLogger(LogLevel.Debug);
            var context = new PipelineContext(logger, process, process.Entities[0]);
            var input   = new InputContext(context);
            var reader  = new ActiveDirectoryReader(input, new RowFactory(context.Entity.Fields.Count, true, false));
            var rows    = reader.Read();

            var count = 0;

            foreach (var row in rows)
            {
                ++count;
                Trace.WriteLine(count + ". " + row.ToString());
            }
        }
        private static string TransformParameters(IComponentContext ctx, string cfg)
        {
            var preProcess = new 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.Load(); // very important to check after creating, as it runs validation and even modifies!

            if (!mini.Errors().Any())
            {
                // modification in Load() 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 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);
        }
Пример #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
        public ActionResult Report(int id)
        {
            var timer = new Stopwatch();

            timer.Start();

            var process = new Process {
                Name = "Report"
            };

            var part = _orchardServices.ContentManager.Get(id).As <PipelineConfigurationPart>();

            if (part == null)
            {
                process.Name = "Not Found";
            }
            else
            {
                var user = _orchardServices.WorkContext.CurrentUser == null ? "Anonymous" : _orchardServices.WorkContext.CurrentUser.UserName ?? "Anonymous";

                if (_orchardServices.Authorizer.Authorize(Permissions.ViewContent, part))
                {
                    process = _processService.Resolve(part.EditorMode, part.EditorMode);

                    var parameters = Common.GetParameters(Request, _secureFileService, _orchardServices);
                    if (part.NeedsInputFile && Convert.ToInt32(parameters[Common.InputFileIdName]) == 0)
                    {
                        _orchardServices.Notifier.Add(NotifyType.Error, T("This transformalize expects a file."));
                        process.Name = "File Not Found";
                    }

                    process.Load(part.Configuration, parameters);
                    process.Buffer   = false; // no buffering for reports
                    process.ReadOnly = true;  // force reporting to omit system fields

                    // secure actions
                    var actions = process.Actions.Where(a => !a.Before && !a.After && !a.Description.StartsWith("Batch", StringComparison.OrdinalIgnoreCase));
                    foreach (var action in actions)
                    {
                        var p = _orchardServices.ContentManager.Get(action.Id);
                        if (!_orchardServices.Authorizer.Authorize(Permissions.ViewContent, p))
                        {
                            action.Description = "BatchUnauthorized";
                        }
                    }

                    var output = process.Output();

                    if (output.Provider.In("internal", "file"))
                    {
                        Common.TranslatePageParametersToEntities(process, parameters, "page");

                        // change process for export and batch purposes
                        var reportType = Request["output"] ?? "page";
                        if (!_renderedOutputs.Contains(reportType))
                        {
                            if (reportType == "batch" && Request.HttpMethod.Equals("POST") && parameters.ContainsKey("action"))
                            {
                                var action = process.Actions.FirstOrDefault(a => a.Description == parameters["action"]);

                                if (action != null)
                                {
                                    // check security
                                    var actionPart = _orchardServices.ContentManager.Get(action.Id);
                                    if (actionPart != null && _orchardServices.Authorizer.Authorize(Permissions.ViewContent, actionPart))
                                    {
                                        // security okay
                                        parameters["entity"] = process.Entities.First().Alias;
                                        var batchParameters = _batchCreateService.Create(process, parameters);

                                        batchParameters["count"] = parameters.ContainsKey("count") ? parameters["count"] : "0";
                                        var count = _batchWriteService.Write(Request, process, batchParameters);

                                        if (count > 0)
                                        {
                                            if (_batchRunService.Run(action, batchParameters))
                                            {
                                                if (action.Url == string.Empty)
                                                {
                                                    if (batchParameters.ContainsKey("BatchId"))
                                                    {
                                                        _orchardServices.Notifier.Information(T($"Processed {count} records in batch {batchParameters["BatchId"]}."));
                                                    }
                                                    else
                                                    {
                                                        _orchardServices.Notifier.Information(T($"Processed {count} records."));
                                                    }
                                                }
                                                else
                                                {
                                                    return(_batchRedirectService.Redirect(action.Url, batchParameters));
                                                }
                                            }
                                        }
                                    }
                                    else
                                    {
                                        return(new HttpUnauthorizedResult("You do not have access to this bulk action."));
                                    }
                                }
                            }
                            else     // export
                            {
                                ConvertToExport(user, process, part, reportType, parameters);
                                process.Load(process.Serialize(), parameters);
                            }
                        }

                        if (Request["sort"] != null)
                        {
                            _sortService.AddSortToEntity(process.Entities.First(), Request["sort"]);
                        }

                        if (process.Errors().Any())
                        {
                            foreach (var error in process.Errors())
                            {
                                _orchardServices.Notifier.Add(NotifyType.Error, T(error));
                            }
                        }
                        else
                        {
                            if (process.Entities.Any(e => !e.Fields.Any(f => f.Input)))
                            {
                                _orchardServices.WorkContext.Resolve <ISchemaHelper>().Help(process);
                            }

                            if (!process.Errors().Any())
                            {
                                var runner = _orchardServices.WorkContext.Resolve <IRunTimeExecute>();
                                try {
                                    runner.Execute(process);
                                    process.Request = "Run";
                                    process.Time    = timer.ElapsedMilliseconds;

                                    if (process.Errors().Any())
                                    {
                                        foreach (var error in process.Errors())
                                        {
                                            _orchardServices.Notifier.Add(NotifyType.Error, T(error));
                                        }
                                        process.Status  = 500;
                                        process.Message = "There are errors in the pipeline.  See log.";
                                    }
                                    else
                                    {
                                        process.Status  = 200;
                                        process.Message = "Ok";
                                    }

                                    var o = process.Output();
                                    switch (o.Provider)
                                    {
                                    case "kml":
                                    case "geojson":
                                    case "file":
                                        Response.AddHeader("content-disposition", "attachment; filename=" + o.File);
                                        switch (o.Provider)
                                        {
                                        case "kml":
                                            Response.ContentType = "application/vnd.google-earth.kml+xml";
                                            break;

                                        case "geojson":
                                            Response.ContentType = "application/vnd.geo+json";
                                            break;

                                        default:
                                            Response.ContentType = "application/csv";
                                            break;
                                        }
                                        Response.Flush();
                                        Response.End();
                                        return(new EmptyResult());

                                    case "excel":
                                        return(new FilePathResult(o.File, ExcelContentType)
                                        {
                                            FileDownloadName = _slugService.Slugify(part.Title()) + ".xlsx"
                                        });

                                    default:      // page and map are rendered to page
                                        break;
                                    }
                                } catch (Exception ex) {
                                    Logger.Error(ex, ex.Message);
                                    _orchardServices.Notifier.Error(T(ex.Message));
                                }
                            }
                        }
                    }
                }
                else
                {
                    _orchardServices.Notifier.Warning(user == "Anonymous" ? T("Sorry. Anonymous users do not have permission to view this report. You may need to login.") : T("Sorry {0}. You do not have permission to view this report.", user));
                }
            }

            return(View(new ReportViewModel(process, part)));
        }
Пример #5
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);
        }