public static void GenerateDefaultTemplates() { var systemWordTemplates = Database.Query <SystemWordTemplateEntity>().Where(se => !se.WordTemplates().Any()).ToList(); List <string> exceptions = new List <string>(); foreach (var se in systemWordTemplates) { try { var defaultTemplate = SystemWordTemplateLogic.CreateDefaultTemplate(se); if (defaultTemplate != null) { defaultTemplate.Save(); } } catch (Exception ex) { exceptions.Add("{0} in {1}:\r\n{2}".FormatWith(ex.GetType().Name, se.FullClassName, ex.Message.Indent(4))); } } if (exceptions.Any()) { throw new Exception(exceptions.ToString("\r\n\r\n")); } }
public static void Start(SchemaBuilder sb, DynamicQueryManager dqm) { sb.Include <WordAttachmentEntity>() .WithQuery(dqm, () => s => new { Entity = s, s.Id, s.FileName, s.WordTemplate, s.OverrideModel, }); EmailTemplateLogic.FillAttachmentTokens.Register((WordAttachmentEntity wa, EmailTemplateLogic.FillAttachmentTokenContext ctx) => { if (wa.FileName != null) { EmailTemplateParser.Parse(wa.FileName, ctx.QueryDescription, ctx.ModelType).FillQueryTokens(ctx.QueryTokens); } }); Validator.PropertyValidator((WordAttachmentEntity e) => e.FileName).StaticPropertyValidation = WordAttachmentFileName_StaticPropertyValidation; EmailTemplateLogic.GenerateAttachment.Register((WordAttachmentEntity wa, EmailTemplateLogic.GenerateAttachmentContext ctx) => { var entity = wa.OverrideModel?.Retrieve() ?? (Entity)ctx.Entity ?? ctx.SystemEmail.UntypedEntity; if (wa.ModelConverter != null) { entity = wa.ModelConverter.Convert(entity); } using (CultureInfoUtils.ChangeBothCultures(ctx.Culture)) { WordTemplateEntity template = WordTemplateLogic.GetFromCache(wa.WordTemplate); var fileName = GetTemplateString(wa.FileName, ref wa.FileNameNode, ctx); var systemWordTemplate = template.SystemWordTemplate != null && !SystemWordTemplateLogic.RequiresExtraParameters(template.SystemWordTemplate) ? SystemWordTemplateLogic.CreateDefaultSystemWordTemplate(template.SystemWordTemplate, entity) : null; var bytes = WordTemplateLogic.CreateReport(template, entity, systemWordTemplate); return(new List <EmailAttachmentEmbedded> { new EmailAttachmentEmbedded { File = Files.FilePathEmbeddedLogic.SaveFile(new Entities.Files.FilePathEmbedded(EmailFileType.Attachment, fileName, bytes)), Type = EmailAttachmentType.Attachment, } }); } }); }
private static SqlPreCommand Regenerate(WordTemplateEntity template, Replacements replacements) { var newTemplate = SystemWordTemplateLogic.CreateDefaultTemplate(template.SystemWordTemplate); if (newTemplate == null) { return(null); } var file = template.Template.Retrieve(); using (file.AllowChanges()) { file.BinaryFile = newTemplate.Template.Entity.BinaryFile; file.FileName = newTemplate.Template.Entity.FileName; return(Schema.Current.Table <FileEntity>().UpdateSqlSync(file, comment: "WordTemplate Regenerated: " + template.Name)); } }
public static bool IsVisible(WordTemplateEntity wt, WordTemplateVisibleOn visibleOn) { if (wt.SystemWordTemplate == null) { return(visibleOn == WordTemplateVisibleOn.Single); } if (SystemWordTemplateLogic.HasDefaultTemplateConstructor(wt.SystemWordTemplate)) { return(false); } var entityType = SystemWordTemplateLogic.GetEntityType(wt.SystemWordTemplate.ToType()); if (entityType.IsEntity()) { return(visibleOn == WordTemplateVisibleOn.Single); } var should = VisibleOnDictionary.TryGet(entityType, WordTemplateVisibleOn.Single); return((should & visibleOn) != 0); }
public static void Start(SchemaBuilder sb, DynamicQueryManager dqm) { if (sb.NotDefined(MethodInfo.GetCurrentMethod())) { sb.Include <WordTemplateEntity>() .WithSave(WordTemplateOperation.Save) .WithDelete(WordTemplateOperation.Delete) .WithQuery(dqm, () => e => new { Entity = e, e.Id, e.Name, e.Query, e.Culture, e.Template.Entity.FileName }); PermissionAuthLogic.RegisterPermissions(WordTemplatePermission.GenerateReport); SystemWordTemplateLogic.Start(sb, dqm); SymbolLogic <WordTransformerSymbol> .Start(sb, dqm, () => Transformers.Keys.ToHashSet()); SymbolLogic <WordConverterSymbol> .Start(sb, dqm, () => Converters.Keys.ToHashSet()); sb.Include <WordTransformerSymbol>() .WithQuery(dqm, () => f => new { Entity = f, f.Key }); sb.Include <WordConverterSymbol>() .WithQuery(dqm, () => f => new { Entity = f, f.Key }); ToDataTableProviders.Add("Model", new ModelDataTableProvider()); ToDataTableProviders.Add("UserQuery", new UserQueryDataTableProvider()); ToDataTableProviders.Add("UserChart", new UserChartDataTableProvider()); dqm.RegisterExpression((SystemWordTemplateEntity e) => e.WordTemplates(), () => typeof(WordTemplateEntity).NiceName()); new Graph <WordTemplateEntity> .Execute(WordTemplateOperation.CreateWordReport) { CanExecute = et => { if (et.SystemWordTemplate != null && SystemWordTemplateLogic.RequiresExtraParameters(et.SystemWordTemplate)) { return(WordTemplateMessage._01RequiresExtraParameters.NiceToString(typeof(SystemWordTemplateEntity).NiceName(), et.SystemWordTemplate)); } return(null); }, Execute = (et, args) => { throw new InvalidOperationException("UI-only operation"); } } .Register(); WordTemplatesLazy = sb.GlobalLazy(() => Database.Query <WordTemplateEntity>() .ToDictionary(et => et.ToLite()), new InvalidateWith(typeof(WordTemplateEntity))); TemplatesByQueryName = sb.GlobalLazy(() => { return(WordTemplatesLazy.Value.Values.GroupToDictionary(a => a.Query.ToQueryName())); }, new InvalidateWith(typeof(WordTemplateEntity))); TemplatesByEntityType = sb.GlobalLazy(() => { return((from wr in WordTemplatesLazy.Value.Values let imp = DynamicQueryManager.Current.GetEntityImplementations(wr.Query.ToQueryName()) where !imp.IsByAll from t in imp.Types select KVP.Create(t, wr)) .GroupToDictionary(a => a.Key, a => a.Value)); }, new InvalidateWith(typeof(WordTemplateEntity))); Schema.Current.Synchronizing += Schema_Synchronize_Tokens; Validator.PropertyValidator((WordTemplateEntity e) => e.Template).StaticPropertyValidation += ValidateTemplate; } }
public static byte[] CreateReport(this WordTemplateEntity template, ModifiableEntity model = null, ISystemWordTemplate systemWordTemplate = null, bool avoidConversion = false) { WordTemplatePermission.GenerateReport.AssertAuthorized(); Entity entity = null; if (template.SystemWordTemplate != null) { if (systemWordTemplate == null) { systemWordTemplate = SystemWordTemplateLogic.CreateDefaultSystemWordTemplate(template.SystemWordTemplate, model); } else if (template.SystemWordTemplate.ToType() != systemWordTemplate.GetType()) { throw new ArgumentException("systemWordTemplate should be a {0} instead of {1}".FormatWith(template.SystemWordTemplate.FullClassName, systemWordTemplate.GetType().FullName)); } } else { entity = model as Entity ?? throw new InvalidOperationException("Model should be an Entity"); } using (template.DisableAuthorization ? ExecutionMode.Global() : null) using (CultureInfoUtils.ChangeBothCultures(template.Culture.ToCultureInfo())) { QueryDescription qd = DynamicQueryManager.Current.QueryDescription(template.Query.ToQueryName()); var array = template.ProcessOpenXmlPackage(document => { Dump(document, "0.Original.txt"); var parser = new TemplateParser(document, qd, template.SystemWordTemplate.ToType(), template); parser.ParseDocument(); Dump(document, "1.Match.txt"); parser.CreateNodes(); Dump(document, "2.BaseNode.txt"); parser.AssertClean(); if (parser.Errors.Any()) { throw new InvalidOperationException("Error in template {0}:\r\n".FormatWith(template) + parser.Errors.ToString(e => e.Message, "\r\n")); } var renderer = new TemplateRenderer(document, qd, entity, template.Culture.ToCultureInfo(), systemWordTemplate, template); renderer.MakeQuery(); renderer.RenderNodes(); Dump(document, "3.Replaced.txt"); renderer.AssertClean(); FixDocument(document); Dump(document, "4.Fixed.txt"); if (template.WordTransformer != null) { Transformers.GetOrThrow(template.WordTransformer)(new WordContext { Template = template, Entity = entity, SystemWordTemplate = systemWordTemplate }, document); } }); if (!avoidConversion && template.WordConverter != null) { array = Converters.GetOrThrow(template.WordConverter)(new WordContext { Template = template, Entity = entity, SystemWordTemplate = systemWordTemplate }, array); } return(array); } }
public static ISystemWordTemplate CreateDefaultSystemWordTemplate(SystemWordTemplateEntity systemWordTemplate, ModifiableEntity entity) { return((ISystemWordTemplate)SystemWordTemplateLogic.GetEntityConstructor(systemWordTemplate.ToType()).Invoke(new[] { entity })); }