using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Web; using System.Xml; namespace Neo.Legitimate.NLTISWebService.Data { internal abstract class OlasEntity : NLTISDataBroker, IEntity { private XmlElement _root; protected XmlElement Root { get { return this._root; } } public string this[string name] { get { return base.GetAttribute(this._root, name); } set { base.SetAttribute(this._root, name, value); } } public int StatusID { get { return this.GetStatus(this._root); } } public OlasEntity(XmlElement root) { this._root = root; } public void Save(string path) { this._root.OwnerDocument.Save(path); } public virtual void Verify() { try { if (this.Exists()) { this.Read(); this.UpdateStatus(1, VerificationStatus.Exists.ToString()); } else { this.UpdateStatus(0, VerificationStatus.New.ToString()); } } catch (Exception ex) { this.UpdateStatus(2, NLTISDataBroker.ToString(ex)); } } protected void UpdateStatus(int statusID, string status) { this.SetStatus(this._root, statusID, status); } public abstract void Create(SqlCommand command); public abstract void Update(SqlCommand command); protected abstract bool Exists(); protected abstract void Read(); protected int SaveAddress(SqlCommand command, int addressID, int provinceID, string line1, string line2, string line3, string postalCode, bool isPostal) { bool exists = false; if (addressID != -1) { command.CommandType = CommandType.Text; command.CommandText = "SELECT COUNT(*) FROM tblAddress WHERE pkiAddressId = @pkiAddressId"; command.Parameters.Clear(); command.Parameters.AddWithValue("@pkiAddressId", addressID); exists = (Convert.ToInt32(command.ExecuteScalar()) > 0); } if (exists) { command.CommandType = CommandType.StoredProcedure; command.CommandText = "spCOMMONAddressU"; command.Parameters.Clear(); command.Parameters.AddWithValue("@pkiAddressId", addressID); command.Parameters.AddWithValue("@fkiProvinceId", provinceID); command.Parameters.AddWithValue("@sAddressLine1", line1); command.Parameters.AddWithValue("@sAddressLine2", line2); command.Parameters.AddWithValue("@sAddressLine3", line3); command.Parameters.AddWithValue("@sPOCode", postalCode); command.Parameters.AddWithValue("@sAuditInfo", base.UserAudit); command.ExecuteNonQuery(); } else { command.CommandType = CommandType.StoredProcedure; command.CommandText = isPostal ? "spCOMMONAddressPostalI" : "spCOMMONAddressPhysicalI"; command.Parameters.Clear(); command.Parameters.AddWithValue("@fkiProvinceId", provinceID); command.Parameters.AddWithValue("@sAddressLine1", line1); command.Parameters.AddWithValue("@sAddressLine2", line2); command.Parameters.AddWithValue("@sAddressLine3", line3); command.Parameters.AddWithValue("@sPOCode", postalCode); command.Parameters.AddWithValue("@sAuditInfo", base.UserAudit); addressID = Convert.ToInt32(command.ExecuteScalar()); } return addressID; } protected bool PostalAddressIsDomicilium(object domiciliumID, object physicalID, object postalID) { return !NLTISDataBroker.IsNull(domiciliumID) && !NLTISDataBroker.IsNull(postalID) && (int)domiciliumID == (int)postalID; } protected void SetStatus(XmlElement element, int statusID, string status) { base.SetAttribute(element, "statusID", statusID.ToString()); base.SetAttribute(element, "status", status); } protected int GetStatus(XmlElement element) { string status = base.GetAttribute(element, "statusID"); int result; try { result = NLTISDataBroker.ParseInt(status, 0); return result; } catch { } result = 0; return result; } } }