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 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);
        }
Ejemplo n.º 3
0
        public void ListMergeTest()
        {
            PFList <string> list1 = new PFList <string>();
            PFList <string> list2 = new PFList <string>();
            PFList <string> list3 = new PFList <string>();


            try
            {
                _msg.Length = 0;
                _msg.Append("ListMergeTest started ...\r\n");
                Program._messageLog.WriteLine(_msg.ToString());

                list1.Add("First");
                list1.Add("Second");
                list1.Add("Third");

                list2.Add("fourth");
                list2.Add("fifth");
                list2.Add("sixth");

                list3.Add("Seventh");
                list3.Add("Eighth");
                list3.Add("Ninth");
                list3.Add("Tenth");

                PFList <string>[]         listArray   = { list1, list2, list3 };
                PFList <PFList <string> > listOfLists = new PFList <PFList <string> >();
                listOfLists.Add(list1);
                listOfLists.Add(list2);
                listOfLists.Add(list3);

                PFList <string> concatListFromArray = PFList <string> .ConcatenateLists(listArray);

                _msg.Length = 0;
                _msg.Append(Environment.NewLine);
                _msg.Append("Concatenated list from Array");
                _msg.Append(Environment.NewLine);
                _msg.Append(concatListFromArray.ToXmlString());
                Program._messageLog.WriteLine(_msg.ToString());


                PFList <string> concatListFromList = PFList <string> .ConcatenateLists(listOfLists);

                _msg.Length = 0;
                _msg.Append(Environment.NewLine);
                _msg.Append("Concatenated list from List");
                _msg.Append(Environment.NewLine);
                _msg.Append(concatListFromList.ToXmlString());
                Program._messageLog.WriteLine(_msg.ToString());

                PFList <string> mergedList = list1.Merge(list2);
                _msg.Length = 0;
                _msg.Append(Environment.NewLine);
                _msg.Append("Merged list");
                _msg.Append(Environment.NewLine);
                _msg.Append(mergedList.ToXmlString());
                Program._messageLog.WriteLine(_msg.ToString());

                mergedList.Clear();
                mergedList = list3.Merge(new PFList <string>[2] {
                    list1, list2
                });
                _msg.Length = 0;
                _msg.Append(Environment.NewLine);
                _msg.Append("Merged list from Array");
                _msg.Append(Environment.NewLine);
                _msg.Append(mergedList.ToXmlString());
                Program._messageLog.WriteLine(_msg.ToString());

                mergedList.Clear();
                mergedList = list3.Merge(new PFList <PFList <string> > {
                    list2, list1
                });
                _msg.Length = 0;
                _msg.Append(Environment.NewLine);
                _msg.Append("Merged list from List");
                _msg.Append(Environment.NewLine);
                _msg.Append(mergedList.ToXmlString());
                Program._messageLog.WriteLine(_msg.ToString());
            }
            catch (System.Exception ex)
            {
                _msg.Length = 0;
                _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex));
                Program._messageLog.WriteLine(_msg.ToString());
                AppMessages.DisplayErrorMessage(_msg.ToString(), _saveErrorMessagesToAppLog);
            }
            finally
            {
                _msg.Length = 0;
                _msg.Append("\r\n... ListMergeTest finished.");
                Program._messageLog.WriteLine(_msg.ToString());
            }
        }
        /// <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