Пример #1
0
        private bool isFieldDefLoaded(string target, Schema.FieldDef def)
        {
            var atr = def[target];

            return((atr == null) ? true : (atr.StoreFlag == StoreFlag.LoadAndStore || atr.StoreFlag == StoreFlag.OnlyLoad));
        }
Пример #2
0
        /// <summary>
        /// Validates row field using Schema.FieldDef settings.
        /// This method is invoked by base Validate() implementation.
        /// The method is not expected to throw exception in case of failed validation, rather return exception instance because
        ///  throwing exception really hampers validation performance when many rows need to be validated
        /// </summary>
        protected virtual Exception ValidateField(string targetName, Schema.FieldDef fdef)
        {
            var atr = fdef[targetName];

            if (atr == null)
            {
                return(null);
            }

            var value = GetFieldValue(fdef);

            if (value == null ||
                (value is string && ((string)value).IsNullOrWhiteSpace()) ||
                (value is Distributed.GDID && ((Distributed.GDID)value).IsZero)
                )
            {
                if (atr.Required)
                {
                    return(new CRUDFieldValidationException(Schema.Name, fdef.Name, StringConsts.CRUD_FIELD_VALUE_REQUIRED_ERROR));
                }

                return(null);
            }

            if (value is IValidatable)
            {
                return(((IValidatable)value).Validate(targetName));
            }

            if (value is IEnumerable <IValidatable> )  //List<IValidatable>, IValidatable[]
            {
                foreach (var v in (IEnumerable <IValidatable>)value)
                {
                    if (v == null)
                    {
                        continue;
                    }
                    var error = v.Validate(targetName);
                    if (error != null)
                    {
                        return(error);
                    }
                }
                return(null);
            }

            if (value is IEnumerable <KeyValuePair <string, IValidatable> > )//Dictionary<string, IValidatable>
            {
                foreach (var kv in (IEnumerable <KeyValuePair <string, IValidatable> >)value)
                {
                    var v = kv.Value;
                    if (v == null)
                    {
                        continue;
                    }
                    var error = v.Validate(targetName);
                    if (error != null)
                    {
                        return(error);
                    }
                }
                return(null);
            }

            if (atr.HasValueList)    //check dictionary
            {
                var parsed = atr.ParseValueList();
                if (!parsed.ContainsKey(value.ToString()))
                {
                    return(new CRUDFieldValidationException(Schema.Name, fdef.Name, StringConsts.CRUD_FIELD_VALUE_IS_NOT_IN_LIST_ERROR));
                }
            }

            if (atr.MinLength > 0)
            {
                if (value.ToString().Length < atr.MinLength)
                {
                    return(new CRUDFieldValidationException(Schema.Name, fdef.Name, StringConsts.CRUD_FIELD_VALUE_MIN_LENGTH_ERROR));
                }
            }

            if (atr.MaxLength > 0)
            {
                if (value.ToString().Length > atr.MaxLength)
                {
                    return(new CRUDFieldValidationException(Schema.Name, fdef.Name, StringConsts.CRUD_FIELD_VALUE_MAX_LENGTH_ERROR));
                }
            }

            if (atr.Kind == DataKind.ScreenName)
            {
                if (!NFX.Parsing.DataEntryUtils.CheckScreenName(value.ToString()))
                {
                    return(new CRUDFieldValidationException(Schema.Name, fdef.Name, StringConsts.CRUD_FIELD_VALUE_SCREEN_NAME_ERROR));
                }
            }
            else if (atr.Kind == DataKind.EMail)
            {
                if (!NFX.Parsing.DataEntryUtils.CheckEMail(value.ToString()))
                {
                    return(new CRUDFieldValidationException(Schema.Name, fdef.Name, StringConsts.CRUD_FIELD_VALUE_EMAIL_ERROR));
                }
            }
            else if (atr.Kind == DataKind.Telephone)
            {
                if (!NFX.Parsing.DataEntryUtils.CheckTelephone(value.ToString()))
                {
                    return(new CRUDFieldValidationException(Schema.Name, fdef.Name, StringConsts.CRUD_FIELD_VALUE_PHONE_ERROR));
                }
            }



            if (value is IComparable)
            {
                var error = CheckMinMax(atr, fdef.Name, (IComparable)value);
                if (error != null)
                {
                    return(error);
                }
            }

            if (atr.FormatRegExp.IsNotNullOrWhiteSpace())
            {
                //For those VERY RARE cases when RegExpFormat may need to be applied to complex types, i.e. StringBuilder
                //set the flag in metadata to true, otherwise regexp gets matched only for STRINGS
                var complex = atr.Metadata == null? false
                                                   : atr.Metadata
                              .AttrByName("validate-format-regexp-complex-types")
                              .ValueAsBool(false);
                if (complex || value is string)
                {
                    if (!System.Text.RegularExpressions.Regex.IsMatch(value.ToString(), atr.FormatRegExp))
                    {
                        return(new CRUDFieldValidationException(Schema.Name, fdef.Name,
                                                                StringConsts.CRUD_FIELD_VALUE_REGEXP_ERROR.Args(atr.FormatDescription ?? "Input format: {0}".Args(atr.FormatRegExp))));
                    }
                }
            }

            return(null);
        }
