using Sarand.Net.Standard.Base.Bus;
using Sarand.Net.Standard.Base.Core;
using Sarand.Net.Standard.Base.Utils;
using System;
using System.Collections.Generic;
using System.Xml.Linq;
namespace Sarand.BusinessMgr.Net.Standard.Base.WebService
{
//=======================================================================
public class SbmWebServiceRequest
{
//-------------------------------------------------------------------
// Definitions.
public enum RequestType
{
Unknown,
IsAlive,
/// Item count
Count,
/// Query list - specific columns, compressed (csv etc) response data
QueryList,
/// Item list - full xml for each item with optional associated items.
ItemList,
/// Single Item with optional associated items.
Item,
/// Validate item properties without update.
Validate,
/// Validate properties and store (insert/update) item.
Store,
/// Delete item.
Delete,
/// List - as per Query List
[Obsolete("Deprecated. Use QueryList instead.", true)]
List,
/// Single Item with optional associated items.
[Obsolete("Deprecated. Use Item instead.", true)]
Data,
}
public enum DataType
{
Unknown,
// Security.
UserProfile,
// Reference data.
CodeType,
Code,
// Accounts.
Affiliate,
Address,
Association,
SysParam,
SystemAccount,
SystemUser,
SystemAccountUser,
// Assets.
Asset,
AssetCategory,
AssetProcess,
VehicleClass,
Vehicle,
VehicleEvent,
VehicleFeature,
// Commodity.
Commodity,
CommodityCustomer,
CommoditySupplier,
Container,
ContainerItem,
PortfolioItem,
// Price.
Price,
PriceSet,
// Stock.
Location,
Movement,
// Transaction.
Transaction,
Requirement,
Constituent,
// Process.
ItemProcess,
Process,
// Data.
ItemBatch,
ItemLink,
// Reference Data.
Currency,
Language,
Region,
TaxCode,
UnitMeasure,
}
//-------------------------------------------------------------------
///
/// Sort column identified by column name with ascending flag.
///
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 SbmWebServiceInstanceDefinition InstanceDefinition { 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 SbmWebServiceRequest(Config config)
{
this.config = config;
}
//-------------------------------------------------------------------
// Properties.
public Dictionary Filters { get; } = new Dictionary();
public List Columns { get; } = new List();
public List SortColumns { get; } = new List();
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();
}
//-------------------------------------------------------------------
}
//=======================================================================
}