コード例 #1
0
ファイル: ImportReader.cs プロジェクト: onesimoh/Andamio
        public virtual void Populate(DataGrid dataGrid)
        {
            if (dataGrid == null) throw new ArgumentNullException("dataGrid");

            try
            {
                Log.Info("Beginning Data Import...");
                DataGrid rawData = ReadRawData(dataGrid.Columns);
                if (!rawData.Rows.Any())
                {
                    throw new AbortException("Specified Data Source is empty.");
                }
                Log.Info(String.Format("Data successfully read from file. Total Rows: {0}.", rawData.Rows.Count()));

                /*
                *  Headers
                */

                Log.Info("Processing headers from imported data...");
                ProcessHeaders(dataGrid.Columns, rawData);
                Log.Info("Headers successfully processed.");

                /*
                *  Data
                */

                dataGrid.Empty();
                Log.Info("Processing imported data...");
                foreach (DataGridRow rawDataRow in rawData.Rows)
                {
                    Log.Trace(String.Format("Processing imported row '{0}'.", rawDataRow.Index()));

                    // Pass 1: Process Raw Data from Source
                    DataGridRow dataGridRow = dataGrid.Rows.New();
                    foreach (DataGridColumn dataColumn in dataGrid.Columns)
                    {
                        try
                        {
                            object cellValue = GetValue(dataColumn, rawDataRow);
                            DataGridCell cell = (cellValue != null) ? dataColumn.Cell(cellValue) : dataColumn.Cell();
                            dataGridRow.Cells.Add(cell);
                        }
                        catch (Exception exception)
                        {
                            throw new AbortException(String.Format("An error occurred while reading cell at Row: '{0}', Column: '{1}'."
                                , dataGridRow.Index() + 1
                                , dataColumn)
                                , exception);
                        }
                    }

                    // Pass 2: Process Mapped Columns
                    foreach (DataGridColumn mappedColumn in dataGrid.Columns.Where(match => match.HasMappedColumns))
                    {
                        try
                        {
                            DataGridCell cell = dataGridRow[mappedColumn.ColumnName];
                            object cellValue = GetMappedValue(mappedColumn, dataGridRow);
                            cell.OriginalValue = cellValue;
                        }
                        catch (Exception exception)
                        {
                            throw new AbortException(String.Format("An error occurred while reading cell at Row: '{0}', Column: '{1}'."
                                , dataGridRow.Index() + 1
                                , mappedColumn)
                                , exception);
                        }
                    }
                }

                Log.Info(String.Format("Imported data successfully processed. Total Rows processed: {0}", dataGrid.Rows.Count()));
            }
            catch (AbortException exception)
            {
                Log.Critrical("Data Import Failed!", exception);
                throw exception;
            }
            catch (Exception exception)
            {
                Log.Error("An unexpected Error occurred…", exception);
                Log.Critrical("Data Import Failed!");
                throw new AbortException(exception.Message, exception);
            }
        }