Ejemplo n.º 1
0
        public void GetRateTestMethod()
        {
            string id = null;

            try
            {
                RateDataLayer rateDataLayer = new RateDataLayer();
                var           rate          = new Rate()
                {
                    Description = "test", Owner = "aaron", Rate1 = 0.1M, Prefix = "012"
                };
                rateDataLayer.Add(rate);

                id = rate.ID;
                var retrievedRate = rateDataLayer.Get("aaron", rate.ID);

                Assert.IsNotNull(retrievedRate);
            }
            finally
            {
                if (id != null)
                {
                    TestHelper.ExecuteQuery("delete from rate where id = '" + id + "'");
                }
            }
        }
Ejemplo n.º 2
0
        private void ProcessBulkRateFile(string fullPath)
        {
            string fileName = Path.GetFileName(fullPath);

            bool      wasSuccess           = true;
            string    updateLog            = "Commencing bulk rate update of file " + fileName + " at " + DateTime.Now.ToString("dd MMM yyyy HH:mm:ss") + ".\r\n";
            string    customerEmailAddress = null;
            Stopwatch sw = new Stopwatch();

            sw.Start();

            try
            {
                logger.Debug("BulkRateFileCreated new file created " + fullPath + ".");

                // Find the customer that the new file belongs to.
                string ftpPrefix = fileName.Substring(0, fileName.IndexOf('_'));
                var    customer  = m_customerDataLayer.GetForFTPPrefix(ftpPrefix);

                if (customer == null)
                {
                    string badFileName = _badUpdateFilesDirectory + DateTime.Now.ToString("ddMMMyyyyHHmmss") + "_" + fileName;
                    logger.Warn("No customer record found with an FTP prefix of " + ftpPrefix + ", moving to bad file directory " + badFileName + ".");
                    File.Move(fullPath, badFileName);
                }
                else
                {
                    string owner = customer.Name;
                    customerEmailAddress = customer.EmailAddress;
                    logger.Debug("Processing bulk rate update file for " + owner + ".");

                    using (FileStream fs = new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        StreamReader sr = new StreamReader(fs);

                        using (var transaction = new TransactionScope())
                        {
                            bool isFirstLine = true;

                            while (!sr.EndOfStream)
                            {
                                string rateUpdate = sr.ReadLine();
                                logger.Debug("Processing rate update line: " + rateUpdate);
                                updateLog += rateUpdate.Trim();

                                if (rateUpdate.NotNullOrBlank())
                                {
                                    if (rateUpdate.Trim() == DELETE_ALL_RATES_KEY && isFirstLine)
                                    {
                                        logger.Debug("Deleting all rates.");
                                        m_rateDataLayer.DeleteAll(owner);
                                        updateLog += " <- All rates successfully deleted.\r\n";
                                    }
                                    else
                                    {
                                        string[] rateUpdateFields = rateUpdate.Split(',');

                                        string command = rateUpdateFields[0].ToUpper();

                                        switch (command)
                                        {
                                        case INSERT_COMMAND_KEY:
                                            if (rateUpdateFields.Length < 8)
                                            {
                                                wasSuccess = false;
                                                updateLog += " <- Insert command failed, the required number of fields were not present.\r\n";
                                                throw new ApplicationException("A rate insert command was not processed as not enough fields were present. " + rateUpdate);
                                            }
                                            else
                                            {
                                                logger.Debug("Inserting new rate for " + rateUpdateFields[1] + ".");

                                                var insertRate = new Rate()
                                                {
                                                    Owner            = owner,
                                                    Description      = rateUpdateFields[1],
                                                    Prefix           = rateUpdateFields[2],
                                                    Rate1            = Convert.ToDecimal(rateUpdateFields[3]),
                                                    SetupCost        = Convert.ToDecimal(rateUpdateFields[4]),
                                                    IncrementSeconds = Convert.ToInt32(rateUpdateFields[5]),
                                                    RateCode         = rateUpdateFields[6],
                                                    RatePlan         = Convert.ToInt32(rateUpdateFields[7])
                                                };

                                                m_rateDataLayer.Add(insertRate);

                                                updateLog += " <- Insert command successful.\r\n";
                                            }

                                            break;

                                        case UPDATE_COMMAND_KEY:
                                            if (rateUpdateFields.Length < 9)
                                            {
                                                wasSuccess = false;
                                                updateLog += " <- Update command failed, the required number of fields were not present.\r\n";
                                                throw new ApplicationException("A rate update command was not processed as not enough fields were present. " + rateUpdate);
                                            }
                                            else
                                            {
                                                string updateRateID = rateUpdateFields[1];
                                                logger.Debug("Updating rate with ID " + updateRateID + ".");

                                                var updateRate = m_rateDataLayer.Get(updateRateID, owner);

                                                if (updateRate != null)
                                                {
                                                    updateRate.Description      = rateUpdateFields[2];
                                                    updateRate.Prefix           = rateUpdateFields[3];
                                                    updateRate.Rate1            = Convert.ToDecimal(rateUpdateFields[4]);
                                                    updateRate.SetupCost        = Convert.ToDecimal(rateUpdateFields[5]);
                                                    updateRate.IncrementSeconds = Convert.ToInt32(rateUpdateFields[6]);
                                                    updateRate.RateCode         = rateUpdateFields[7];
                                                    updateRate.RatePlan         = Convert.ToInt32(rateUpdateFields[8]);

                                                    m_rateDataLayer.Update(updateRate);

                                                    updateLog += " <- Update command successful.\r\n";
                                                }
                                                else
                                                {
                                                    wasSuccess = false;
                                                    updateLog += " <- Update command failed, the rate to update could not be found.\r\n";
                                                    throw new ApplicationException("The rate to update could not be found.");
                                                }
                                            }

                                            break;

                                        case DELETE_COMMAND_KEY:
                                            string deleteRateID = rateUpdateFields[1];
                                            logger.Debug("Deleting rate with ID " + deleteRateID + ".");

                                            var deleteRate = m_rateDataLayer.Get(deleteRateID, owner);

                                            if (deleteRate != null)
                                            {
                                                m_rateDataLayer.Delete(deleteRate.ID);
                                            }
                                            else
                                            {
                                                wasSuccess = false;
                                                updateLog += " <- Delete command failed, the rate to delete could not be found.\r\n";
                                                throw new ApplicationException("The rate to delete could not be found.");
                                            }
                                            break;

                                        default:
                                            wasSuccess = false;
                                            updateLog += " <- Command was not recognised.\r\n";
                                            throw new ApplicationException("Command " + command + " was not recognised, ignoring.");
                                        }
                                    }

                                    isFirstLine = false;
                                }
                            }

                            transaction.Complete();
                        }
                    }

                    sw.Stop();
                    updateLog += "Successfully completed bulk rate update of " + fileName + " at " + DateTime.Now.ToString("dd MMM yyyy HH:mm:ss") + " in " + sw.Elapsed.TotalSeconds.ToString("0") + "s.";
                    logger.Debug("Successfully processed bulk rate update file " + fileName + ", moving to processed directory.");
                    File.Move(fullPath, _processedUpdateFilesDirectory + DateTime.Now.ToString("ddMMMyyyyHHmmss") + "_" + fileName);
                }
            }
            catch (Exception excp)
            {
                wasSuccess = false;
                updateLog += " <- Exception " + excp.GetType().ToString() + " " + excp.Message + ".\r\n";
                logger.Error("Exception ProcessBulkRateFile. " + excp);

                try
                {
                    File.Move(fullPath, _badUpdateFilesDirectory + DateTime.Now.ToString("ddMMMyyyyHHmmss") + "_" + fileName);
                }
                catch (Exception moveExcp)
                {
                    logger.Error("Exception ProcessBulkRateFile moving bad file. " + moveExcp);
                }
            }
            finally
            {
                if (customerEmailAddress.NotNullOrBlank())
                {
                    try
                    {
                        logger.Debug("Sending bulk rate update result to " + customerEmailAddress + ".");
                        string subject = (wasSuccess) ? "SIP Sorcery Bulk Rate Update Success" : "SIP Sorcery Bulk Rate Update Failure";
                        SIPSorcerySMTP.SendEmail(customerEmailAddress, EMAIL_FROM_ADDRESS, null, EMAIL_FROM_ADDRESS, subject, updateLog);
                    }
                    catch (Exception sendResultExcp)
                    {
                        logger.Error("Exception ProcessBulkRateFile sending result email. " + sendResultExcp);
                    }
                }
            }
        }