Exemplo n.º 1
0
        public ReportRetrieveResponse Retrieve(ReportRetrieveRequest request)
        {
            request.CheckNotNull();

            if (request.ReportKey.IsEmptyOrNull())
            {
                throw new ArgumentNullException("reportKey");
            }

            var reportInfo = ReportRegistry.GetReport(request.ReportKey);

            if (reportInfo == null)
            {
                throw new ArgumentOutOfRangeException("reportKey");
            }

            if (reportInfo.Permission != null)
            {
                Authorization.ValidatePermission(reportInfo.Permission);
            }

            var response = new ReportRetrieveResponse();

            response.Properties = PropertyItemHelper.GetPropertyItemsFor(reportInfo.Type);
            response.ReportKey  = reportInfo.Key;
            response.Title      = reportInfo.Title;
            var reportInstance = Activator.CreateInstance(reportInfo.Type);

            response.InitialSettings  = reportInstance;
            response.IsDataOnlyReport = reportInstance is IDataOnlyReport;

            return(response);
        }
Exemplo n.º 2
0
        private ActionResult Execute(string key, string opt, string ext, bool download, bool printing)
        {
            if (key.IsEmptyOrNull())
            {
                throw new ArgumentNullException("reportKey");
            }

            var reportInfo = ReportRegistry.GetReport(key);

            if (reportInfo == null)
            {
                throw new ArgumentOutOfRangeException("reportKey");
            }

            if (reportInfo.Permission != null)
            {
                if (reportInfo.Permission == "")
                {
                    Authorization.ValidateLoggedIn();
                }
                else
                {
                    Authorization.ValidatePermission(reportInfo.Permission);
                }
            }

            var report = (IReport)JsonConvert.DeserializeObject(opt.TrimToNull() ?? "{}",
                                                                reportInfo.Type, JsonSettings.Tolerant);

            byte[] renderedBytes = null;

            if (report is IDataOnlyReport)
            {
                ext           = "xlsx";
                renderedBytes = new ReportRepository().Render((IDataOnlyReport)report);
            }
            else
            {
                ext = (ext ?? "html").ToLowerInvariant();

                if (ext == "htm" || ext == "html")
                {
                    var result = RenderAsHtml(report, download, printing, ref renderedBytes);
                    if (!download)
                    {
                        return(result);
                    }
                }
                else if (ext == "pdf")
                {
                    renderedBytes = RenderAsPdf(report, key, opt);
                }
                else
                {
                    throw new ArgumentOutOfRangeException("ext");
                }
            }

            return(PrepareFileResult(report, ext, download, renderedBytes, reportInfo));
        }
Exemplo n.º 3
0
        public ReportRetrieveResponse Retrieve(IDbConnection connection, ReportRetrieveRequest request)
        {
            request.CheckNotNull();

            if (request.ReportKey.IsTrimmedEmpty())
            {
                throw new ArgumentNullException("reportKey");
            }

            var report = ReportRegistry.GetReport(request.ReportKey);

            if (report == null)
            {
                throw new ArgumentOutOfRangeException("reportKey");
            }

            var response = new ReportRetrieveResponse();

            response.Properties = Serenity.PropertyGrid.PropertyItemHelper.GetPropertyItemsFor(report.Type);
            response.Designs    = GetReportDesigns(connection, request.ReportKey);
            response.ReportKey  = report.Key;
            response.Title      = report.Title;
            var reportInstance = Activator.CreateInstance(report.Type);

            response.InitialSettings  = reportInstance;
            response.IsDataOnlyReport = reportInstance is IDataOnlyReport;

            return(response);
        }
