예제 #1
0
        TextTemplateParser.BlockNode TextNode(EmailTemplateMessageEmbedded message)
        {
            if (message.TextParsedNode == null)
            {
                string body = message.Text;

                if (template.MasterTemplate != null)
                {
                    var emt  = template.MasterTemplate.RetrieveAndRemember();
                    var emtm =
                        emt.GetCultureMessage(message.CultureInfo.ToCultureInfo()) ??
                        emt.GetCultureMessage(message.CultureInfo.ToCultureInfo().Parent) ??
                        emt.GetCultureMessage(EmailLogic.Configuration.DefaultCulture.ToCultureInfo()) ??
                        emt.GetCultureMessage(EmailLogic.Configuration.DefaultCulture.ToCultureInfo().Parent);

                    if (emtm != null)
                    {
                        body = EmailMasterTemplateEntity.MasterTemplateContentRegex.Replace(emtm.Text, m => body);
                    }
                }

                message.TextParsedNode = TextTemplateParser.Parse(body, qd, template.Model?.ToType());
            }

            return((TextTemplateParser.BlockNode)message.TextParsedNode);
        }
예제 #2
0
        static void Newsletter_PreSaving(NewsletterEntity newsletter, PreSavingContext ctx)
        {
            var queryname       = QueryLogic.ToQueryName(newsletter.Query !.Key);
            QueryDescription qd = QueryLogic.Queries.QueryDescription(queryname);

            newsletter.Subject = TextTemplateParser.Parse(newsletter.Subject, qd, null).ToString();
            newsletter.Text    = TextTemplateParser.Parse(newsletter.Text, qd, null).ToString();
        }
