Example #1
0
        protected virtual JsonDataMap FieldDefToJSON(Doc doc,
                                                     string schema,
                                                     Schema.FieldDef fdef,
                                                     string target,
                                                     Atom 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
                var names  = Enum.GetNames(fdef.NonNullableType);
                var values = new JsonDataMap(true);
                foreach (var name in names)
                {
                    values[name] = name;
                }

                result["LookupDict"] = values;
            }

            var attr = fdef[target];

            if (attr != null)
            {
                if (attr.Description != null)
                {
                    result["Description"] = OnLocalizeString(schema, "Description", attr.Description, isoLang);
                }
                var str = attr.StoreFlag == StoreFlag.OnlyStore || attr.StoreFlag == StoreFlag.LoadAndStore;
                if (!str)
                {
                    result["Stored"] = false;
                }
                if (attr.Required)
                {
                    result["Required"] = attr.Required;
                }
                if (!attr.Visible)
                {
                    result["Visible"] = attr.Visible;
                }
                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.Default != null)
                {
                    result["DefaultValue"] = attr.Default;
                }
                if (attr.ValueList.IsNotNullOrWhiteSpace())
                {
                    var vl = OnLocalizeString(schema, "LookupDict", attr.ValueList, isoLang);
                    result["LookupDict"] = FieldAttribute.ParseValueListString(vl);
                }
                else
                {
                    var valueList = valueListLookup != null?valueListLookup(this, doc, fdef, target, isoLang)
                                        : doc.GetDynamicFieldValueList(fdef, target, isoLang);

                    if (valueList == null && attr.HasValueList)
                    {
                        valueList = attr.ParseValueList();
                    }

                    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)
                {
                    var sect = attr.Metadata[METADATA_CONFIG_FIELD];
                    if (sect.Exists)
                    {
                        result[METADATA_CONFIG_FIELD] = sect.ToConfigurationJSONDataMap();
                    }

                    foreach (var mAt in attr.Metadata.Attributes)
                    {
                        var fn = mAt.Name;
                        if (!METADATA_FIELDS.Contains(fn))
                        {
                            continue;
                        }

                        var mv = mAt.Value;
                        if (mv.IsNullOrWhiteSpace())
                        {
                            continue;
                        }

                        if (fn == "Description" || fn == "Placeholder" || fn == "LookupDict" || fn == "Hint")
                        {
                            mv = OnLocalizeString(schema, fn, mv, isoLang);
                        }

                        result[fn] = mv;
                    }
                }
            }


            return(result);
        }
Example #2
0
        /// <summary>
        /// Generates JSON object suitable for passing into WV.RecordModel.Record(...) constructor on the client.
        /// Pass target to select attributes targeted to ANY target or to the specified one, for example
        ///  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(Doc doc,
                                                       Exception validationError,
                                                       Atom isoLang,
                                                       string recID  = null,
                                                       string target = null,
                                                       ModelFieldValueListLookupFunc valueListLookup = null)
        {
            var result = new JsonDataMap();

            if (doc == null)
            {
                return(result);
            }
            if (recID.IsNullOrWhiteSpace())
            {
                recID = Guid.NewGuid().ToString();
            }

            result["OK"] = true;
            result["ID"] = recID;
            if (!isoLang.IsZero)
            {
                result["ISOLang"] = isoLang;
            }

            //20140914 DKh
            var form = doc as Form;

            if (form != null)
            {
                result[Form.JSON_MODE_PROPERTY] = form.FormMode;
                result[Form.JSON_CSRF_PROPERTY] = form.CSRFToken;

                //20160123 DKh
                if (form.HasRoundtripBag)
                {
                    result[Form.JSON_ROUNDTRIP_PROPERTY] = form.RoundtripBag.ToJson(JsonWritingOptions.CompactASCII);
                }
            }

            var fields = new JsonDataArray();

            result["fields"] = fields;

            var schemaName = doc.Schema.Name;

            if (doc.Schema.TypedDocType != null)
            {
                schemaName = doc.Schema.TypedDocType.FullName;
            }

            foreach (var sfdef in doc.Schema.FieldDefs.Where(fd => !fd.NonUI))
            {
                var fdef = doc.GetClientFieldDef(sfdef, target, isoLang);
                if (fdef == null || fdef.NonUI)
                {
                    continue;
                }

                var fld = new JsonDataMap();
                fields.Add(fld);
                fld["def"] = FieldDefToJSON(doc, schemaName, fdef, target, isoLang, valueListLookup);
                var val = doc.GetClientFieldValue(sfdef, target, isoLang);
                if (val is GDID && ((GDID)val).IsZero)
                {
                    val = null;
                }
                fld["val"] = val;
                var ferr = validationError as FieldValidationException;
                //field level exception
                if (ferr != null && ferr.FieldName == fdef.Name)
                {
                    fld["error"]     = ferr.ToMessageWithType();
                    fld["errorText"] = OnLocalizeString(schemaName, "errorText", ferr.ClientMessage, isoLang);
                }
            }

            //record level
            if (validationError != null && !(validationError is FieldValidationException))
            {
                result["error"]     = validationError.ToMessageWithType();
                result["errorText"] = OnLocalizeString(schemaName, "errorText", validationError.Message, isoLang);
            }

            return(result);
        }
