Files

127 lines
4.6 KiB
C#

using Sarand.Core.App;
using Sarand.Core.Base;
using Sarand.Core.Net;
using Sarand.Core.Xml;
using Sarand.PatientMgr.Core.Data;
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Linq;
namespace Sarand.PatientMgr.Core.WebService
{
//=======================================================================
public class SpmWebServiceResponse
{
//-------------------------------------------------------------------
// Definitions.
//-------------------------------------------------------------------
// Configuration.
public class Config
{
public DateTime RequestStarted { get; set; } = DateTime.Now;
public DateTime RequestCompleted { get; set; }
public SacsHttpResponseData HttpResponseData { get; set; }
}
//-------------------------------------------------------------------
// Data members.
protected readonly Config config;
//-------------------------------------------------------------------
// Construction.
public SpmWebServiceResponse (Config config)
{
this.config = config;
ResponseData = SpmWindowsServiceCrypter.DecryptResponseData(config.HttpResponseData.Data);
try
{
// Identify the response element in the xml data.
ResponseDoc = XDocument.Parse(ResponseData, LoadOptions.None);
XElement rootElement = ResponseDoc.Root;
if (rootElement.Name.LocalName.EndsWithCI("Response"))
ResponseElement = rootElement;
else if (rootElement.Name.LocalName.EqualsCI(SacsSoapUtils.GetElementName(SoapElementType.Envelope)))
{
SoapEnvelope = true;
SoapPrefix = rootElement.GetNamespacePrefix();
foreach (XElement soapElement in rootElement.Elements())
{
if (soapElement.Name.LocalName.EqualsCI(SacsSoapUtils.GetElementName(SoapElementType.Body)))
{
foreach (XElement bodyElement in soapElement.Elements())
{
if (bodyElement.Name.LocalName.EndsWithCI("Response"))
{
ResponseElement = bodyElement;
}
}
}
}
}
if (ResponseElement != null)
{
// Extract status code.
XElement statusElement = ResponseElement.GetFirstChildElement(SpmWebServiceDfn.ElementName.Status);
if (statusElement != null)
{
StatusCode = statusElement.GetAttributeValue(SpmWebServiceDfn.AttributeName.Code);
// Extract messages.
XElement messagesElement = statusElement.GetFirstChildElement(SpmWebServiceDfn.ElementName.Messages);
if (messagesElement != null)
{
foreach (XElement messageElement in messagesElement.GetChildElements(SpmWebServiceDfn.ElementName.Message))
{
String context = messageElement.GetAttributeValue(SpmWebServiceDfn.AttributeName.Context);
String code = messageElement.GetAttributeValue(SpmWebServiceDfn.AttributeName.Code);
SaMessageLevel level = SacsSystem.GetMessageLevelFromCode(messageElement.GetAttributeValue(SpmWebServiceDfn.AttributeName.Level));
SpmWebServiceMessage message = new SpmWebServiceMessage(context, code, messageElement.Value, level);
Messages.Add(message);
}
}
}
}
}
catch (Exception) { }
}
public SpmWebServiceResponse (SpmWebServiceResponse serviceResponse)
{
// Copied from another service reponse - don't re-intrepert properties.
this.config = serviceResponse.config;
// Structure.
SoapEnvelope = serviceResponse.SoapEnvelope;
SoapPrefix = serviceResponse.SoapPrefix;
// Response.
ResponseData = serviceResponse.ResponseData;
ResponseDoc = serviceResponse.ResponseDoc;
ResponseElement = serviceResponse.ResponseElement;
// Status.
StatusCode = serviceResponse.StatusCode;
Messages = serviceResponse.Messages;
}
//-------------------------------------------------------------------
// Properties.
public DateTime RequestStarted { get { return config.RequestStarted; } }
public DateTime RequestCompleted { get { return config.RequestCompleted; } }
public SacsHttpResponseData HttpResponseData { get { return config.HttpResponseData; } }
// Structure.
public bool SoapEnvelope { get; }
public String SoapPrefix { get; } = "";
// Response.
public String ResponseData { get; }
public XDocument ResponseDoc { get; }
public XElement ResponseElement { get; }
// Status.
public bool TaskCancelled { get; set; }
public String StatusCode { get; } = "";
public List<SpmWebServiceMessage> Messages { get; } = new List<SpmWebServiceMessage>();
public String ErrorStateMessage { get; set; }
//-------------------------------------------------------------------
}
//=======================================================================
}