public void CheckString(Field fieldInfo, string val) { if (val == null) return; if (fieldInfo.maxLength > 0) { if (!string.IsNullOrEmpty(val)) { if (val.Length > fieldInfo.maxLength) { throw new ValidationException(string.Format(ErrorStrings.ERR_VAL_EXCEEDS_MAXLENGTH, fieldInfo.fieldName, fieldInfo.maxLength)); } } } if (!string.IsNullOrEmpty(val) && !string.IsNullOrEmpty(fieldInfo.regex)) { var rx = new System.Text.RegularExpressions.Regex(fieldInfo.regex, System.Text.RegularExpressions.RegexOptions.IgnoreCase); if (!rx.IsMatch(val)) { throw new ValidationException(string.Format(ErrorStrings.ERR_VAL_IS_NOT_VALID, fieldInfo.fieldName)); } } }
public override string SerializeField(Type propType, Field fieldInfo, object value) { if (propType == typeof(System.Data.Linq.Binary)) return LinqBinaryToString(value); else return base.SerializeField(propType, fieldInfo, value); }
public void CheckRange(Field fieldInfo, string val) { if (val == null) return; if (!string.IsNullOrEmpty(fieldInfo.range)) { string[] rangeParts = fieldInfo.range.Split(','); switch (fieldInfo.dataType) { case DataType.Integer: case DataType.Decimal: case DataType.Float: { double dblval = Double.Parse(val, System.Globalization.CultureInfo.InvariantCulture); if (!string.IsNullOrEmpty(rangeParts[0])) { double minDbl = Double.Parse(rangeParts[0], System.Globalization.CultureInfo.InvariantCulture); if (dblval < minDbl) throw new ValidationException(string.Format(ErrorStrings.ERR_VAL_RANGE_NOT_VALID, fieldInfo.fieldName, fieldInfo.range)); } if (!string.IsNullOrEmpty(rangeParts[1])) { double maxDbl = Double.Parse(rangeParts[1], System.Globalization.CultureInfo.InvariantCulture); if (dblval > maxDbl) throw new ValidationException(string.Format(ErrorStrings.ERR_VAL_RANGE_NOT_VALID, fieldInfo.fieldName, fieldInfo.range)); } } break; case DataType.Date: case DataType.DateTime: { DateTime dtval = (DateTime)this._serviceContainer.ValueConverter.DeserializeValue(typeof(DateTime), DataType.DateTime, fieldInfo.dateConversion, val); if (!string.IsNullOrEmpty(rangeParts[0])) { DateTime minDt = DateTime.ParseExact(rangeParts[0], "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture); if (dtval < minDt) throw new ValidationException(string.Format(ErrorStrings.ERR_VAL_RANGE_NOT_VALID, fieldInfo.fieldName, fieldInfo.range)); } if (!string.IsNullOrEmpty(rangeParts[1])) { DateTime maxDt = DateTime.ParseExact(rangeParts[1], "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture); if (dtval > maxDt) throw new ValidationException(string.Format(ErrorStrings.ERR_VAL_RANGE_NOT_VALID, fieldInfo.fieldName, fieldInfo.range)); } } break; default: return; } } }
private static FieldsList _XElementsToFieldList(IEnumerable<XElement> xFields) { FieldsList fields = new FieldsList(); foreach (XElement xField in xFields) { Field field = new Field() { fieldName = (string)xField.Attribute("fieldName") }; if (xField.Attributes("isPrimaryKey").Any()) field.isPrimaryKey = (short)xField.Attribute("isPrimaryKey"); if (xField.Attributes("dataType").Any()) field.dataType = (DataType)Enum.Parse(typeof(DataType), xField.Attribute("dataType").Value); if (xField.Attributes("maxLength").Any()) field.maxLength = (short)xField.Attribute("maxLength"); if (xField.Attributes("isNullable").Any()) field.isNullable = (bool)xField.Attribute("isNullable"); if (xField.Attributes("isReadOnly").Any()) field.isReadOnly = (bool)xField.Attribute("isReadOnly"); if (xField.Attributes("isAutoGenerated").Any()) field.isAutoGenerated = (bool)xField.Attribute("isAutoGenerated"); if (xField.Attributes("allowClientDefault").Any()) field.allowClientDefault = (bool)xField.Attribute("allowClientDefault"); if (xField.Attributes("isNeedOriginal").Any()) field.isNeedOriginal = (bool)xField.Attribute("isNeedOriginal"); if (xField.Attributes("dateConversion").Any()) field.dateConversion = (DateConversion)Enum.Parse(typeof(DateConversion), xField.Attribute("dateConversion").Value); if (xField.Attributes("fieldType").Any()) field.fieldType = (FieldType)Enum.Parse(typeof(FieldType), xField.Attribute("fieldType").Value); if (xField.Attributes("range").Any()) field.range = (string)xField.Attribute("range"); if (xField.Attributes("regex").Any()) field.regex = (string)xField.Attribute("regex"); if (xField.Attributes("dependentOn").Any()) field.dependentOn = (string)xField.Attribute("dependentOn"); if (xField.Elements(NS_DATA + "Field.nested").Any()) { field.nested.AddRange(_XElementsToFieldList(xField.Element(NS_DATA + "Field.nested").Elements(NS_DATA + "Field"))); } fields.Add(field); } return fields; }
private string GetFieldDataType(Field fieldInfo) { string fieldName = fieldInfo.fieldName; string fieldType = "any"; DataType dataType = fieldInfo.dataType; if (fieldInfo.fieldType == FieldType.Navigation) { fieldType = fieldInfo._TypeScriptDataType; } else if (fieldInfo.fieldType == FieldType.Object) { fieldType = fieldInfo._TypeScriptDataType; } else { fieldType = DotNet2TS.GetTSTypeNameFromDataType(dataType); } return fieldType; }
public virtual string SerializeField(Type propType, Field fieldInfo, object value) { if (value == null) return null; bool isNullable = ValueConverter.IsNullableTypeCore(propType); Type realType = null; if (!isNullable) realType = propType; else realType = Nullable.GetUnderlyingType(propType); if (isNullable) { string str = value.ToString(); if (str == String.Empty) return null; } if (realType == typeof(Guid)) { return this.GuidToString(value); } else if (realType == typeof(DateTime)) { return this.DateToString(value, isNullable); } else if (realType == typeof(DateTimeOffset)) { return this.DateOffsetToString(value, isNullable); } else if (realType == typeof(Boolean)) { return this.BoolToString(value); } else if (value is byte[]) { return this.BytesToString(value); } else { if (realType.IsValueType) { return (string)Convert.ChangeType(value, typeof(string), System.Globalization.CultureInfo.InvariantCulture); } else { return value.ToString(); } } }
public virtual object DeserializeField(Type propType, Field fieldInfo, string value) { return this.DeserializeValue(propType, fieldInfo.dataType, fieldInfo.dateConversion, value); }
private static void SetOrdinal(Field[] fieldInfos) { int i = 0, cnt = fieldInfos.Length; for (i = 0; i < cnt; ++i) { fieldInfos[i]._ordinal = i; if (fieldInfos[i].fieldType == FieldType.Object) { SetOrdinal(fieldInfos[i].nested.ToArray()); } } }
public void CheckValue(Field fieldInfo, string val) { if (val == null && !fieldInfo.isNullable) { throw new ValidationException(string.Format(ErrorStrings.ERR_FIELD_IS_NOT_NULLABLE, fieldInfo.fieldName)); } if (fieldInfo.dataType == DataType.String) { this.CheckString(fieldInfo, val); } this.CheckRange(fieldInfo, val); }
public string CreateComplexType(DbSetInfo dbSetInfo, Field fieldInfo, int level) { string typeName; if (level == 0) { typeName = string.Format("{0}_{1}", dbSetInfo.dbSetName, fieldInfo.fieldName); } else { //to prevent names collision the type name is a three part name typeName = string.Format("{0}_{1}{2}", dbSetInfo.dbSetName, fieldInfo.fieldName, level); } fieldInfo._TypeScriptDataType = typeName; var sb = new StringBuilder(512); var sbProperties = new StringBuilder(); var sbFieldsDef = new StringBuilder(); var sbFieldsInit = new StringBuilder(); Action<Field> AddProperty = (Field f) => { string dataType = this.GetFieldDataType(f); sbProperties.AppendFormat("\tget {0}(): {2} {{ return this.getValue('{1}'); }}", f.fieldName, f._FullName, dataType); sbProperties.AppendLine(); if (!f.isReadOnly) { sbProperties.AppendFormat("\tset {0}(v: {2}) {{ this.setValue('{1}', v); }}", f.fieldName, f._FullName, dataType); sbProperties.AppendLine(); } }; Action<Field> AddCalculatedProperty = (Field f) => { string dataType = this.GetFieldDataType(f); sbProperties.AppendFormat("\tget {0}(): {2} {{ return this.getEntity()._getCalcFieldVal('{1}'); }}", f.fieldName, f._FullName, dataType); sbProperties.AppendLine(); }; Action<Field, string> AddComplexProperty = (Field f, string dataType) => { sbProperties.AppendFormat("\tget {0}(): {1} {{ if (!this._{0}) {{this._{0} = new {1}('{0}', this);}} return this._{0}; }}", f.fieldName, dataType); sbProperties.AppendLine(); sbFieldsDef.AppendFormat("\tprivate _{0}: {1};", f.fieldName, dataType); sbFieldsDef.AppendLine(); sbFieldsInit.AppendFormat("\t\tthis._{0} = null;", f.fieldName); sbFieldsInit.AppendLine(); }; fieldInfo.nested.ForEach((f) => { if (f.fieldType == FieldType.Calculated) { AddCalculatedProperty(f); } else if (f.fieldType == FieldType.Navigation) { throw new InvalidOperationException("Navigation fields are not allowed on complex type properties"); } else if (f.fieldType == FieldType.Object) { string dataType = this.CreateComplexType(dbSetInfo, f, level + 1); AddComplexProperty(f, dataType); } else { AddProperty(f); } }); string templateName = "RootComplexProperty.txt"; if (level > 0) templateName = "ChildComplexProperty.txt"; (new TemplateParser(templateName)).ProcessParts((part) => { if (!part.isPlaceHolder) { sb.Append(part.value); } else { switch (part.value) { case "PROPERTIES": sb.Append(sbProperties.ToString()); break; case "TYPE_NAME": sb.Append(typeName); break; case "FIELDS_DEF": sb.Append(sbFieldsDef.ToString()); break; case "FIELDS_INIT": sb.Append(sbFieldsInit.ToString()); break; } } }); this._complexTypes.Add(typeName, sb.ToString()); return typeName; }
private string GetFieldDataType(Field fieldInfo) { string fieldName = fieldInfo.fieldName; string fieldType = "any"; DataType dataType = fieldInfo.dataType; fieldType = DotNet2TS.GetTSTypeNameFromDataType(dataType); return fieldType; }