Example #3
0
        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
              var names = Enum.GetNames(fdef.NonNullableType);
              var values = new JSONDataMap(true);
              foreach(var name in names)
            values[name] = name;

              result["LookupDict"] = values;
            }

            var attr = fdef[target];
            if (attr!=null)
            {
            if (attr.Description!=null) result["Description"] = OnLocalizeString(schema, "Description", attr.Description, isoLang);
            var str =  attr.StoreFlag==StoreFlag.OnlyStore || attr.StoreFlag==StoreFlag.LoadAndStore;
            if (!str) result["Stored"] = str;
            if (attr.Required) result["Required"] = attr.Required;
            if (!attr.Visible) result["Visible"] = attr.Visible;
            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.Default!=null) result["DefaultValue"] = attr.Default;
            if (attr.ValueList.IsNotNullOrWhiteSpace())
            {
              var vl = OnLocalizeString(schema, "LookupDict", attr.ValueList, isoLang);
              result["LookupDict"] = FieldAttribute.ParseValueListString(vl);
            }
            else
            {
              var valueList = valueListLookup!=null ? valueListLookup(this, row, fdef, target, isoLang)
                                                    : row.GetClientFieldValueList(this, fdef, target, isoLang);

              if (valueList==null && attr.HasValueList)
                valueList = attr.ParseValueList();

              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)
            {
            foreach(var fn in METADATA_FIELDS)
            {
              var mv = attr.Metadata.AttrByName(fn).Value;
              if (mv.IsNullOrWhiteSpace()) continue;

              if (fn=="Description"||fn=="Placeholder"||fn=="LookupDict"||fn=="Hint")
                mv = OnLocalizeString(schema, fn, mv, isoLang);

              result[fn] = mv;
            }
            }

            return result;
        }
