Skip to content

Commit

Permalink
Merge pull request aumcode#7 from aumcode/master
Browse files Browse the repository at this point in the history
Get latest
  • Loading branch information
itadapter committed Mar 6, 2016
2 parents ad0c666 + 8bf2051 commit c881819
Show file tree
Hide file tree
Showing 93 changed files with 6,705 additions and 2,850 deletions.
56 changes: 50 additions & 6 deletions Source/NFX.Wave/Client/RecordModelGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,26 @@ namespace NFX.Wave.Client
public delegate string ModelLocalizationEventHandler(RecordModelGenerator sender, string schema, string field, string value, string isoLang);


/// <summary>
/// Invoked by generator to obtain a list of dynamic lookup values for a field.
/// This event is invoked ONLY for fields that DO NOT have valueList specified
/// </summary>
/// <param name="sender">Generator</param>
/// <param name="row">Row that data model is generated for</param>
/// <param name="fdef">Field definition</param>
/// <param name="target">Target name</param>
/// <param name="isoLang">Desired isoLang for localization</param>
/// <returns>JSONDataMap populated by business logic or null to indicate that no lookup values are available</returns>
public delegate JSONDataMap ModelFieldValueListLookupFunc(RecordModelGenerator sender,
Row row,
Schema.FieldDef fdef,
string target,
string isoLang);

