Beispiel #1
0
        /// <summary>
        /// Test reading a TNT Result document, using filed examples
        /// </summary>
        public static void DoTheTests()
        {
            //examples to choose from
            string[] filenames = new string[] {
                "multi_3con_createship.txt",
                "typical_1con_createship.txt",
                "typical_1con_createship_just2docs.txt",
                "printonly.txt",
                "error.txt",
                "runtime_error.txt",
            };

            //edit this line to choose an example
            string fullpath = FilePath + filenames[2];

            //edit this line to choose one of the two indexing options
            TNTResult.IndexBy indexBy = TNTResult.IndexBy.CONREF;

            //initialize the class to test
            XmlReader XReader = XmlReader.Create(fullpath);
            TNTResult res     = new TNTResult(XReader, indexBy);

            //look for error messages
            if (res.hasAnyError)
            {
                Debug.Print("Error: " + res.GetFirstErrorDescr());
            }

            if (res.OK)
            {
                //worthy of further investigation

                //available documents on TNT server
                Debug.Print("IsConnoteCreated: " + res.IsConnoteCreated);
                Debug.Print("IsInvoiceCreated: " + res.IsInvoiceCreated);
                Debug.Print("IsLabelCreated: " + res.IsLabelCreated);
                Debug.Print("IsManifestCreated: " + res.IsManifestCreated);

                //iterate the consignments in the doc and get their properties
                foreach (var conId in res)
                {
                    Debug.Print("Id value used for indexing: " + conId);
                    Debug.Print("ConRef: " + res.ConRef);
                    Debug.Print("ConNumber: " + res.ConNumber);
                    Debug.Print("Success: " + res.Success);
                }
            }
            else
            {
                Debug.Print("The document is not worthy of further investigation.");
            }
        }
        /// <summary>
        /// Use the Accesscode to get the TNTResult document that will show us if the consignments were booked
        /// </summary>
        private async Task GetResult()
        {
            this.Status = Statusses.GetResult;
            FormUrlEncodedContent content = new FormUrlEncodedContent(new Dictionary <string, string>()
            {
                { "xml_in", "GET_RESULT:" + this.accessCode }  //application/x-www-form-urlencoded like it's meant to be... no xml really!
            });
            PostResults resp = await PostAsync(content, expectXml : true);

            //this time the result is xml and we go analyse it
            TNTResult.IndexBy indexBy = (this.shipReq.reqestType == TNTShipRequest.RequestTypes.Full) ?
                                        TNTResult.IndexBy.CONREF : TNTResult.IndexBy.CONNUMBER;
            this.shipRes = new TNTResult(resp.Xml, indexBy);

            if (shipRes.OK && !shipRes.hasAnyError)
            {
                if (shipReq.reqestType == TNTShipRequest.RequestTypes.Full && shipRes.hasShip)
                {
                    foreach (var c in shipRes) //iterate the consignments
                    {
                        if (shipRes.Success)   //consignment booked?
                        {
                            FireConsignmentSuccess(shipRes.ConRef, shipRes.ConNumber);
                        }
                        else
                        {
                            FireConsignmentNoSuccess(shipRes.ConRef);
                        }
                    }
                }
                else
                {
                    if (shipReq.reqestType == TNTShipRequest.RequestTypes.PrintOnly && !shipRes.hasShip)
                    {
                        //good! just proceed
                    }
                    else //surely wrong
                    {
                        if (shipReq.reqestType == TNTShipRequest.RequestTypes.Full && !shipRes.hasShip)
                        {
                            FireShipResultError(0, "Error in result document: full request, but SHIP element not found in result.");
                        }
                        else
                        {
                            FireShipResultError(0, "Error in result document: request was 'printonly' and still SHIP is present.");
                        }
                        return;  //no follow up after these errors
                    }
                }
                //Path of (at least partial) success: which docs do we need to get from te server?
                if (shipRes.IsLabelCreated)
                {
                    this.docStatus[DocTypes.Label] = DocStatusses.OnTNTServer;
                }
                if (shipRes.IsManifestCreated)
                {
                    this.docStatus[DocTypes.Manifest] = DocStatusses.OnTNTServer;
                }
                if (shipRes.IsConnoteCreated)
                {
                    this.docStatus[DocTypes.Connote] = DocStatusses.OnTNTServer;
                }
                if (shipRes.IsInvoiceCreated)
                {
                    this.docStatus[DocTypes.Invoice] = DocStatusses.OnTNTServer;
                }
                //event
                FireResultReceived(docStatus[DocTypes.Label],
                                   docStatus[DocTypes.Manifest],
                                   docStatus[DocTypes.Connote],
                                   docStatus[DocTypes.Invoice]
                                   );
                //start follow up requests!
                this.Status = Statusses.GetDocs;
                await GetNextExpectedDocument();
            }
            else
            {
                FireShipResultError(0, "Error in result document: " + shipRes.GetFirstErrorDescr());
            }
        }