/// <summary> /// Extract the collection <see cref="QueryInfo"/> used as context for /// template generation based on a collection of entities. /// </summary> public static QueryInfo EntitiesPreloadedQuery <TKey>(PrintEntitiesArguments <TKey> args, string collection, int?defId) { // Preloaded Query QueryInfo preloadedQuery; if (args.I != null && args.I.Any()) { preloadedQuery = new QueryEntitiesByIdsInfo( collection: collection, definitionId: defId, ids: args.I); } else { preloadedQuery = new QueryEntitiesInfo( collection: collection, definitionId: defId, filter: args.Filter, orderby: args.OrderBy, top: args.Top, skip: args.Skip); } return(preloadedQuery); }
public async Task <ActionResult <EmailCommandPreview> > EmailCommandPreviewEntities( [FromBody] EmailTemplate template, [FromQuery] PrintEntitiesArguments <int> args, CancellationToken cancellation) { var result = await _service.EmailCommandPreviewEntities(template, args, cancellation); return(Ok(result)); }
public async Task <ActionResult> EmailEntities(int templateId, [FromQuery] PrintEntitiesArguments <int> args, [FromBody] EmailCommandVersions versions, CancellationToken cancellation) { args.Custom = Request.Query.ToDictionary(e => e.Key, e => e.Value.FirstOrDefault()); var service = GetService(); await service.SendByEmail(templateId, args, versions, cancellation); return(Ok()); }
public async Task <ActionResult <PrintPreviewResponse> > EmailPreviewEntities( [FromRoute] int index, [FromBody] NotificationTemplate template, [FromQuery] PrintEntitiesArguments <int> args, CancellationToken cancellation) { var result = await _service.EmailPreviewEntities(template, index, args, cancellation); return(Ok(result)); }
public async Task <ActionResult> MessageEntities(int templateId, [FromQuery] PrintEntitiesArguments <int> args, [FromQuery] string version, CancellationToken cancellation) { args.Custom = Request.Query.ToDictionary(e => e.Key, e => e.Value.FirstOrDefault()); var service = GetService(); var commandId = await service.SendByMessage(templateId, args, version, cancellation); return(Ok(new IdResult { Id = commandId })); }
public async Task <FileContentResult> PrintEntities(int templateId, [FromQuery] PrintEntitiesArguments <TKey> args, CancellationToken cancellation) { var service = GetFactWithIdService(); var result = await service.PrintEntities(templateId, args, cancellation); var fileBytes = result.FileBytes; var fileName = result.FileName; var contentType = ControllerUtilities.ContentType(fileName); Response.Headers.Add("x-filename", fileName); return(File(fileContents: fileBytes, contentType: contentType, fileName)); }
public async Task <FileContentResult> Print(int templateId, [FromQuery] PrintEntitiesArguments <int> args, CancellationToken cancellation) { args.Custom = Request.Query.ToDictionary(e => e.Key, e => e.Value.FirstOrDefault()); var result = await _service.Print(templateId, args, cancellation); var fileBytes = result.FileBytes; var fileName = result.FileName; var contentType = ControllerUtilities.ContentType(fileName); Response.Headers.Add("x-filename", fileName); return(File(fileContents: fileBytes, contentType: contentType, fileName)); }
public async Task <ActionResult <EmailPreview> > EmailPreviewEntities(int templateId, int index, [FromQuery] PrintEntitiesArguments <int> args, CancellationToken cancellation) { args.Custom = Request.Query.ToDictionary(e => e.Key, e => e.Value.FirstOrDefault()); var service = GetService(); var result = await service.EmailPreviewEntities(templateId, index, args, cancellation); return(Ok(result)); }
public async Task <PreviewResult> PreviewByFilter(PrintingPreviewTemplate template, PrintEntitiesArguments <int> args, CancellationToken cancellation) { await Initialize(cancellation); var collection = template.Collection; var defId = template.DefinitionId; // (1) The templates var nameP = new TemplatePlanLeaf(template.DownloadName, TemplateLanguage.Text); var bodyP = new TemplatePlanLeaf(template.Body, TemplateLanguage.Html); var printoutP = new TemplatePlanTuple(nameP, bodyP); TemplatePlan plan; if (string.IsNullOrWhiteSpace(template.Context)) { plan = printoutP; } else { plan = new TemplatePlanDefine("$", template.Context, printoutP); } QueryInfo contextQuery = BaseUtil.EntitiesPreloadedQuery(args, collection, defId); plan = new TemplatePlanDefineQuery("$", contextQuery, plan); // (2) Functions + Variables var globalFunctions = new Dictionary <string, EvaluationFunction>(); var localFunctions = new Dictionary <string, EvaluationFunction>(); var globalVariables = new Dictionary <string, EvaluationVariable>(); var localVariables = BaseUtil.EntitiesLocalVariables(args, collection, defId); await FactBehavior.SetPrintingFunctions(localFunctions, globalFunctions, cancellation); await FactBehavior.SetPrintingVariables(localVariables, globalVariables, cancellation); // (3) Generate output var culture = GetCulture(args, await _behavior.Settings(cancellation)); var genArgs = new TemplateArguments( customGlobalFunctions: globalFunctions, customGlobalVariables: globalVariables, customLocalFunctions: localFunctions, customLocalVariables: localVariables, culture: culture); await _templateService.GenerateFromPlan(plan : plan, args : genArgs, cancellation : cancellation); var downloadName = AppendExtension(nameP.Outputs[0], template); var body = bodyP.Outputs[0]; // Return as a file return(new PreviewResult(body, downloadName)); }
public async Task <EmailPreview> EmailPreviewEntities(NotificationTemplate template, int emailIndex, PrintEntitiesArguments <int> args, CancellationToken cancellation) { await Initialize(cancellation); await FillNavigationProperties(template, cancellation); int index = emailIndex; var preloadedQuery = BaseUtil.EntitiesPreloadedQuery(args: args, collection: template.Collection, defId: template.DefinitionId); var localVars = BaseUtil.EntitiesLocalVariables(args: args, collection: template.Collection, defId: template.DefinitionId); var preview = await _behavior.UnversionedEmailCommandPreview( template : template, preloadedQuery : preloadedQuery, localVariables : localVars, fromIndex : index, toIndex : index, cultureString : args.Culture, cancellation : cancellation); // Return the email and the specific index if (index < preview.Emails.Count) { return(preview.Emails[index]); } else { throw new ServiceException($"Index {index} is outside the range."); } }
public async Task <EmailCommandPreview> EmailCommandPreviewEntities(NotificationTemplate template, PrintEntitiesArguments <int> args, CancellationToken cancellation) { await Initialize(cancellation); if (template == null) { throw new ServiceException($"Email template was not supplied."); } await FillNavigationProperties(template, cancellation); int index = 0; var preloadedQuery = BaseUtil.EntitiesPreloadedQuery(args: args, collection: template.Collection, defId: template.DefinitionId); var localVars = BaseUtil.EntitiesLocalVariables(args: args, collection: template.Collection, defId: template.DefinitionId); var preview = await _behavior.UnversionedEmailCommandPreview( template : template, preloadedQuery : preloadedQuery, localVariables : localVars, fromIndex : index, toIndex : index, cultureString : args.Culture, cancellation : cancellation); // Add the versions preview.Version = ApplicationFactServiceBehavior.GetEmailCommandPreviewVersion(preview); if (preview.Emails.Count > 0) { var email = preview.Emails[0]; email.Version = ApplicationFactServiceBehavior.GetEmailPreviewVersion(email); } return(preview); }
/// <summary> /// Returns a template-generated text file that is evaluated based on the given <paramref name="templateId"/>. /// The text generation will implicitly contain a variable $ that evaluates to the results of the query specified in <paramref name="args"/>. /// </summary> public async Task <FileResult> PrintEntities(int templateId, PrintEntitiesArguments <TKey> args, CancellationToken cancellation) { await Initialize(cancellation); var collection = typeof(TEntity).Name; var defId = DefinitionId; // (1) The Template Plan var template = await FactBehavior.GetPrintingTemplate(templateId, cancellation); var nameP = new TemplatePlanLeaf(template.DownloadName, TemplateLanguage.Text); var bodyP = new TemplatePlanLeaf(template.Body, TemplateLanguage.Html); var printoutP = new TemplatePlanTuple(nameP, bodyP); TemplatePlan plan; if (string.IsNullOrWhiteSpace(template.Context)) { plan = printoutP; } else { plan = new TemplatePlanDefine("$", template.Context, printoutP); } QueryInfo contextQuery = BaseUtil.EntitiesPreloadedQuery(args, collection, defId); plan = new TemplatePlanDefineQuery("$", contextQuery, plan); // (2) Functions + Variables var globalFunctions = new Dictionary <string, EvaluationFunction>(); var localFunctions = new Dictionary <string, EvaluationFunction>(); var globalVariables = new Dictionary <string, EvaluationVariable>(); var localVariables = BaseUtil.EntitiesLocalVariables(args, collection, defId); await FactBehavior.SetPrintingFunctions(localFunctions, globalFunctions, cancellation); await FactBehavior.SetPrintingVariables(localVariables, globalVariables, cancellation); // (3) Generate the output CultureInfo culture = GetCulture(args.Culture); var genArgs = new TemplateArguments(globalFunctions, globalVariables, localFunctions, localVariables, culture); await _templateService.GenerateFromPlan(plan : plan, args : genArgs, cancellation : cancellation); var downloadName = nameP.Outputs[0]; var body = bodyP.Outputs[0]; // Change the body to bytes var bodyBytes = Encoding.UTF8.GetBytes(body); // Use a default download name if none is provided if (string.IsNullOrWhiteSpace(downloadName)) { var meta = await GetMetadata(cancellation); var titlePlural = meta.PluralDisplay(); if (args.I != null && args.I.Count > 0) { downloadName = $"{titlePlural} ({args.I.Count})"; } else { int from = args.Skip + 1; int to = Math.Max(from, args.Skip + args.Top); downloadName = $"{titlePlural} {from}-{to}"; } } if (!downloadName.ToLower().EndsWith(".html")) { downloadName += ".html"; } // Return as a file return(new FileResult(bodyBytes, downloadName)); }
// Studio Preview public async Task <EmailCommandPreview> EmailCommandPreviewEntities(EmailTemplate template, PrintEntitiesArguments <int> args, CancellationToken cancellation) { await Initialize(cancellation); if (template == null) { throw new ServiceException($"Email template was not supplied."); } await FillNavigationProperties(template, cancellation); int index = 0; var preloadedQuery = BaseUtil.EntitiesPreloadedQuery(args: args, collection: template.Collection, defId: template.DefinitionId); var localVars = BaseUtil.EntitiesLocalVariables(args: args, collection: template.Collection, defId: template.DefinitionId); return(await _behavior.CreateEmailCommandPreview( template : template, preloadedQuery : preloadedQuery, localVariables : localVars, fromIndex : index, toIndex : index, cultureString : args.Culture, cancellation : cancellation)); }
public async Task <ActionResult <PrintPreviewResponse> > PreviewByFilter([FromBody] PrintingPreviewTemplate entity, [FromQuery] PrintEntitiesArguments <int> args, CancellationToken cancellation) { var result = await _service.PreviewByFilter(entity, args, cancellation); // Prepare and return the response var response = new PrintPreviewResponse { Body = result.Body, DownloadName = result.DownloadName }; return(Ok(response)); }
/// <summary> /// Create the local variables dictionary used for template generation /// based on a collection of entities. /// </summary> public static Dictionary <string, EvaluationVariable> EntitiesLocalVariables <TKey>(PrintEntitiesArguments <TKey> args, string collection, int?defId) { return(new Dictionary <string, EvaluationVariable> { ["$Source"] = new EvaluationVariable($"{collection}/{defId}"), ["$Filter"] = new EvaluationVariable(args.Filter), ["$OrderBy"] = new EvaluationVariable(args.OrderBy), ["$Top"] = new EvaluationVariable(args.Top), ["$Skip"] = new EvaluationVariable(args.Skip), ["$Ids"] = new EvaluationVariable(args.I) }); }