Пример #3
0
        /// <summary>
        /// Converts field value to the type specified by Schema.FieldDef. For example converts GDID->ulong or ulong->GDID.
        /// This method can be overridden to perform custom handling of types,
        ///  for example one can assign bool field as "Si" that would convert to TRUE.
        /// This method is called by SetFieldValue(...) before assigning actual field buffer
        /// </summary>
        /// <param name="fdef">Field being converted</param>
        /// <param name="value">Value to convert</param>
        /// <returns>Converted value before assignment to field buffer</returns>
        public virtual object ConvertFieldValueToDef(Schema.FieldDef fdef, object value)
        {
            if (value == DBNull.Value)
            {
                value = null;
            }

            if (value == null)
            {
                return(null);
            }

            var tv = value.GetType();

            if (tv != fdef.NonNullableType && !fdef.NonNullableType.IsAssignableFrom(tv))
            {
                if (value is ObjectValueConversion.TriStateBool)
                {
                    var tsb = (ObjectValueConversion.TriStateBool)value;
                    if (tsb == ObjectValueConversion.TriStateBool.Unspecified)
                    {
                        value = null;
                    }
                    else
                    {
                        value = tsb == ObjectValueConversion.TriStateBool.True;
                    }

                    return(value);
                }

                if (fdef.NonNullableType == typeof(ObjectValueConversion.TriStateBool))
                {
                    var nb = value.AsNullableBool();
                    if (!nb.HasValue)
                    {
                        value = ObjectValueConversion.TriStateBool.Unspecified;
                    }
                    else
                    {
                        value = nb.Value ? ObjectValueConversion.TriStateBool.True : ObjectValueConversion.TriStateBool.False;
                    }

                    return(value);
                }


                // 20150224 DKh, addedEra to GDID. Only GDIDS with ERA=0 can be converted to/from INT64
                if (fdef.NonNullableType == typeof(NFX.DataAccess.Distributed.GDID))
                {
                    if (tv == typeof(byte[]))//20151103 DKh GDID support for byte[]
                    {
                        value = new Distributed.GDID((byte[])value);
                    }
                    else if (tv == typeof(string))//20160504 Spol GDID support for string
                    {
                        var sv = (string)value;
                        if (sv.IsNotNullOrWhiteSpace())
                        {
                            value = Distributed.GDID.Parse((string)value);
                        }
                        else
                        {
                            value = fdef.Type == typeof(Distributed.GDID?) ? (Distributed.GDID?)null : Distributed.GDID.Zero;
                        }
                    }
                    else
                    {
                        value = new Distributed.GDID(0, (UInt64)Convert.ChangeType(value, typeof(UInt64)));
                    }

                    return(value);
                }

                if (tv == typeof(Distributed.GDID))
                {
                    if (fdef.NonNullableType == typeof(byte[]))
                    {
                        value = ((Distributed.GDID)value).Bytes;
                    }
                    else if (fdef.NonNullableType == typeof(string))
                    {
                        value = value.ToString();
                    }
                    else
                    {
                        var gdid = (Distributed.GDID)value;
                        if (gdid.Era != 0)
                        {
                            throw new CRUDException(StringConsts.CRUD_GDID_ERA_CONVERSION_ERROR.Args(fdef.Name, fdef.NonNullableType.Name));
                        }
                        value = gdid.ID;
                    }
                }
                else
                {
                    value = Convert.ChangeType(value, fdef.NonNullableType);
                }
            }  //Types Differ

            return(value);
        }
