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,
                        }
                    });
                }
            });
        }
示例#2
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;
            }
        }