Ejemplo n.º 1
0
        static SqlPreCommand?Schema_Synchronizing_DefaultTemplates(Replacements replacements)
        {
            if (AvoidSynchronizeDefaultTemplates)
            {
                return(null);
            }

            var table = Schema.Current.Table(typeof(EmailTemplateEntity));

            var emailModels = Database.Query <EmailModelEntity>().Where(se => !se.EmailTemplates().Any()).ToList();

            string cis = Database.Query <CultureInfoEntity>().Select(a => a.Name).ToString(", ").Etc(60);

            if (!emailModels.Any())
            {
                return(null);
            }

            if (!replacements.Interactive || !SafeConsole.Ask("{0}\r\n have no EmailTemplates. Create in {1}?".FormatWith(emailModels.ToString("\r\n"), cis.DefaultText("No CultureInfos registered!"))))
            {
                return(null);
            }

            using (replacements.WithReplacedDatabaseName())
            {
                var cmd = emailModels.Select(se =>
                {
                    try
                    {
                        return(table.InsertSqlSync(EmailModelLogic.CreateDefaultTemplate(se), includeCollections: true));
                    }
                    catch (Exception e)
                    {
                        return(new SqlPreCommandSimple("Exception on SystemEmail {0}: {1}".FormatWith(se, e.Message)));
                    }
                }).Combine(Spacing.Double);

                if (cmd != null)
                {
                    return(SqlPreCommand.Combine(Spacing.Double, new SqlPreCommandSimple("DECLARE @parentId INT"), cmd));
                }

                return(cmd);
            }
        }
Ejemplo n.º 2
0
        public static void GenerateDefaultTemplates()
        {
            var systemEmails = Database.Query <EmailModelEntity>().Where(se => !se.EmailTemplates().Any()).ToList();

            List <string> exceptions = new List <string>();

            foreach (var se in systemEmails)
            {
                try
                {
                    EmailModelLogic.CreateDefaultTemplate(se).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"));
            }
        }