Пример #4
0
 /// <summary>
 /// Sets value of the field, for typerows it accesses property using reflection; for dynamic rows it sets data into
 ///  row buffer array using field index(order)
 /// </summary>
 public abstract void SetFieldValue(Schema.FieldDef fdef, object value);
Пример #5
0
 /// <summary>
 /// Gets value of the field, for typerows it accesses property using reflection; for dynamic rows it reads data from
 ///  row buffer array using field index(order)
 /// </summary>
 public abstract object GetFieldValue(Schema.FieldDef fdef);
Пример #6
0
Файл: Row.cs Проект: ame89/nfx
        /// <summary>
        /// Validates row field using Schema.FieldDef settings.
        /// This method is invoked by base Validate() implementation.
        /// The method is not expected to throw exception in case of failed validation, rather return exception instance because
        ///  throwing exception really hampers validation performance when many rows need to be validated
        /// </summary>
        protected virtual Exception ValidateField(string targetName, Schema.FieldDef fdef)
        {
            var atr = fdef[targetName];

            if (atr == null)
            {
                return(null);
            }

            var value = GetFieldValue(fdef);

            if (value == null || (value is string && ((string)value).IsNullOrWhiteSpace()))
            {
                if (atr.Required)
                {
                    return(new CRUDFieldValidationException(Schema.Name, fdef.Name, StringConsts.CRUD_FIELD_VALUE_REQUIRED_ERROR));
                }

                return(null);
            }

            if (value is IValidatable)
            {
                return(((IValidatable)value).Validate(targetName));
            }

            if (value is IEnumerable <IValidatable> )  //List<IValidatable>, IValidatable[]
            {
                foreach (var v in (IEnumerable <IValidatable>)value)
                {
                    if (v == null)
                    {
                        continue;
                    }
                    var error = v.Validate(targetName);
                    if (error != null)
                    {
                        return(error);
                    }
                }
                return(null);
            }

            if (value is IEnumerable <KeyValuePair <string, IValidatable> > )//Dictionary<string, IValidatable>
            {
                foreach (var kv in (IEnumerable <KeyValuePair <string, IValidatable> >)value)
                {
                    var v = kv.Value;
                    if (v == null)
                    {
                        continue;
                    }
                    var error = v.Validate(targetName);
                    if (error != null)
                    {
                        return(error);
                    }
                }
                return(null);
            }

            if (atr.ValueList.IsNotNullOrWhiteSpace())    //check dictionary
            {
                var parsed = atr.ParseValueList();
                if (!parsed.ContainsKey(value.ToString()))
                {
                    return(new CRUDFieldValidationException(Schema.Name, fdef.Name, StringConsts.CRUD_FIELD_VALUE_IS_NOT_IN_LIST_ERROR));
                }
            }

            if (atr.MinLength > 0)
            {
                if (value.ToString().Length < atr.MinLength)
                {
                    return(new CRUDFieldValidationException(Schema.Name, fdef.Name, StringConsts.CRUD_FIELD_VALUE_MAX_LENGTH_ERROR));
                }
            }

            if (atr.MaxLength > 0)
            {
                if (value.ToString().Length > atr.MaxLength)
                {
                    return(new CRUDFieldValidationException(Schema.Name, fdef.Name, StringConsts.CRUD_FIELD_VALUE_MAX_LENGTH_ERROR));
                }
            }

            if (atr.Kind == DataKind.ScreenName)
            {
                if (!NFX.Parsing.DataEntryUtils.CheckScreenName(value.ToString()))
                {
                    return(new CRUDFieldValidationException(Schema.Name, fdef.Name, StringConsts.CRUD_FIELD_VALUE_SCREEN_NAME_ERROR));
                }
            }

            if (value is IComparable)
            {
                var error = CheckMinMax(atr, fdef.Name, (IComparable)value);
                if (error != null)
                {
                    return(error);
                }
            }

            return(null);
        }
Пример #7
0
 public override void SetFieldValue(Schema.FieldDef fdef, object value)
 {
     value = ConvertFieldValueToDef(fdef, value);
     m_Data[fdef.Order] = value;
 }