/* * Relevant fields for each action type (1: exactly one, *: many): * * Submit Update GetStatus Cancel * StatusReport 1 1 * 1 * targetTask 1 * * "targetTask" is redundant (?) with "taskId" in StatusReport, but the * XML schema requires it. */ #region Functions for read /// <summary> /// Constructor. Use this to deserialise the object from an XML document. /// </summary> /// <param name="xmlBytes">XML data.</param> public TaskResponse(byte[] xmlBytes) : base() { try { XsdNs.ExtensibleResponseType proxy = null; string rootElName; try { // Mapping the names of root elements and message classes rootElName = XNeut.Helper.GetRootElNameFromXmlDoc(xmlBytes); } catch (InvalidOperationException e) { throw new XNeut.InvalidMessageException("Fail to parse task-related request", e); } switch (rootElName) { case "SubmitResponse": proxy = ReadSubmitXml(xmlBytes); Operation = TaskOperationType.Submit; break; case "UpdateResponse": proxy = ReadUpdateXml(xmlBytes); Operation = TaskOperationType.Update; break; case "GetStatusResponse": proxy = ReadGetStatusXml(xmlBytes); Operation = TaskOperationType.GetStatus; break; case "CancelResponse": proxy = ReadCancelXml(xmlBytes); Operation = TaskOperationType.Cancel; break; default: throw new XNeut.InvalidMessageException("Unexpected root element in task response: " + rootElName); } // Reading the data of ExtensibleResponse ReadExtensibleResponseItemsFromProxy(proxy); } catch (NullReferenceException e) { throw new XNeut.InvalidMessageException("Something expected missing from task response", e); } }
/// <summary> /// Reads values from the XML proxy. /// </summary> /// <param name="proxy">Proxy.</param> protected void ReadExtensibleResponseItemsFromProxy(XsdNs.ExtensibleResponseType proxy) { // The default result is "unknown" when reading from XML RequestResult = RequestResultType.Unknown; // Checking if the status data record is there if (proxy.Extension != null && proxy.Extension.Length > 0) { foreach (var ext in proxy.Extension) { // Is it a data record? if (!(ext is XsdNs.DataRecordPropertyType dataRecordProp)) { continue; } // Does the data record have a body with the expected identifier? if (dataRecordProp.DataRecord == null || dataRecordProp.DataRecord.identifier == null || !dataRecordProp.DataRecord.identifier.Equals(IDENTIFIER)) { continue; } // Processing the data record var dataRecord = new Item_DataRecord(dataRecordProp); // Getting status information if (dataRecord.ItemNames.Contains(RESULT_FIELD_NAME)) { RequestResult = ParseRequestResult(dataRecord[RESULT_FIELD_NAME]); } else { RequestResult = RequestResultType.Unknown; } // Getting status message if (dataRecord.ItemNames.Contains(MESSAGE_FIELD_NAME)) { RequestResultMessage = GetStatusMessage(dataRecord[MESSAGE_FIELD_NAME]); } } } }
/// <summary> /// Serialises the object to XML. /// </summary> /// <returns>Object in XML.</returns> public byte[] ToXmlBytes() { // There must be exactly 1 status report if the operation is not GetStatus if (Operation != TaskOperationType.GetStatus && StatusReports.Count != 1) { throw new ArgumentException("Expected exactly one status report for response to " + Operation.ToString()); } XsdNs.ExtensibleResponseType proxy = null; switch (Operation) { case TaskOperationType.Submit: proxy = GetSubmitResponseProxy(); break; case TaskOperationType.Update: proxy = GetUpdateResponseProxy(); break; case TaskOperationType.GetStatus: proxy = GetGetStatusResponseProxy(); break; case TaskOperationType.Cancel: proxy = GetCancelResponseProxy(); break; default: throw new NotSupportedException("Unsupported task operation " + Operation.ToString()); } // Populating the items of ExtensibleResponse PopulateExtensibleResponseToProxy(proxy); try { // Serialising return(XNeut.Helper.ToXmlBytes(proxy)); } catch (InvalidOperationException e) { throw new XNeut.InvalidMessageException("XML serialisation failed", e); } }
/// <summary> /// This method populates the base class data to the given proxy. /// </summary> /// <param name="proxy">Proxy.</param> protected void PopulateExtensibleResponseToProxy(XsdNs.ExtensibleResponseType proxy) { // Creating a data record to enclose result information var record = new Item_DataRecord(); var resultItem = new Item_Category(RequestResult.ToString()); record.Add(RESULT_FIELD_NAME, resultItem); var messageItem = new Item_Text(RequestResultMessage); record.Add(MESSAGE_FIELD_NAME, messageItem); // Creating a proxy and adding it as an extension proxy.Extension = new object[1]; var recordProp = (XsdNs.DataRecordPropertyType)record.GetObjectForXml_Result("ExtResp_"); recordProp.DataRecord.identifier = IDENTIFIER; proxy.Extension[0] = recordProp; }