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 Legitimate data
///
using System.Web.Services;
using System.Xml;
[WebService(Namespace = "http://www.legiti-mate.net/")]
public class LegitimateInterface : System.Web.Services.WebService
{
public LegitimateInterface()
{
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.
/// permit licence number.
/// id number.
/// vehicle chassis number.
///
/// A document containing a list of all related applications as per LegitimateApplicationSchema.xsd.
///
[WebMethod(Description = "Gets a list of all applications related to the criteria specified.")]
public XmlDocument GetApplications(string interfaceKey, string olNumber, string idNumber, string registrationNumber, string chassisNumber)
{
string spName = ConfigurationManager.AppSettings["GetApplicationsStoredProcedure"].ToString();
string[] spParamNames = { "IDNumber", "OperatingLicenceNumber", "RegistrationNumber", "VehicleChassisNumber" };
string[] spParamValues = { idNumber, olNumber, 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 GetAssociationMasterData
///
/// Gets a list of all verified associations
///
/// The shared web service interface key.
///
/// A document containing a list of all the verified associations.
///
[WebMethod(Description = "Gets a list of all associations.")]
public XmlDocument GetAssociationMasterData(string interfaceKey)
{
string spName = ConfigurationManager.AppSettings["GetAssociationsMasterdataStoredProcedure"].ToString();
string[] spParamNames = { };
string[] spParamValues = { };
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 GetOLASApplications
///
/// Gets the xml from the Legitimate_OLAS database based on the ol number specified
///
/// The shared web service interface key.
/// ol number.
///
/// A document containing a list of all related applications as per LegitimateApplicationSchema.xsd.
///
[WebMethod(Description = "Gets a list of all OLAS applications related to the criteria specified.")]
public XmlDocument GetOLASApplications(string interfaceKey, string olNumber)
{
if (IsValid(interfaceKey))
{
try
{
bool found = false;
XmlDocument xml = new XmlDocument();
using (SqlConnection connection = new SqlConnection(ConfigurationManager.AppSettings["LegitimateOLASConnection"]))
{
using (SqlCommand command = connection.CreateCommand())
{
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "dbo.Pr_ImportedOLASLicenceSearch";
command.Parameters.Add(new SqlParameter("OperatingLicenceNumber", olNumber));
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)
{
xml.LoadXml(sb.ToString());
}
else
{
return GetXmlError("Licence " + olNumber + " not found in imported OLAS data. Please contact your system administrator.");
}
}
}
return xml;
}
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["LegitimateConnection"]))
{
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["LegitimateInterfaceKey"] == interfaceKey;
}
#endregion
}