using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Net; using System.Web.Mvc; using BeWo.Service.Security; using BeWoPlanerMobil.Models; using BeWoPlanerMobil.Service; using BeWoPlanerMobil.Util; using BS.Shared; using BS.Shared.Core; using BS.Shared.DataContracts; using DevExpress.CodeParser; using DevExpress.XtraReports.UI; namespace BeWoPlanerMobil.Controllers { public class ReportViewerController : AbstractBaseController { private ReportViewerModel _Model; public ReportViewerModel Model { get { if(!MobileSessionFacade.IsUserLoggedIn()) { RedirectToActionPermanent("Index", "Login"); return null; } if(((ReportViewerModel)Session[ModelSessionConstants.ReportViewerModelKey])?.Employee is null) { _Model = new ReportViewerModel { Employee = MobileSessionFacade.LoggedInEmployee }; Session[ModelSessionConstants.ReportViewerModelKey] = _Model; } else { _Model = (ReportViewerModel)Session[ModelSessionConstants.ReportViewerModelKey]; } return _Model; } } public ActionResult ReportViewer() { var month = DateTime.Today.Month; var year = DateTime.Today.Year; var first = new DateTime(year, month, 1).AddMonths(-1); Model.SelectedYear = Model.SelectedYear ?? first.Year; Model.SelectedMonth = Model.SelectedMonth ?? first.Month; Model.AllEmployees = EmployeeService.GetActiveCompactEmployeesForEmployee(MobileSessionFacade.LoggedInCompactEmployee.EmployeeOid); Model.AllTeams = EmployeeService.GetAllActiveCompactTeamsForEmployee(MobileSessionFacade.LoggedInEmployee.EmployeeOid); Model.ReportTypes = new List { new SelectListItem { Text = "Berichte", Value = "0" }, new SelectListItem { Text = "Mitarbeiterstundenkonto", Value = "1" } }; Model.SelectedReportType = ReportType.Berichte; Model.Queries = QueryService.GetAllQueriesOfType(QueryType.Public); return View(Model); } [Authorize] public ActionResult DocumentWebViewerPartial() { return PartialView("DocumentWebViewerPartial", Model); } [Authorize] public ActionResult ExportDocumentViewer() { throw new NotImplementedException("Service dafür implementieren!"); } [Authorize] public ActionResult LoadCustomReport(long? reportOid) { if(Model?.Employee?.EmployeeOid is null || reportOid is null) { return Logout(); } var selectedQuery = Model.Queries.FirstOrDefault(query => query.Oid.Equals(reportOid.Value)); if(selectedQuery is null) { Model.Report = null; return PartialView("DocumentWebViewerPartial", Model); } var queryReportUri = GetQueryReportUrl(selectedQuery); var baseUri = SecurityUtils.RemoveAuthenticationInfoFromUri(queryReportUri); var test = QueryService.ExecuteQuery(selectedQuery.Oid, null); if(test is null) { return PartialView("DocumentWebViewerPartial", Model); } QueryService.PrepareQueryReport(test, $"{MobileSessionFacade.Tenant}queryeoid{Model.Employee.EmployeeOid.Value}"); var tempToken = DownloadService.CreateTemporaryToken(baseUri, false); var addChar = baseUri.Contains("?") ? "&" : "?"; var finalUri = $"{baseUri}{addChar}token={tempToken}"; var webClient = new WebClient(); var data = webClient.DownloadData(finalUri); var report = new XtraReport(); using(var memoryStream = new MemoryStream(data)) { report.PrintingSystem.LoadDocument(memoryStream); } Model.Report = report; return PartialView("DocumentWebViewerPartial", Model); } [Authorize] private string GetQueryReportUrl(QueryDC query) { var url = Request?.Url; if(Model?.Employee?.EmployeeOid is null || url is null || query is null) { return null; } //DEBUG: http://localhost:3777/service //RELEASE: https://app4.bewoplaner.de/service // http://localhost:3777/Host/ReportView.aspx?dcid=demoqueryeoid372&qoid=1304&type=QueryReport var urlSuffix = $"/ReportView.aspx?dcid={MobileSessionFacade.Tenant}queryeoid{Model.Employee.EmployeeOid.Value}&qoid={query.Oid}&type={Utils.EnumName(ReportTypes.QueryReport)}"; #if DEBUG return $"http://localhost:3777/Host{urlSuffix}"; #endif return $"{url.Scheme}//{url.Host}{urlSuffix}"; } } }