public virtual string ExecuteHtml(string viewTemplate, OperationPackage package) { string date = $"{DateTime.Now:dd.MM.yy HH:mm:ss}"; TemplateServiceConfiguration templateConfig = new TemplateServiceConfiguration { DisableTempFileLocking = true, CachingProvider = new DefaultCachingProvider(t => { }) }; var serv = RazorEngineService.Create(templateConfig); Engine.Razor = serv; Engine.Razor.Compile(viewTemplate, "somekey"); if (!package.DataSets.Any()) { return("No information obtained by query"); } var packageValues = PackageParser.GetPackageValues(package); var dataSet = packageValues.First(); var model = new { dataSet.Headers, Content = dataSet.Rows, Date = date }; return(Engine.Razor.Run("somekey", null, model)); }
public virtual string ExecuteHtml(string viewTemplate, OperationPackage package) { string date = $"{DateTime.Now:dd.MM.yy HH:mm:ss}"; if (!package.DataSets.Any()) { return("No information obtained by query"); } var packageValues = PackageParser.GetPackageValues(package); var dataSet = packageValues.First(); var model = new { dataSet.Headers, Content = dataSet.Rows, Date = date }; var engine = new RazorLightEngineBuilder() .UseEmbeddedResourcesProject(typeof(Program)) .UseMemoryCachingProvider() .Build(); var result = engine.CompileRenderStringAsync("templateKey", viewTemplate, model).Result; return(result); }
public void Execute(IReportTaskRunContext taskContext) { var sqlContext = SqlContextProvider.DefaultInstance .CreateContext(ConnectionString); var package = taskContext.Packages[Properties.PackageName]; if (!RunIfVoidPackage && package.DataSets.Count == 0) { return; } var firstSet = packageParser.GetPackageValues(package).First(); var columns = package.DataSets.First().Columns; if (CreateTable) { sqlContext.CreateSimple(new QueryOptions(DbTimeOut), CreateTableByColumnInfo(columns)) .ExecuteNonQuery(); } if (DropBefore) { sqlContext.CreateSimple(new QueryOptions(DbTimeOut), $"IF OBJECT_ID('{TableName}') IS NOT NULL DELETE {TableName}") .ExecuteNonQuery(); } StringBuilder comm = new StringBuilder($@"INSERT INTO {TableName} ("); for (int i = 0; i < columns.Count - 1; i++) { comm.Append($@"[{columns[i].Name}],"); } comm.Append($@"[{columns.Last().Name}]) VALUES ("); foreach (var row in firstSet.Rows) { var fullRowData = new StringBuilder(comm.ToString()); int i; for (i = 0; i < columns.Count - 1; i++) { fullRowData.Append($"@p{i},"); } fullRowData.Append($"@p{i})"); sqlContext.CreateSimple(new QueryOptions(DbTimeOut), fullRowData.ToString(), row.ToArray()) .ExecuteNonQuery(); } }
protected virtual async Task ExportDataSet(IReportTaskRunContext taskContext, DbConnection connection) { var token = taskContext.CancelSource.Token; var package = taskContext.Packages[Properties.PackageName]; if (!RunIfVoidPackage && package.DataSets.Count == 0) { return; } var firstSet = packageParser.GetPackageValues(package).First(); var columns = package.DataSets.First().Columns; if (CreateTable) { await connection.ExecuteAsync(new CommandDefinition(BuildCreateTableQuery(columns), commandTimeout : DbTimeOut, cancellationToken : token)); //todo:logic for auto-creating table by user-defined list of columns? } connection.Open(); await using var transaction = connection.BeginTransaction(); try { if (DropBefore) { await connection.ExecuteAsync(new CommandDefinition(CleanTableQuery, commandTimeout : DbTimeOut, cancellationToken : token, transaction : transaction)); } var query = BuildInsertQuery(columns); var dynamicRows = firstSet.Rows.Select(row => { var p = new DynamicParameters(); for (int i = 0; i < row.Count; i++) { p.Add($"@p{i}", row[i]); } return(p); }); await connection.ExecuteAsync(new CommandDefinition( query, dynamicRows, commandTimeout : DbTimeOut, cancellationToken : token, transaction : transaction)); transaction.Commit(); } catch { transaction.Rollback(); throw; } }
public async Task ExecuteAsync(IReportTaskRunContext taskContext) { var package = taskContext.Packages[Properties.PackageName]; if (!RunIfVoidPackage && package.DataSets.Count == 0) { if (!RunIfVoidPackage && package.DataSets.Count == 0) { return; } } string fileName = (string.IsNullOrEmpty(ReportName) ? $@"{Properties.PackageName}" : taskContext.SetStringParameters(ReportName)) + (DateInName ? $" {DateTime.Now:dd.MM.yy}" : null); using var msg = ConfigureMessage(taskContext, fileName); var dataSets = parser.GetPackageValues(package); var firstSet = dataSets.First(); viewExecutor = firstSet.GroupColumns != null && firstSet.GroupColumns.Any() ? autofac.ResolveNamed <IViewExecutor>("GroupedViewex") : string.IsNullOrEmpty(ViewTemplate) ? autofac.ResolveNamed <IViewExecutor>("CommonTableViewEx") : autofac.ResolveNamed <IViewExecutor>("commonviewex"); if (HasHtmlBody) { msg.IsBodyHtml = true; msg.Body = viewExecutor.ExecuteHtml(ViewTemplate, package); } MemoryStream streamJson = null; MemoryStream streamXlsx = null; try { if (HasJsonAttachment) { var dataToSave = UseAllSetsJson ? JsonConvert.SerializeObject(dataSets) : JsonConvert.SerializeObject(dataSets.First()); streamJson = new MemoryStream(System.Text.Encoding.UTF8 .GetBytes(dataToSave)); AddDataSetsJson(msg, streamJson, fileName); } if (HasXlsxAttachment) { streamXlsx = new MemoryStream(); AddDataSetsXlsx(package, msg, streamXlsx, fileName); } using var client = ConfigureClient(); await using (taskContext.CancelSource.Token.Register(() => client.SendAsyncCancel())) { var tryCount = 0; while (tryCount < 3) { try { await client.SendMailAsync(msg); break; } catch (Exception exc) { if (tryCount == 2) { throw new Exception("Message not sent", exc); } else { tryCount++; } } } } } finally { streamJson?.Dispose(); streamXlsx?.Dispose(); } }
} //ctor public void Execute(IReportTaskRunContext taskContext) { var package = taskContext.Packages[Properties.PackageName]; if (!RunIfVoidPackage && package.DataSets.Count == 0) { return; } string filename = (string.IsNullOrEmpty(ReportName) ? $@"{Properties.PackageName}" : taskContext.SetStringParameters(ReportName)) + (DateInName ? null : $" {DateTime.Now:dd.MM.yy}"); string filenameJson = $@"{filename}.json"; string filenameXlsx = $@"{filename}.xlsx"; using (var client = new SmtpClient(ConfigurationManager.AppSettings["SMTPServer"], 25)) using (var msg = new MailMessage()) { client.DeliveryFormat = SmtpDeliveryFormat.International; client.EnableSsl = true; client.DeliveryMethod = SmtpDeliveryMethod.Network; msg.From = new MailAddress(ConfigurationManager.AppSettings["from"]); msg.AddRecepientsFromGroup(addresses); if (!string.IsNullOrEmpty(RecepientsDatasetName)) { msg.AddRecepientsFromPackage(taskContext.Packages[RecepientsDatasetName]); } msg.Subject = filename; var dataset = parser.GetPackageValues(package).First(); viewExecutor = dataset.GroupColumns != null && dataset.GroupColumns.Any() ? autofac.ResolveNamed <IViewExecutor>("GroupedViewex") : !string.IsNullOrEmpty(ViewTemplate) ? autofac.ResolveNamed <IViewExecutor>("CommonTableViewEx") : autofac.ResolveNamed <IViewExecutor>("commonviewex"); if (HasHtmlBody) { msg.IsBodyHtml = true; msg.Body = viewExecutor.ExecuteHtml(ViewTemplate, package); } MemoryStream streamJson = null; MemoryStream streamXlsx = null; try { if (HasJsonAttachment) { var sets = parser.GetPackageValues(package); var dataToSave = UseAllSetsJson ? JsonConvert.SerializeObject(sets) : JsonConvert.SerializeObject(sets.First()); streamJson = new MemoryStream(System.Text.Encoding.UTF8 .GetBytes(dataToSave)); msg.Attachments.Add(new Attachment(streamJson, filenameJson, @"application/json")); } if (HasXlsxAttachment) { streamXlsx = new MemoryStream(); var excel = viewExecutor.ExecuteXlsx(package, filename, UseAllSetsXlsx); excel.SaveAs(streamXlsx); excel.Dispose(); streamXlsx.Position = 0; msg.Attachments.Add(new Attachment(streamXlsx, filenameXlsx, @"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")); } var tryCount = 0; while (tryCount < 3) { try { client.Send(msg); break; } catch (Exception exc) { if (tryCount == 2) { throw new Exception("Message not sent", exc); } else { tryCount++; } } } } finally { streamJson?.Dispose(); streamXlsx?.Dispose(); } } } //method