private bool FillRecord(object rec, Tuple <long, string> pair)
        {
            long   lineNo;
            string line;

            lineNo = pair.Item1;
            line   = pair.Item2;

            string[] fieldValues = line.Split(Configuration.Delimiter, Configuration.StringSplitOptions, Configuration.QuoteChar);
            if (Configuration.ColumnCountStrict)
            {
                if (fieldValues.Length != Configuration.CSVRecordFieldConfigurations.Count)
                {
                    throw new ChoParserException("Incorrect number of field values found at line [{2}]. Expected [{0}] field values. Found [{1}] field values.".FormatString(Configuration.CSVRecordFieldConfigurations.Count, fieldValues.Length, pair.Item1));
                }
            }

            if (_fieldNames != null) //Configuration.FileHeaderConfiguration.HasHeaderRecord && Configuration.ColumnOrderStrict)
            {
                if (this.fieldNameValues == null)
                {
                    this.fieldNameValues = InitFieldNameValuesDict();
                }
                ToFieldNameValues(fieldNameValues, fieldValues);
            }
            ValidateLine(pair.Item1, fieldValues);

            object fieldValue = null;
            ChoCSVRecordFieldConfiguration fieldConfig = null;
            PropertyInfo pi = null;

            foreach (KeyValuePair <string, ChoCSVRecordFieldConfiguration> kvp in Configuration.FCArray)
            {
                fieldValue  = null;
                fieldConfig = kvp.Value;
                if (Configuration.PIDict != null)
                {
                    Configuration.PIDict.TryGetValue(kvp.Key, out pi);
                }

                if (fieldNameValues != null)
                {
                    if (fieldNameValues.ContainsKey(fieldConfig.FieldName))
                    {
                        fieldValue = fieldNameValues[fieldConfig.FieldName];
                    }
                    else
                    {
                        if (Configuration.ColumnOrderStrict)
                        {
                            throw new ChoParserException("No matching '{0}' field header found.".FormatString(fieldConfig.FieldName));
                        }
                    }
                }
                else
                {
                    if (fieldConfig.FieldPosition - 1 < fieldValues.Length)
                    {
                        fieldValue = fieldValues[fieldConfig.FieldPosition - 1];
                    }
                    else
                    {
                        throw new ChoParserException("Missing field value for '{0}' [Position: {1}] field.".FormatString(fieldConfig.FieldName, fieldConfig.FieldPosition));
                    }
                }

                //if (Configuration.FileHeaderConfiguration.HasHeaderRecord && Configuration.ColumnOrderStrict)
                //{
                //    if (fieldNameValues.ContainsKey(fieldConfig.FieldName))
                //        fieldValue = fieldNameValues[fieldConfig.FieldName];
                //    else if (Configuration.ColumnCountStrict)
                //        throw new ChoParserException("No matching '{0}' field header found.".FormatString(fieldConfig.FieldName));
                //}
                //else
                //{
                //    if (fieldConfig.FieldPosition - 1 < fieldValues.Length)
                //        fieldValue = fieldValues[fieldConfig.FieldPosition - 1];
                //    else if (Configuration.ColumnCountStrict)
                //        throw new ChoParserException("Missing field value for '{0}' [Position: {1}] field.".FormatString(fieldConfig.FieldName, fieldConfig.FieldPosition));
                //}

                if (Configuration.IsDynamicObject)
                {
                    if (kvp.Value.FieldType == null)
                    {
                        kvp.Value.FieldType = typeof(string);
                    }
                }
                else
                {
                    if (pi != null)
                    {
                        kvp.Value.FieldType = pi.PropertyType;
                    }
                    else
                    {
                        kvp.Value.FieldType = typeof(string);
                    }
                }

                fieldValue = CleanFieldValue(fieldConfig, kvp.Value.FieldType, fieldValue as string);

                if (!RaiseBeforeRecordFieldLoad(rec, pair.Item1, kvp.Key, ref fieldValue))
                {
                    continue;
                }

                try
                {
                    bool ignoreFieldValue = fieldConfig.IgnoreFieldValue(fieldValue);
                    if (ignoreFieldValue)
                    {
                        fieldValue = fieldConfig.IsDefaultValueSpecified ? fieldConfig.DefaultValue : null;
                    }

                    if (Configuration.IsDynamicObject)
                    {
                        var dict = rec as IDictionary <string, Object>;

                        dict.ConvertNSetMemberValue(kvp.Key, kvp.Value, ref fieldValue, Configuration.Culture);

                        if ((Configuration.ObjectValidationMode & ChoObjectValidationMode.MemberLevel) == ChoObjectValidationMode.MemberLevel)
                        {
                            dict.DoMemberLevelValidation(kvp.Key, kvp.Value, Configuration.ObjectValidationMode);
                        }
                    }
                    else
                    {
                        if (pi != null)
                        {
                            rec.ConvertNSetMemberValue(kvp.Key, kvp.Value, ref fieldValue, Configuration.Culture);
                        }
                        else
                        {
                            throw new ChoMissingRecordFieldException("Missing '{0}' property in {1} type.".FormatString(kvp.Key, ChoType.GetTypeName(rec)));
                        }

                        if ((Configuration.ObjectValidationMode & ChoObjectValidationMode.MemberLevel) == ChoObjectValidationMode.MemberLevel)
                        {
                            rec.DoMemberLevelValidation(kvp.Key, kvp.Value, Configuration.ObjectValidationMode);
                        }
                    }

                    if (!RaiseAfterRecordFieldLoad(rec, pair.Item1, kvp.Key, fieldValue))
                    {
                        return(false);
                    }
                }
                catch (ChoParserException)
                {
                    throw;
                }
                catch (ChoMissingRecordFieldException)
                {
                    if (Configuration.ThrowAndStopOnMissingField)
                    {
                        throw;
                    }
                }
                catch (Exception ex)
                {
                    ChoETLFramework.HandleException(ex);

                    if (fieldConfig.ErrorMode == ChoErrorMode.ThrowAndStop)
                    {
                        throw;
                    }

                    try
                    {
                        if (Configuration.IsDynamicObject)
                        {
                            var dict = rec as IDictionary <string, Object>;

                            if (dict.SetFallbackValue(kvp.Key, kvp.Value, Configuration.Culture, ref fieldValue))
                            {
                                dict.DoMemberLevelValidation(kvp.Key, kvp.Value, Configuration.ObjectValidationMode);
                            }
                            else if (dict.SetDefaultValue(kvp.Key, kvp.Value, Configuration.Culture))
                            {
                                dict.DoMemberLevelValidation(kvp.Key, kvp.Value, Configuration.ObjectValidationMode);
                            }
                            else
                            {
                                throw new ChoReaderException($"Failed to parse '{fieldValue}' value for '{fieldConfig.FieldName}' field.", ex);
                            }
                        }
                        else if (pi != null)
                        {
                            if (rec.SetFallbackValue(kvp.Key, kvp.Value, Configuration.Culture))
                            {
                                rec.DoMemberLevelValidation(kvp.Key, kvp.Value, Configuration.ObjectValidationMode);
                            }
                            else if (rec.SetDefaultValue(kvp.Key, kvp.Value, Configuration.Culture))
                            {
                                rec.DoMemberLevelValidation(kvp.Key, kvp.Value, Configuration.ObjectValidationMode);
                            }
                            else
                            {
                                throw new ChoReaderException($"Failed to parse '{fieldValue}' value for '{fieldConfig.FieldName}' field.", ex);
                            }
                        }
                        else
                        {
                            throw new ChoReaderException($"Failed to parse '{fieldValue}' value for '{fieldConfig.FieldName}' field.", ex);
                        }
                    }
                    catch (Exception innerEx)
                    {
                        if (ex == innerEx.InnerException)
                        {
                            if (fieldConfig.ErrorMode == ChoErrorMode.IgnoreAndContinue)
                            {
                                continue;
                            }
                            else
                            {
                                if (!RaiseRecordFieldLoadError(rec, pair.Item1, kvp.Key, fieldValue, ex))
                                {
                                    throw new ChoReaderException($"Failed to parse '{fieldValue}' value for '{fieldConfig.FieldName}' field.", ex);
                                }
                            }
                        }
                        else
                        {
                            throw new ChoReaderException("Failed to assign '{0}' fallback value to '{1}' field.".FormatString(fieldValue, fieldConfig.FieldName), innerEx);
                        }
                    }
                }
            }

            return(true);
        }
