using System; using Ease.Core.Model; using Ease.Core.DataAccess; using Ease.Core.Utility; using System.Collections.Generic; using HRM.BO; using System.Data; using Ease.Core; using System.Linq; namespace HRM.DA { public class VendorService : ServiceTemplate, IVendorService { #region Map Functions private void MapObject(Vendor oVendor, DataReader oReader) { base.SetObjectID(oVendor, (oReader.GetInt32("VendorID").Value)); oVendor.Name = oReader.GetString("Name"); oVendor.Code = oReader.GetString("Code"); oVendor.Sequence = oReader.GetInt32("SequenceNo").Value; oVendor.Status = (EnumStatus)oReader.GetUInt32("Status").GetValueOrDefault(); oVendor.CreatedBy = oReader.GetInt32("CreatedBy", 0); oVendor.CreatedDate = oReader.GetDateTime("CreatedDate").GetValueOrDefault(DateTime.MinValue); oVendor.ModifiedBy = oReader.GetInt32("ModifiedBy", 0); oVendor.ModifiedDate = oReader.GetDateTime("ModifiedDate").GetValueOrDefault(DateTime.MinValue); oVendor.Address = oReader.GetString("Address",null); oVendor.EmailAddress = oReader.GetString("EmailAddress", null); oVendor.Mobile = oReader.GetString("Mobile", null); oVendor.Address = oReader.GetString("Address", null); oVendor.Telephone = oReader.GetString("Telephone", null); oVendor.ContactPerson = oReader.GetString("ContactPerson", null); oVendor.VendorType = (EnumVendorType)oReader.GetUInt32("VendorType").GetValueOrDefault(); oVendor.VendorTypeString = ((EnumVendorType)oReader.GetUInt32("VendorType")).ToString(); this.SetObjectState(oVendor, Ease.Core.ObjectState.Saved); } protected override T CreateObject(DataReader dr) { Vendor oVendor = new Vendor(); MapObject(oVendor, dr); return oVendor as T; } #endregion #region Implementation of IVendorService public Vendor Get(int id) { Vendor oVendor = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(VendorDA.Get(tc, id)); if (oreader.Read()) { oVendor = CreateObject(oreader); } oreader.Close(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } finally { tc.End(); } return oVendor; } public List Get() { List vendors = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(VendorDA.Get(tc)); vendors = CreateObjects(oreader); oreader.Close(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } finally { tc.End(); } return vendors; } public List Get(EnumVendorType vendorType) { List vendors = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(VendorDA.Get(tc, vendorType)); vendors = CreateObjects(oreader); oreader.Close(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } finally { tc.End(); } return vendors; } public List Get(EnumStatus status,int payrollTypeID) { List vendors = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(VendorDA.Get(tc, status, payrollTypeID)); vendors = CreateObjects(oreader); oreader.Close(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } finally { tc.End(); } return vendors; } public List Get(string subStr) { List vendors = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(VendorDA.Get(tc, subStr)); vendors = CreateObjects(oreader); oreader.Close(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } finally { tc.End(); } return vendors; } public string GetNextCode() { TransactionContext tc = null; string _code = ""; try { tc = TransactionContext.Begin(); _code = GlobalFunctionService.GetMaxCode(tc, "vendor", "codeautogenerate", "Vendor", "Code"); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return _code; } public int Save(Vendor oVendor) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); if (oVendor.IsNew) { int id = tc.GenerateID("Vendor", "VendorID"); base.SetObjectID(oVendor, id); int sequenceId = tc.GenerateID("Vendor", "SequenceNO"); oVendor.Sequence = sequenceId; base.SetObjectID(oVendor, (id)); VendorDA.Insert(tc, oVendor); } else { VendorDA.Update(tc, oVendor); } return oVendor.ID; } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } finally { tc.End(); } } public void Delete(int id) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); VendorDA.Delete(tc, id); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } finally { tc.End(); } } #endregion } }