Exemplo n.º 1
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.º 2
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.º 3
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.º 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 Index(string id)
        {
            id = "Cengaver";
            var reports = ReportRegistry.GetAvailableReportsInCategory(null);
            var model   = CreatePageModel(reports, id);

            return(View("~/Modules/Common/Reporting/ReportIndex.cshtml", model));
        }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            if (!settings.ParseArgs(args))
            {
                settings.PrintHelp();
                return;
            }

            settings.SetDefaults();
            if (settings.Report != null)
            {
                report    = new ReportRegistry();
                oldReport = ReportRegistry.Load(settings.Report);
            }

            if (settings.Verbose)
            {
                Console.WriteLine($"Protobuf descriptor: {settings.File}");
                Console.WriteLine($"Output directory:    {settings.OutputDir}");
                Console.WriteLine($"Namespace base:      {settings.NamespaceBase}");
            }

            if (settings.SearchDir != null)
            {
                WorkAtDir(settings.SearchDir);
            }
            else
            {
                WorkSingleFile(settings.File);
            }

            if (oldReport != null && settings.RemoveWidowFiles)
            {
                if (settings.Verbose)
                {
                    Console.WriteLine("searching for widow files");
                }
                var remove = oldReport.Generateds.Select(x => x.File)
                             .Except(report.Generateds.Select(x => x.File));
                foreach (var file in remove)
                {
                    if (File.Exists(file))
                    {
                        File.Delete(file);
                    }
                }
            }

            report?.Save(settings.Report);

            if (settings.Verbose)
            {
                Console.WriteLine("Finish.");
            }
        }
Exemplo n.º 7
0
        public static ReportTree FromList(IEnumerable <ReportRegistry.Report> reports,
                                          string rootPath = null, string categoryOrder = null)
        {
            if (reports == null)
            {
                throw new ArgumentNullException("reports");
            }

            rootPath      = rootPath ?? "";
            categoryOrder = categoryOrder ?? "";

            var tree = new ReportTree();

            var categoryByKey = new Dictionary <string, ReportTree.Category>(StringComparer.CurrentCultureIgnoreCase);

            foreach (var report in reports)
            {
                ReportTree.Category category;
                if (categoryByKey.TryGetValue(report.Category.Key ?? "", out category))
                {
                    category.Reports.Add(report);
                    continue;
                }

                var parts = (report.Category.Key ?? "Other")
                            .Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

                string current = "";
                category = null;
                foreach (var part in parts)
                {
                    string prior = current;

                    if (current.Length > 0)
                    {
                        current += "/";
                    }

                    current += part;

                    if (current.Length <= rootPath.Length)
                    {
                        continue;
                    }

                    if (!categoryByKey.TryGetValue(current ?? "", out category))
                    {
                        category               = new ReportTree.Category();
                        category.Key           = current;
                        category.Title         = ReportRegistry.GetReportCategoryTitle(current);
                        categoryByKey[current] = category;

                        if (!categoryByKey.ContainsKey(prior))
                        {
                            tree.Root.SubCategories.Add(category);
                        }
                        else
                        {
                            var x = categoryByKey[prior];
                            x.SubCategories.Add(category);
                        }
                    }
                }

                if (category == null)
                {
                    tree.Root.Reports.Add(report);
                }
                else
                {
                    category.Reports.Add(report);
                }
            }

            var order = new Dictionary <string, int>(StringComparer.InvariantCultureIgnoreCase);
            var i     = 0;

            foreach (var x in categoryOrder.Split(new char[] { ';' }))
            {
                var xt = x.TrimToNull();
                if (xt != null)
                {
                    order[xt] = i++;
                }
            }

            Comparison <ReportTree.Category> sort = (x, y) =>
            {
                var c = 0;

                if (x.Key != y.Key)
                {
                    var c1 = order.ContainsKey(x.Key) ? (Int32?)order[x.Key] : null;
                    var c2 = order.ContainsKey(y.Key) ? (Int32?)order[y.Key] : null;
                    if (c1 != null && c2 != null)
                    {
                        c = c1.Value - c2.Value;
                    }
                    else if (c1 != null)
                    {
                        c = -1;
                    }
                    else if (c2 != null)
                    {
                        c = 1;
                    }
                }

                if (c == 0)
                {
                    c = String.Compare(x.Title, y.Title, StringComparison.CurrentCultureIgnoreCase);
                }

                return(c);
            };

            foreach (var category in categoryByKey.Values)
            {
                if (category.SubCategories != null)
                {
                    category.SubCategories.Sort(sort);
                }
            }

            tree.Root.SubCategories.Sort(sort);

            return(tree);
        }
Exemplo n.º 8
0
        public ReportTree GetReportTree(string category)
        {
            var reports = ReportRegistry.GetAvailableReportsInCategory(category);

            return(ReportTree.FromList(reports, category));
        }
Exemplo n.º 9
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
                });
            }
        }
Exemplo n.º 10
0
        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));
        }