using System;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Text;
///
/// Web service interface to be used by 3rd parties to interact with NLTIS (OLAS and RAS) data
///
using System.Web.Services;
using System.Xml;
[WebService(Namespace = "http://nltis.transport.gov.za/")]
public class NLTISInterface : System.Web.Services.WebService
{
public NLTISInterface()
{
InitializeComponent();
}
#region Component Designer generated code
//Required by the Web Services Designer
private IContainer components = null;
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
}
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if(disposing && components != null)
{
components.Dispose();
}
base.Dispose(disposing);
}
#endregion
#region IsAlive
///
/// Determines if this service is alive.
///
/// Name of the user.
/// The password.
///
/// true if the user name and password are correct; false otherwise.
///
[WebMethod(Description = "Determines if this service is alive.")]
public bool IsAlive(string interfaceKey)
{
if (IsValid(interfaceKey))
{
return true;
}
return false;
}
#endregion
#region GetApplications
///
/// Gets a list of all applications related to the criteria specified
///
/// The shared web service interface key.
/// operating licence number.
/// id number.
/// vehicle chassis number.
///
/// A document containing a list of all related applications as per OLASApplicationSchema.xsd.
///
[WebMethod(Description = "Gets a list of all applications related to the criteria specified.")]
public XmlDocument GetApplications(string interfaceKey, string operatingLicenceNumber, string idNumber, string registrationNumber, string chassisNumber)
{
string spName = ConfigurationManager.AppSettings["GetApplicationsStoredProcedure"].ToString();
string[] spParamNames = {"IDNumber","OperatingLicenceNumber","RegistrationNumber","VehicleChassisNumber"};
string[] spParamValues = { idNumber, operatingLicenceNumber, registrationNumber, chassisNumber };
if (IsValid(interfaceKey))
{
try
{
return GetXml(interfaceKey, spName, spParamNames, spParamValues);
}
catch (Exception Ex)
{
return GetXmlError(Ex.Message);
}
}
else
{
return GetXmlError("Invalid interface key");
}
}
#endregion
#region GetXml
private XmlDocument GetXml(string interfaceKey, string spName, string[] spParamNames, object[] spParamValues)
{
XmlDocument xmlDoc = new XmlDocument();
XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement);
XmlElement rootNode = xmlDoc.CreateElement("response");
bool found = false;
using (SqlConnection connection = new SqlConnection(ConfigurationManager.AppSettings["NltisConnection"]))
{
using (SqlCommand command = connection.CreateCommand())
{
command.CommandType = CommandType.StoredProcedure;
command.CommandText = spName;
for (int i = 0; i < spParamNames.Length; i++)
{
command.Parameters.Add(new SqlParameter(spParamNames[i], spParamValues[i]));
}
StringBuilder sb = new StringBuilder();
using (StringWriter stringWriter = new StringWriter(sb))
{
connection.Open();
XmlReader reader = command.ExecuteXmlReader();
XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);
while (reader.MoveToContent() != XmlNodeType.None)
{
xmlWriter.WriteNode(reader, false);
}
reader.Close();
connection.Close();
xmlWriter.Flush();
xmlWriter.Close();
}
found = sb.Length > 0;
if (found)
{
rootNode.InnerXml = sb.ToString();
}
}
}
XmlAttribute foundAttr = xmlDoc.CreateAttribute("found");
foundAttr.Value = found.ToString().ToLower();
rootNode.Attributes.Append(foundAttr);
xmlDoc.AppendChild(rootNode);
return xmlDoc;
}
#endregion
#region GetXmlError(string Error)
private XmlDocument GetXmlError(string error)
{
XmlDocument xmlDoc = new XmlDocument();
XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement);
XmlElement rootNode = xmlDoc.CreateElement("response");
XmlAttribute foundAttr = xmlDoc.CreateAttribute("found");
foundAttr.Value = "false";
rootNode.Attributes.Append(foundAttr);
xmlDoc.AppendChild(rootNode);
XmlElement messageNode = xmlDoc.CreateElement("message");
XmlElement messageType = xmlDoc.CreateElement("messagetype");
messageType.InnerText = "Error";
XmlElement description = xmlDoc.CreateElement("description");
description.InnerText = error;
messageNode.AppendChild(messageType);
messageNode.AppendChild(description);
rootNode.AppendChild(messageNode);
return xmlDoc;
}
#endregion
#region IsValid
///
/// Confirms that the specified interface key matches the web config setting
///
///
///
private bool IsValid(string interfaceKey)
{
return ConfigurationManager.AppSettings["NltisInterfaceKey"] == interfaceKey;
}
#endregion
}