예제 #3
0
        public void Execute(ExecutingProcess executingProcess)
        {
            NewsletterEntity newsletter = (NewsletterEntity)executingProcess.Data !;

            var queryName = QueryLogic.ToQueryName(newsletter.Query !.Key);

            QueryDescription qd = QueryLogic.Queries.QueryDescription(queryName);

            List <QueryToken> list = new List <QueryToken>();

            using (ExecutionMode.Global())
            {
                list.Add(QueryUtils.Parse("Entity", qd, 0));
                list.Add(QueryUtils.Parse(".".Combine("Entity", "NewsletterDeliveries", "Element"), qd, SubTokensOptions.CanElement));
                list.Add(QueryUtils.Parse(".".Combine("Entity", "EmailOwnerData"), qd, 0));

                TextTemplateParser.Parse(newsletter.Subject, qd, null).FillQueryTokens(list);
                TextTemplateParser.Parse(newsletter.Text, qd, null).FillQueryTokens(list);

                list = list.Distinct().ToList();
            }

            var columns = list.Select(qt => new Column(qt, null)).ToList();

            //var columns = new List<QueryToken>();
            //columns.Add(QueryUtils.Parse("Entity.NewsletterDeliveries.Element", qd, canAggregate: false));
            //columns.Add(QueryUtils.Parse("Entity.Email", qd, canAggregate: false));
            //columns.AddRange(NewsletterLogic.GetTokens(queryName, newsletter.Subject));
            //columns.AddRange(NewsletterLogic.GetTokens(queryName, newsletter.Text));

            columns = columns.Distinct().ToList();

            var resultTable = QueryLogic.Queries.ExecuteQuery(new QueryRequest
            {
                QueryName = queryName,
                Filters   = new List <Filter>
                {
                    new FilterCondition(QueryUtils.Parse("Entity.NewsletterDeliveries.Element.Newsletter", qd, SubTokensOptions.CanElement), FilterOperation.EqualTo, newsletter.ToLite()),
                    new FilterCondition(QueryUtils.Parse("Entity.NewsletterDeliveries.Element.Sent", qd, SubTokensOptions.CanElement), FilterOperation.EqualTo, false),
                },
                Orders     = new List <Order>(),
                Columns    = columns,
                Pagination = new Pagination.All(),
            });

            var dicTokenColumn = resultTable.Columns.ToDictionary(rc => rc.Column.Token);

            var entityColumn     = resultTable.Columns.SingleEx(c => c.Column.Token.FullKey() == "Entity");
            var deliveryColumn   = resultTable.Columns.SingleEx(c => c.Column.Token.FullKey() == "Entity.NewsletterDeliveries.Element");
            var emailOwnerColumn = resultTable.Columns.SingleEx(c => c.Column.Token.FullKey() == "Entity.EmailOwnerData");

            var lines = resultTable.Rows.GroupBy(r => (Lite <Entity>)r[entityColumn] !).Select(g => new SendLine
            {
                NewsletterDelivery = (Lite <NewsletterDeliveryEntity>)g.DistinctSingle(deliveryColumn) !,
                Email = (EmailOwnerData)g.DistinctSingle(emailOwnerColumn) !,
                Rows  = g,
            }).ToList();
예제 #4
0
        TextTemplateParser.BlockNode SubjectNode(EmailTemplateMessageEmbedded message)
        {
            if (message.SubjectParsedNode == null)
            {
                message.SubjectParsedNode = TextTemplateParser.Parse(message.Subject, qd, template.Model?.ToType());
            }

            return((TextTemplateParser.BlockNode)message.SubjectParsedNode);
        }
예제 #5
0
        private static string GetTemplateString(string?title, ref object?titleNode, EmailTemplateLogic.GenerateAttachmentContext ctx)
        {
            var block = titleNode != null ? (TextTemplateParser.BlockNode)titleNode :
                        (TextTemplateParser.BlockNode)(titleNode = TextTemplateParser.Parse(title, ctx.QueryDescription, ctx.ModelType));

            return(block.Print(new TextTemplateParameters(ctx.Entity, ctx.Culture, ctx.ResultColumns, ctx.CurrentRows)
            {
                Model = ctx.Model
            }));
        }
예제 #6
0
        public static void Start(SchemaBuilder sb)
        {
            sb.Include <ExcelAttachmentEntity>()
            .WithQuery(() => s => new
            {
                Entity = s,
                s.Id,
                s.FileName,
                s.UserQuery,
                s.Related,
            });

            EmailTemplateLogic.FillAttachmentTokens.Register((ExcelAttachmentEntity ea, EmailTemplateLogic.FillAttachmentTokenContext ctx) =>
            {
                if (ea.FileName != null)
                {
                    TextTemplateParser.Parse(ea.FileName, ctx.QueryDescription, ctx.ModelType).FillQueryTokens(ctx.QueryTokens);
                }

                if (ea.Title != null)
                {
                    TextTemplateParser.Parse(ea.Title, ctx.QueryDescription, ctx.ModelType).FillQueryTokens(ctx.QueryTokens);
                }
            });

            Validator.PropertyValidator((ExcelAttachmentEntity e) => e.FileName).StaticPropertyValidation = ExcelAttachmentFileName_StaticPropertyValidation;
            Validator.PropertyValidator((ExcelAttachmentEntity e) => e.Title).StaticPropertyValidation    = ExcelAttachmentTitle_StaticPropertyValidation;

            EmailTemplateLogic.GenerateAttachment.Register((ExcelAttachmentEntity ea, EmailTemplateLogic.GenerateAttachmentContext ctx) =>
            {
                var finalEntity = ea.Related?.RetrieveAndRemember() ?? (Entity?)ctx.Entity ?? ctx.Model !.UntypedEntity as Entity;

                using (finalEntity == null ? null : CurrentEntityConverter.SetCurrentEntity(finalEntity))
                    using (CultureInfoUtils.ChangeBothCultures(ctx.Culture))
                    {
                        QueryRequest request = UserQueryLogic.ToQueryRequest(ea.UserQuery.RetrieveAndRemember());

                        var title    = GetTemplateString(ea.Title, ref ea.TitleNode, ctx);
                        var fileName = GetTemplateString(ea.FileName, ref ea.FileNameNode, ctx);

                        var bytes = ExcelLogic.ExecutePlainExcel(request, title);

                        return(new List <EmailAttachmentEmbedded>
                        {
                            new EmailAttachmentEmbedded
                            {
                                File = Files.FilePathEmbeddedLogic.SaveFile(new Entities.Files.FilePathEmbedded(EmailFileType.Attachment, fileName, bytes)),
                                Type = EmailAttachmentType.Attachment,
                            }
                        });
                    }
            });
        }
예제 #7
0
        public static void Start(SchemaBuilder sb)
        {
            sb.Include <WordAttachmentEntity>()
            .WithQuery(() => s => new
            {
                Entity = s,
                s.Id,
                s.FileName,
                s.WordTemplate,
                s.OverrideModel,
            });

            EmailTemplateLogic.FillAttachmentTokens.Register((WordAttachmentEntity wa, EmailTemplateLogic.FillAttachmentTokenContext ctx) =>
            {
                if (wa.FileName != null)
                {
                    TextTemplateParser.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?.RetrieveAndRemember() ?? (Entity?)ctx.Entity ?? ctx.Model !.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 model = template.Model != null && !WordModelLogic.RequiresExtraParameters(template.Model) ?
                                WordModelLogic.CreateDefaultWordModel(template.Model, entity) : null;

                    var bytes = WordTemplateLogic.CreateReport(template, entity, model);

                    return(new List <EmailAttachmentEmbedded>
                    {
                        new EmailAttachmentEmbedded
                        {
                            File = Files.FilePathEmbeddedLogic.SaveFile(new Entities.Files.FilePathEmbedded(EmailFileType.Attachment, fileName, bytes)),
                            Type = EmailAttachmentType.Attachment,
                        }
                    });
                }
            });
        }
예제 #8
0
        static void EmailTemplate_PreSaving(SMSTemplateEntity smsTemplate, PreSavingContext ctx)
        {
            using (smsTemplate.DisableAuthorization ? ExecutionMode.Global() : null)
            {
                var queryName       = QueryLogic.ToQueryName(smsTemplate.Query.Key);
                QueryDescription qd = QueryLogic.Queries.QueryDescription(queryName);

                List <QueryToken> list = new List <QueryToken>();

                foreach (var message in smsTemplate.Messages)
                {
                    message.Message = TextTemplateParser.Parse(message.Message, qd, smsTemplate.Model?.ToType()).ToString();
                }
            }
        }
예제 #9
0
        public static SMSMessageEntity CreateSMSMessage(Lite <SMSTemplateEntity> template, Entity?entity, ISMSModel?model, CultureInfo?forceCulture)
        {
            var t = SMSLogic.SMSTemplatesLazy.Value.GetOrThrow(template);

            var defaultCulture = SMSLogic.Configuration.DefaultCulture.ToCultureInfo();

            if (t.Query != null)
            {
                var qd = QueryLogic.Queries.QueryDescription(t.Query.ToQueryName());

                List <QueryToken> tokens = new List <QueryToken>();
                t.ParseData(qd);

                tokens.Add(t.To !.Token);

                var parsedNodes = t.Messages.ToDictionary(
                    tm => tm.CultureInfo.ToCultureInfo(),
                    tm => TextTemplateParser.Parse(tm.Message, qd, t.Model?.ToType())
                    );

                parsedNodes.Values.ToList().ForEach(n => n.FillQueryTokens(tokens));

                var columns = tokens.Distinct().Select(qt => new Column(qt, null)).ToList();

                var filters = model != null?model.GetFilters(qd) :
                                  new List <Filter>
                {
                    new FilterCondition(QueryUtils.Parse("Entity", qd, 0), FilterOperation.EqualTo, entity !.ToLite())
                };


                var table = QueryLogic.Queries.ExecuteQuery(new QueryRequest
                {
                    QueryName  = qd.QueryName,
                    Columns    = columns,
                    Pagination = model?.GetPagination() ?? new Pagination.All(),
                    Filters    = filters,
                    Orders     = model?.GetOrders(qd) ?? new List <Order>(),
                });

                var columnTokens = table.Columns.ToDictionary(a => a.Column.Token);

                var ownerData = (SMSOwnerData)table.Rows[0][columnTokens.GetOrThrow(t.To !.Token)] !;
예제 #10
0
    public static void Start(SchemaBuilder sb)
    {
        sb.Settings.AssertImplementedBy((EmailTemplateEntity e) => e.Attachments.First(), typeof(ImageAttachmentEntity));

        sb.Include <ImageAttachmentEntity>()
        .WithQuery(() => s => new
        {
            Entity = s,
            s.Id,
            s.FileName,
            s.ContentId
        });

        Validator.PropertyValidator((ImageAttachmentEntity e) => e.FileName).StaticPropertyValidation = ImageAttachmentFileName_StaticPropertyValidation;

        EmailTemplateLogic.FillAttachmentTokens.Register((ImageAttachmentEntity wa, EmailTemplateLogic.FillAttachmentTokenContext ctx) =>
        {
            if (wa.FileName != null)
            {
                TextTemplateParser.Parse(wa.FileName, ctx.QueryDescription, ctx.ModelType).FillQueryTokens(ctx.QueryTokens);
            }
        });

        EmailTemplateLogic.GenerateAttachment.Register((ImageAttachmentEntity a, EmailTemplateLogic.GenerateAttachmentContext ctx) =>
        {
            using (CultureInfoUtils.ChangeBothCultures(ctx.Culture))
            {
                var fileName = !a.FileName.HasText() ? a.File.FileName : GetTemplateString(a.FileName, ref a.FileNameNode, ctx);

                return(new List <EmailAttachmentEmbedded>
                {
                    new EmailAttachmentEmbedded
                    {
                        File = new FilePathEmbedded(EmailFileType.Attachment, fileName, a.File.BinaryFile),
                        Type = a.Type,
                        ContentId = a.ContentId,
                    }
                });
            }
        });
    }
예제 #11
0
        public IEnumerable <EmailMessageEntity> CreateEmailMessageInternal()
        {
            ExecuteQuery();

            foreach (EmailAddressEmbedded from in GetFrom())
            {
                foreach (List <EmailOwnerRecipientData> recipients in GetRecipients())
                {
                    CultureInfo ci = recipients.Where(a => a.Kind == EmailRecipientKind.To).Select(a => a.OwnerData.CultureInfo).FirstOrDefault()?.ToCultureInfo() ??
                                     EmailLogic.Configuration.DefaultCulture.ToCultureInfo();

                    EmailMessageEntity email = new EmailMessageEntity
                    {
                        Target     = entity?.ToLite() ?? (this.model !.UntypedEntity as Entity)?.ToLite(),
                        Recipients = recipients.Select(r => new EmailRecipientEmbedded(r.OwnerData)
                        {
                            Kind = r.Kind
                        }).ToMList(),
                        From            = from,
                        IsBodyHtml      = template.IsBodyHtml,
                        EditableMessage = template.EditableMessage,
                        Template        = template.ToLite(),
                        Attachments     = template.Attachments.SelectMany(g => EmailTemplateLogic.GenerateAttachment.Invoke(g,
                                                                                                                            new EmailTemplateLogic.GenerateAttachmentContext(this.qd, template, dicTokenColumn, currentRows, ci)
                        {
                            ModelType = template.Model?.ToType(),
                            Model     = model,
                            Entity    = entity,
                        })).ToMList()
                    };

                    EmailTemplateMessageEmbedded message = template.GetCultureMessage(ci) ?? template.GetCultureMessage(EmailLogic.Configuration.DefaultCulture.ToCultureInfo());

                    if (message == null)
                    {
                        throw new InvalidOperationException("Message {0} does not have a message for CultureInfo {1} (or Default)".FormatWith(template, ci));
                    }

                    using (CultureInfoUtils.ChangeBothCultures(ci))
                    {
                        email.Subject = SubjectNode(message).Print(
                            new TextTemplateParameters(entity, ci, dicTokenColumn, currentRows)
                        {
                            IsHtml = false,
                            Model  = model
                        });

                        email.Body = TextNode(message).Print(
                            new TextTemplateParameters(entity, ci, dicTokenColumn, currentRows)
                        {
                            IsHtml = template.IsBodyHtml,
                            Model  = model,
                        });
                    }


                    yield return(email);
                }
            }
        }

        TextTemplateParser.BlockNode TextNode(EmailTemplateMessageEmbedded message)
        {
            if (message.TextParsedNode == null)
            {
                string body = message.Text;

                if (template.MasterTemplate != null)
                {
                    var emt  = template.MasterTemplate.RetrieveAndRemember();
                    var emtm = emt.GetCultureMessage(message.CultureInfo.ToCultureInfo()) ??
                               emt.GetCultureMessage(EmailLogic.Configuration.DefaultCulture.ToCultureInfo());

                    if (emtm != null)
                    {
                        body = EmailMasterTemplateEntity.MasterTemplateContentRegex.Replace(emtm.Text, m => body);
                    }
                }

                message.TextParsedNode = TextTemplateParser.Parse(body, qd, template.Model?.ToType());
            }

            return((TextTemplateParser.BlockNode)message.TextParsedNode);
        }

        TextTemplateParser.BlockNode SubjectNode(EmailTemplateMessageEmbedded message)
        {
            if (message.SubjectParsedNode == null)
            {
                message.SubjectParsedNode = TextTemplateParser.Parse(message.Subject, qd, template.Model?.ToType());
            }

            return((TextTemplateParser.BlockNode)message.SubjectParsedNode);
        }

        IEnumerable <EmailAddressEmbedded> GetFrom()
        {
            if (template.From != null)
            {
                if (template.From.Token != null)
                {
                    ResultColumn owner = dicTokenColumn.GetOrThrow(template.From.Token.Token);

                    if (!template.SendDifferentMessages)
                    {
                        yield return(new EmailAddressEmbedded(currentRows.Select(r => (EmailOwnerData)r[owner] !).Distinct().SingleEx()));
                    }
                    else
                    {
                        var groups = currentRows.GroupBy(r => (EmailOwnerData)r[owner] !);

                        if (groups.Count() == 1 && groups.Single().Key?.Owner == null)
                        {
                            yield break;
                        }
                        else
                        {
                            foreach (var gr in groups)
                            {
                                var old = currentRows;
                                currentRows = gr;

                                yield return(new EmailAddressEmbedded(gr.Key));

                                currentRows = old;
                            }
                        }
                    }
                }
                else
                {
                    yield return(new EmailAddressEmbedded(new EmailOwnerData
                    {
                        CultureInfo = null,
                        Email = template.From.EmailAddress !,
                        DisplayName = template.From.DisplayName,
                    }));
예제 #12
0
        public static SMSMessageEntity CreateSMSMessage(Lite <SMSTemplateEntity> template, Entity entity, ISMSModel?model, CultureInfo?forceCulture)
        {
            var t = SMSLogic.SMSTemplatesLazy.Value.GetOrThrow(template);

            var defaultCulture = SMSLogic.Configuration.DefaultCulture.ToCultureInfo();

            var qd = QueryLogic.Queries.QueryDescription(t.Query.ToQueryName());

            List <QueryToken> tokens = new List <QueryToken>();

            t.ParseData(qd);

            tokens.Add(t.To.Token);
            var parsedNodes = t.Messages.ToDictionary(
                tm => tm.CultureInfo.ToCultureInfo(),
                tm => TextTemplateParser.Parse(tm.Message, qd, t.Model?.ToType())
                );

            parsedNodes.Values.ToList().ForEach(n => n.FillQueryTokens(tokens));

            var columns = tokens.Distinct().Select(qt => new Column(qt, null)).ToList();

            var filters = model != null?model.GetFilters(qd) :
                              new List <Filter>
            {
                new FilterCondition(QueryUtils.Parse("Entity", qd, 0), FilterOperation.EqualTo, entity.ToLite())
            };


            var table = QueryLogic.Queries.ExecuteQuery(new QueryRequest
            {
                QueryName  = qd.QueryName,
                Columns    = columns,
                Pagination = model?.GetPagination() ?? new Pagination.All(),
                Filters    = filters,
                Orders     = model?.GetOrders(qd) ?? new List <Order>(),
            });

            var columnTokens = table.Columns.ToDictionary(a => a.Column.Token);

            var ownerData = (SMSOwnerData)table.Rows[0][columnTokens.GetOrThrow(t.To.Token)] !;

            var ci = forceCulture ?? ownerData.CultureInfo?.ToCultureInfo() ?? defaultCulture;

            var node = parsedNodes.TryGetC(ci) ?? parsedNodes.GetOrThrow(defaultCulture);

            return(new SMSMessageEntity
            {
                Template = t.ToLite(),
                Message = node.Print(new TextTemplateParameters(entity, ci, columnTokens, table.Rows)
                {
                    Model = model
                }),
                From = t.From,
                EditableMessage = t.EditableMessage,
                State = SMSMessageState.Created,
                Referred = ownerData.Owner,
                DestinationNumber = ownerData.TelephoneNumber,
                Certified = t.Certified
            });
        }