Exemplo n.º 4
0
        private ActionResult Execute(string key, string opt, string ext, bool download, bool printing)
        {
            if (key.IsEmptyOrNull())
            {
                throw new ArgumentNullException("reportKey");
            }

            var reportInfo = ReportRegistry.GetReport(key);

            if (reportInfo == null)
            {
                throw new ArgumentOutOfRangeException("reportKey");
            }

            if (reportInfo.Permission != null)
            {
                Context.Permissions.ValidatePermission(reportInfo.Permission, Context.Localizer);
            }
            ;

            var report = ActivatorUtilities.CreateInstance(HttpContext.RequestServices, reportInfo.Type) as IReport;
            var json   = opt.TrimToNull();

            if (json != null)
            {
                JsonConvert.PopulateObject(json, report);
            }

            byte[] renderedBytes = null;

            if (report is IDataOnlyReport dataOnlyReport)
            {
                ext           = "xlsx";
                renderedBytes = ReportRepository.Render(dataOnlyReport);
            }
            else
            {
                ext = (ext ?? "html").ToLowerInvariant();

                if (ext == "htm" || ext == "html")
                {
                    var result = RenderAsHtml(report, download, printing, ref renderedBytes);
                    if (!download)
                    {
                        return(result);
                    }
                }
                else if (ext == "pdf")
                {
                    renderedBytes = RenderAsPdf(report, key, opt);
                }
                else
                {
                    throw new ArgumentOutOfRangeException("ext");
                }
            }

            return(PrepareFileResult(report, ext, download, renderedBytes, reportInfo));
        }
Exemplo n.º 5
0
        public ActionResult Execute(ReportExecuteRequest request)
        {
            request.CheckNotNull();

            if (request.ReportKey.IsEmptyOrNull())
            {
                throw new ArgumentNullException("reportKey");
            }

            if (request.DesignId.IsEmptyOrNull())
            {
                throw new ArgumentNullException("designId");
            }

            var reportInfo = ReportRegistry.GetReport(request.ReportKey);

            if (reportInfo == null)
            {
                throw new ArgumentOutOfRangeException("reportKey");
            }

            var report = JsonConvert.DeserializeObject(request.Parameters.ToJson(), reportInfo.Type, JsonSettings.Tolerant);

            var exportType = request.ExportType ?? ReportExportType.Pdf;

            byte[] rendered = new ReportRepository().RenderReport((IReport)report,
                                                                  request.ReportKey, request.DesignId, exportType);

            string fileDownloadName;
            var    customFileName = report as ICustomFileName;

            if (customFileName != null)
            {
                fileDownloadName = customFileName.GetFileName();
            }
            else
            {
                fileDownloadName = (reportInfo.Title ?? reportInfo.Key) + "_" +
                                   DateTime.Now.ToString("yyyyMMdd_HHss");
            }

            fileDownloadName += "." + ExportExtensions[exportType];

            if (request.ExportType == null)
            {
                var cd = new ContentDisposition
                {
                    Inline   = true,
                    FileName = fileDownloadName
                };

                Response.AddHeader("Content-Disposition", cd.ToString());
                return(File(rendered, "application/pdf"));
            }
            else
            {
                return(new FileContentResult(rendered, "application/octet-stream")
                {
                    FileDownloadName = fileDownloadName
                });
            }
        }
        private ActionResult Execute(string key, string opt, string ext, bool download, bool printing)
        {
            if (key.IsEmptyOrNull())
            {
                throw new ArgumentNullException(nameof(key));
            }

            var reportInfo = ReportRegistry.GetReport(key);

            if (reportInfo == null)
            {
                throw new ArgumentOutOfRangeException(nameof(key));
            }

            if (reportInfo.Permission != null)
            {
                Context.Permissions.ValidatePermission(reportInfo.Permission, Context.Localizer);
            }

            var report = ActivatorUtilities.CreateInstance(HttpContext.RequestServices, reportInfo.Type) as IReport;
            var json   = opt.TrimToNull();

            if (json != null)
            {
                JsonConvert.PopulateObject(json, report);
            }

            byte[] renderedBytes = null;

            if (report is IDataOnlyReport dataOnlyReport)
            {
                ext           = "xlsx";
                renderedBytes = ExcelRenderer.Render(dataOnlyReport);
            }
            else if (report is IExternalReport)
            {
                var url = report.GetData() as string;
                if (string.IsNullOrEmpty(url))
                {
                    throw new InvalidProgramException("External reports must return a URL string from GetData() method!");
                }

                return(new RedirectResult(url));
            }
            else
            {
                ext = (ext ?? "html").ToLowerInvariant();

                if (ext == "htm" || ext == "html")
                {
                    var result = RenderAsHtml(report, download, printing, ref renderedBytes);
                    if (!download)
                    {
                        return(result);
                    }
                }
                else if (ext == "pdf")
                {
                    renderedBytes = RenderAsPdf(report, key, opt);
                }
                else
                {
                    throw new ArgumentOutOfRangeException(nameof(ext));
                }
            }

            return(PrepareFileResult(report, ext, download, renderedBytes, reportInfo));
        }