Beispiel #2
0
        private bool FillRecord(object rec, Tuple <int, string> pair)
        {
            int    lineNo;
            string line;

            lineNo = pair.Item1;
            line   = pair.Item2;

            object fieldValue = null;

            string[] fieldValues = (from x in line.Split(Configuration.Delimiter, Configuration.StringSplitOptions, Configuration.QuoteChar)
                                    select x).ToArray();
            if (Configuration.ColumnCountStrict)
            {
                if (fieldValues.Length != Configuration.RecordFieldConfigurations.Count)
                {
                    throw new ChoParserException("Incorrect number of field values found at line [{2}]. Expected [{0}] field values. Found [{1}] field values.".FormatString(Configuration.RecordFieldConfigurations.Count, fieldValues.Length, pair.Item1));
                }
            }

            Dictionary <string, string> _fieldNameValues = ToFieldNameValues(fieldValues);

            ValidateLine(pair.Item1, fieldValues);

            ChoCSVRecordFieldConfiguration fieldConfig = null;

            foreach (KeyValuePair <string, ChoCSVRecordFieldConfiguration> kvp in Configuration.RecordFieldConfigurationsDict)
            {
                fieldConfig = kvp.Value;

                if (Configuration.CSVFileHeaderConfiguration.HasHeaderRecord)
                {
                    if (_fieldNameValues.ContainsKey(fieldConfig.FieldName))
                    {
                        fieldValue = _fieldNameValues[fieldConfig.FieldName];
                    }
                    else if (Configuration.ColumnCountStrict)
                    {
                        throw new ChoParserException("No matching '{0}' field header found.".FormatString(fieldConfig.FieldName));
                    }
                }
                else
                {
                    if (fieldConfig.FieldPosition - 1 < fieldValues.Length)
                    {
                        fieldValue = fieldValues[fieldConfig.FieldPosition - 1];
                    }
                    else if (Configuration.ColumnCountStrict)
                    {
                        throw new ChoParserException("Missing field value for {0} [Position: {1}] field.".FormatString(fieldConfig.FieldName, fieldConfig.FieldPosition));
                    }
                }

                fieldValue = CleanFieldValue(fieldConfig, fieldValue as string);

                if (!RaiseBeforeRecordFieldLoad(rec, pair.Item1, kvp.Key, ref fieldValue))
                {
                    continue;
                }

                try
                {
                    bool ignoreFieldValue = fieldConfig.IgnoreFieldValue(fieldValue);
                    if (!ignoreFieldValue)
                    {
                        if (rec is ExpandoObject)
                        {
                            if (fieldConfig.FieldType != typeof(string))
                            {
                                fieldValue = ChoConvert.ConvertTo(fieldValue, fieldConfig.FieldType, Configuration.Culture);
                            }
                            var x = rec as IDictionary <string, Object>;
                            x.Add(kvp.Key, fieldValue);
                        }
                        else
                        {
                            if (ChoType.HasProperty(rec.GetType(), kvp.Key))
                            {
                                ChoType.ConvertNSetMemberValue(rec, kvp.Key, fieldValue);
                                fieldValue = ChoType.GetMemberValue(rec, kvp.Key);

                                if ((Configuration.ObjectValidationMode & ChoObjectValidationMode.MemberLevel) == ChoObjectValidationMode.MemberLevel)
                                {
                                    ChoValidator.ValididateFor(rec, kvp.Key);
                                }
                            }
                            else
                            {
                                throw new ChoMissingRecordFieldException("Missing '{0}' property in {1} type.".FormatString(kvp.Key, ChoType.GetTypeName(rec)));
                            }
                        }
                    }

                    if (!RaiseAfterRecordFieldLoad(rec, pair.Item1, kvp.Key, fieldValue))
                    {
                        return(false);
                    }
                }
                catch (ChoParserException)
                {
                    throw;
                }
                catch (ChoMissingRecordFieldException)
                {
                    if (Configuration.ThrowAndStopOnMissingField)
                    {
                        throw;
                    }
                }
                catch (Exception ex)
                {
                    ChoETLFramework.HandleException(ex);

                    if (fieldConfig.ErrorMode == ChoErrorMode.ThrowAndStop)
                    {
                        throw;
                    }
                    try
                    {
                        ChoFallbackValueAttribute fbAttr = ChoTypeDescriptor.GetPropetyAttribute <ChoFallbackValueAttribute>(rec.GetType(), kvp.Key);
                        if (fbAttr != null)
                        {
                            if (!fbAttr.Value.IsNullOrDbNull())
                            {
                                ChoType.ConvertNSetMemberValue(rec, kvp.Key, fbAttr.Value);
                            }
                        }
                        else
                        {
                            throw;
                        }
                    }
                    catch
                    {
                        if (fieldConfig.ErrorMode == ChoErrorMode.IgnoreAndContinue)
                        {
                            continue;
                        }
                        else if (fieldConfig.ErrorMode == ChoErrorMode.ReportAndContinue)
                        {
                            if (!RaiseRecordFieldLoadError(rec, pair.Item1, kvp.Key, fieldValue, ex))
                            {
                                throw;
                            }
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
            }

            return(true);
        }
Beispiel #3
0
        private bool FillRecord(object rec, Tuple <int, string> pair)
        {
            int    lineNo;
            string line;

            lineNo = pair.Item1;
            line   = pair.Item2;

            object fieldValue = null;

            string[] fieldValues = (from x in line.Split(Configuration.Delimiter, Configuration.StringSplitOptions, Configuration.QuoteChar)
                                    select x).ToArray();
            if (Configuration.ColumnCountStrict)
            {
                if (fieldValues.Length != Configuration.CSVRecordFieldConfigurations.Count)
                {
                    throw new ChoParserException("Incorrect number of field values found at line [{2}]. Expected [{0}] field values. Found [{1}] field values.".FormatString(Configuration.CSVRecordFieldConfigurations.Count, fieldValues.Length, pair.Item1));
                }
            }

            Dictionary <string, string> fieldNameValues = ToFieldNameValues(fieldValues);

            ValidateLine(pair.Item1, fieldValues);

            ChoCSVRecordFieldConfiguration fieldConfig = null;

            foreach (KeyValuePair <string, ChoCSVRecordFieldConfiguration> kvp in Configuration.RecordFieldConfigurationsDict)
            {
                fieldValue  = null;
                fieldConfig = kvp.Value;

                if (Configuration.FileHeaderConfiguration.HasHeaderRecord)
                {
                    if (fieldNameValues.ContainsKey(fieldConfig.FieldName))
                    {
                        fieldValue = fieldNameValues[fieldConfig.FieldName];
                    }
                    else if (Configuration.ColumnCountStrict)
                    {
                        throw new ChoParserException("No matching '{0}' field header found.".FormatString(fieldConfig.FieldName));
                    }
                }
                else
                {
                    if (fieldConfig.FieldPosition - 1 < fieldValues.Length)
                    {
                        fieldValue = fieldValues[fieldConfig.FieldPosition - 1];
                    }
                    else if (Configuration.ColumnCountStrict)
                    {
                        throw new ChoParserException("Missing field value for {0} [Position: {1}] field.".FormatString(fieldConfig.FieldName, fieldConfig.FieldPosition));
                    }
                }

                fieldValue = CleanFieldValue(fieldConfig, fieldValue as string);

                if (!RaiseBeforeRecordFieldLoad(rec, pair.Item1, kvp.Key, ref fieldValue))
                {
                    return(false);
                }

                try
                {
                    bool ignoreFieldValue = fieldConfig.IgnoreFieldValue(fieldValue);
                    if (ignoreFieldValue)
                    {
                        fieldValue = null;
                    }

                    if (rec is ExpandoObject)
                    {
                        var dict = rec as IDictionary <string, Object>;

                        dict.SetDefaultValue(kvp.Key, kvp.Value, Configuration.Culture);

                        if (ignoreFieldValue)
                        {
                            dict.AddOrUpdate(kvp.Key, fieldValue);
                        }
                        else
                        {
                            dict.ConvertNSetMemberValue(kvp.Key, kvp.Value, ref fieldValue, Configuration.Culture);
                        }

                        dict.DoMemberLevelValidation(kvp.Key, kvp.Value, Configuration.ObjectValidationMode);
                    }
                    else
                    {
                        if (ChoType.HasProperty(rec.GetType(), kvp.Key))
                        {
                            rec.SetDefaultValue(kvp.Key, kvp.Value, Configuration.Culture);

                            if (!ignoreFieldValue)
                            {
                                rec.ConvertNSetMemberValue(kvp.Key, kvp.Value, ref fieldValue, Configuration.Culture);
                            }
                        }
                        else
                        {
                            throw new ChoMissingRecordFieldException("Missing '{0}' property in {1} type.".FormatString(kvp.Key, ChoType.GetTypeName(rec)));
                        }

                        rec.DoMemberLevelValidation(kvp.Key, kvp.Value, Configuration.ObjectValidationMode);
                    }

                    if (!RaiseAfterRecordFieldLoad(rec, pair.Item1, kvp.Key, fieldValue))
                    {
                        return(false);
                    }
                }
                catch (ChoParserException)
                {
                    throw;
                }
                catch (ChoMissingRecordFieldException)
                {
                    if (Configuration.ThrowAndStopOnMissingField)
                    {
                        throw;
                    }
                }
                catch (Exception ex)
                {
                    ChoETLFramework.HandleException(ex);

                    if (fieldConfig.ErrorMode == ChoErrorMode.ThrowAndStop)
                    {
                        throw;
                    }

                    try
                    {
                        if (rec is ExpandoObject)
                        {
                            var dict = rec as IDictionary <string, Object>;

                            if (dict.SetFallbackValue(kvp.Key, kvp.Value, Configuration.Culture, ref fieldValue))
                            {
                                dict.DoMemberLevelValidation(kvp.Key, kvp.Value, Configuration.ObjectValidationMode);
                            }
                            else
                            {
                                throw new ChoParserException($"Failed to parse '{fieldValue}' value for '{fieldConfig.FieldName}' field.", ex);
                            }
                        }
                        else if (ChoType.HasProperty(rec.GetType(), kvp.Key) && rec.SetFallbackValue(kvp.Key, kvp.Value, Configuration.Culture))
                        {
                            rec.DoMemberLevelValidation(kvp.Key, kvp.Value, Configuration.ObjectValidationMode);
                        }
                        else
                        {
                            throw new ChoParserException($"Failed to parse '{fieldValue}' value for '{fieldConfig.FieldName}' field.", ex);
                        }
                    }
                    catch (Exception innerEx)
                    {
                        if (ex == innerEx.InnerException)
                        {
                            if (fieldConfig.ErrorMode == ChoErrorMode.IgnoreAndContinue)
                            {
                                continue;
                            }
                            else
                            {
                                if (!RaiseRecordFieldLoadError(rec, pair.Item1, kvp.Key, fieldValue, ex))
                                {
                                    throw new ChoParserException($"Failed to parse '{fieldValue}' value for '{fieldConfig.FieldName}' field.", ex);
                                }
                            }
                        }
                        else
                        {
                            throw new ChoParserException("Failed to assign '{0}' fallback value to '{1}' field.".FormatString(fieldValue, fieldConfig.FieldName), innerEx);
                        }
                    }
                }
            }

            return(true);
        }