Ejemplo n.º 1
0
 public static string CreateRegisterTeamRequest(string teamName)
 {
     var message = new Message();
     message.AddSegment(CommandDirectiveElement, RegisterTeamElement, "", "");
     message.AddSegment(InfoDirectiveElement, teamName,"","");
     return HL7Utility.Serialize(message);
 }
Ejemplo n.º 2
0
        public static Message Deserialize(string message)
        {
            if (message == null) { throw new ArgumentNullException(); }
            if (message == "") { throw new ArgumentException("Message cannot be empty."); }
            if (!message.StartsWith(HL7Utility.BeginOfMessage.ToString())) { throw new ArgumentException("Message not in valid format. Begin of message char '" + HL7Utility.BeginOfMessage.ToString() + "' not found."); }
            if (!message.EndsWith(HL7Utility.EndOfMessage.ToString())) { throw new ArgumentException("Message not in valid format. End of message char '" + HL7Utility.EndOfMessage.ToString() + "' not found."); }

            // Get rid of BeginOfMessage and EndOfMessage chars because they are just used by us to be able to parse out the message.
            message = message.Substring(1, message.Length - 3); // -1 (since we are sub-stringing from 1) + -1 (since we do not want to grab last char) = -2.
            // Now we are only left with segments of the message that are separated by EndOfSegment chars.
            var segments = message.Split(HL7Utility.EndOfSegment).ToList<string>();
            // usually as a side-effect of the Split we end up with extra element that is an empty string.
            if (segments[segments.Count - 1] == "") { segments.RemoveAt(segments.Count - 1); }
            if (segments == null || segments.Count() == 0)
            {
                throw new ArgumentException("Message has to contain at least 1 segment.");
            }
            // remove last if it is empty.

            var deserializedMessage = new Message() { Segments = new List<Segment>()};
            try
            {
                foreach (var segment in segments)
                {
                    var segmetnElements = HL7Utility.ParseSegment(segment);
                    deserializedMessage.Segments.Add(new Segment() { Elements = segmetnElements });
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Segment not in valid format. Check inner exception for details.", ex);
            }

            return deserializedMessage;
        }
Ejemplo n.º 3
0
 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);
 }
Ejemplo n.º 4
0
 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);
 }
Ejemplo n.º 5
0
        public static string Serialize(Message message)
        {
            StringBuilder serializedMessage = new StringBuilder();
            serializedMessage.Append(BeginOfMessage);

            foreach (var segment in message.Segments)
            {
                serializedMessage.Append(CreateSegment(segment.Elements));
            }
            serializedMessage.Append(EndOfMessage);

            return serializedMessage.ToString();
        }
Ejemplo n.º 6
0
        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());
            }
        }
Ejemplo n.º 7
0
        int GetPortNumber(Message msg)
        {
            int result = 0;
            int segPos = 0;

            //Search the segments
            foreach (Segment seg in msg.Segments)
            {
                foreach (string s in seg.Elements)
                {
                    if (s.Contains("MCH"))
                    {
                        Int32.TryParse(msg.Segments[segPos].Elements[2], out result);
                    }
                }

                ++segPos;
            }

            return result;
        }
Ejemplo n.º 8
0
        string GetIPAddress(Message msg)
        {
            string result = "";
            int segPos = 0;

            //Search the segments
            foreach (Segment seg in msg.Segments)
            {
                foreach (string s in seg.Elements)
                {
                    if (s.Contains("MCH"))
                    {
                        result = msg.Segments[segPos].Elements[1];
                    }
                }

                ++segPos;
            }

            return result;
        }
Ejemplo n.º 9
0
        List<string> GetArgument(int argumentPos, Message msg)
        {
            List<string> result = new List<string>();
            int segPos = 0;
            bool pass = true;

            //Search the segments
            foreach (Segment seg in msg.Segments)
            {
                foreach (string s in seg.Elements)
                {
                    if (s.Contains("ARG"))
                    {
                        if (seg.Elements[4].Contains("mandatory"))
                        {
                            if (argumentPos == 1)
                            {
                                result.Add(msg.Segments[segPos].Elements[2]);
                                result.Add(msg.Segments[segPos].Elements[3]);
                                break;
                            }
                            else if (argumentPos == 2)
                            {
                                if (pass)
                                {
                                    pass = false;
                                }
                                else
                                {
                                    result.Add(msg.Segments[segPos].Elements[2]);
                                    result.Add(msg.Segments[segPos].Elements[3]);
                                    break;
                                }
                            }
                        }
                    }
                }

                ++segPos;
            }

            return result;
        }
Ejemplo n.º 10
0
        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;
            //}
        }
Ejemplo n.º 11
0
        private static TaxSummary MessageToTaxSummary(Message msg)
        {
            TaxSummary ts = new TaxSummary();
            double temp = 0.0;

            //For sanity...
            //If its ok to parse...
            if (msg != null)
            {
                if (msg.Segments[0].Elements[1] == SOAOkElement)
                {
                    if (msg.Segments[1].Elements[2].Equals("NetAmount") &&
                        msg.Segments[2].Elements[2].Equals("PstAmount") &&
                        msg.Segments[3].Elements[2].Equals("HstAmount") &&
                        msg.Segments[4].Elements[2].Equals("GstAmount") &&
                        msg.Segments[5].Elements[2].Equals("TotalAmount"))
                    {
                        double.TryParse(msg.Segments[1].Elements[4], out temp);
                        ts.NetAmount = temp;

                        double.TryParse(msg.Segments[2].Elements[4], out temp);
                        ts.PstAmount = temp;

                        double.TryParse(msg.Segments[3].Elements[4], out temp);
                        ts.HstAmount = temp;

                        double.TryParse(msg.Segments[4].Elements[4], out temp);
                        ts.GstAmount = temp;

                        double.TryParse(msg.Segments[5].Elements[4], out temp);
                        ts.TotalAmount = temp;
                    }
                }
            }

            return ts;
        }
Ejemplo n.º 12
0
        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);
            }
        }