public static string RenderContent(Template template, string Content, Hashtable templateItems) { string retVal = String.Empty; //List<ContentDataItem> Eminent.CodeGenerator.Template t = new Eminent.CodeGenerator.Template(); t.ClassName = "ContentTemplate"; t.Language = "C#"; t.Assemblies.Add("System.Data"); t.Namespaces.Add("System.Collections.Generic"); t.Namespaces.Add("System.Data"); t.Namespaces.Add("System.Linq"); t.ParseTemplate(Content); foreach (TemplateDataItem item in template.DataItems) { Eminent.CodeGenerator.Property p = new Eminent.CodeGenerator.Property(); p.Name = item.Name; p.Type = (item.Type == TemplateDataItemType.DataRow) ? "System.Data.DataRow" : "System.Data.DataRowCollection"; t.Properties.Add(p); } Eminent.CodeGenerator.TemplateEngine engine = new Eminent.CodeGenerator.TemplateEngine(); object templateObject = engine.Compile(t); if (engine.Errors.Count == 0) { //populate properties foreach (TemplateDataItem item in template.DataItems) { if ((templateItems.ContainsKey(item.Name)) && (templateItems[item.Name] != null)) { if (templateItems[item.Name] is DataTable) { DataTable data = (DataTable)templateItems[item.Name]; if (item.Type == TemplateDataItemType.DataRow) { if (data.Rows.Count > 0) { PropertyInfo itemProperty = templateObject.GetType().GetProperty(item.Name); itemProperty.SetValue(templateObject, data.Rows[0], null); } } else { if (data.Rows.Count > 0) { PropertyInfo itemProperty = templateObject.GetType().GetProperty(item.Name); itemProperty.SetValue(templateObject, data.Rows, null); } } } } } retVal = Eminent.CodeGenerator.TemplateEngine.GenerateCode(templateObject); } else { retVal = engine.GenerateCode(t); StringBuilder sb = new StringBuilder(); foreach (System.CodeDom.Compiler.CompilerError error in engine.Errors) { sb.AppendFormat("\r\n({0},{1}): {2} {3}: {4}", error.Line, error.Column, ((error.IsWarning) ? "warning" : "error"), error.ErrorNumber, error.ErrorText); } throw new ApplicationException(sb.ToString()); } return(retVal); }
private string GetBody(DataRow mail, Template contentTemplate, Hashtable dataItems) { string retVal = String.Empty; //use code generator to generate body message Eminent.CodeGenerator.Template t = new Eminent.CodeGenerator.Template(); t.ClassName = "EmailWorkflowActionTemplate"; t.Language = "C#"; t.Assemblies.Add("System.Data"); t.Namespaces.Add("System.Collections.Generic"); t.Namespaces.Add("System.Data"); t.Namespaces.Add("System.Linq"); t.ParseTemplate(contentTemplate.Content); var items = from ct in contentTemplate.DataItems join c in this.DataItems on ct.Name.ToLower() equals c.DataItem.ToLower() select new { ct.Name, ct.Type, Item = c }; foreach (var item in items) { Eminent.CodeGenerator.Property p = new Eminent.CodeGenerator.Property(); p.Name = item.Name; p.Type = (item.Type == TemplateDataItemType.DataRow) ? "System.Data.DataRow" : "System.Data.DataRowCollection"; t.Properties.Add(p); } Eminent.CodeGenerator.TemplateEngine engine = new Eminent.CodeGenerator.TemplateEngine(); object templateObject = engine.Compile(t); if (engine.Errors.Count == 0) { //populate properties foreach (var item in items) { if ((dataItems.ContainsKey(item.Name)) && (dataItems[item.Name] != null)) { if (dataItems[item.Name] is DataTable) { DataTable data = (DataTable)dataItems[item.Name]; if (item.Type == TemplateDataItemType.DataRow) { if (data.Rows.Count > 0) { DataRow[] rows = null; if (item.Item.ContentToAddressField != null) { //"(TO = '{0}') OR (TO IS NULL) rows = data.Select(String.Format("({0} = '{1}') OR ({0} IS NULL)", item.Item.ContentToAddressField, mail["TO"])); } else { rows = data.Select(); } if (rows.Length > 0) { PropertyInfo itemProperty = templateObject.GetType().GetProperty(item.Name); itemProperty.SetValue(templateObject, rows[0], null); } } } else { if (data.Rows.Count > 0) { DataRow[] rows = null; if (item.Item.ContentToAddressField != null) { //"(TO = '{0}') OR (TO IS NULL) rows = data.Select(String.Format("({0} = '{1}') OR ({0} IS NULL)", item.Item.ContentToAddressField, mail["TO"])); } else { rows = data.Select(); } List <DataRow> rowList = new List <DataRow>(); rowList = rows.ToList <DataRow>(); PropertyInfo itemProperty = templateObject.GetType().GetProperty(item.Name); itemProperty.SetValue(templateObject, rowList, null); } } } } } retVal = Eminent.CodeGenerator.TemplateEngine.GenerateCode(templateObject); } else { retVal = engine.GenerateCode(t); StringBuilder sb = new StringBuilder(); foreach (System.CodeDom.Compiler.CompilerError error in engine.Errors) { sb.AppendFormat("\r\n({0},{1}): {2} {3}: {4}", error.Line, error.Column, ((error.IsWarning) ? "warning" : "error"), error.ErrorNumber, error.ErrorText); } throw new ApplicationException(sb.ToString()); } return(retVal); }