示例#1
0
        public static Stream GetReport(bool useReturnStream, string pluginName, string guid,
                                       StorageValues storageValues, string culture, Page page, string format, string command,
                                       LogMonitor logMonitor, bool expToWord, Dictionary <string, object> constants, StiWebViewer report, out string fileNameExtention, bool RoleCheck)
        {
            var originalUICulture = Thread.CurrentThread.CurrentUICulture;
            var originalCulture   = Thread.CurrentThread.CurrentCulture;

            try
            {
                if (string.IsNullOrEmpty(culture))
                {
                    culture = "ru-ru";
                }
                Thread.CurrentThread.CurrentUICulture   =
                    Thread.CurrentThread.CurrentCulture =
                        new CultureInfo(culture == "ru" ? "ru-ru" : (culture == "kz" ? "kk-kz" : culture));

                fileNameExtention = "";
                WebInitializer.Initialize();
                var webReportManager = new WebReportManager(new TreeView());
                if (string.IsNullOrEmpty(pluginName))
                {
                    return(null);
                }
                webReportManager.RoleCheck = RoleCheck;
                webReportManager.Plugin    = webReportManager.GetPlugins()[pluginName];
                if (webReportManager.Plugin != null)
                {
                    webReportManager.Plugin.InitializeReportCulture(culture);
                    var values          = storageValues;
                    var webReportPlugin = (IWebReportPlugin)webReportManager.Plugin;
                    var stiPlugin       = (IStimulsoftReportPlugin)webReportPlugin;
                    webReportPlugin.Page = page;

                    webReportManager.CreateView();

                    if (!string.IsNullOrEmpty(webReportManager.Plugin.CultureParameter))
                    {
                        values.SetStorageValues(
                            webReportManager.Plugin.CultureParameter, webReportManager.Plugin.InitializedKzCulture);
                    }

                    webReportManager.InitValues(values, webReportPlugin.InitSavedValuesInvisibleConditions);
                    webReportPlugin.Constants = constants;
                    if (!string.IsNullOrEmpty(webReportManager.Plugin.CultureParameter) &&
                        stiPlugin.Report.Dictionary.Variables.Contains(webReportManager.Plugin.CultureParameter))
                    {
                        if (webReportManager.Plugin.SupportRuKz)
                        {
                            stiPlugin.Report[webReportManager.Plugin.CultureParameter] =
                                webReportManager.Plugin.InitializedKzCulture;
                        }
                        else
                        {
                            stiPlugin.Report[webReportManager.Plugin.CultureParameter] =
                                webReportManager.Plugin.InitializedCulture;
                        }
                    }

                    try
                    {
                        webReportManager.ShowReport();
                    }
                    catch (ConstraintException e)
                    {
                        var allErrors = webReportManager.Plugin.Table.DataSet?.GetAllErrors();
                        if (!string.IsNullOrEmpty(allErrors))
                        {
                            throw new Exception(allErrors, e);
                        }
                        var errors = webReportManager.Plugin.Table.GetErrors();
                        if (errors.Length > 0)
                        {
                            throw new Exception(errors.Select(r => r.RowError).Aggregate((v1, v2) => v1 + "; " + v2), e);
                        }
                        throw;
                    }

                    var section = ReportInitializerSection.GetReportInitializerSection();
                    if (section != null && !string.IsNullOrEmpty(section.PropSaveDataFile))
                    {
                        if (webReportManager.Plugin.Table.DataSet != null)
                        {
                            webReportManager.Plugin.Table.DataSet.WriteXml(section.PropSaveDataFile);
                        }
                        else
                        {
                            webReportManager.Plugin.Table.WriteXml(section.PropSaveDataFile);
                        }
                    }

                    if (HttpContext.Current != null && HttpContext.Current.Request.QueryString["GetDataSet"] == "on" && UserRoles.IsInRole(UserRoles.ADMIN))
                    {
                        using (var stream = new MemoryStream())
                        {
                            if (webReportManager.Plugin.Table.DataSet != null)
                            {
                                webReportManager.Plugin.Table.DataSet.WriteXml(stream);
                            }
                            else
                            {
                                webReportManager.Plugin.Table.WriteXml(stream);
                            }

                            stream.Position = 0;
                            PageHelper.DownloadFile(stream, "data.xml", HttpContext.Current.Response);
                        }
                    }

                    var autoExport = webReportPlugin as IReportAutoExport;

                    DBDataContext.AddViewReports(
                        Tools.Security.User.GetSID(),
                        HttpContext.Current?.User.Identity.Name ?? "anonymous",
                        HttpContext.Current?.User.Identity.Name ?? "anonymous",
                        ReportInitializerSection.GetReportInitializerSection().ReportPageViewer + "?ClassName=" + pluginName,
                        HttpContext.Current?.Request.Url.GetLeftPart(UriPartial.Authority) ?? "https://maxat",
                        Environment.MachineName,
                        useReturnStream || autoExport != null || expToWord || stiPlugin.AutoExportTo != null,
                        webReportManager.Plugin.GetType());

                    Log(logMonitor, guid, webReportManager);
                    if (useReturnStream)
                    {
                        StiExportFormat stiExportFormat;
                        try
                        {
                            stiExportFormat = (StiExportFormat)Enum.Parse(typeof(StiExportFormat), format);
                        }
                        catch (Exception)
                        {
                            var stiCustomExportType = (CustomExportType)Enum.Parse(typeof(CustomExportType), format);

                            return(ExportWithoutShow(
                                       webReportManager.Report,
                                       stiCustomExportType,
                                       true,
                                       out fileNameExtention));
                        }

                        fileNameExtention = WebReportManager.GetFileExtension(stiExportFormat);
                        return(ExportWithoutShow(webReportManager.Report, stiExportFormat, true));
                    }

                    //webReportManager.Report.EndRender += (sender, args) => LogMessages(webReportManager.Report, logMonitor);
                    if (autoExport != null)
                    {
                        autoExport.Export();
                    }

                    if (!expToWord && stiPlugin.AutoExportTo == null)
                    {
                        report.Report = webReportManager.Report;
                        page.Cache.Insert(report.Report.ReportName,
                                          report.Report,
                                          null,
                                          Cache.NoAbsoluteExpiration,
                                          new TimeSpan(0, 10, 0),
                                          CacheItemPriority.NotRemovable,
                                          null);
                        report.ResetCurrentPage();
                    }
                    else if (webReportPlugin.CustomExportType != CustomExportType.None)
                    {
                        return(ExportWithoutShow(
                                   webReportManager.Report,
                                   webReportPlugin.CustomExportType,
                                   false,
                                   out fileNameExtention));
                    }
                    else if (stiPlugin.AutoExportTo != null)
                    {
                        return(ExportWithoutShow(webReportManager.Report, stiPlugin.AutoExportTo.Value, false));
                    }
                    else
                    {
                        return(ExportWithoutShow(webReportManager.Report, StiExportFormat.Word2007, false));
                    }
                }
            }
            finally
            {
                Thread.CurrentThread.CurrentUICulture = originalUICulture;
                Thread.CurrentThread.CurrentCulture   = originalCulture;
            }

            return(null);
        }
