Exemple #1
0
        private void ConvertToExport(string user, Process process, PipelineConfigurationPart part, string exportType, IDictionary <string, string> parameters)
        {
            var o = process.Output();

            switch (exportType)
            {
            case "xlsx":
                if (!_appDataFolder.DirectoryExists(Common.FileFolder))
                {
                    _appDataFolder.CreateDirectory(Common.FileFolder);
                }

                var fileName = $"{user}-{_clock.UtcNow.ToString(FileTimestamp)}-{_slugService.Slugify(part.Title())}.xlsx";

                o.Provider = "excel";
                o.File     = _appDataFolder.MapPath(_appDataFolder.Combine(Common.FileFolder, fileName));
                break;

            case "geojson":
                o.Stream   = true;
                o.Provider = "geojson";
                o.File     = _slugService.Slugify(part.Title()) + ".geojson";
                break;

            case "kml":
                o.Stream   = true;
                o.Provider = "kml";
                o.File     = _slugService.Slugify(part.Title()) + ".kml";
                break;

            default:     //csv
                o.Stream    = true;
                o.Provider  = "file";
                o.Delimiter = ",";
                o.File      = _slugService.Slugify(part.Title()) + ".csv";
                break;
            }

            parameters["page"] = "0";

            foreach (var entity in process.Entities)
            {
                entity.Page = 0;
                entity.Fields.RemoveAll(f => f.System);

                foreach (var field in entity.GetAllFields())
                {
                    field.Output = field.Output && field.Export == "defer" || field.Export == "true";
                }
            }
        }
        private void ConvertToExport(string user, Process process, PipelineConfigurationPart part, string exportType)
        {
            var o = process.Output();

            switch (exportType)
            {
            case "xlsx":
                var folder = Common.GetAppFolder();
                if (!_appDataFolder.DirectoryExists(folder))
                {
                    _appDataFolder.CreateDirectory(folder);
                }

                var fileName = Common.GetSafeFileName(user, _slugService.Slugify(part.Title()), "xlsx");

                o.Provider = "excel";
                o.File     = _appDataFolder.MapPath(_appDataFolder.Combine(folder, fileName));
                break;

            case "geojson":
                o.Stream   = true;
                o.Provider = "geojson";
                o.File     = _slugService.Slugify(part.Title()) + ".geo.json";

                var suppress = new HashSet <string>()
                {
                    Common.BatchValueFieldName, part.MapColorField, part.MapPopUpField
                };
                var coordinates = new HashSet <string>()
                {
                    part.MapLatitudeField, part.MapLongitudeField
                };
                foreach (var entity in process.Entities)
                {
                    foreach (var field in entity.GetAllFields())
                    {
                        if (suppress.Contains(field.Alias))
                        {
                            field.Output   = false;
                            field.Property = false;
                            field.Alias   += "Suppressed";
                        }
                        else if (coordinates.Contains(field.Alias))
                        {
                            field.Property = field.Export == "true";
                        }
                        else
                        {
                            field.Property = field.Property || field.Output && field.Export == "defer" || field.Export == "true";
                        }
                    }
                }

                break;

            case "map":
                o.Stream   = true;
                o.Provider = "geojson";
                o.File     = _slugService.Slugify(part.Title()) + ".geo.json";

                var mapFields = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase)
                {
                    { part.MapColorField, "geojson-color" },
                    { part.MapPopUpField, "geojson-description" },
                    { part.MapLatitudeField, "latitude" },
                    { part.MapLongitudeField, "longitude" },
                    { Common.BatchValueFieldName, Common.BatchValueFieldName }
                };

                ConfineData(process, mapFields);
                break;

            case "json":
                o.Stream   = true;
                o.Provider = "json";
                o.File     = _slugService.Slugify(part.Title()) + ".json";
                break;

            case "calendar":
                o.Stream   = true;
                o.Provider = "json";
                o.File     = _slugService.Slugify(part.Title()) + ".json";
                var calendarFields = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase)
                {
                    { part.CalendarIdField, "id" },
                    { part.CalendarTitleField, "title" },
                    { part.CalendarUrlField, "url" },
                    { part.CalendarClassField, "class" },
                    { part.CalendarStartField, "start" },
                    { part.CalendarEndField, "end" },
                    { Common.BatchValueFieldName, Common.BatchValueFieldName }
                };
                ConfineData(process, calendarFields);
                break;

            case "kml":
                o.Stream   = true;
                o.Provider = "kml";
                o.File     = _slugService.Slugify(part.Title()) + ".kml";
                break;

            default: // csv
                o.Stream        = true;
                o.Provider      = "file";
                o.Delimiter     = ",";
                o.TextQualifier = "\"";
                o.File          = _slugService.Slugify(part.Title()) + ".csv";
                break;
            }

            // common
            foreach (var entity in process.Entities)
            {
                entity.Fields.RemoveAll(f => f.System);

                foreach (var field in entity.GetAllFields())
                {
                    field.T = string.Empty; // because short-hand has already been expanded
                    switch (exportType)
                    {
                    case "map":
                    case "calendar":
                    case "geojson":
                        // already been modified
                        break;

                    default:
                        if (field.Alias == Common.BatchValueFieldName)
                        {
                            field.Output = false;
                        }
                        field.Output = field.Output && field.Export == "defer" || field.Export == "true";
                        break;
                    }
                }
            }
        }
Exemple #3
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)));
        }