Ejemplo n.º 1
0
        private void LoadWordList()
        {
            try
            {
                _words = PFList <string> .LoadFromXmlFile(_wordListFile, _fileXlatKey, _fileXlatIV);

                _minListInx = 0;
                _maxListInx = _words.Count - 1;
            }
            catch (System.Exception ex)
            {
                _msg.Length = 0;
                _msg.Append("Unable to find word list file: ");
                _msg.Append(_wordListFile);
                _msg.Append(Environment.NewLine);
                _msg.Append(PFTextProcessor.FormatErrorMessage(ex));
                throw new System.Exception(_msg.ToString());
            }
        }
        private void InitInstance(enLogFileStorageType logFileStorageType, string logFileConnectionString,
                                  enLogRetryQueueStorageType logRetryQueueStorageType, string logRetryQueueConnectionString)
        {
            _logFileStorageType            = logFileStorageType;
            _logFileConnectionString       = logFileConnectionString;
            _logRetryQueueStorageType      = logRetryQueueStorageType;
            _logRetryQueueConnectionString = logRetryQueueConnectionString;

            if (File.Exists(_logFileConnectionString) == false)
            {
                FileStream fs = File.Create(_logFileConnectionString);
                fs.Close();
            }

            if (File.Exists(_logRetryQueueConnectionString) == false)
            {
                //create the retry queue
                _logRetryQueue.SaveToXmlFile(_logRetryQueueConnectionString);
            }

            _logRetryQueue = PFList <PFLogMessage> .LoadFromXmlFile(_logRetryQueueConnectionString);
        }
