forked from jamesnovak/CrmSvcUtilTS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXrm2TSRenderAngular.cs
181 lines (152 loc) · 7.92 KB
/
Xrm2TSRenderAngular.cs
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
using System.Text;
using Microsoft.Xrm.Sdk.Metadata;
namespace Xrm.Tools
{
/// <summary>
/// Implementation for Angular libraries
/// </summary>
public class Xrm2TSRenderAngular : Xrm2TSRenderBase, IXrm2TSRenderActions
{
#region Constructors
public Xrm2TSRenderAngular(EntityMetadata entityMeta, StringBuilder appendTo, StringBuilder logger, ConfigurationInfo config) :
base(entityMeta, appendTo, logger, config) {}
#endregion
/// <summary>
/// Render the entire class and return the results as a StringBuilder
/// </summary>
/// <returns></returns>
public StringBuilder RenderEntityTemplate() {
return RenderEntityTemplate(_sb);
}
public StringBuilder RenderEntityTemplate(StringBuilder appendTo)
{
Logger.AppendLine(string.Format("{0}: Begin Rendering Entity template", _formattedName));
appendTo.Append(Constants.GetTabs())
.AppendLine(string.Format("//** @description AUTO GENERATED CLASSES FOR {0}", _formattedName));
RenderEntityInterface(appendTo);
RenderEntityAttributes(appendTo);
RenderEntityClass(appendTo);
Logger.AppendLine(string.Format("{0}: End Rendering Entity template", _formattedName));
return appendTo;
}
/// <summary>
///
/// </summary>
public void RenderEntityInterface() {
RenderEntityInterface(_sb);
}
public void RenderEntityInterface(StringBuilder appendTo)
{
Logger.AppendLine(string.Format("{0}: Rendering Entity Interface Web API method calls", _formattedName));
appendTo.Append(Constants.GetTabs())
.AppendLine(string.Format("//** @description WebAPI collection interface for {0} */", _formattedName))
.Append(Constants.GetTabs())
.AppendLine(string.Format("export interface I{0}s extends IRetrieveMultipleData<I{1}> {{}}", _formattedName, _formattedName))
.Append(Constants.GetTabs())
.AppendLine(string.Format("//** @description WebAPI interface for {0} */", _formattedName))
.Append(Constants.GetTabs())
.AppendLine(string.Format("export interface I{0} {{", _formattedName))
.Append(Constants.GetTabs(2))
.AppendLine("[key: string]: string | number");
Logger.AppendLine(string.Format("{0}: Rendering Entity Interface Web API attributes", _formattedName));
foreach (var attr in _entityMeta.Attributes)
{
// filter based on configuration settings
if (_configuration.FilterAttribute(attr.SchemaName))
continue;
appendTo.Append(Constants.GetTabs(2))
.AppendLine(string.Format("{0}?: {1}", lookupFixer(attr), getDataType(attr)));
}
// close out the class
appendTo.AppendLine(Constants.GetTabs(1) + "}");
}
/// <summary>
/// Render the list of Attributes for both the Entity Form and the Web API calls
/// </summary>
public void RenderEntityAttributes()
{
RenderEntityAttributes(_sb);
}
/// <summary>
/// Render the list of Attributes for both the Entity Form and the Web API calls
/// </summary>
/// <param name="appendTo">String builder to which the results will be appeneded</param>
public void RenderEntityAttributes(StringBuilder appendTo)
{
Logger.AppendLine(string.Format("{0}: Rendering Entity Attributes class for Forms and Web API", _formattedName));
appendTo.Append(Constants.GetTabs())
.AppendLine(string.Format("//** Collection of Attribute structures for {0} */", _formattedName))
.Append(Constants.GetTabs())
.AppendLine(string.Format("export class {0}Attributes {{", _formattedName));
Logger.AppendLine(string.Format("{0}: Rendering Entity Attributes Class collection", _formattedName));
// render each of the entity attribute structures here
foreach (var attr in _entityMeta.Attributes) {
// filter based on configuration settings
if (_configuration.FilterAttribute(attr.SchemaName))
continue;
appendTo.Append(Constants.GetTabs(2))
.AppendLine(string.Format("{0}:IAttribName = {{ name:\"{0}\", api_name:\"{1}\" }}", attr.LogicalName, lookupFixer(attr)));
}
// close out the class
appendTo.Append(Constants.GetTabs())
.AppendLine("}");
}
/// <summary>
/// Render the Entity class passed to the web API call for init and return values
/// </summary>
public void RenderEntityClass() {
RenderEntityClass(_sb);
}
public void RenderEntityClass(StringBuilder appendTo) {
Logger.AppendLine(string.Format("{0}: Rendering Entity Class details", _formattedName));
appendTo.Append(Constants.GetTabs())
.AppendLine(string.Format("/** @description Instantiates a {0} Entity to be used for CRUD based operations", _formattedName))
.Append(Constants.GetTabs())
.AppendLine("* @param {object} initData An optional parameter for a create and update entities */")
.Append(Constants.GetTabs())
.AppendLine(string.Format("export class {0} extends Entity {{", _formattedName))
.Append(Constants.GetTabs())
.AppendLine("[key: string]: string|number")
.Append(Constants.GetTabs())
.AppendLine(string.Format("public route: string = \"{0}s\";", _formattedName.ToLower()))
.AppendLine();
Logger.AppendLine(string.Format("{0}: Rendering Entity Attributes for Entity class", _formattedName));
// generate the public class attributes
foreach (var attr in _entityMeta.Attributes)
{
// filter based on configuration settings
if (_configuration.FilterAttribute(attr.SchemaName))
continue;
appendTo.Append(Constants.GetTabs(2))
.AppendLine(string.Format("public {0}: {1};", lookupFixer(attr), getDataType(attr)));
}
Logger.AppendLine(string.Format("{0}: Rendering Entity Class constructor", _formattedName));
// generate the class constructor, populating the attributes
appendTo.AppendLine()
.Append(Constants.GetTabs(2))
.AppendLine(string.Format("constructor(initData?: I{0}) {{", _formattedName))
.Append(Constants.GetTabs(3))
.AppendLine(string.Format("super(\"{0}s\");", _formattedName.ToLower()))
.Append(Constants.GetTabs(3))
.AppendLine("if (initData == undefined) { return; }")
.AppendLine();
Logger.AppendLine(string.Format( "{0}: Rendering attribute initilization in class constructor", _formattedName));
foreach (var attr in _entityMeta.Attributes)
{
// filter based on configuration settings
if (_configuration.FilterAttribute(attr.SchemaName))
continue;
var name = lookupFixer(attr);
appendTo.Append(Constants.GetTabs(3))
.AppendLine(string.Format("this.{0} = initData.{1};", name, name));
}
// close out the class
appendTo.Append(Constants.GetTabs(3))
.AppendLine(string.Format("this.id = initData.{0};", _entityMeta.PrimaryIdAttribute))
.Append(Constants.GetTabs(2))
.AppendLine("}")
.Append(Constants.GetTabs())
.AppendLine("}");
}
}
}