private PFList <string> GetCurrentRandomDataValues(PFList <DataTableRandomizerColumnSpec> colSpecs, PFList <PFList <string> > randomDataValueLists) { PFList <string> currentRandomDataValues = new PFList <string>(); currentRandomDataValues.Clear(); for (int valInx = 0; valInx < randomDataValueLists.Count; valInx++) { PFList <string> randDataValues = randomDataValueLists[valInx]; int randNum = _randNumber.GenerateRandomInt(0, randDataValues.Count - 1); string randStringValue = randDataValues[randNum]; currentRandomDataValues.Add(randStringValue); } for (int specInx = 0; specInx < colSpecs.Count; specInx++) { DataTableRandomizerColumnSpec spec = colSpecs[specInx]; if (spec.RandomDataType == enRandomDataType.CustomRandomValues) { spec.CurrentValueIndex = spec.RandomDataListIndex; } } return(currentRandomDataValues); }
private PFList <RandomName> GetCurrentRandomNames(PFList <DataTableRandomizerColumnSpec> colSpecs, PFList <PFList <RandomName> > randomNameLists) { PFList <RandomName> currentRandomNames = new PFList <RandomName>(); currentRandomNames.Clear(); for (int nameInx = 0; nameInx < randomNameLists.Count; nameInx++) { PFList <RandomName> randNames = randomNameLists[nameInx]; int randNum = _randNumber.GenerateRandomInt(0, randNames.Count - 1); RandomName randName = randNames[randNum]; currentRandomNames.Add(randName); } for (int specInx = 0; specInx < colSpecs.Count; specInx++) { DataTableRandomizerColumnSpec spec = colSpecs[specInx]; //if (spec.RandomDataType == enRandomDataType.RandomNamesAndLocationsFile) //{ // spec.CurrentValueIndex = spec.RandomDataListIndex; //} } return(currentRandomNames); }
private int GetRandomDataValueListIndex(DataTableRandomizerColumnSpec spec, PFKeyValueList <string, int> randomDataValueListIndexes) { int listInx = -1; for (int inx = 0; inx < randomDataValueListIndexes.Count; inx++) { if (spec.RandomDataFileName == randomDataValueListIndexes[inx].Key) { listInx = randomDataValueListIndexes[inx].Value; break; } } return(listInx); }
private bool RandomListAlreadyStored(DataTableRandomizerColumnSpec spec, int currInx, PFList <DataTableRandomizerColumnSpec> colSpecs) { bool retval = false; for (int inx = 0; inx < currInx; inx++) { if (spec.RandomDataFileName == colSpecs[inx].RandomDataFileName && spec.RandomDataType == colSpecs[inx].RandomDataType) { retval = true; break; } } return(retval); }
}//end method private int GetDataTableColumnIndex(DataTable dt, DataTableRandomizerColumnSpec spec) { int retval = -1; for (int colInx = 0; colInx < dt.Columns.Count; colInx++) { DataColumn dc = dt.Columns[colInx]; if (dc.ColumnName == spec.DataTableColumnName) { retval = colInx; break; } } return(retval); }
/// <summary> /// Routine to use column definitions in a DataTable to initialize a DataTableRandomizerColumnSpec list. /// </summary> /// <param name="dt">DataTable object containing columns to include in the column specification list.</param> /// <returns>List of DataTableRandomizerColumnSpec objects.</returns> public PFList <DataTableRandomizerColumnSpec> GetInitColSpecListFromDataTable(DataTable dt) { PFList <DataTableRandomizerColumnSpec> colSpecs = new PFList <DataTableRandomizerColumnSpec>(); if (dt == null) { _msg.Length = 0; _msg.Append("DataTable is null. You must specify an instance of a DataTable in order to get a list of column specs."); throw new System.Exception(_msg.ToString()); } for (int colInx = 0; colInx < dt.Columns.Count; colInx++) { DataColumn dc = dt.Columns[colInx]; DataTableRandomizerColumnSpec colSpec = new DataTableRandomizerColumnSpec(); colSpec.DataTableColumnName = dc.ColumnName; colSpec.DataTableColumnDataType = dc.DataType.FullName; //remainder of properties are left at their default values: to be filled in by application using the column specs. colSpecs.Add(colSpec); } return(colSpecs); }
/// <summary> /// Main routine for inserting random values into a data table. /// </summary> /// <param name="dt">DataTable object containing data to be randomized.</param> /// <param name="colSpecs">List of specifications for which columns to randomize and values to use in the randomizing.</param> /// <param name="nameSpecs">Object containing various criteria for determining types of random names and locations to include.</param> public void RandomizeDataTableValues(DataTable dt, PFList <DataTableRandomizerColumnSpec> colSpecs, RandomNamesAndLocationsDataRequest nameSpecs) { PFList <PFList <RandomName> > randomNameLists = new PFList <PFList <RandomName> >(); PFList <PFList <string> > randomDataValueLists = new PFList <PFList <string> >(); PFKeyValueList <string, int> randomNamesListIndexes = new PFKeyValueList <string, int>(); PFKeyValueList <string, int> randomDataValueListIndexes = new PFKeyValueList <string, int>(); PFList <RandomName> currentRandomNames = null; PFList <string> currentRandomDataValues = null; PFList <RandomName> generatedRandomNamesList = null; RandomName currentGeneratedRandomName = null; bool generatedRandomNameRequested = false; int generatedRandomNameIndex = this.BatchSizeForGeneratedRandomNames; RandomDataProcessor rdp = null; if (dt == null || colSpecs == null || nameSpecs == null) { _msg.Length = 0; _msg.Append("You must specify a non-null value for following parameter(s): "); if (dt == null) { _msg.Append("dt, "); } if (colSpecs == null) { _msg.Append("colSpecs, "); } if (nameSpecs == null) { _msg.Append("nameSpecs, "); } char[] charsToTrim = { ',', ' ' }; throw new System.Exception(_msg.ToString().TrimEnd(charsToTrim) + "."); } if (dt.Rows.Count < 1 || colSpecs.Count == 0) { _msg.Length = 0; _msg.Append("You must specify non-empty collections for following parameter(s): "); if (dt.Rows.Count < 1) { _msg.Append("dt, "); } if (colSpecs.Count == 0) { _msg.Append("colSpecs, "); } char[] charsToTrim = { ',', ' ' }; throw new System.Exception(_msg.ToString().TrimEnd(charsToTrim) + "."); } if (this.BatchSizeForGeneratedRandomNames == _defaultBatchSizeForGeneratedRandomNumbers) { if (dt.Rows.Count > 0) { this.BatchSizeForGeneratedRandomNames = dt.Rows.Count; } } if (this.BatchSizeForGeneratedRandomNames > _defaultMaxBatchSizeForGeneratedRandomNumbers) { this.BatchSizeForGeneratedRandomNames = _defaultMaxBatchSizeForGeneratedRandomNumbers; } //test int numDups = 0; string prevAddressLine1 = string.Empty; string prevGeneratedAddressLine1 = string.Empty; //end test this.DataTableToRandomize = dt; this.RandomizerColumnSpecs = colSpecs; this.RandomizerNameSpecs = nameSpecs; if (colSpecs == null || dt == null) { _msg.Length = 0; _msg.Append("You must specify both the data table to be randomized and the list of column randomizer specifications in order to run OLD_RandomizeDataTableValues method."); throw new System.Exception(_msg.ToString()); } if (dt.Rows.Count < 1 || colSpecs.Count == 0) { //no changes nee3d to be made; return; } try { for (int inx = 0; inx < colSpecs.Count; inx++) { DataTableRandomizerColumnSpec spec = colSpecs[inx]; spec.DataTableColumnIndex = GetDataTableColumnIndex(dt, spec); if (spec.DataTableColumnIndex != -1) { if (spec.RandomDataType == enRandomDataType.RandomNamesAndLocations) { generatedRandomNameRequested = true; spec.RandomDataFileName = string.Empty; spec.RandomDataListIndex = -1; spec.CurrentValueIndex = -1; if (generatedRandomNamesList == null) { rdp = new RandomDataProcessor(nameSpecs.DatabaseFilePath, nameSpecs.DatabasePassword, nameSpecs.RandomDataXmlFilesFolder); rdp.CountryRandomDataSpec = nameSpecs; generatedRandomNamesList = rdp.GenerateRandomNameList(this.BatchSizeForGeneratedRandomNames); } } else if (spec.RandomDataType == enRandomDataType.CustomRandomValues) { if (RandomListAlreadyStored(spec, inx, colSpecs) == false) { PFList <string> randomDataValueList; randomDataValueList = PFList <string> .LoadFromXmlFile(spec.RandomDataFileName); randomDataValueLists.Add(randomDataValueList); spec.RandomDataListIndex = randomDataValueLists.Count - 1; randomDataValueListIndexes.Add(new stKeyValuePair <string, int>(spec.RandomDataFileName, spec.RandomDataListIndex)); } else { spec.RandomDataListIndex = GetRandomDataValueListIndex(spec, randomDataValueListIndexes); } } else { _msg.Length = 0; _msg.Append("Invalid or not specified random data file type detected: "); _msg.Append(spec.RandomDataType.ToString()); throw new System.Exception(_msg.ToString()); } } else { _msg.Length = 0; _msg.Append("Invalid or missing column name detected: "); _msg.Append(spec.DataTableColumnName); _msg.Append(" Column name not found in DataTable."); throw new System.Exception(_msg.ToString()); } }//end for generatedRandomNameIndex = -1; for (int rowInx = 0; rowInx < dt.Rows.Count; rowInx++) { //test******************************************** if (currentRandomNames != null) { if (currentRandomNames.Count > 0) { if (rowInx > 0) { prevAddressLine1 = currentRandomNames[0].AddressLine1; } else { prevAddressLine1 = string.Empty; } } } if (generatedRandomNameRequested) { if (currentGeneratedRandomName != null) { if (rowInx > 0) { prevGeneratedAddressLine1 = currentGeneratedRandomName.AddressLine1; } else { prevGeneratedAddressLine1 = string.Empty; } } } //end test**************************************** if (generatedRandomNameRequested) { generatedRandomNameIndex++; if (generatedRandomNameIndex >= this.BatchSizeForGeneratedRandomNames) { generatedRandomNamesList.Clear(); generatedRandomNamesList = null; generatedRandomNamesList = rdp.GenerateRandomNameList(this.BatchSizeForGeneratedRandomNames); generatedRandomNameIndex = 0; } currentGeneratedRandomName = generatedRandomNamesList[generatedRandomNameIndex]; } currentRandomNames = GetCurrentRandomNames(colSpecs, randomNameLists); currentRandomDataValues = GetCurrentRandomDataValues(colSpecs, randomDataValueLists); //test*********************************************** if (currentRandomNames.Count > 0) { if (currentRandomNames[0].AddressLine1 == prevAddressLine1) { numDups++; } } if (generatedRandomNameRequested) { if (currentGeneratedRandomName.AddressLine1 == prevGeneratedAddressLine1) { numDups++; } } //end test****************************************** for (int specInx = 0; specInx < colSpecs.Count; specInx++) { DataTableRandomizerColumnSpec spec = colSpecs[specInx]; DataRow dr = dt.Rows[rowInx]; string val = string.Empty; try { if (spec.RandomDataType == enRandomDataType.RandomNamesAndLocations) { val = currentGeneratedRandomName.GetPropertyValue(spec.RandomDataFieldName).ToString(); } else { val = currentRandomDataValues[spec.CurrentValueIndex]; } if (dt.Columns[spec.DataTableColumnIndex].DataType == Type.GetType("System.String")) { dr[spec.DataTableColumnIndex] = val; } else { dr[spec.DataTableColumnIndex] = Convert.ChangeType(val, dt.Columns[spec.DataTableColumnIndex].DataType); } } catch (System.Exception ex) { _msg.Length = 0; _msg.Append("Unable to randomize data for "); _msg.Append(spec.DataTableColumnName); if (spec.DataTableColumnIndex < dt.Columns.Count) { _msg.Append(" Randomized value is of type System.String. DataTable column type is "); _msg.Append(dt.Columns[spec.DataTableColumnIndex].DataType.FullName); if (spec.RandomDataType == enRandomDataType.RandomNamesAndLocations) { _msg.Append("."); _msg.Append(" Random field is "); _msg.Append(spec.RandomDataFieldName); } _msg.Append("."); } else { _msg.Append("Invalid column index. Column with index of "); _msg.Append(spec.DataTableColumnIndex.ToString()); _msg.Append(" does not exit. Thre are "); _msg.Append(dt.Columns.Count.ToString()); _msg.Append(" columns in the DataTable with indexes from 0 to "); _msg.Append((dt.Columns.Count - 1).ToString()); _msg.Append("."); } _msg.Append(Environment.NewLine); _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex)); throw new System.Exception(_msg.ToString()); } } // end for loop on colSpecs } // end for loop on dt.rows dt.AcceptChanges(); //commit changes }//end try catch (System.Exception ex) { _msg.Length = 0; _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex)); throw new System.Exception(_msg.ToString()); } finally { _msg.Length = 0; _msg.Append("Number of dup persons or businesses: "); _msg.Append(numDups.ToString("#,##0")); //Console.WriteLine(_msg.ToString()); DllMessageLogExt.WriteLine(_msg.ToString()); } }//end method