Ejemplo n.º 3
0
        //tests
        public void GenericListTest()
        {
            PFList <string>            testList         = new PFList <string>();
            PFList <string>            testList2        = new PFList <string>();
            PFList <string>            testList3        = new PFList <string>();
            PFList <string>            testList4        = new PFList <string>();
            string                     testListXmlFile  = @"c:\temp\testlist.xml";
            string                     connectionString = string.Empty;
            string                     configValue      = string.Empty;
            PFDBListProcessor <string> listProcessor    = new PFDBListProcessor <string>();

            try
            {
                _msg.Length = 0;
                _msg.Append("GenericListTest started ...");
                Program._messageLog.WriteLine(_msg.ToString());

                configValue = AppConfig.GetStringValueFromConfigFile("ListsDatabaseFile", string.Empty);
                if (configValue.Length > 0)
                {
                    connectionString = configValue;
                }
                else
                {
                    connectionString = _listsDatabaseConnectionString;
                }


                _msg.Length = 0;
                _msg.Append("First = ");
                _msg.Append(testList.FirstItem == null ? "<null>" : testList.FirstItem);
                Program._messageLog.WriteLine(_msg.ToString());

                testList.Add("First item");
                testList.Add("Second item");
                testList.Add("Third item");

                _msg.Length = 0;
                _msg.Append("Number of items in list: ");
                Program._messageLog.WriteLine(_msg.ToString());

                foreach (string s in testList)
                {
                    _msg.Length = 0;
                    _msg.Append(s);
                    Program._messageLog.WriteLine(_msg.ToString());
                }

                _msg.Length = 0;
                _msg.Append("First = ");
                _msg.Append(testList.FirstItem);
                Program._messageLog.WriteLine(_msg.ToString());

                _msg.Length = 0;
                _msg.Append("Last = ");
                _msg.Append(testList.LastItem);
                Program._messageLog.WriteLine(_msg.ToString());


                _msg.Length = 0;
                _msg.Append("NextItem loop: ");
                Program._messageLog.WriteLine(_msg.ToString());

                string res = testList.FirstItem;
                while (!testList.EOF)
                {
                    _msg.Length = 0;
                    _msg.Append(res);
                    Program._messageLog.WriteLine(_msg.ToString());
                    res = testList.NextItem;
                }

                _msg.Length = 0;
                _msg.Append("PrevItem loop: ");
                Program._messageLog.WriteLine(_msg.ToString());

                res = testList.LastItem;
                while (!testList.EOF)
                {
                    _msg.Length = 0;
                    _msg.Append(res);
                    Program._messageLog.WriteLine(_msg.ToString());
                    res = testList.PrevItem;
                }

                _msg.Length = 0;
                _msg.Append("ToString Test: ");
                Program._messageLog.WriteLine(_msg.ToString());

                _msg.Length = 0;
                _msg.Append(testList.ToString());
                Program._messageLog.WriteLine(_msg.ToString());

                _msg.Length = 0;
                _msg.Append("SaveToXmlFile Test: ");
                _msg.Append(testListXmlFile);
                Program._messageLog.WriteLine(_msg.ToString());

                testList.SaveToXmlFile(testListXmlFile);

                _msg.Length = 0;
                _msg.Append("LoadFromXmlFile Test: ");
                _msg.Append(testListXmlFile);
                Program._messageLog.WriteLine(_msg.ToString());

                testList2 = PFList <string> .LoadFromXmlFile(testListXmlFile);

                _msg.Length = 0;
                _msg.Append("testList2 ToString Test: ");
                Program._messageLog.WriteLine(_msg.ToString());

                _msg.Length = 0;
                _msg.Append(testList2.ToString());
                Program._messageLog.WriteLine(_msg.ToString());

                //save and read to database tests next
                listProcessor.SaveToDatabase(testList, connectionString, "TestprogCollections_testList");

                testList3 = listProcessor.LoadFromDatabase(connectionString, "TestprogCollections_testList");

                if (testList3 != null)
                {
                    _msg.Length = 0;
                    _msg.Append("testList3 ToXmlString Test: ");
                    Program._messageLog.WriteLine(_msg.ToString());
                    _msg.Length = 0;
                    _msg.Append(testList3.ToXmlString());
                    _msg.Append(Environment.NewLine);
                    _msg.Append("+++ testlist3 retrieved. +++");
                    Program._messageLog.WriteLine(_msg.ToString());
                }
                else
                {
                    _msg.Length = 0;
                    _msg.Append("testList3 ToXmlString Test: ");
                    Program._messageLog.WriteLine(_msg.ToString());
                    _msg.Length = 0;
                    _msg.Append("*** TESTLIST3 IS NULL ***");
                    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
            {
                listProcessor = null;
                _msg.Length   = 0;
                _msg.Append("... GenericListTest finished.");
                Program._messageLog.WriteLine(_msg.ToString());
            }
        }//end GenericListTest
Ejemplo n.º 4
0
        public void GenerateRandomWords()
        {
            string          randomDataFilePath  = string.Empty;
            PFList <string> wordList            = new PFList <string>();
            int             numWordsOutput      = 0;
            int             maxNumWordsToOutput = 0;
            string          word            = string.Empty;
            RandomNumber    rn              = new RandomNumber();
            int             minRNum         = 0;
            int             maxRNum         = -1;
            int             numWordsOnLine  = 0;
            int             maxWordsPerLine = 10;

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

                if (_frm.txtRandomDataXmlFilesFolder.Text.Trim().Length == 0)
                {
                    _msg.Length = 0;
                    _msg.Append("You must specify the folder containing the random data XML files.");
                    throw new System.Exception(_msg.ToString());
                }

                if (_frm.cboRandomDataXmlFile.Text.Trim().Length == 0)
                {
                    _msg.Length = 0;
                    _msg.Append("You must specify the file containing the random data.");
                    throw new System.Exception(_msg.ToString());
                }

                randomDataFilePath = Path.Combine(_frm.txtRandomDataXmlFilesFolder.Text, _frm.cboRandomDataXmlFile.Text);
                if (File.Exists(randomDataFilePath) == false)
                {
                    _msg.Length = 0;
                    _msg.Append("Unable to find file: ");
                    _msg.Append(randomDataFilePath);
                    throw new System.Exception(_msg.ToString());
                }

                wordList = PFList <string> .LoadFromXmlFile(randomDataFilePath);

                _msg.Length = 0;
                _msg.Append("Num words in list ");
                _msg.Append(_frm.cboRandomDataXmlFile.Text);
                _msg.Append(": ");
                _msg.Append(wordList.Count.ToString("#,##0"));
                _msg.Append(Environment.NewLine);
                Program._messageLog.WriteLine(_msg.ToString());

                maxNumWordsToOutput = Convert.ToInt32(_frm.txtNumWordsToOutput.Text);
                numWordsOutput      = 0;
                minRNum             = 0;
                maxRNum             = wordList.Count - 1;
                numWordsOnLine      = 0;
                _msg.Length         = 0;

                while (numWordsOutput < maxNumWordsToOutput)
                {
                    word = wordList[rn.GenerateRandomInt(minRNum, maxRNum)];
                    numWordsOutput++;
                    numWordsOnLine++;
                    _msg.Append(word);
                    _msg.Append(" ");
                    if (numWordsOnLine >= maxWordsPerLine)
                    {
                        Program._messageLog.WriteLine(_msg.ToString());
                        _msg.Length    = 0;
                        numWordsOnLine = 0;
                    }
                }
                if (_msg.Length > 0)
                {
                    Program._messageLog.WriteLine(_msg.ToString());
                    _msg.Length = 0;
                }
            }
            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... GenerateRandomWords 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