220 lines
7.1 KiB
C#
220 lines
7.1 KiB
C#
using Sarand.Core.Base;
|
|
using Sarand.Core.Xml;
|
|
using Sarand.PatientMgr.Core.App;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Xml.Linq;
|
|
using Sarand.Core.Bus;
|
|
|
|
namespace Sarand.PatientMgr.Core.WebService
|
|
{
|
|
//=======================================================================
|
|
public class SpmWebServiceRequest
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// Definitions.
|
|
public enum RequestType
|
|
{
|
|
Unknown,
|
|
IsAlive,
|
|
/// <summary>Item count</summary>
|
|
Count,
|
|
/// <summary>Query list - specific columns, compressed (csv etc) response data</summary>
|
|
QueryList,
|
|
/// <summary>Item list - full xml for each item with optional associated items.</summary>
|
|
ItemList,
|
|
/// <summary>Single Item with optional associated items.</summary>
|
|
Item,
|
|
/// <summary>Validate item properties without update.</summary>
|
|
Validate,
|
|
/// <summary>Validate properties and store (insert/update) item.</summary>
|
|
Store,
|
|
/// <summary>Delete item.</summary>
|
|
Delete,
|
|
/// <summary>List - as per Query List</summary>
|
|
[Obsolete("Deprecated. Use QueryList instead.", true)]
|
|
List,
|
|
/// <summary>Single Item with optional associated items.</summary>
|
|
[Obsolete("Deprecated. Use Item instead.", true)]
|
|
Data,
|
|
}
|
|
public enum DataType
|
|
{
|
|
Unknown,
|
|
// Security.
|
|
UserProfile,
|
|
// Reference data.
|
|
CodeType,
|
|
Code,
|
|
// Admission.
|
|
Admission,
|
|
AdmissionAction,
|
|
AdmissionData,
|
|
AdmissionRole,
|
|
// Department.
|
|
Department,
|
|
EmployeeRole,
|
|
Facility,
|
|
FacilitySession,
|
|
FacilityTemplate,
|
|
Procedure,
|
|
TemplateData,
|
|
// Institution.
|
|
Employee,
|
|
Institution,
|
|
Operation,
|
|
Patient
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
/// <summary>
|
|
/// Sort column identified by column name with ascending flag.
|
|
/// </summary>
|
|
public class SortColumnData
|
|
{
|
|
public SortColumnData (String columnName, bool ascending)
|
|
{
|
|
ColumnName = columnName;
|
|
Ascending = ascending;
|
|
}
|
|
public String ColumnName { get; }
|
|
public bool Ascending { get; }
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Configuration.
|
|
public class Config
|
|
{
|
|
public bool SoapEnvelope { get; set; }
|
|
public String SoapPrefix { get; set; }
|
|
public DataType DataType { get; set; }
|
|
public RequestType RequestType { get; set; }
|
|
public class ContextProperties
|
|
{
|
|
public SpmWebServiceInstanceDefinition InstanceDefinition { get; set; }
|
|
public String Url { get; set; }
|
|
public String User { get; set; }
|
|
public String Email { get; set; }
|
|
public SysId AffiliateIdOrganisation { get; set; }
|
|
public bool Commit { get; set; }
|
|
public bool Debug { get; set; }
|
|
}
|
|
public ContextProperties Context { get; } = new ContextProperties();
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Data members.
|
|
private readonly Config config;
|
|
|
|
//-------------------------------------------------------------------
|
|
// Construction.
|
|
public SpmWebServiceRequest (Config config)
|
|
{
|
|
this.config = config;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Properties.
|
|
public Dictionary<String, String> Filters { get; } = new Dictionary<String, String>();
|
|
public List<String> Columns { get; } = new List<String>();
|
|
public List<String> SortColumns { get; } = new List<String>();
|
|
public XElement ItemElement { get; set; }
|
|
|
|
//-------------------------------------------------------------------
|
|
// D a t a G e n e r a t i o n
|
|
//-------------------------------------------------------------------
|
|
public String GetRequestName ()
|
|
{
|
|
if (config.RequestType == RequestType.IsAlive) return "";
|
|
String requestName = "";
|
|
if (config.DataType > 0)
|
|
requestName += config.DataType.ToString();
|
|
if (config.RequestType > 0)
|
|
requestName += config.RequestType.ToString();
|
|
return requestName + "Request";
|
|
}
|
|
public String GenerateRequestData (bool formatted = false)
|
|
{
|
|
if (config.RequestType == RequestType.IsAlive) return "";
|
|
|
|
// Request document.
|
|
XDocument requestDoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));
|
|
// Request root.
|
|
XElement requestElement = null;
|
|
if (config.SoapEnvelope)
|
|
{
|
|
XElement envelopeElement = null;
|
|
XElement headerElement = null;
|
|
XElement bodyElement = null;
|
|
if (!StringUtil.IsEmpty(config.SoapPrefix))
|
|
{
|
|
XNamespace soapNamespace = SacsSoapUtils.GetSoapNamespace(SoapNamespaceType.Soap_1_1);
|
|
envelopeElement = requestDoc.AppendElement(soapNamespace, SacsSoapUtils.GetElementName(SoapElementType.Envelope));
|
|
envelopeElement.SetAttributeValue(XNamespace.Xmlns + config.SoapPrefix, soapNamespace);
|
|
headerElement = envelopeElement.AppendElement(soapNamespace, SacsSoapUtils.GetElementName(SoapElementType.Header));
|
|
bodyElement = envelopeElement.AppendElement(soapNamespace, SacsSoapUtils.GetElementName(SoapElementType.Body));
|
|
}
|
|
else
|
|
{
|
|
// No namespace.
|
|
envelopeElement = requestDoc.AppendElement(SacsSoapUtils.GetElementName(SoapElementType.Envelope));
|
|
headerElement = envelopeElement.AppendElement(SacsSoapUtils.GetElementName(SoapElementType.Header));
|
|
bodyElement = envelopeElement.AppendElement(SacsSoapUtils.GetElementName(SoapElementType.Body));
|
|
}
|
|
requestElement = bodyElement.AppendElement(GetRequestName());
|
|
}
|
|
else
|
|
{
|
|
// Otherwise add the request directly to the document.
|
|
requestElement = requestDoc.AppendElement(GetRequestName());
|
|
}
|
|
|
|
// Config.
|
|
XElement configElement = requestElement.AppendElement("Config");
|
|
|
|
// Context.
|
|
XElement contextElement = configElement.AppendElement("Context");
|
|
if (!StringUtil.IsEmpty(config.Context.User)) contextElement.AppendElement("User", config.Context.User);
|
|
if (!StringUtil.IsEmpty(config.Context.Email)) contextElement.AppendElement("Email", config.Context.Email);
|
|
if (SysId.IsValid(config.Context.AffiliateIdOrganisation)) contextElement.AppendElement("AffiliateIdOrganisation", SysId.ToString(config.Context.AffiliateIdOrganisation));
|
|
if (config.Context.Commit) contextElement.AppendElement("Commit", "1");
|
|
if (config.Context.Debug) contextElement.AppendElement("Debug", "1");
|
|
|
|
// Filter.
|
|
if (Filters.Count > 0)
|
|
{
|
|
XElement filterElement = configElement.AppendElement("Filter");
|
|
foreach (String name in Filters.Keys)
|
|
filterElement.AppendElement(name, Filters[name]);
|
|
}
|
|
|
|
// Columns.
|
|
if (Columns.Count > 0)
|
|
{
|
|
XElement columnsElement = configElement.AppendElement("Columns", Columns.Concat(",", "", ""));
|
|
}
|
|
|
|
// Sort columns.
|
|
if (SortColumns.Count > 0)
|
|
{
|
|
XElement sortColumnsElement = configElement.AppendElement("SortColumns", SortColumns.Concat(",", "", ""));
|
|
}
|
|
|
|
// Item.
|
|
if (ItemElement != null)
|
|
{
|
|
XElement dataElement = requestElement.AppendElement("Data");
|
|
dataElement.AppendElement(ItemElement);
|
|
}
|
|
|
|
if (formatted)
|
|
return requestDoc.FormatXml();
|
|
return requestDoc.GetXml();
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|