/// <summary>
/// Facilitates tasks of JSON generation for record models/rows as needed by WV.RecordModel client library
/// Facilitates tasks of JSON generation for record models/rows as needed by WV.RecordModel client library.
/// This class does not generate nested models, only flat models for particular row
/// (i.e. id row has a complex type field, it will be serialized as "object")
/// </summary>
public class RecordModelGenerator
{
Expand Down Expand Up @@ -106,7 +123,12 @@ public RecordModelGenerator(IConfigSectionNode conf){ }
/// may get attributes for client data entry screen that sees field metadata differently, in which case target will reflect the name
/// of the screen
/// </summary>
public virtual JSONDataMap RowToRecordInitJSON(Row row, Exception validationError, string recID = null, string target = null, string isoLang = null)
public virtual JSONDataMap RowToRecordInitJSON(Row row,
Exception validationError,
string recID = null,
string target = null,
string isoLang = null,
ModelFieldValueListLookupFunc valueListLookup = null)
{
var result = new JSONDataMap();
if (row==null) return result;
Expand Down Expand Up @@ -139,7 +161,7 @@ public virtual JSONDataMap RowToRecordInitJSON(Row row, Exception validationErro
{
var fld = new JSONDataMap();
fields.Add(fld);
fld["def"] = FieldDefToJSON(schemaName, fdef, target);
fld["def"] = FieldDefToJSON(row, schemaName, fdef, target, isoLang, valueListLookup);
fld["val"] = row.GetFieldValue(fdef);
var ferr = validationError as CRUDFieldValidationException;
//field level exception
Expand All @@ -161,15 +183,20 @@ public virtual JSONDataMap RowToRecordInitJSON(Row row, Exception validationErro
}


protected virtual JSONDataMap FieldDefToJSON(string schema, Schema.FieldDef fdef, string target, string isoLang = null)
protected virtual JSONDataMap FieldDefToJSON(Row row,
string schema,
Schema.FieldDef fdef,
string target,
string isoLang,
ModelFieldValueListLookupFunc valueListLookup)
{
var result = new JSONDataMap();

result["Name"] = fdef.Name;
result["Type"] = MapCLRTypeToJS(fdef.NonNullableType);
var key = fdef.AnyTargetKey;
if (key) result["Key"] = key;


if (fdef.NonNullableType.IsEnum)
{ //Generate default lookupdict for enum
Expand All @@ -192,14 +219,26 @@ protected virtual JSONDataMap FieldDefToJSON(string schema, Schema.FieldDef fdef
if (attr.Min!=null) result["MinValue"] = attr.Min;
if (attr.Max!=null) result["MaxValue"] = attr.Max;
if (attr.MinLength>0) result["MinSize"] = attr.MinLength;
if (attr.MaxLength>0) result["Size"] = attr.MaxLength;
if (attr.MaxLength>0) result["Size"] = attr.MaxLength;
if (attr.Default!=null) result["DefaultValue"] = attr.Default;
if (attr.ValueList.IsNotNullOrWhiteSpace())
{
var vl = OnLocalizeString(schema, "LookupDict", attr.ValueList, isoLang);
result["LookupDict"] = FieldAttribute.ParseValueListString(vl);
}
else
{
if (valueListLookup!=null)
{
var valueList = valueListLookup(this, row, fdef, target, isoLang);
if (valueList!=null)
result["LookupDict"] = valueList;
}
}

if (attr.Kind!=DataKind.Text) result["Kind"] = MapCLRKindToJS(attr.Kind);

if (attr.CharCase!=CharCase.AsIs) result["Case"] = MapCLRCharCaseToJS(attr.CharCase);
}

if (attr.Metadata!=null)
Expand Down Expand Up @@ -266,6 +305,11 @@ protected virtual string MapCLRKindToJS(DataKind kind)
}
}

protected virtual string MapCLRCharCaseToJS(CharCase kind)
{
return kind.ToString().ToLowerInvariant();
}


}
}
54 changes: 52 additions & 2 deletions Source/NFX.Wave/Exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,61 @@ public override string Message
public class HTTPStatusException : WaveException
{

public static HTTPStatusException NotFound_404
public static HTTPStatusException BadRequest_400(string descr = null)
{
get { return new HTTPStatusException(SysConsts.STATUS_404, SysConsts.STATUS_404_DESCRIPTION);}
var d = SysConsts.STATUS_400_DESCRIPTION;
if (descr.IsNotNullOrWhiteSpace()) d += (": "+descr);

return new HTTPStatusException(SysConsts.STATUS_400, d);
}

public static HTTPStatusException Forbidden_403(string descr = null)
{
var d = SysConsts.STATUS_403_DESCRIPTION;
if (descr.IsNotNullOrWhiteSpace()) d += (": "+descr);

return new HTTPStatusException(SysConsts.STATUS_403, d);
}

public static HTTPStatusException NotFound_404(string descr = null)
{
var d = SysConsts.STATUS_404_DESCRIPTION;
if (descr.IsNotNullOrWhiteSpace()) d += (": "+descr);

return new HTTPStatusException(SysConsts.STATUS_404, d);
}

public static HTTPStatusException MethodNotAllowed_405(string descr = null)
{
var d = SysConsts.STATUS_405_DESCRIPTION;
if (descr.IsNotNullOrWhiteSpace()) d += (": "+descr);

return new HTTPStatusException(SysConsts.STATUS_405, d);
}

public static HTTPStatusException NotAcceptable_406(string descr = null)
{
var d = SysConsts.STATUS_406_DESCRIPTION;
if (descr.IsNotNullOrWhiteSpace()) d += (": "+descr);

return new HTTPStatusException(SysConsts.STATUS_406, d);
}

public static HTTPStatusException TooManyRequests_429(string descr = null)
{
var d = SysConsts.STATUS_429_DESCRIPTION;
if (descr.IsNotNullOrWhiteSpace()) d += (": "+descr);

return new HTTPStatusException(SysConsts.STATUS_429, d);
}

public static HTTPStatusException InternalError_500(string descr = null)
{
var d = SysConsts.STATUS_500_DESCRIPTION;
if (descr.IsNotNullOrWhiteSpace()) d += (": "+descr);

return new HTTPStatusException(SysConsts.STATUS_500, d);
}


/// <summary>
Expand Down
38 changes: 28 additions & 10 deletions Source/NFX.Wave/Filters/ErrorFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,33 @@ public static void HandleException(WorkContext work,
var actual = error;
if (actual is FilterPipelineException)
actual = ((FilterPipelineException)actual).RootException;

if (actual is MVCActionException)
actual = ((MVCActionException)actual).InnerException;


var securityError = actual is NFX.Security.AuthorizationException || actual.InnerException is NFX.Security.AuthorizationException;

if (actual is HTTPStatusException)
{
var se = (HTTPStatusException)actual;
work.Response.StatusCode = se.StatusCode;
work.Response.StatusDescription = se.StatusDescription;
}
else
{
if (securityError)
{
work.Response.StatusCode = SysConsts.STATUS_403;
work.Response.StatusDescription = SysConsts.STATUS_403_DESCRIPTION;
}
else
{
work.Response.StatusCode = SysConsts.STATUS_500;
work.Response.StatusDescription = SysConsts.STATUS_500_DESCRIPTION;
}
}


if (json)
{
Expand All @@ -165,7 +192,7 @@ public static void HandleException(WorkContext work,
}
else
{
if (securityRedirectURL.IsNotNullOrWhiteSpace() && (actual is NFX.Security.AuthorizationException || actual.InnerException is NFX.Security.AuthorizationException))
if (securityRedirectURL.IsNotNullOrWhiteSpace() && securityError)
work.Response.RedirectAndAbort(securityRedirectURL);
else
{
Expand All @@ -192,15 +219,6 @@ public static void HandleException(WorkContext work,
}
}



if (actual is HTTPStatusException)
{
var se = (HTTPStatusException)actual;
work.Response.StatusCode = se.StatusCode;
work.Response.StatusDescription = se.StatusDescription;
}

if (logMatches!=null && logMatches.Count>0)
{
JSONDataMap matched = null;
Expand Down
29 changes: 1 addition & 28 deletions Source/NFX.Wave/Handlers/MVCHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ protected virtual void BindParameters(Controller controller, string action, Acti

if (mpars.Length==0) return;

var requested = GetRequestAsJSONDataMap(work);
var requested = work.WholeRequestAsJSONDataMap;

var strictParamBinding = attrAction.StrictParamBinding;

Expand Down Expand Up @@ -230,33 +230,6 @@ protected virtual void BindParameters(Controller controller, string action, Acti
}
}

/// <summary>
/// Converts request into JSONDataMap
/// </summary>
protected JSONDataMap GetRequestAsJSONDataMap(WorkContext work)
{
if (!work.Request.HasEntityBody) return work.MatchedVars;

JSONDataMap result = null;

var ctp = work.Request.ContentType;

//Multipart
if (ctp.IndexOf(ContentType.FORM_MULTIPART_ENCODED)>=0)
result = MultiPartContent.ToJSONDataMap(work.Request.InputStream, ctp, work.Request.ContentEncoding);
else //Form URL encoded
if (ctp.IndexOf(ContentType.FORM_URL_ENCODED)>=0)
result = JSONDataMap.FromURLEncodedStream(new NFX.IO.NonClosingStreamWrap(work.Request.InputStream),
work.Request.ContentEncoding);
else//JSON
if (ctp.IndexOf(ContentType.JSON)>=0)
result = JSONReader.DeserializeDataObject(new NFX.IO.NonClosingStreamWrap(work.Request.InputStream),
work.Request.ContentEncoding) as JSONDataMap;

return result==null ? work.MatchedVars : result.Append(work.MatchedVars);
}


/// <summary>
/// Turns result object into appropriate response
/// </summary>
Expand Down
11 changes: 11 additions & 0 deletions Source/NFX.Wave/Handlers/TypeLookupHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,17 @@ protected virtual string GetTargetTypeNameFromWorkContext(WorkContext work)
if (result.IsNullOrWhiteSpace())
result = DefaultTypeName;

//20160217 DKh
var match = work.Match;
if (match!=null && match.TypeNsPrefix.IsNotNullOrWhiteSpace())
{
var pfx = match.TypeNsPrefix;

if (pfx[pfx.Length-1]!='/' && pfx[pfx.Length-1]!='\\') pfx = pfx + '/';

result = pfx + result;
}

return result;
}

Expand Down
1 change: 0 additions & 1 deletion Source/NFX.Wave/Handlers/TypeLookupUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,5 @@ public TypeLookup(TypeLookup clone) : base(clone, StringComparer.Ordinal)
{

}

}
}
Loading

0 comments on commit c881819

Please sign in to comment.