/// <summary> /// Take some raw Json text and convert it to a datatable for the provider /// to handle e.g. updates or insertions /// </summary> /// <param name="json">A JObject (Queryable) representation of the json data</param> /// <returns>A datatable with the given format</returns> public static DataTable ToDataTable(JObject json, ApiDefinition apiDefinition, DataItemDefinition dataItemDefinition) { // Create the table from the definition DataTable result = dataItemDefinition.ToDataTable(); // Call the data conversion for the root object DataRow row = ToDataRow(json, apiDefinition, dataItemDefinition, result); if (row != null) { result.Rows.Add(row); // Add the row to the results table } return(result); }
/// <summary> /// Take some raw Json text and convert it to a datatable for the provider /// to handle e.g. updates or insertions /// </summary> /// <param name="json">A JArray (Queryable) representation of the json data</param> /// <returns>A datatable with the given format</returns> public static DataTable ToDataTable(JArray json, ApiDefinition apiDefinition, DataItemDefinition dataItemDefinition) { // Create the table from the definition DataTable result = dataItemDefinition.ToDataTable(); // For each item in the array, call the data conversion foreach (JObject item in json.Children()) { DataRow row = ToDataRow(item, apiDefinition, dataItemDefinition, result); if (row != null) { result.Rows.Add(row); // Add the row to the results table } } ; return(result); }
/// <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 }
/// <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(); // 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)) { // Create an instance of the CSV Reader using (CsvReader csvReader = SetupReader(textReader, connection, definition)) { // Get the record header if needed if (csvReader.Configuration.HasHeaderRecord) { csvReader.Read(); // Do a read first csvReader.ReadHeader(); // Parse the header records so that they do not include enclosing quotes Int32 headerId = 0; while (headerId < csvReader.Context.HeaderRecord.Length) { // Clean the header csvReader.Context.HeaderRecord[headerId] = DataFormatHelper.CleanString( csvReader.Context.HeaderRecord[headerId], csvReader.Configuration.Quote); headerId++; // Move to the next header } } // Loop the records List <DataItemProperty> propertyValues = definition.ItemProperties .Where(prop => prop.PropertyType == DataItemPropertyType.Property) .ToList(); while (csvReader.Read()) { DataRow dataRow = dataItems.NewRow(); // Create a new row to populate // Match all of the properties in the definitions lists propertyValues.ForEach( property => { try { // Try and get the value Object field = null; Boolean fieldFound = GetPropertyValue(csvReader, property, definition, ref field); // Found something? if (fieldFound && field != null) { dataRow[property.Name] = field; } } catch { } }); // Add the row to the result data table dataItems.Rows.Add(dataRow); } } } } return(dataItems); // Send the datatable back }