/// <summary> /// Method transfers data format from <seealso cref="DataSet"/> with <seealso cref="DataTable"/> items /// and <seealso cref="DataRow"/> items into the current schema of options /// and option values for data read from XML persistence into the in-memory model. /// Converts external XML format into internal format. /// </summary> /// <param name="engine"></param> /// <param name="dataSet"></param> private void ConvertFromDataSetToModel(IEngine engine, DataSet dataSet) { AlternativeDataTypeHandler handle = new AlternativeDataTypeHandler(); foreach (DataTable table in dataSet.Tables) { string optionName; // Locate options group for this table IOptionGroup optionsGroup = FindOptionsGroup(engine, table.TableName, out optionName); // Found a table that refers to a list of values stored in an optiongroup/option if (string.IsNullOrEmpty(optionName) == false) { // Empty list is allowed but requires no processing // -> remove defaults if actual data is available if (table.Rows.Count > 0) { optionsGroup.List_Clear(optionName); for (int i = 0; i < table.Rows.Count; i++) { // assumption: Address first column in each row since list // table contains only 1 data column optionsGroup.SetValue(optionName, table.Rows[i].ItemArray[0]); } } } else { // This code requires at least a header row and 1 data row in order to work! if (table.Rows.Count >= 2) { for (int i = 0; i < table.Columns.Count; i++) { IOptionsSchema schema = null; schema = optionsGroup.GetOptionDefinition(table.Columns[i].ColumnName); var handler = handle.FindHandler(schema.TypeOfValue); if (handler != null) { object s = handler.ConvertBack(table.Rows[1].ItemArray[i] as string); optionsGroup.SetValue(table.Columns[i].ColumnName, s); } else { optionsGroup.SetValue(table.Columns[i].ColumnName, table.Rows[1].ItemArray[i]); } } } } } dataSet = null; }
/// <summary> /// Builds a <seealso cref="DataSet"/> with <seealso cref="DataTable"/> items /// and <seealso cref="DataRow"/> items for the current schema of options and /// option values for data write XML to persistence. /// </summary> /// <param name="engine"></param> /// <returns></returns> private DataSet ConvertFromModelToDataSet(IEngine engine) { DataSet mDataSet = new DataSet(); DataTable dataTable = null; AlternativeDataTypeHandler handle = new AlternativeDataTypeHandler(); foreach (var optionsGroup in engine.GetOptionGroups()) { // Create a new table (per Option group) and add a data row dataTable = CreateTable(optionsGroup); mDataSet.Tables.Add(dataTable); DataRow row = dataTable.NewRow(); // Fill data row with data to be stored foreach (var optionDefinition in optionsGroup.GetOptionDefinitions()) { var handler = handle.FindHandler(optionDefinition.TypeOfValue); if (optionDefinition.SchemaType == OptionSchemaType.SingleValue) { if (handler != null) { row[optionDefinition.OptionName] = handler.Convert(optionDefinition.Value as SecureString); } else { row[optionDefinition.OptionName] = optionDefinition.Value; } } else { // Create a unique id-able name for this list item and then create the list item table with that name var tableName = CreateListItemTableName(optionsGroup, optionDefinition); var listTable = CreateListTable(tableName, optionDefinition, handler, row); mDataSet.Tables.Add(listTable); ////ForeignKeyConstraint custOrderFK = new ForeignKeyConstraint(optionsGroup.Name + "_" + optionDefinition.OptionName + "_FK", //// listTable.Columns[optionDefinition.OptionName], //// dataTable.Columns[optionDefinition.OptionName]); ////custOrderFK.DeleteRule = Rule.None; //// ////// Cannot delete a customer value that has associated existing orders. ////dataTable.Constraints.Add(custOrderFK); } } // Check if last data row has data and add it with table in collection, if so if (row.ItemArray.Count() > 0) { dataTable.Rows.Add(row); } } return(mDataSet); }