Example #4
0
        /// <summary>
        /// Generates JSON object suitable for passing into WV.RecordModel.Record(...) constructor on the client.
        /// Pass target to select attributes targeted to ANY target or to the specified one, for example
        ///  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,
                                                       ModelFieldValueListLookupFunc valueListLookup = null)
        {
            var result = new JSONDataMap();

            if (row == null)
            {
                return(result);
            }
            if (recID.IsNullOrWhiteSpace())
            {
                recID = Guid.NewGuid().ToString();
            }

            result["OK"] = true;
            result["ID"] = recID;
            if (isoLang.IsNotNullOrWhiteSpace())
            {
                result["ISOLang"] = isoLang;
            }

            //20140914 DKh
            var form = row as FormModel;

            if (form != null)
            {
                result[FormModel.JSON_MODE_PROPERTY] = form.FormMode;
                result[FormModel.JSON_CSRF_PROPERTY] = form.CSRFToken;

                //20160123 DKh
                if (form.HasRoundtripBag)
                {
                    result[FormModel.JSON_ROUNDTRIP_PROPERTY] = form.RoundtripBag.ToJSON(JSONWritingOptions.CompactASCII);
                }
            }

            var fields = new JSONDataArray();

            result["fields"] = fields;

            var schemaName = row.Schema.Name;

            if (row.Schema.TypedRowType != null)
            {
                schemaName = row.Schema.TypedRowType.FullName;
            }

            foreach (var fdef in row.Schema.FieldDefs.Where(fd => !fd.NonUI))
            {
                var fld = new JSONDataMap();
                fields.Add(fld);
                fld["def"] = FieldDefToJSON(row, schemaName, fdef, target, isoLang, valueListLookup);
                fld["val"] = row.GetFieldValue(fdef);
                var ferr = validationError as CRUDFieldValidationException;
                //field level exception
                if (ferr != null && ferr.FieldName == fdef.Name)
                {
                    fld["error"]     = ferr.ToMessageWithType();
                    fld["errorText"] = OnLocalizeString(schemaName, "errorText", ferr.ClientMessage, isoLang);
                }
            }

            //record level
            if (validationError != null && !(validationError is CRUDFieldValidationException))
            {
                result["error"]     = validationError.ToMessageWithType();
                result["errorText"] = OnLocalizeString(schemaName, "errorText", validationError.Message, isoLang);
            }

            return(result);
        }
Example #5
0
        /// <summary>
        /// Generates JSON object suitable for passing into WV.RecordModel.Record(...) constructor on the client.
        /// Pass target to select attributes targeted to ANY target or to the specified one, for example
        ///  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,
            ModelFieldValueListLookupFunc valueListLookup = null)
        {
            var result = new JSONDataMap();
            if (row==null) return result;
            if (recID.IsNullOrWhiteSpace()) recID = Guid.NewGuid().ToString();

            result["OK"] = true;
            result["ID"] = recID;
            if (isoLang.IsNotNullOrWhiteSpace())
              result["ISOLang"] = isoLang;

            //20140914 DKh
            var form = row as FormModel;
            if (form != null)
            {
              result[FormModel.JSON_MODE_PROPERTY] = form.FormMode;
              result[FormModel.JSON_CSRF_PROPERTY] = form.CSRFToken;

              //20160123 DKh
              if (form.HasRoundtripBag)
            result[FormModel.JSON_ROUNDTRIP_PROPERTY] = form.RoundtripBag.ToJSON(JSONWritingOptions.CompactASCII);
            }

            var fields = new JSONDataArray();
            result["fields"] = fields;

            var schemaName = row.Schema.Name;
            if (row.Schema.TypedRowType!=null) schemaName = row.Schema.TypedRowType.FullName;

            foreach(var sfdef in row.Schema.FieldDefs.Where(fd=>!fd.NonUI))
            {
              var fdef = row.GetClientFieldDef(this, sfdef, target, isoLang);
              if (fdef==null || fdef.NonUI) continue;

              var fld = new JSONDataMap();
              fields.Add(fld);
              fld["def"] = FieldDefToJSON(row, schemaName, fdef, target, isoLang, valueListLookup);
              var val = row.GetClientFieldValue(this, sfdef, target, isoLang);
              if (val is GDID && ((GDID)val).IsZero) val = null;
              fld["val"] = val;
              var ferr = validationError as CRUDFieldValidationException;
              //field level exception
              if (ferr!= null && ferr.FieldName==fdef.Name)
              {
            fld["error"] = ferr.ToMessageWithType();
            fld["errorText"] = OnLocalizeString(schemaName, "errorText", ferr.ClientMessage, isoLang);
              }
            }

            //record level
            if (validationError!=null && !(validationError is CRUDFieldValidationException))
            {
              result["error"] = validationError.ToMessageWithType();
              result["errorText"] = OnLocalizeString(schemaName, "errorText", validationError.Message, isoLang);
            }

            return result;
        }