示例#2
0
        public static Stream GetReport(bool useReturnStream, string pluginName, string guid,
                                       StorageValues storageValues, string culture, Page page, string format, string command,
                                       LogMonitor logMonitor, ServerReport report, out string fileNameExtention, bool RoleCheck)
        {
            fileNameExtention = "";
            WebInitializer.Initialize();
            report = ReportViewerInitializer(report);
            var webReportManager = new WebReportManager(new TreeView());

            if (string.IsNullOrEmpty(pluginName))
            {
                return(null);
            }
            webReportManager.RoleCheck = RoleCheck;
            webReportManager.Plugin    = webReportManager.GetPlugins()[pluginName];
            webReportManager.Plugin.InitializeReportCulture(culture);
            var webReportPlugin = (IWebReportPlugin)webReportManager.Plugin;

            webReportPlugin.Page = page;
            webReportManager.CreateView();
            webReportManager.InitValues(storageValues, true);
            var list = new List <ReportParameter>();

            foreach (var reportCondition in webReportManager.Plugin.Conditions)
            {
                var conditions = reportCondition.GetQueryConditions().ToList();
                if (conditions.Count == 0)
                {
                    list.Add(new ReportParameter(reportCondition.ColumnFilter.GetStorage().Name, (string)null, false));
                }
                else
                {
                    foreach (var condition in conditions)
                    {
                        if (condition.Parameters.Length == 0)
                        {
                            throw new Exception("Условие без значения не поддерживаются при формировании отчета");
                        }
                        var values = condition.GetParamValues().Select(r => r.ToString()).ToArray();
                        list.Add(new ReportParameter(condition.ColumnName, values, false));
                    }
                }
            }

            if (!string.IsNullOrEmpty(webReportManager.Plugin.CultureParameter))
            {
                if (webReportManager.Plugin.SupportRuKz)
                {
                    list.Add(new ReportParameter(webReportManager.Plugin.CultureParameter, webReportManager.Plugin.InitializedKzCulture.ToString(), false));
                }
                else
                {
                    list.Add(new ReportParameter(webReportManager.Plugin.CultureParameter, webReportManager.Plugin.InitializedCulture, false));
                }
            }
            //+= потому что инициализируется папка в которой лежат отчеты
            report.ReportPath += ((ISqlReportingServicesPlugin)webReportManager.Plugin).ReportUrl;
            report.SetParameters(list);

            Log(logMonitor, guid, webReportManager);

            var export = !string.IsNullOrEmpty(format) && "render".Equals(command, StringComparison.OrdinalIgnoreCase);

            DBDataContext.AddViewReports(
                Tools.Security.User.GetSID(),
                HttpContext.Current?.User.Identity.Name ?? "anonymous",
                HttpContext.Current?.User.Identity.Name ?? "anonymous",
                ReportInitializerSection.GetReportInitializerSection().ReportPageViewer + "?ClassName=" + webReportManager.Plugin.GetType().FullName,
                HttpContext.Current?.Request.Url.GetLeftPart(UriPartial.Authority) ?? "https://maxat",
                Environment.MachineName,
                export,
                webReportManager.Plugin.GetType());

            if (export)
            {
                string mimeType;
                if (useReturnStream)
                {
                    return(report.Render(format, "", null, out mimeType, out fileNameExtention));
                }

                PageHelper.DownloadFile(
                    report.Render(format, "", null, out mimeType, out fileNameExtention),
                    webReportManager.Plugin.Description + "." + fileNameExtention, HttpContext.Current.Response);
            }

            return(null);
        }