コード例 #1
0
        /// <summary>
        /// Prepares the mapping set.
        /// </summary>
        /// <param name="mapElColl">The map el coll.</param>
        /// <param name="dataSet">The data set.</param>
        /// <returns></returns>
        private MappedObject PrepareMappingSet(MappingElement mapElColl,
                                               DataTable dataTable, List <MappedObject> mappedObjectList)
        {
            DataColumn   dataCol  = null;
            MetaClass    metaType = DataContext.Current.GetMetaClass(mapElColl.ClassName);
            MappedObject retVal   = new MappedObject(mapElColl.ClassName, mapElColl);

            //Process each row
            foreach (DataRow dataRow in dataTable.Rows)
            {
                MetaObjectData moData = new MetaObjectData();

                //Always add primary key in MappingData
                if (!String.IsNullOrEmpty(mapElColl.PrimaryKeyName))
                {
                    moData.MapColumnData.Add(mapElColl.PrimaryKeyName,
                                             dataRow[mapElColl.PrimaryKeyName]);
                }
                //Prepare mapping values
                foreach (MappingRule mapEl in mapElColl)
                {
                    object value = null;

                    try
                    {
                        if (String.IsNullOrEmpty(mapEl.ColumnName))
                        {
                            continue;
                        }

                        dataCol = dataTable.Columns[mapEl.ColumnName];

                        //try find record in mapping definition document
                        BaseSourceValue valueGenerator =
                            BaseSourceValue.CreateValueSourceInstance(mapEl);

                        value = valueGenerator.GetValue(dataRow, dataCol,
                                                        mappedObjectList);

                        value = Value2MetaPropertyType(value, metaType.Fields[mapEl.FieldName]);
                    }
                    catch (Exception e)
                    {
                        MappingError errorInfo = new MappingError(MappingErrorType.SourceValueError,
                                                                  e.Message);
                        errorInfo.Row       = dataRow;
                        errorInfo.Column    = dataCol;
                        errorInfo.Table     = dataTable;
                        errorInfo.Exception = e;

                        MappingEngineErrorEventArgs args = new MappingEngineErrorEventArgs(errorInfo);
                        args.MappingRule    = mapEl;
                        args.MappingElement = mapElColl;

                        RaiseErrorEvent(args);

                        if ((args.ResolveError) && (_errResolveAtteptNum < _errResolveAtteptMax))
                        {
                            _errResolveAtteptNum++;
                            //Try resolve error
                            return(PrepareMappingSet(args.MappingElement, args.Error.Table,
                                                     mappedObjectList));
                        }
                        else
                        {
                            _errorInfoList.Add(errorInfo);
                            continue;
                        }
                    }

                    if (!moData.MapMetaFieldData.ContainsKey(mapEl.FieldName))
                    {
                        //Save mapped value
                        moData.MapMetaFieldData.Add(mapEl.FieldName, value);
                    }
                    if (!moData.MapColumnData.ContainsKey(dataCol.ColumnName))
                    {
                        //save previous row column value
                        moData.MapColumnData.Add(dataCol.ColumnName, dataRow[dataCol]);
                    }
                    //reset error resolve attempt
                    _errResolveAtteptNum = 0;
                }

                retVal.MetaObjets.Add(moData);
            }

            return(retVal);
        }
コード例 #2
0
ファイル: MappingEngine.cs プロジェクト: 0anion0/IBN
        /// <summary>
        /// Prepares the mapping set.
        /// </summary>
        /// <param name="mapElColl">The map el coll.</param>
        /// <param name="dataSet">The data set.</param>
        /// <returns></returns>
        private MappedObject PrepareMappingSet(MappingElement mapElColl, 
                                               DataTable dataTable, List<MappedObject> mappedObjectList)
        {
            DataColumn dataCol = null;
            MetaClass metaType = DataContext.Current.GetMetaClass(mapElColl.ClassName);
            MappedObject retVal = new MappedObject(mapElColl.ClassName, mapElColl);

            //Process each row
            foreach (DataRow dataRow in dataTable.Rows)
            {
                MetaObjectData moData = new MetaObjectData();

                //Always add primary key in MappingData
                if (!String.IsNullOrEmpty(mapElColl.PrimaryKeyName))
                {
                    moData.MapColumnData.Add(mapElColl.PrimaryKeyName,
                                             dataRow[mapElColl.PrimaryKeyName]);
                }
                //Prepare mapping values
                foreach (MappingRule mapEl in mapElColl)
                {
                    object value = null;

                    try
                    {
                        if (String.IsNullOrEmpty(mapEl.ColumnName))
                            continue;

                        dataCol = dataTable.Columns[mapEl.ColumnName];

                        //try find record in mapping definition document
                        BaseSourceValue valueGenerator =
                                        BaseSourceValue.CreateValueSourceInstance(mapEl);

                        value = valueGenerator.GetValue(dataRow, dataCol,
                                                               mappedObjectList);

                        value = Value2MetaPropertyType(value, metaType.Fields[mapEl.FieldName]);

                    }
                    catch (Exception e)
                    {
                        MappingError errorInfo = new MappingError(MappingErrorType.SourceValueError,
                                                       e.Message);
                        errorInfo.Row = dataRow;
                        errorInfo.Column = dataCol;
                        errorInfo.Table = dataTable;
                        errorInfo.Exception = e;

                        MappingEngineErrorEventArgs args = new MappingEngineErrorEventArgs(errorInfo);
                        args.MappingRule = mapEl;
                        args.MappingElement = mapElColl;

                        RaiseErrorEvent(args);

                        if ((args.ResolveError) && (_errResolveAtteptNum < _errResolveAtteptMax))
                        {
                            _errResolveAtteptNum++;
                            //Try resolve error
                            return PrepareMappingSet(args.MappingElement, args.Error.Table,
                                                     mappedObjectList);
                        }
                        else
                        {
                            _errorInfoList.Add(errorInfo);
                            continue;
                        }

                    }

                    if (!moData.MapMetaFieldData.ContainsKey(mapEl.FieldName))
                    {
                        //Save mapped value
                        moData.MapMetaFieldData.Add(mapEl.FieldName, value);
                    }
                    if (!moData.MapColumnData.ContainsKey(dataCol.ColumnName))
                    {
                        //save previous row column value
                        moData.MapColumnData.Add(dataCol.ColumnName, dataRow[dataCol]);
                    }
                    //reset error resolve attempt
                    _errResolveAtteptNum = 0;

                }

                retVal.MetaObjets.Add(moData);
            }

            return retVal;
        }