using Neo.LegitimateLicences.Data.Models; using System; using System.Web.Http; using System.Xml; using System.Data.SqlClient; using System.Data; using System.Text; using System.IO; namespace Neo.LegitimateLicences.Api { public partial class LegitimateLicencesApiController { #region GetVerifiedLicenceToUseXml [Route("~/1.0/GetData/GetVerifiedLicenceToUseXml")] [HttpGet] public XmlElement GetVerifiedLicenceToUseXml(string olNumber) { #region Is Valid if (!IsValidClaim) { string error = Error_ClaimDetailsNotFound.Replace("#EXPOSE_ERROR#", ""); throw new Exception(error); } #endregion try { var docXML = GetXml(olNumber); XmlElement xElemental = docXML.DocumentElement; return xElemental; } catch (Exception Ex) { var errorXml = GetXmlError(Ex.Message); XmlElement xElemental = errorXml.DocumentElement; return xElemental; } } #endregion #region GetXml private XmlDocument GetXml(string olNumber) { var db = new LegitimateDatabase(); 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; olNumber = !String.IsNullOrEmpty(olNumber) ? olNumber : ""; using (SqlConnection connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) { using (SqlCommand command = connection.CreateCommand()) { command.CommandType = CommandType.StoredProcedure; command.CommandText = "Verified.Pr_VerifiedLicenceToUseGet"; 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) { 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 GetImportedLicenceToUseXml [Route("~/1.0/GetData/GetImportedLicenceToUseXml")] [HttpGet] public XmlElement GetImportedLicenceToUseXml(string olpermitNumber, short sourceApplicationID) { #region Is Valid if (!IsValidClaim) { string error = Error_ClaimDetailsNotFound.Replace("#EXPOSE_ERROR#", ""); throw new Exception(error); } #endregion try { var docXML = GetImportedXml(olpermitNumber, sourceApplicationID); XmlElement xElemental = docXML.DocumentElement; return xElemental; } catch (Exception Ex) { var errorXml = GetXmlError(Ex.Message); XmlElement xElemental = errorXml.DocumentElement; return xElemental; } } #endregion #region GetImportedXml private XmlDocument GetImportedXml(string olNumberorPermitNumber, short sourceApplicationID) { var db = new LegitimateDatabase(); 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; olNumberorPermitNumber = !String.IsNullOrEmpty(olNumberorPermitNumber) ? olNumberorPermitNumber : ""; using (SqlConnection connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) { using (SqlCommand command = connection.CreateCommand()) { command.CommandType = CommandType.StoredProcedure; command.CommandText = "Imported.Pr_ImportedLicenceToUseXmlGet"; command.Parameters.Add(new SqlParameter("OlNumberorPermitNumber", olNumberorPermitNumber)); command.Parameters.Add(new SqlParameter("SourceApplicationID", sourceApplicationID)); 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 LTPSApplicationSearch [Route("~/1.0/GetData/LTPSApplicationXmlSearch")] [HttpGet] public XmlElement LTPSApplicationXmlSearch(string permitNumber) { #region Is Valid if (!IsValidClaim) { string error = Error_ClaimDetailsNotFound.Replace("#EXPOSE_ERROR#", ""); throw new Exception(error); } #endregion try { var db = new LegitimateDatabase(); 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; permitNumber = !String.IsNullOrEmpty(permitNumber) ? permitNumber : ""; using (SqlConnection connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) { using (SqlCommand command = connection.CreateCommand()) { command.CommandType = CommandType.StoredProcedure; command.CommandText = "Imported.Pr_ImportedLTPSLicenceXmlGet"; command.Parameters.Add(new SqlParameter("PermitNumber", permitNumber)); 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); XmlElement xElemental = xmlDoc.DocumentElement; return xElemental; } catch (Exception Ex) { var errorXml = GetXmlError(Ex.Message); XmlElement xElemental = errorXml.DocumentElement; return xElemental; } } #endregion #region GetApplications [Route("~/1.0/GetData/GetApplications")] [HttpGet] public XmlElement GetApplications(string operatingLicenceNumber, string idNumber, string registrationNumber, string chassisNumber, string divisionTypeIDs) { #region Is Valid if (!IsValidClaim) { string error = Error_ClaimDetailsNotFound.Replace("#EXPOSE_ERROR#", ""); throw new Exception(error); } #endregion try { var db = new LegitimateDatabase(); 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; operatingLicenceNumber = !String.IsNullOrEmpty(operatingLicenceNumber) ? operatingLicenceNumber : ""; idNumber = !String.IsNullOrEmpty(idNumber) ? idNumber : ""; registrationNumber = !String.IsNullOrEmpty(registrationNumber) ? registrationNumber : ""; chassisNumber = !String.IsNullOrEmpty(chassisNumber) ? chassisNumber : ""; divisionTypeIDs = !String.IsNullOrEmpty(divisionTypeIDs) ? divisionTypeIDs : "1"; using (SqlConnection connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) { using (SqlCommand command = connection.CreateCommand()) { command.CommandType = CommandType.StoredProcedure; command.CommandText = "App.Pr_ApplicationPrecheckInterfaceSearch"; command.Parameters.Add(new SqlParameter("IDNumber", idNumber)); command.Parameters.Add(new SqlParameter("OperatingLicenceNumber", operatingLicenceNumber)); command.Parameters.Add(new SqlParameter("RegistrationNumber", registrationNumber)); command.Parameters.Add(new SqlParameter("VehicleChassisNumber", chassisNumber)); command.Parameters.Add(new SqlParameter("DivisionTypeIDs", divisionTypeIDs)); 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); XmlElement xElemental = xmlDoc.DocumentElement; return xElemental; } catch (Exception Ex) { var errorXml = GetXmlError(Ex.Message); XmlElement xElemental = errorXml.DocumentElement; return xElemental; } } #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 } }