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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web;
using System.Reflection;
using System.ComponentModel.DataAnnotations;
using System.Collections;
namespace JsAction.mvcct
{
/// <summary>
/// JsAction queryable handler
/// </summary>
public class JsActionQueryableHandler : JsActionHandler
{
/// <summary>
/// Methodcall generation
/// </summary>
/// <param name="js">StringBuilder</param>
/// <param name="method">Method</param>
/// <param name="Controller">Controller</param>
/// <param name="documentate">Documentate funziont</param>
protected override void GenerateMethodCall(StringBuilder js, System.Reflection.MethodInfo method, string Controller, bool documentate)
{
var jsattribute = method.GetCustomAttributes(true).Where(attr => attr is JsqSelectMethodAttribute || attr is JsqUpdateMethodAttribute).First();
var pars = method.GetParameters();
string UrlToMethod = string.Empty;
UrlToMethod = GenerateUrlToMethod(method, Controller, UrlToMethod);
if (string.IsNullOrEmpty(UrlToMethod))
throw new Exception("Unable to generate method Url");
#region Select Method
if (jsattribute.GetType() == typeof(JsqSelectMethodAttribute))
{
if (method.ReturnType.GetGenericTypeDefinition() != typeof(IQueryable<>))
throw new Exception("JsqSelectMethod Attribute should be applied only on IQueryable<> returning methods");
var parameters = string.Join(",", pars.Select(m => m.Name).Union(new string[] { "fop", "options", "negate" }));
if (parameters.Length > 0)
parameters += ',';
pars.FirstOrDefault(m =>
{
if (m.ParameterType.IsGenericType)
ComplexTypeList.Value.Add(m.ParameterType.GetGenericArguments().First());
else if (this.FindIEnumerable(m.ParameterType) == null && m.ParameterType != typeof(DateTime) && m.ParameterType != typeof(DateTimeOffset) && m.ParameterType.IsPrimitive == false)
ComplexTypeList.Value.Add(m.ParameterType);
return false;
});
js.AppendFormat("{0}:function({1}){{", method.Name, parameters.Substring(0, parameters.Length - 1));
if (documentate)
{
js.AppendFormat("///<summary>Queries the \"{0}.{2}\" ApiController method.</summary>{1}///<param name=\"fop\" type=\"LogicalOperator\">The top level logical operator used to combine the filter conditions</param>{1}///<param name=\"options\" type=\"Object\">Options; see documentation</param>{1}///<param name=\"negate\" type=\"Boolean\">If true the filter is negated</param>{1}///<returns type=\"mvcct.oDataQueryable\">oDataQueryable</returns>}},", Controller, Environment.NewLine, method.Name);
return;
}
js.AppendFormat("return mvcct.oDataQueryable('{0}',fop,options,negate);}},", UrlToMethod);
}
#endregion
#region Update Method
else //JsqUpdateMethodAttribute
{
foreach (var param in method.GetParameters())
{
if (param.IsDefined(typeof(JsqChangesetAttribute), false))
SingleChangeset(js, method, UrlToMethod, documentate);
else if (this.FindIEnumerable(param.ParameterType) == null &&
param.ParameterType != typeof(DateTime) &&
param.ParameterType != typeof(DateTimeOffset) &&
param.ParameterType.IsPrimitive == false)
MultipleChangeset(js, param.ParameterType, UrlToMethod, documentate, string.Empty);
}
}
#endregion
}
private string GenerateUrlToMethod(System.Reflection.MethodInfo method, string Controller, string UrlToMethod)
{
if (method.DeclaringType == typeof(Controller))
UrlToMethod = RouteTable.Routes.GetVirtualPath(this.requestContext, new RouteValueDictionary(new { controller = Controller, action = method.Name })).VirtualPath;
else //ApiController
UrlToMethod = RouteTable.Routes.GetVirtualPath(this.requestContext, new RouteValueDictionary(new { httproute = "", controller = Controller })).VirtualPath;
return UrlToMethod;
}
private void MultipleChangeset(StringBuilder js, Type param, string url, bool documentate, string currentNavigationProp)
{
ComplexTypeList.Value.Add(param);
foreach (var prop in param.GetProperties())
{
if (prop.GetCustomAttributes(false).Where(a => a.GetType() == typeof(JsqChangesetAttribute)).Count() > 0)
SingleChangeset(js, prop, url, documentate, currentNavigationProp);
else
if (prop.PropertyType.IsClass && prop.PropertyType.IsAssignableFrom(typeof(IEnumerable)) == false)
MultipleChangeset(js, prop.PropertyType, url, documentate, string.Concat(currentNavigationProp, string.Format("{0}.", prop.Name)));
}
}
private void SingleChangeset(StringBuilder js, MethodInfo method, string url, bool documentate)
{
try
{
var par = method.GetParameters().Where(p => p.IsDefined(typeof(JsqChangesetAttribute), false)).Single();
var attr = par.GetCustomAttributes(true).Where(attri => attri.GetType() == typeof(JsqChangesetAttribute)).Single() as JsqChangesetAttribute;
var viewmodel = this.FindIEnumerable(par.ParameterType.GetProperties().Where(w => w.Name == attr.InsertedEntities).Single().PropertyType).GetGenericArguments().Single();
string key = string.Empty;
var props = viewmodel.GetProperties();
foreach (var prop in props)
{
if (prop.IsDefined(typeof(KeyAttribute), false) || prop.Name.EndsWith("id", StringComparison.InvariantCultureIgnoreCase))
{
key = prop.Name;
break;
}
}
if (documentate)
js.AppendFormat("{0}:function(sourceViewModel,sourceExpression,options){{///<summary>Handles the '{1}' collection, on the client and the post of its change set to the server.</summary>{2}///<param name='sourceViewModel' mayBeNull='false' optional='false' type='String' parameterArray='false' integer='false' domElement='false'>The Client Side ViewModel that hosts the collection.</param>{2}///<param name='sourceExpression' mayBeNull='false' optional='false' type='String' parameterArray='false' integer='false' domElement='false'>The property of the ViewModel that hosts the collection as a string expression.</param>{2}///<param name='key' mayBeNull='true' optional='true' type='String' parameterArray='false' integer='false' domElement='false'>[OPTIONAL] Overrides the object key retrieved automatically.</param>{2}///<param name='options' mayBeNull='true' optional='true' type='Object' parameterArray='false' integer='false' domElement='false'>Options (see documentation).</param>{2}///<returns type='mvcct.updatesManager' integer='false' domElement='false' mayBeNull='false'>updatesManager</returns>{2}}},", method.Name, viewmodel.Name, Environment.NewLine);
else
js.AppendFormat("{0}:function(sourceViewModel,sourceExpression,options){{return mvcct.updatesManager('{1}',sourceViewModel,sourceExpression,'{2}',null,null,jQuery.extend({{updater: {{u: '{3}', i: '{4}', d: '{5}', f: '{6}'}}}},options)); }},", method.Name, url, key, attr.ModifiedEntities, attr.InsertedEntities, attr.DeletedEntities, attr.FatherReferencesEntities);
}
catch (Exception ex)
{
Type exType = ex.GetType();
if (exType == typeof(ArgumentNullException) || exType == typeof(NullReferenceException))
throw new Exception(string.Format("JsqUpdateMethod attribute was placed on {0} method, but no Changeset/Multiple changeset attribute has been placed on parameters or its properties!", method.Name));
else
throw;
}
}
private void SingleChangeset(StringBuilder js, PropertyInfo prop, string url, bool documentate, string currentNavigationProp)
{
try
{
var attr = prop.GetCustomAttributes(true).Where(attri => attri.GetType() == typeof(JsqChangesetAttribute)).Single() as JsqChangesetAttribute;
var viewmodel = this.FindIEnumerable(prop.PropertyType.GetProperties().Where(w => w.Name == attr.ModifiedEntities).Single().PropertyType).GetGenericArguments().Single();
string key = string.Empty;
var props = viewmodel.GetProperties();
foreach (var prp in prop.PropertyType.GetProperties())
{
if (prp.IsDefined(typeof(KeyAttribute), false) || prp.Name.EndsWith("id", StringComparison.InvariantCultureIgnoreCase))
{
key = prp.Name;
break;
}
}
if (documentate)
js.AppendFormat("{0}:function(sourceViewModel,sourceExpression,options){{///<summary>Handles the '{1}' collection, on the client and the post of its change set to the server.</summary>{2}///<param name='sourceViewModel' mayBeNull='false' optional='false' type='String' parameterArray='false' integer='false' domElement='false'>The Client Side ViewModel that hosts the collection.</param>{2}///<param name='sourceExpression' mayBeNull='false' optional='false' type='String' parameterArray='false' integer='false' domElement='false'>The property of the ViewModel that hosts the collection as a string expression.</param>{2}///<param name='key' mayBeNull='true' optional='true' type='String' parameterArray='false' integer='false' domElement='false'>[OPTIONAL] Overrides the object key retrieved automatically.</param>{2}///<param name='options' mayBeNull='true' optional='true' type='Object' parameterArray='false' integer='false' domElement='false'>Options (see documentation).</param>{2}///<returns type='mvcct.updatesManager' integer='false' domElement='false' mayBeNull='false'>updatesManager</returns>{2}}},", prop.Name, viewmodel.Name, Environment.NewLine);
else
js.AppendFormat("{0}:function(sourceViewModel,sourceExpression,options){{return mvcct.updatesManager('{1}',sourceViewModel,sourceExpression,'{2}',null,'{7}',jQuery.extend({{updater: {{u: '{3}', i: '{4}', d: '{5}', f: '{6}'}}}},options)); }},", prop.Name, url, key, attr.ModifiedEntities, attr.InsertedEntities, attr.DeletedEntities, attr.FatherReferencesEntities, currentNavigationProp);
}
catch (Exception ex)
{
Type exType = ex.GetType();
if (exType == typeof(ArgumentNullException) || exType == typeof(NullReferenceException))
throw new Exception(string.Format("JsqUpdateMethod attribute was placed on {0} method, but no Changeset/Multiple changeset attribute has been placed on parameters or its properties!", prop.Name));
else
throw;
}
}
/// <summary>
/// Get methods on which attribute are set
/// </summary>
/// <param name="asm">External assembly</param>
/// <returns>Methods</returns>
protected override IEnumerable<IGrouping<Type, System.Reflection.MethodInfo>> GetMethodsWith(params System.Reflection.Assembly[] asm)
{
var methods =
from ass in asm
from t in ass.GetTypes()
from m in t.GetMethods()
where m.Name.StartsWith("GET", StringComparison.InvariantCultureIgnoreCase)
&& (m.IsDefined(typeof(JsqSelectMethodAttribute), false) == true
&& m.ReturnType.IsGenericType == true)
|| m.IsDefined(typeof(JsqUpdateMethodAttribute), false)
select m;
return methods.GroupBy(m => m.DeclaringType);
}
/// <summary>
/// Processes current request
/// </summary>
/// <param name="context">HttpContext to handle with</param>
public override void ProcessRequest(System.Web.HttpContext context)
{
string endJsCode = "";
this.qstring = context.Request.QueryString["data"];
var documentate = !string.IsNullOrEmpty(context.Request.QueryString["doc"]);
if (context.Cache.Get(cacheKey) == null || documentate == true)
{
var type = context.ApplicationInstance.GetType();
while (type != null && type.Namespace == "ASP")
type = type.BaseType;
var asm = type == null ? null : type.Assembly;
this.SearchAsm.Value.Add(asm);
var groupedMethods = this.GetMethodsWith(SearchAsm.Value.ToArray());
if (groupedMethods.Count() == 0)
return;
var js = new StringBuilder();
js.Append("if (typeof JsActions=='undefined'){var JsActions={};}JsActions.JsQueryables={};");
foreach (var controller in groupedMethods)
{
string ControllerName = controller.Key.Name.Substring(0, controller.Key.Name.IndexOf("Controller"));
js.AppendFormat("JsActions{1}.{0}={{", ControllerName, InnerObject);
foreach (var method in controller)
GenerateMethodCall(js, method, ControllerName, documentate);
js.Remove(js.Length - 1, 1).Append("};");
}
if (documentate)
{
JsAction.ext.JSBeautify j = new ext.JSBeautify(js.ToString(), new ext.JSBeautifyOptions());
endJsCode = j.GetResult();
context.Response.Cache.SetCacheability(HttpCacheability.NoCache); //regenerate a new fresh file every time.
}
else
{
endJsCode = js.ToString();
context.Cache.Insert(cacheKey, endJsCode);
}
}
else
{
endJsCode = context.Cache.Get(cacheKey).ToString();
}
context.Response.ContentType = "application/javascript";
context.Response.Charset = string.Empty;
context.Response.Write(endJsCode);
context.Response.End();
}
/// <summary>
/// CacheKey
/// </summary>
protected override string cacheKey
{
get { return "_JSQUERY_"; }
}
/// <summary>
/// InnerObject
/// </summary>
protected override string InnerObject
{
get { return ".JsQueryables"; }
}
}
}