Last active
August 29, 2015 13:56
T4 templates for auto-generating strongly typed classes for Kentico BizForms, and registering them with the CMS Type system.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<#@ template debug="True" hostspecific="True" language="C#" #> | |
<#@ output extension=".cs" #> | |
<#@ Assembly Name="System" #> | |
<#@ Assembly Name="System.Configuration" #> | |
<#@ assembly name="System.Core" #> | |
<#@ Assembly Name="System.Collections" #> | |
<#@ Assembly Name="System.Data" #> | |
<#@ Assembly Name="System.Linq" #> | |
<#@ Assembly Name="System.Xml" #> | |
<#@ Assembly Name="System.Xml.Linq" #> | |
<#@ assembly name="Microsoft.SqlServer.ConnectionInfo" #> | |
<#@ assembly name="Microsoft.SqlServer.Smo" #> | |
<#@ import namespace="Microsoft.SqlServer.Management.Smo" #> | |
<#@ import namespace="System.Collections" #> | |
<#@ import namespace="System.Collections.Generic" #> | |
<#@ Import Namespace="System.Configuration" #> | |
<#@ Import Namespace="System.Data" #> | |
<#@ import namespace="System.Data.SqlClient" #> | |
<#@ import namespace="System.Linq" #> | |
<#@ Import Namespace="System.Xml" #> | |
<#@ Import Namespace="System.Xml.Linq" #> | |
<#@ Import Namespace="System.Text" #> | |
<#@ Import Namespace="System.Text.RegularExpressions" #> | |
<# | |
var namespaceName = "CurriculumAssociates.Models.BizForms"; | |
#> | |
using System; | |
using System.Collections.Generic; | |
using System.Data; | |
using System.Linq; | |
using System.Text; | |
using CMS.FormEngine; | |
using CMS.GlobalHelper; | |
using CMS.SiteProvider; | |
namespace <#= namespaceName #> | |
{ | |
<# | |
var path = Host.ResolvePath(@".."); | |
var config = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap { ExeConfigFilename = path + @"\app.config" }, ConfigurationUserLevel.None); | |
var connectionString = config.ConnectionStrings.ConnectionStrings["CMSConnectionString"].ConnectionString; | |
var sqlCommandText = @"SELECT ClassDisplayName, ClassName, ClassFormDefinition | |
FROM CMS_Form f | |
JOIN CMS_Class c ON c.ClassID = f.FormClassID"; | |
using (var sqlConnection = new SqlConnection(connectionString)) | |
using (var sqlCommand = new SqlCommand(sqlCommandText, sqlConnection)) | |
{ | |
sqlCommand.Connection.Open(); | |
using (var reader = sqlCommand.ExecuteReader()) | |
{ | |
while (reader.Read()) | |
{ | |
var displayName = reader["ClassDisplayName"].ToString(); | |
var className = reader["ClassName"].ToString(); | |
var classFormDefinition = reader["ClassFormDefinition"].ToString(); | |
var formDefinitionXml = XDocument.Parse(classFormDefinition); | |
displayName = Regex.Replace(displayName, "[^A-Za-z ]", "").Trim(); | |
var displayNameItems = displayName.Split(' '); | |
for(var i = 0; i < displayNameItems.Length; i++) | |
{ | |
displayNameItems[i] = displayNameItems[i][0].ToString().ToUpper() + displayNameItems[i].Substring(1).ToLower(); | |
} | |
displayName = string.Join("", displayNameItems) + "Item"; | |
#> | |
public partial class <#= displayName #> : BizFormItem | |
{ | |
public const string CLASS_NAME = "<#= className #>"; | |
<# | |
foreach (var xField in formDefinitionXml.Elements("form").Elements("field")) | |
{ | |
var columnName = xField.Attribute("column").Value; | |
var columnType = xField.Attribute("columntype").Value; | |
var isSystem = xField.Attributes("system").Any(); | |
if (columnName != "ItemID" && !isSystem) | |
{ | |
if (columnType == "text") | |
{ | |
#> | |
public string <#= columnName #> | |
{ | |
get | |
{ | |
return ValidationHelper.GetString(GetValue("<#= columnName #>"), ""); | |
} | |
set | |
{ | |
SetValue("<#= columnName #>", value); | |
} | |
} | |
<# | |
} | |
else if (columnType == "integer") | |
{ | |
#> | |
public int <#= columnName #> | |
{ | |
get | |
{ | |
return ValidationHelper.GetInteger(GetValue("<#= columnName #>"), 0); | |
} | |
set | |
{ | |
SetValue("<#= columnName #>", value); | |
} | |
} | |
<# | |
} | |
else if (columnType == "double") | |
{ | |
#> | |
public decimal <#= columnName #> | |
{ | |
get | |
{ | |
return this.GetDecimalValue("<#= columnName #>", 0); | |
} | |
set | |
{ | |
SetValue("<#= columnName #>", value); | |
} | |
} | |
<# | |
} | |
else if (columnType == "boolean") | |
{ | |
#> | |
public bool <#= columnName #> | |
{ | |
get | |
{ | |
return ValidationHelper.GetBoolean(GetValue("<#= columnName #>"), false); | |
} | |
set | |
{ | |
SetValue("<#= columnName #>", value); | |
} | |
} | |
<# | |
} | |
else if (columnType == "guid") | |
{ | |
#> | |
public Guid <#= columnName #> | |
{ | |
get | |
{ | |
return ValidationHelper.GetGuid(GetValue("<#= columnName #>"), Guid.Empty); | |
} | |
set | |
{ | |
SetValue("<#= columnName #>", value); | |
} | |
} | |
<# | |
} | |
else if (columnType == "datetime") | |
{ | |
#> | |
public DateTime <#= columnName #> | |
{ | |
get | |
{ | |
return ValidationHelper.GetDateTime(GetValue("<#= columnName #>"), DateTime.MinValue); | |
} | |
set | |
{ | |
SetValue("<#= columnName #>", value); | |
} | |
} | |
<# | |
} | |
} | |
} | |
#> | |
public <#= displayName #>() : base(CLASS_NAME) | |
{ | |
} | |
public static <#= displayName #> GetItem(int itemId) | |
{ | |
var bizFormItemProvider = new BizFormItemProvider(); | |
var items = bizFormItemProvider.GetItems(<#= displayName #>.CLASS_NAME, "ItemId = " + itemId, null); | |
if (!DataHelper.DataSourceIsEmpty(items)) | |
{ | |
return <#= displayName #>.New<<#= displayName #>>(items.Tables[0].Rows[0], <#= displayName #>.CLASS_NAME, bizFormItemProvider); | |
} | |
return null; | |
} | |
public static List<<#= displayName #>> GetItems(string where) | |
{ | |
var bizFormItemProvider = new BizFormItemProvider(); | |
var items = bizFormItemProvider.GetItems(<#= displayName #>.CLASS_NAME, where, null); | |
var list = new List<<#= displayName #>>(); | |
if (!DataHelper.DataSourceIsEmpty(items)) | |
{ | |
foreach(DataRow dataRow in items.Tables[0].Rows) | |
{ | |
list.Add(<#= displayName #>.New<<#= displayName #>>(dataRow, <#= displayName #>.CLASS_NAME, bizFormItemProvider)); | |
} | |
return list; | |
} | |
return list; | |
} | |
} | |
<# | |
} | |
} | |
} | |
#> | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<#@ template debug="True" hostspecific="True" language="C#" #> | |
<#@ output extension=".cs" #> | |
<#@ Assembly Name="System" #> | |
<#@ Assembly Name="System.Configuration" #> | |
<#@ assembly name="System.Core" #> | |
<#@ Assembly Name="System.Collections" #> | |
<#@ Assembly Name="System.Data" #> | |
<#@ Assembly Name="System.Linq" #> | |
<#@ Assembly Name="System.Xml" #> | |
<#@ Assembly Name="System.Xml.Linq" #> | |
<#@ assembly name="Microsoft.SqlServer.ConnectionInfo" #> | |
<#@ assembly name="Microsoft.SqlServer.Smo" #> | |
<#@ import namespace="Microsoft.SqlServer.Management.Smo" #> | |
<#@ import namespace="System.Collections" #> | |
<#@ import namespace="System.Collections.Generic" #> | |
<#@ Import Namespace="System.Configuration" #> | |
<#@ Import Namespace="System.Data" #> | |
<#@ import namespace="System.Data.SqlClient" #> | |
<#@ import namespace="System.Linq" #> | |
<#@ Import Namespace="System.Xml" #> | |
<#@ Import Namespace="System.Xml.Linq" #> | |
<#@ Import Namespace="System.Text" #> | |
<#@ Import Namespace="System.Text.RegularExpressions" #> | |
<# | |
var namespaceName = "CurriculumAssociates.Models.BizForms"; | |
#> | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using CMS.DocumentEngine; | |
using CMS.FormEngine; | |
using CMS.GlobalHelper; | |
using CMS.SiteProvider; | |
using <#= namespaceName #>; | |
/// <summary> | |
/// Document type registration | |
/// </summary> | |
<# | |
var path = Host.ResolvePath(@"../../Web.config"); | |
var config = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap { ExeConfigFilename = path }, ConfigurationUserLevel.None); | |
var connectionString = config.ConnectionStrings.ConnectionStrings["CMSConnectionString"].ConnectionString; | |
var sqlCommandText = @"SELECT ClassDisplayName, ClassName, ClassFormDefinition | |
FROM CMS_Form f | |
JOIN CMS_Class c ON c.ClassID = f.FormClassID"; | |
using (var sqlConnection = new SqlConnection(connectionString)) | |
using (var sqlCommand = new SqlCommand(sqlCommandText, sqlConnection)) | |
{ | |
sqlCommand.Connection.Open(); | |
using (var reader = sqlCommand.ExecuteReader()) | |
{ | |
while (reader.Read()) | |
{ | |
var displayName = reader["ClassDisplayName"].ToString(); | |
var className = reader["ClassName"].ToString(); | |
var classFormDefinition = reader["ClassFormDefinition"].ToString(); | |
var formDefinitionXml = XDocument.Parse(classFormDefinition); | |
displayName = Regex.Replace(displayName, "[^A-Za-z ]", "").Trim(); | |
var displayNameItems = displayName.Split(' '); | |
for(var i = 0; i < displayNameItems.Length; i++) | |
{ | |
displayNameItems[i] = displayNameItems[i][0].ToString().ToUpper() + displayNameItems[i].Substring(1).ToLower(); | |
} | |
displayName = string.Join("", displayNameItems) + "Item"; | |
#> | |
[BizForm(<#= displayName #>.CLASS_NAME, typeof(<#= displayName #>))] | |
<# | |
} | |
} | |
} | |
#> | |
public partial class CMSModuleLoader | |
{ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment