public static string CreateRegisterTeamRequest(string teamName)
 {
     var message = new Message();
     message.AddSegment(CommandDirectiveElement, RegisterTeamElement, "", "");
     message.AddSegment(InfoDirectiveElement, teamName,"","");
     return HL7Utility.Serialize(message);
 }
 public static string CreateRegisterServiceRequest()
 {
     var message = new Message();
     // DRC|PUB-SERVICE|<team name>|<teamID>|
     message.AddSegment(CommandDirectiveElement, PublishServiceElement, _settings.TeamName, _teamId);
     // SRV|<tag name>|<service name>|<security level>|<num args>|<num responses>|<description>|
     message.AddSegment(ServiceDirectiveElement, _settings.ServiceTagName, _settings.ServiceName, _settings.ServiceSecurityLevel, "2", "5", _settings.ServiceDescription);
     // ARG|<arg position>|<arg name>|<arg data type>|[mandatory | optional]||
     message.AddSegment(ArgumentDirectiveElement, "1", "province", "string", "mandatory","");
     // ARG|<arg position>|<arg name>|<arg data type>|[mandatory | optional]||
     message.AddSegment(ArgumentDirectiveElement, "2", "amount", "double", "mandatory","");
     // RSP|<resp position>|<resp name>|<resp data type>||
     message.AddSegment(ResponseDirectiveElement, "1", "NetAmount", "double", "");
     // RSP|<resp position>|<resp name>|<resp data type>||
     message.AddSegment(ResponseDirectiveElement, "2", "PstAmount", "double", "");
     // RSP|<resp position>|<resp name>|<resp data type>||
     message.AddSegment(ResponseDirectiveElement, "3", "HstAmount", "double", "");
     // RSP|<resp position>|<resp name>|<resp data type>||
     message.AddSegment(ResponseDirectiveElement, "4", "GstAmount", "double", "");
     // RSP|<resp position>|<resp name>|<resp data type>||
     message.AddSegment(ResponseDirectiveElement, "5", "TotalAmount", "double", "");
     // MCH|<published server IP>|<published port>|
     message.AddSegment(MCHDirectiveElement, _settings.ServicePublishIp, _settings.ServicePublishPort.ToString());
     return HL7Utility.Serialize(message);
 }
 public static string CreateValidateTeamRequest(string teamName, string teamId)
 {
     var message = new Message();
     // DRC|QUERY-TEAM|<team name>|<teamID>|
     message.AddSegment(CommandDirectiveElement, QueryTeamElement, _settings.TeamName, _teamId);
     // INF|<team name>|<teamID>|<service tag name>|
     message.AddSegment(InfoDirectiveElement, teamName, teamId, _settings.ServiceTagName);
     return HL7Utility.Serialize(message);
 }
        protected void submitButton_Click(object sender, EventArgs e)
        {
            Message msg = new Message();
            string request = "";
            string response = "";
            int idVerification = 0;

            try
            {
                //  DRC|QUERY-SERVICE|<team name>|<team ID>|
                msg.AddSegment(CommandDirectiveElement, QueryServiceElement, nameTextBox.Text, idTextBox.Text);
                //  SRV|GIORP-TOTAL||||||
                msg.AddSegment(ServiceDirectiveElement, "GIORP-TOTAL", "", "", "", "", "");

                //Serialize the message
                request = HL7Utility.Serialize(msg);
                //Send the request
                response = ServiceClient.SendRequest(request, "10.113.21.139", 3128);
                //Deserialize the response
                msg = HL7Utility.Deserialize(response.TrimEnd('\n'));

                if (msg.Segments[0].Elements[1] == SOAOkElement)
                {
                    errorServiceDiv.Visible = false;
                    noServiceDiv.Visible = false;

                    // double check if the idTextBox value is valid
                    if (Int32.TryParse(idTextBox.Text, out idVerification) == false)
                    {
                        throw new Exception("Team ID was invalid");
                    }

                    // load up the session variables
                    Session["TeamName"] = nameTextBox.Text;
                    Session["TeamID"] = idTextBox.Text;
                    Session["ServiceTeamName"] = msg.Segments[1].Elements[1];
                    Session["ServiceName"] = msg.Segments[1].Elements[2];
                    Session["NumArgs"] = msg.Segments[1].Elements[4];
                    Session["ServiceDescription"] = msg.Segments[1].Elements[6];
                    Session["ArgName1"] = GetArgument(1, msg)[0];
                    Session["ArgDataType1"] = GetArgument(1, msg)[1];
                    Session["ArgName2"] =  GetArgument(2, msg)[0];
                    Session["ArgDataType2"] = GetArgument(2, msg)[1];
                    Session["IPAddress"] = GetIPAddress(msg);
                    Session["portNumber"] = GetPortNumber(msg);

                    // transfer to the next page
                    Server.Transfer("default.aspx", true);

                }
                else if (msg.Segments[0].Elements[1] == SOANotOkElement)
                {
                    ErrorMessageID.InnerText = msg.Segments[0].Elements[3];
                    errorServiceDiv.Visible = true;
                    noServiceDiv.Visible = false;
                }
                else
                {
                    errorServiceDiv.Visible = false;
                    noServiceDiv.Visible = true;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }
        }
        protected void submitButton_Click(object sender, EventArgs e)
        {
            string province;
            double amount = 0;
            string request = "";
            string response = "";
            TaxSummary taxes = new GIORP_TOTAL.Models.TaxSummary();
            Message msg = new Message();

            province = provinceList.SelectedValue;
            double.TryParse(priceBox.Text, out amount);

            try
            {
                Regex priceRegex = new Regex(amountPattern);

                if (!priceRegex.IsMatch(amount.ToString()))
                {
                    throw new Exception("The amount value was invalid");
                }

                //  DRC|EXEC-SERVICE|PhantomPower|0|
                msg.AddSegment(CommandDirectiveElement, ExecuteServiceElement, teamName, teamID);
                //  SRV||PP-GIORP-TOTAL||2|||
                msg.AddSegment(ServiceDirectiveElement, "", serviceName, "", numArgs, "", "");
                //  ARG|1|province|string||PROVINCE CODE|
                if (argDataType1.IndexOf("string", 0, StringComparison.CurrentCultureIgnoreCase) != -1)
                {
                    msg.AddSegment(ArgumentDirectiveElement, "1", argName1, argDataType1, "", province);
                }
                else
                {
                    msg.AddSegment(ArgumentDirectiveElement, "1", argName1, argDataType1, "", amount.ToString());
                }
                //  ARG|2|amount|double||AMOUNT|
                if (argDataType2.IndexOf("double", 0, StringComparison.CurrentCultureIgnoreCase) != -1 ||
                    argDataType2.IndexOf("float", 0, StringComparison.CurrentCultureIgnoreCase) != -1)
                {
                    msg.AddSegment(ArgumentDirectiveElement, "2", argName2, argDataType2, "", amount.ToString());
                }
                else
                {
                    msg.AddSegment(ArgumentDirectiveElement, "2", argName2, argDataType2, "", province);
                }

                //Serialize the message
                request = HL7Utility.Serialize(msg);
                //Send the request
                response = ServiceClient.SendRequest(request, IP, port);
                //Deserialize the response
                msg = HL7Utility.Deserialize(response);

                if (msg.Segments[0].Elements[1] == SOAOkElement)
                {
                    taxes = null; //MessageToTaxSummary(msg);
                    response1.Text = msg.Segments[1].Elements[2] + ": " + msg.Segments[1].Elements[4];
                    response2.Text = msg.Segments[2].Elements[2] + ": " + msg.Segments[2].Elements[4];
                    response3.Text = msg.Segments[3].Elements[2] + ": " + msg.Segments[3].Elements[4];
                    response4.Text = msg.Segments[4].Elements[2] + ": " + msg.Segments[4].Elements[4];
                    response5.Text = msg.Segments[5].Elements[2] + ": " + msg.Segments[5].Elements[4];

                    // show the results div with the results
                    results.Visible = true;
                    alertDiv.Visible = false;
                }
                else if(msg.Segments[0].Elements[1] == SOANotOkElement)
                {
                    taxes = null;
                    alert.Text = msg.Segments[0].Elements[3];
                    alertDiv.Visible = true;
                    results.Visible = false;
                }
            }
            catch (Exception ex)
            {
                alert.Text = ex.Message;
                Logger.logException(ex);
            }

            //if (taxes != null)
            //{
            //    // fill in the labels with the responses
            //    try
            //    {
            //        // go through each segment and get the second element (name of field) and forth element (value)
            //        response1.Text = msg.Segments[1].Elements[2] + ": " + msg.Segments[1].Elements[4];
            //        response2.Text = msg.Segments[2].Elements[2] + ": " + msg.Segments[2].Elements[4];
            //        response3.Text = msg.Segments[3].Elements[2] + ": " + msg.Segments[3].Elements[4];
            //        response4.Text = msg.Segments[4].Elements[2] + ": " + msg.Segments[4].Elements[4];
            //        response5.Text = msg.Segments[5].Elements[2] + ": " + msg.Segments[5].Elements[4];
            //    }
            //    catch (Exception ex)
            //    {
            //        alert.Text = ex.Message;
            //        Logger.logException(ex);
            //    }

            //    // format the table
            //    //subtotalAmount.Text = taxes.NetAmount.ToString("C2");
            //    //pstAmount.Text = taxes.PstAmount.ToString("C2");
            //    //hstAmount.Text = taxes.HstAmount.ToString("C2");
            //    //gstAmount.Text = taxes.GstAmount.ToString("C2");
            //    //totalPurchaseAmount.Text = taxes.TotalAmount.ToString("C2");

            //    // show the results div with the results
            //    results.Visible = true;
            //    alertDiv.Visible = false;
            //}
            //else
            //{
            //    alertDiv.Visible = true;
            //    results.Visible = false;
            //}
        }
        static string HandleRequest(string request)
        {
            Message requestMessage = null;
            try
            {
                requestMessage = HL7Utility.Deserialize(request);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: " + ex.Message);

                Logger.logException(ex);
            }
            if (requestMessage != null)
            {
                // Check if incoming request is in valid format.
                if (requestMessage.Segments != null &&
                    requestMessage.Segments.Count == 4 && // DRC, SRV, ARG, ARG = 4
                    requestMessage.Segments[0].Elements != null && // DRC|EXEC-SERVICE|<team name>|<teamID>|
                    requestMessage.Segments[0].Elements.Count == 4 &&
                    requestMessage.Segments[0].Elements[0] == CommandDirectiveElement &&
                    requestMessage.Segments[0].Elements[1] == ExecuteServiceElement &&
                    requestMessage.Segments[1].Elements != null && // SRV||<service name>||<num args>|||
                    requestMessage.Segments[1].Elements.Count == 7 &&
                    requestMessage.Segments[1].Elements[0] == ServiceDirectiveElement &&
                    requestMessage.Segments[1].Elements[1] == "" &&
                    requestMessage.Segments[1].Elements[3] == "" &&
                    requestMessage.Segments[1].Elements[5] == "" &&
                    requestMessage.Segments[1].Elements[6] == "" &&
                    requestMessage.Segments[2].Elements != null && // ARG|<arg position>|<arg name>|<arg data type>||<arg value>|
                    requestMessage.Segments[2].Elements.Count == 6 &&
                    requestMessage.Segments[2].Elements[0] == ArgumentDirectiveElement &&
                    requestMessage.Segments[2].Elements[2] ==  "province" &&
                    requestMessage.Segments[2].Elements[3] == "string" &&
                    requestMessage.Segments[3].Elements != null && // ARG|<arg position>|<arg name>|<arg data type>||<arg value>|
                    requestMessage.Segments[3].Elements.Count == 6 &&
                    requestMessage.Segments[3].Elements[0] == ArgumentDirectiveElement &&
                    requestMessage.Segments[3].Elements[2] ==  "amount" &&
                    requestMessage.Segments[3].Elements[3] == "double")
                {
                    // Now we parse out the team requesting to execute.
                    var teamName = requestMessage.Segments[0].Elements[2];
                    var teamId = requestMessage.Segments[0].Elements[3];

                    if (ValidateTeam(teamName, teamId))
                    {
                        // Now we parse out the arguments province and amount
                        string provinceArg = requestMessage.Segments[2].Elements[5];
                        double amountArg = 0;
                        if (double.TryParse(requestMessage.Segments[3].Elements[5], out amountArg))
                        {
                            Models.TaxSummary taxSummary = null;
                            try
                            {
                                taxSummary = _taxCalculator.CalculateTax(provinceArg, amountArg);
                            }
                            catch (ArgumentException ae)
                            {
                                Logger.logException(ae);

                                var errorResponseMessage = new Message();
                                errorResponseMessage.AddSegment(PublishedServiceDirectiveElement, SOANotOkElement, "Arguments not valid.", ae.Message);
                                return HL7Utility.Serialize(errorResponseMessage);
                            }
                            catch (Exception ex)
                            {
                                Logger.logException(ex);

                                var errorResponseMessage = new Message();
                                errorResponseMessage.AddSegment(PublishedServiceDirectiveElement, SOANotOkElement, "There was a problem with calculating the tax summary.", "");
                                return HL7Utility.Serialize(errorResponseMessage);
                            }
                            if (taxSummary != null)
                            {
                                // SUCCESS //
                                var responseMessage = new Message();
                                // PUB|OK|||<num segments>|
                                responseMessage.AddSegment(PublishedServiceDirectiveElement, SOAOkElement, "", "", "5");
                                // RSP|<resp position>|<resp name>|<resp data type>|<resp value>|
                                responseMessage.AddSegment(ResponseDirectiveElement, "1", "NetAmount", "double", taxSummary.NetAmount.ToString());
                                // RSP|<resp position>|<resp name>|<resp data type>|<resp value>|
                                responseMessage.AddSegment(ResponseDirectiveElement, "2", "PstAmount", "double", taxSummary.PstAmount.ToString());
                                // RSP|<resp position>|<resp name>|<resp data type>|<resp value>|
                                responseMessage.AddSegment(ResponseDirectiveElement, "3", "HstAmount", "double", taxSummary.HstAmount.ToString());
                                // RSP|<resp position>|<resp name>|<resp data type>|<resp value>|
                                responseMessage.AddSegment(ResponseDirectiveElement, "4", "GstAmount", "double", taxSummary.GstAmount.ToString());
                                // RSP|<resp position>|<resp name>|<resp data type>|<resp value>|
                                responseMessage.AddSegment(ResponseDirectiveElement, "5", "TotalAmount", "double", taxSummary.TotalAmount.ToString());
                                return HL7Utility.Serialize(responseMessage);
                            }
                            else
                            {
                                var errorResponseMessage = new Message();
                                errorResponseMessage.AddSegment(PublishedServiceDirectiveElement, SOANotOkElement, "There was a problem with calculating the tax summary.", "");
                                return HL7Utility.Serialize(errorResponseMessage);
                            }
                        }
                        else
                        {
                            var errorResponseMessage = new Message();
                            errorResponseMessage.AddSegment(PublishedServiceDirectiveElement, SOANotOkElement, "Could not parse amount argument.", "Make sure that amount argument is of type double.");
                            return HL7Utility.Serialize(errorResponseMessage);
                        }
                    }
                    else
                    {
                        var errorResponseMessage = new Message();
                        errorResponseMessage.AddSegment(PublishedServiceDirectiveElement, SOANotOkElement, "Assess denied.", string.Format("Team {0} with ID {1} is not allowed to execute the service.", teamName, teamId));
                        return HL7Utility.Serialize(errorResponseMessage);
                    }
                }
                else
                {
                    var errorResponseMessage = new Message();
                    errorResponseMessage.AddSegment(PublishedServiceDirectiveElement, SOANotOkElement, "Request not in valid format.", "Make sure that you comply to SOA Registry message format.");
                    return HL7Utility.Serialize(errorResponseMessage);
                }
            }
            else
            {
                var errorResponseMessage = new Message();
                errorResponseMessage.AddSegment(PublishedServiceDirectiveElement, SOANotOkElement, "There was a problem with calculating the tax summary.", "");
                return HL7Utility.Serialize(errorResponseMessage);
            }
        }