public static BComOrderStatusResponse OrderStatus(DataRow orderRow)
        {
            // Create an HTTPS_ Interface.
            HTTPS_Interface httpsInterface = new HTTPS_Interface();

            // Create SipPeer Request
            BComOrderStatusRequest request =
                new BComOrderStatusRequest();

            // Load the request.
            try
            {
                request.OrderId = orderRow["OrderId"].SafeToString();
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Failed loading Aviator data into order: " + ex.Message);
            }

            // Send the request and get the response.
            BComOrderStatusResponse response =
                httpsInterface.OrderStatus(request);

            // Return the response.
            return(response);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            // Clear the page.
            Response.Clear();

            // Set the type to XML.
            Response.ContentType = "text/xml";
            //Response.ContentType = "text/plain";

            // Response declared.
            this.response = new ConfirmBandwidthFOCResponse();

            // Make sure parameters are there.
            SetParameters();

            // Failed? Then Jeop.
            if (this.response.Action != "PENDING")
            {
                Response.Write(this.response.ToXml());
                return;
            }

            // Create a database interface.
            try
            {
                this.db = new DB();
            }
            catch (Exception ex)
            {
                this.response.SetJeop(
                    "Unable to open a database connection: " + ex.Message);
                Response.Write(this.response.ToXml());
                this.db.Close();
                return;
            }

            //Get Order ID
            try
            {
                this.dr =
                    this.db.ConfirmBandwidthFOC(oppid);
            }
            catch (Exception ex)
            {
                string jeop = "Unable to retrieve the orderID: " + ex.Message;
                this.response.SetJeop(jeop);
                Response.Write(this.response.ToXml());
                this.db.JeopStep(
                    this.oppid, this.wf_id, this.wf_step_id, this.jeopname, jeop);
                this.db.Close();
                return;
            }

            // DataRow to load active port order information.
            DataRow aviatorData = null;

            // Load the aviator data.
            try
            {
                aviatorData = db.LoadDataRowAviatorLnpInstallData(this.oppid);
            }
            catch (Exception ex)
            {
                string jeop = "Unable to load data from Aviator: " + ex.Message;
                this.response.SetJeop(jeop);
                Response.Write(this.response.ToXml());
                this.db.Close();
                return;
            }

            // Load Data Row for active port order using oppid.
            try
            {
                this.orderRow = db.LoadDataRowLnpOrderIdFromOppid(this.oppid);
            }
            catch (Exception ex)
            {
                string jeop = "Unable to load data Data Row from Port Orders: " + ex.Message;
                this.response.SetJeop(jeop);
                Response.Write(this.response.ToXml());
                this.db.JeopStep(
                    this.oppid, this.wf_id, this.wf_step_id, this.jeopname, jeop);
                this.db.Close();
                return;
            }

            // Get the status.
            BComOrderStatusResponse orderStatusResponse = null;

            try
            {
                orderStatusResponse = UpdatePortStatus.OrderStatus(orderRow);
            }
            catch (Exception ex)
            {
                string jeop = "Unable to get status from Bandwidth: " + ex.Message;
                this.response.SetJeop(jeop);
                Response.Write(this.response.ToXml());
                this.db.JeopStep(
                    this.oppid, this.wf_id, this.wf_step_id, this.jeopname, jeop);
                this.db.Close();
                return;
            }

            if (dr["Action"].SafeToString() == "JEOP")
            {
                string jeop = dr["Message"].SafeToString();
                this.response.SetJeop(jeop);
                Response.Write(this.response.ToXml());
                this.db.JeopStep(
                    this.oppid, this.wf_id, this.wf_step_id, this.jeopname, jeop);
                this.db.Close();
                return;
            }
            // Check if the Aviator cut date is same as the Actual FOC date from Bandwidth.
            else if (aviatorData["RequestedFocDate"].SafeToDateTime().Date !=
                     orderStatusResponse.LnpOrderResponse.ActualFocDate.SafeToDateTime().Date)
            {
                string jeop = "The Cut Date in aviator " + aviatorData["RequestedFocDate"].SafeToDateTime() + " is not matching the Actual FOC date " +
                              orderStatusResponse.LnpOrderResponse.ActualFocDate.SafeToDateTime() + " from Bandwidth.";
                this.response.SetJeop(jeop);
                Response.Write(this.response.ToXml());
                this.db.JeopStep(
                    this.oppid, this.wf_id, this.wf_step_id, this.jeopname, jeop);
                this.db.Close();
                return;
            }
            else
            {
                //Assume Success
                this.response.SetComplete();
                this.db.CompleteStep(
                    this.oppid, this.wf_id, this.wf_step_id);

                // Return result.
                Response.Write(response.ToXml());
                return;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            // Clear the page.
            Response.Clear();

            // Set the type to XML.
            Response.ContentType = "text/xml";
            //Response.ContentType = "text/plain"; // For debuggin purposes.

            // Declare a response.
            this.response = new UpdatePortStatusResponse();

            // Create a database interface.
            try
            {
                this.db = new DB();
            }
            catch (Exception ex)
            {
                this.response.SetJeop(
                    "Unable to open a database connection: " + ex.Message);
                Response.Write(this.response.ToXml());
                this.db.Close();
                return;
            }

            // DataTable to load active port order information.
            DataTable aviatorData = null;

            // Load the active port orders.
            try
            {
                aviatorData =
                    this.db.LoadActivePortOrders();
            }
            catch (Exception ex)
            {
                string jeop = "Unable to load data from Aviator: " + ex.Message;
                this.response.SetJeop(jeop);
                Response.Write(this.response.ToXml());
                this.db.Close();
                return;
            }

            // Check status and perform logic on each active order.
            foreach (DataRow orderRow in aviatorData.Rows)
            {
                // Get Partner Name.
                DataRow partnerName = this.db.LoadDataRowAviatorLnpInstallData(orderRow["oppid"].SafeToString());

                // Get the coid.
                string coid = orderRow["coid"].SafeToString();

                // Get the status.
                BComOrderStatusResponse orderStatusResponse = null;
                try
                {
                    orderStatusResponse = OrderStatus(orderRow);
                }
                catch
                {
                    // Skip this order.
                    continue;
                }

                // Get the Processing Status.
                string processingStatus = string.Empty;
                try
                {
                    processingStatus = orderStatusResponse.LnpOrderResponse.ProcessingStatus.SafeToString();
                }
                catch
                {
                    // Skip this order.
                    continue;
                }

                // Determins the action and the message.
                string Action  = string.Empty;
                string Message = string.Empty;

                switch (processingStatus)
                {
                case "PENDING_DOCUMENTS":
                    Action  = "CONTINUE";
                    Message = "Badnwidth is checking the authorization.";
                    try
                    {
                        this.db.UpdatePortOrderFlags(coid, true, true, false); // Order is active.
                    }
                    catch { continue; }                                        // Skip this order.
                    break;

                case "SUBMITTED":
                    Action  = "CONTINUE";
                    Message = "Port Order Submitted to Bandwidth.";
                    try
                    {
                        this.db.UpdatePortOrderFlags(coid, true, true, false); // Order is active.
                    }
                    catch { continue; }                                        // Skip this order.
                    break;

                case "REQUESTED_CANCEL":
                    Action  = "JEOP";
                    Message = "Port Order cancel has been requested.";
                    try
                    {
                        this.db.UpdatePortOrderFlags(coid, true, true, true); // Set the jeop flag.
                    }
                    catch { continue; }                                       // Skip this order.
                    break;

                case "CANCELLED":
                    Action = "CONTINUE";
                    Action = "JEOP";
                    if (orderStatusResponse.LnpOrderResponse != null &&
                        orderStatusResponse.LnpOrderResponse.Errors != null &&
                        orderStatusResponse.LnpOrderResponse.Errors.Length > 0)
                    {
                        foreach (Errors error in orderStatusResponse.LnpOrderResponse.Errors)
                        {
                            Message += error.Code.SafeToString() + " - " + error.Description.SafeToString() + "; ";
                        }
                    }
                    else
                    {
                        Message = "The order has been cancelled for unknown reason";
                    }
                    try
                    {
                        this.db.UpdatePortOrderFlags(coid, true, true, true); // Set the jeop flag.
                    }
                    catch { continue; }                                       // Skip this order.
                    break;

                case "EXCEPTION":
                    Action = "JEOP";
                    // Build message from status.
                    if (orderStatusResponse.LnpOrderResponse != null &&
                        orderStatusResponse.LnpOrderResponse.Errors != null &&
                        orderStatusResponse.LnpOrderResponse.Errors.Length > 0)
                    {
                        foreach (Errors error in orderStatusResponse.LnpOrderResponse.Errors)
                        {
                            Message += error.Code.SafeToString() + " - " + error.Description.SafeToString() + "; ";
                        }
                    }
                    else
                    {
                        Message = "Unknown exception.";
                    }
                    // Check for Partner Name.
                    if ((partnerName["PartnerName"].SafeToString() == "Hughes Consumer") && (orderRow["timestamp"].SafeToDateTime() > "2018-01-01".SafeToDateTime()))
                    {
                        // Cancel LNP.
                        try
                        {
                            // Make request
                            BComLnpOrderResponse cancelResponse = null;
                            try
                            {
                                cancelResponse = CancelTN(orderRow["OrderId"].SafeToString());
                            }
                            catch (Exception ex)
                            {
                                string jeop = "Unable to cancel Hughes Consumer tn: " + ex.Message;
                                this.response.SetJeop(jeop);
                                Response.Write(this.response.ToXml());
                                this.db.JeopStep(
                                    orderRow["oppid"].SafeToString(), int.Parse(orderRow["wf_id"].SafeToString()),
                                    int.Parse(orderRow["wf_step_id"].SafeToString()), orderRow["JeopName"].SafeToString(), jeop);
                                this.db.Close();
                                return;
                            }
                        }
                        catch (Exception Ex)
                        {
                            continue;
                        }
                    }
                    try
                    {
                        this.db.UpdatePortOrderFlags(coid, true, true, true); // Set the jeop flag.
                    }
                    catch { continue; }                                       // Skip this order.
                    break;

                case "REQUESTED_SUPP":
                    Action  = "CONTINUE";
                    Message = "Port Order has had a Supplement requested.";
                    try
                    {
                        this.db.UpdatePortOrderFlags(coid, true, true, false); // Order is active.
                    }
                    catch { continue; }                                        // Skip this order.
                    break;

                case "FOC":
                    Action  = "COMPLETE";
                    Message = "Port Order has reached FOC.";
                    try
                    {
                        this.db.UpdatePortOrderFlags(coid, true, true, false); // LSR Step is complete, but Order is active.
                    }
                    catch { continue; }                                        // Skip this order.
                    break;

                case "COMPLETE":
                    Action  = "COMPLETE";
                    Message = "Port Order is completed.";
                    try
                    {
                        this.db.UpdatePortOrderFlags(coid, false, false, false); // Order is complete.
                    }
                    catch { continue; }                                          // Skip this order.
                    break;

                default:
                    Action  = "JEOP";
                    Message = "Unknown processing status: " + processingStatus;
                    break;
                }

                // Record the status.
                try
                {
                    this.db.UpdatePortOrderStatus(
                        orderRow["oppid"].SafeToString(),
                        long.Parse(orderRow["coid"].SafeToString()),
                        long.Parse(orderRow["cpid"].SafeToString()),
                        orderRow["OrderId"].SafeToString(),
                        orderStatusResponse.LnpOrderResponse.ProcessingStatus.SafeToString(),
                        Action,
                        Message,
                        orderStatusResponse.ToXml(),
                        orderStatusResponse.LnpOrderResponse.RequestedFocDate.SafeToString()
                        );
                }
                catch
                {
                    // Skip this order.
                    continue;
                }

                // Jeop, if action is jeop.
                if (Action == "JEOP")
                {
                    try
                    {
                        this.db.JeopStep(
                            orderRow["oppid"].SafeToString(),
                            int.Parse(orderRow["wf_id"].SafeToString()),
                            int.Parse(orderRow["wf_step_id"].SafeToString()),
                            orderRow["JeopName"].SafeToString(),
                            Message);
                    }
                    catch
                    {
                        // Skip this order.
                        continue;
                    }
                }
                else if (Action == "COMPLETE")
                {
                    try
                    {
                        this.db.CompleteStep(
                            orderRow["oppid"].SafeToString(),
                            int.Parse(orderRow["wf_id"].SafeToString()),
                            int.Parse(orderRow["wf_step_id"].SafeToString()));
                    }
                    catch
                    {
                        // Skip this order.
                        continue;
                    }
                }
            }

            // Action finished.
            this.response.SetComplete();
            Response.Write(this.response.ToXml());
            this.db.Close();
            return;
        }