Last active
December 20, 2015 05:59
-
-
Save jbubriski/6082127 to your computer and use it in GitHub Desktop.
Auto generate Kentico custom table classes for each one in the database. Yes, I know the formatting is awful. For using this with a project, change the Host.Resolve() path, and change it to app.config instead of the web.config.
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.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" #> | |
<# | |
var namespaceName = "CurriculumAssociates.Models.CustomTables"; | |
#> | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
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_Class | |
WHERE ClassIsCustomTable = 1"; | |
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); | |
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); | |
#> | |
public class <#= displayName #>Item : CustomTableItem | |
{ | |
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 #>Item() : base(CLASS_NAME) | |
{ | |
} | |
} | |
<# | |
} | |
} | |
} | |
#> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment