-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathGlassItem.tt
210 lines (182 loc) · 6.31 KB
/
GlassItem.tt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<#@ template language="C#" #>
<#@ output encoding="utf-8"#>
<#@ include file="Helpers.tt" #>
<#@ include file="StringExtensions.tt" #>
<#@ include file="GeneralExtensions.tt" #>
<#@ include file="Inflector.tt" #>
<#@ assembly name="System.Core.dll" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="HedgehogDevelopment.SitecoreProject.VSIP.CodeGeneration.Models" #>
<#@ parameter name="Model" type="HedgehogDevelopment.SitecoreProject.VSIP.CodeGeneration.Models.SitecoreItem" #>
<#@ parameter name="DefaultNamespace" type="System.String" #>
<#
/*
This TDS Code Generation template is used to generate objects that are compatible with the
Glass Sitecore Mapper that is available @ http://www.glass.lu/
There are a few things you can put in the 'Custom Data' property of a field in TDS.
To use multiple settings put them in as a querystring (key1=value&key2=value)
ignore=true
Sets a field to be skipped over for code gen
name=[name]
Forces the name of the generated property.
If not specified, then the generated property is the name of the Sitecore field.
If the field stores multiple values, the property name is pluralized.
type=[type]
Sets the return type of the generated property
generic=[type]
In the event the type (either specificed or auto mapped) is a generic it will use this generic type. i.e. List<generic>
*/
#>
<#
// we only act on Templates
SitecoreTemplate template = Model as SitecoreTemplate;
if (template == null)
{
return "";
}
string Tool = "Team Development for Sitecore - GlassItem.tt";
string ToolVersion = "1.0";
#>
namespace <#= GetNamespace(DefaultNamespace, template)#>
{
/// <summary>
/// <#= AsInterfaceName(template.Name) #> Interface
/// <para><#= GetValue(template.SitecoreFields, "__Short description")#></para>
/// <para>Path: <#= template.Path #></para>
/// <para>ID: <#= template.ID.ToString() #></para>
/// </summary>
public interface <#= AsInterfaceName(template.Name) #> : IGlassBase <#=GetObjectInheritanceDefinition(DefaultNamespace, template, true, (string s) => AsInterfaceName(s))#>
{
<#foreach(SitecoreField field in GetFieldsForTemplate(template, false)){#>
/// <summary>
/// The <#=field.Name#> field.
/// <para><#= GetValue(field.SitecoreFields, "__Short description")#></para>
/// <para>Field Type: <#=field.Type#></para>
/// <para>Field ID: <#=field.ID.ToString()#></para>
/// <para>Custom Data: <#=field.Data#></para>
/// </summary>
<#=GetGlassFieldType(field)#> <#= GetPropertyName(field) #> {get; set;}
<#}#>
}
<#
// If the name of the template looks like an Interface, then don't generate a class definition
if (!IsInterfaceWord(template.Name)){ #>
/// <summary>
/// <#= AsClassName(template.Name) #>
/// <para><#= GetValue(template.SitecoreFields, "__Short description")#></para>
/// <para>Path: <#= template.Path #></para>
/// <para>ID: <#= template.ID.ToString() #></para>
/// </summary>
[SitecoreClass(TemplateId="<#=template.ID#>")]
public partial class <#= AsClassName(template.Name) #> : GlassBase, <#=AsInterfaceName(template.Name)#>
{
<#foreach(SitecoreField field in GetFieldsForTemplate(template, true)){#>
/// <summary>
/// The <#=field.Name#> field.
/// <para><#= GetValue(field.SitecoreFields, "__Short description")#></para>
/// <para>Field Type: <#=field.Type#></para>
/// <para>Field ID: <#=field.ID.ToString()#></para>
/// <para>Custom Data: <#=field.Data#></para>
/// </summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("<#=Tool#>", "<#=ToolVersion#>")]
[SitecoreField("<#=field.Name#>")]
public virtual <#=GetGlassFieldType(field)#> <#= GetPropertyName(field) #> {get; set;}
<#}#>
}
<#}#>
}
<#+
/// <summary>
/// Gets the inheritance string for the generated template
/// </summary>
/// <param name="defaultNamespace">The default namespace.</param>
/// <param name="template">The template to get the bases for.</param>
/// <param name="nameFunc">The function to run the base templates names through.</param>
/// <returns></returns>
public static string GetObjectInheritanceDefinition(string defaultNamespace, SitecoreTemplate item, bool includeLeadingComma, Func<string, string> nameFunc)
{
if (item.BaseTemplates.Count > 0)
{
return string.Concat(includeLeadingComma ? ", " : "",
item.BaseTemplates
.Select( bt => GetFullyQualifiedName(defaultNamespace, bt, nameFunc)) // select the name of the template with an 'I' prefix
.Aggregate( (total,next) => total + ", " + next) // basically a string.join(string[], '')
);
}
return "";
}
public static string GetGlassFieldType(SitecoreField field)
{
if (field != null && field.Type != null)
{
// Pull out any 'type' param from the custom data field on the field in TDS
string customType = GetCustomProperty(field.Data, "type");
string generic = GetCustomProperty(field.Data, "generic");
if (customType != "")
{
if (generic != "")
{
return string.Format("{0}<{1}>", customType, generic);
}
else
{
return customType;
}
}
switch(field.Type.ToLower())
{
case "tristate":
return "TriState";
case "checkbox":
return "bool";
case "date":
case "datetime":
return "DateTime";
case "number":
return "float";
case "integer":
return "int";
case "treelist":
case "treelistex":
case "treelist descriptive":
case "checklist":
case "multilist":
return string.Format("IEnumerable<{0}>", string.IsNullOrEmpty(generic) ? "Guid" : generic);
case "grouped droplink":
case "droplink":
case "lookup":
case "droptree":
case "reference":
case "tree":
return "Guid";
case "file":
return "File";
case "image":
return "Image";
case "rich text":
case "html":
return "string";
case "general link":
return "Link";
case "single-line text":
case "multi-line text":
case "frame":
case "text":
case "memo":
case "droplist":
case "grouped droplist":
case "valuelookup":
return "string";
default:
return "object /* UNKNOWN */";
}
}
else
{
throw new Exception("There is no 'Type' field on the " + field.Name + " field.");
}
}
#>