예제 #1
0
        //application routines

        /// <summary>
        /// Retrieves a DataTable containing a list of random names.
        /// </summary>
        /// <param name="rdr">Object containing the constraints to be used in generating random names.</param>
        /// <param name="numEntriesToGenerate">Number of name entries to generate</param>
        /// <param name="outputToXmlFile">Set to true to output to an XML file.</param>
        /// <param name="xmlOutputFolder">Path to XML output folder.</param>
        /// <param name="listName">Name given to the list of names. This name will serveras the DataTable.TableName value.</param>
        /// <param name="outputToGrid">Set to true to output the names to a DataViewGrid.</param>
        /// <returns>DataTable populated with rows containing the generated names.</returns>
        public DataTable GetRandomNamesList(RandomNamesAndLocationsDataRequest rdr, int numEntriesToGenerate, bool outputToXmlFile, string xmlOutputFolder, string listName, bool outputToGrid)
        {
            RandomDataProcessor rdp = new RandomDataProcessor(rdr.DatabaseFilePath, rdr.DatabasePassword, rdr.RandomDataXmlFilesFolder);
            string    xmlFileName   = string.Empty;
            Stopwatch sw            = new Stopwatch();
            DataTable dtRData       = null;

            try
            {
                sw.Start();

                //rdr.SaveToXmlFile(@"c:\temp\CountryRequest.xml");

                rdp.CountryRandomDataSpec = rdr;

                PFList <RandomName> rn = rdp.GenerateRandomNameList(numEntriesToGenerate);

                //rn.SaveToXmlFile(@"c:\temp\RandomNamesPfList.xml");

                dtRData           = rdp.ConvertRandomNameListToDataTable(rn);
                dtRData.TableName = listName;
                //for (int c = 0; c < dtRData.Columns.Count; c++)
                //{
                //    //workaround: RowNum column name causes Oracle ODBC and OLEDB drivers to fail
                //    if (dtRData.Columns[c].ColumnName == "RowNum")
                //        dtRData.Columns[c].ColumnName="NameRowNum";
                //}

                if (outputToXmlFile)
                {
                    string outputFile = string.Empty;
                    if (xmlOutputFolder.Trim() == string.Empty)
                    {
                        outputFile = Path.Combine(_defaultRandomDataUserXmlFilesFolder, listName + ".xml");
                    }
                    else
                    {
                        outputFile = Path.Combine(xmlOutputFolder, listName + ".xml");
                    }
                    XMLFileOutputProcessor xout = new XMLFileOutputProcessor(outputFile, true);
                    xout.XMLOutputType = enXMLOutputType.DataPlusSchema;
                    xout.WriteDataToOutput(dtRData);
                    //save data definition to an xml file
                    if (xmlOutputFolder.Trim() == string.Empty)
                    {
                        outputFile = Path.Combine(_defaultRandomDataUserXmlFilesFolder, listName + ".nlistdef");
                    }
                    else
                    {
                        outputFile = Path.Combine(xmlOutputFolder, listName + ".nlistdef");
                    }
                    rdr.SaveToXmlFile(outputFile);
                }

                sw.Stop();

                _msg.Length = 0;
                _msg.Append(Environment.NewLine);
                _msg.Append("Random name list generation finished.");
                _msg.Append(Environment.NewLine);
                _msg.Append("Elapsed time for name list generation: ");
                _msg.Append(sw.FormattedElapsedTime);
                _msg.Append(Environment.NewLine);
                WriteToMessageLog(_msg.ToString());

                if (outputToGrid)
                {
                    PFDataOutputGrid.DataOutputGridProcessor grid = new PFDataOutputGrid.DataOutputGridProcessor();
                    grid.ShowInstalledDatabaseProvidersOnly    = this.ShowInstalledDatabaseProvidersOnly;
                    grid.DefaultOutputDatabaseType             = this.DefaultOutputDatabaseType;
                    grid.DefaultOutputDatabaseConnectionString = this.DefaultOutputDatabaseConnectionString;
                    if (_gridExportFolder != _defaultGridExportFolder)
                    {
                        grid.DefaultGridExportFolder = _gridExportFolder;
                    }
                    else
                    {
                        grid.DefaultGridExportFolder = Path.Combine(_defaultGridExportFolder, "NamesAndLocations");
                    }
                    grid.GridColumnFilters.Add(new PFDataOutputGrid.GridColumnFilter("NameRowNum", PFDataOutputGrid.enFilterType.NumRangeColumnFilter));
                    grid.GridColumnFilters.Add(new PFDataOutputGrid.GridColumnFilter("Country", PFDataOutputGrid.enFilterType.ComboBoxColumnFilter));
                    grid.GridColumnFilters.Add(new PFDataOutputGrid.GridColumnFilter("NameType", PFDataOutputGrid.enFilterType.ComboBoxColumnFilter));
                    grid.GridColumnFilters.Add(new PFDataOutputGrid.GridColumnFilter("City", PFDataOutputGrid.enFilterType.ComboBoxColumnFilter));
                    grid.GridColumnFilters.Add(new PFDataOutputGrid.GridColumnFilter("StateProvince", PFDataOutputGrid.enFilterType.ComboBoxColumnFilter));
                    grid.GridColumnFilters.Add(new PFDataOutputGrid.GridColumnFilter("StateProvinceName", PFDataOutputGrid.enFilterType.ComboBoxColumnFilter));
                    grid.WriteDataToGrid(dtRData);
                }
            }
            catch (System.Exception ex)
            {
                _msg.Length = 0;
                _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex));
                WriteToMessageLog(_msg.ToString());
                AppMessages.DisplayErrorMessage(_msg.ToString(), _saveErrorMessagesToAppLog);
            }
            finally
            {
                if (sw.StopwatchIsRunning)
                {
                    sw.Stop();
                }
            }

            return(dtRData);
        }
        /// <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