/// <summary> /// Constructor. /// </summary> /// <param name="dt">DataTable object containing data to be randomized.</param> /// <param name="randomizerRequests">List of specifications for which columns to randomize and values to use in the randomizing.</param> /// <param name="randomizerNameSpecs">Object containing various criteria for determining types of random names and locations to include.</param> public TEST_DataTableRandomizer(DataTable dt, PFList <DataTableRandomizerColumnSpec> randomizerRequests, RandomNamesAndLocationsDataRequest randomizerNameSpecs) { _dt = dt; _randomizerColumnSpecs = randomizerRequests; _randomizerNameSpecs = randomizerNameSpecs; DllMessageLogExt.WriteLine("TEST_DataTableRandomizer allocated ..."); }
/// <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