/// <summary>
        /// Create and set up a csv reader based on the data item definition given
        /// </summary>
        /// <param name="textReader">The text reader to inject in to the CSV reader</param>
        /// <param name="definition">The definition of the file</param>
        /// <returns>The newly configured CSV Reader</returns>
        private static CsvReader SetupReader(TextReader textReader, DataConnection connection, DataItemDefinition definition)
        {
            // Produce a new CSV Reader
            CsvReader result = new CsvReader(textReader);

            // Create a helper to read the property bag items
            PropertyBagHelper propertyBagHelper = new PropertyBagHelper(connection);

            // Configure the CSV Reader
            result.Configuration.HasHeaderRecord = (definition == null) ? true :
                                                   propertyBagHelper.Get <Boolean>(PropertyBagItemTypeEnum.HasHeaderRecord, true);
            result.Configuration.BadDataFound = null; // Don't pipe bad data
            result.Configuration.CultureInfo  = (definition == null) ?
                                                CultureInfo.CurrentCulture : definition.Culture;
            result.Configuration.TrimOptions = TrimOptions.Trim;
            result.Configuration.Delimiter   = (definition == null) ? "," :
                                               propertyBagHelper.Get <String>(PropertyBagItemTypeEnum.DelimiterCharacter, ",");
            result.Configuration.Quote = (definition == null) ? '"' :
                                         propertyBagHelper.Get <Char>(PropertyBagItemTypeEnum.QuoteCharacter, '"');
            result.Configuration.IgnoreQuotes = (definition == null) ? true :
                                                propertyBagHelper.Get <Boolean>(PropertyBagItemTypeEnum.IgnoreQuotes, true);
            result.Configuration.MissingFieldFound        = null;
            result.Configuration.ReadingExceptionOccurred = null;

            // Send the reader back
            return(result);
        }
Example #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="dataTable"></param>
        /// <returns></returns>
        public static String DataTableToString(DataItemDefinition definition, DataConnection connection, DataTable dataTable)
        {
            String result = ""; // New empty string to populate

            // Create a helper to read the property bag items
            PropertyBagHelper propertyBagHelper = new PropertyBagHelper(connection);

            // Get the stream from the file
            using (MemoryStream textStream = new MemoryStream())
            {
                // Set up the writer
                StreamWriter streamWriter = new StreamWriter(textStream);
                using (CsvWriter writer = SetupWriter(definition, streamWriter))
                {
                    // Do we need to write a header?
                    if (propertyBagHelper.Get <Boolean>(PropertyBagItemTypeEnum.HasHeaderRecord, false))
                    {
                        // Loop the header records and output the header record line manually
                        definition.ItemProperties
                        .Where(prop => prop.PropertyType == DataItemPropertyType.Property)
                        .ToList()
                        .ForEach(
                            header =>
                        {
                            writer.WriteField(header.Name);
                        });

                        // Move to the next line and flush the data
                        writer.NextRecord();
                        streamWriter.Flush();
                    }

                    // Loop through the actual records and add them to the csv
                    foreach (DataRow row in dataTable.Rows)
                    {
                        // Loop the header records and output the header record line manually
                        definition.ItemProperties
                        .Where(prop => prop.PropertyType == DataItemPropertyType.Property)
                        .ToList()
                        .ForEach(
                            property =>
                        {
                            writer.WriteField(DataFormatHelper.WriteData(row[property.Name], property, definition), property.Quoted);
                        });

                        // Move to the next line and flush the data
                        writer.NextRecord();
                        streamWriter.Flush();
                    }

                    // Put the data back in the buffer
                    textStream.Position = 0;
                    result = ((new StreamReader(textStream)).ReadToEnd() ?? "");
                }
            }

            return(result); // Send the formatted flat file data back
        }
        /// <summary>
        /// Read the raw data file and populate the in-memory data table with it
        /// </summary>
        /// <param name="rawData">The raw flat file data from wherever it came from</param>
        /// <returns>If the translation was successful</returns>
        public static DataTable TextToDataTable(DataItemDefinition definition, DataConnection connection, String rawData)
        {
            // Create a list of data items to return
            DataTable dataItems = definition.ToDataTable();

            // Create a helper to read the property bag items
            PropertyBagHelper propertyBagHelper = new PropertyBagHelper(connection);

            // Raw data has something to convert?
            if ((rawData ?? "") != "")
            {
                // Open up a text reader to stream the data to the CSV Reader
                using (TextReader textReader = new StringReader(rawData))
                {
                    // Get properties needed to process the file (total lines to skip etc.)
                    Int32 lineNo      = 0;
                    Int32 linesToSkip =
                        propertyBagHelper.Get <Int32>(PropertyBagItemTypeEnum.RowsToSkip, 0) +
                        (propertyBagHelper.Get <Boolean>(PropertyBagItemTypeEnum.HasHeaderRecord, false) ? 1 : 0);

                    // Loop each line of the file (ignoring lines that do not need to be processed)
                    String line = "";
                    while ((line = textReader.ReadLine()) != null)
                    {
                        // Is this a line we should be processing
                        if (lineNo >= linesToSkip)
                        {
                            DataRow row = dataItems.NewRow(); // The new row to populate based on the defintion

                            // Process the row
                            if (ProcessRow(line, row, definition))
                            {
                                dataItems.Rows.Add(row); // Add the row if processing was successful
                            }
                        }

                        lineNo++; // Increment the line number counter
                    }
                }
            }

            return(dataItems); // Send the datatable back
        }
Example #4
0
        /// <summary>
        /// Set up a new csv writer based on the definition given
        /// </summary>
        /// <param name="definition">The data item definition</param>
        /// <returns></returns>
        private static CsvWriter SetupWriter(DataItemDefinition definition, TextWriter textWriter)
        {
            // Create the new writer
            CsvWriter writer = new CsvWriter(textWriter);

            // Create a helper to read the property bag items
            PropertyBagHelper propertyBagHelper = new PropertyBagHelper(definition);

            // Force all fields to be quoted or not
            writer.Configuration.QuoteAllFields =
                propertyBagHelper.Get <Boolean>(PropertyBagItemTypeEnum.QuoteAllFields, false);

            return(writer);
        }