using System; using System.Data; using Ease.Core.Model; using Ease.Core.DataAccess; using Ease.Core; using System.Collections.Generic; using Ease.Core.Utility; using System.IO; using System.Data.SqlClient; using HRM.BO; using Microsoft.Data.SqlClient; namespace HRM.DA { public class AuthorizedPersonService : ServiceTemplate, IAuthorizedPersonService { public AuthorizedPersonService() { } private void MapObject(AuthorizedPerson oAuthorizedPerson, DataReader oReader) { base.SetObjectID(oAuthorizedPerson, (oReader.GetInt32("PERSONID").Value)); oAuthorizedPerson.Name = oReader.GetString("Name"); oAuthorizedPerson.Sequence = oReader.GetInt32("Position").Value; oAuthorizedPerson.Designation = oReader.GetString("DESIGNATION"); oAuthorizedPerson.Email = oReader.GetString("Email", null); oAuthorizedPerson.Extension = oReader.GetString("Extension", null); oAuthorizedPerson.Phone = oReader.GetString("Phone", null); oAuthorizedPerson.FileName = oReader.GetString("FileName", null); oAuthorizedPerson.EmployeeId = oReader.GetInt32("EmployeeId", 0); oAuthorizedPerson.Signature = oReader.GetLob("SignatureData"); oAuthorizedPerson.CreatedBy = oReader.GetInt32("CreatedBy", 0); oAuthorizedPerson.CreatedDate = oReader.GetDateTime("CreatedDate").HasValue ? oReader.GetDateTime("CreatedDate").Value : DateTime.MinValue; oAuthorizedPerson.ModifiedBy = oReader.GetInt32("ModifiedBy", 0); oAuthorizedPerson.ModifiedDate = oReader.GetDateTime("ModifiedDate").HasValue ? oReader.GetDateTime("ModifiedDate").Value : (DateTime?)null; this.SetObjectState(oAuthorizedPerson, Ease.Core.ObjectState.Saved); } protected override T CreateObject(DataReader oReader) { AuthorizedPerson oAuthorizedPerson = new AuthorizedPerson(); MapObject(oAuthorizedPerson, oReader); return oAuthorizedPerson as T; } protected AuthorizedPerson CreateObject(DataReader oReader) { AuthorizedPerson oAuthorizedPerson = new AuthorizedPerson(); MapObject(oAuthorizedPerson, oReader); return oAuthorizedPerson; } #region Service implementation public AuthorizedPerson Get(int id) { AuthorizedPerson oAuthorizedPerson = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(AuthorizedPersonDA.Get(tc, id)); if (oreader.Read()) { oAuthorizedPerson = this.CreateObject(oreader); } oreader.Close(); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return oAuthorizedPerson; } public int GetMaxPosition(TransactionContext tc) { try { object position = AuthorizedPersonDA.GetMaxPosition(tc); if (position == null || position == DBNull.Value) return 1; else return (Convert.ToInt32(position) + 1); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException("Failed to Get Max Position", e); #endregion } } public List Get() { List oAuthorizedPersons = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oReader = new DataReader(AuthorizedPersonDA.Get(tc)); while (oReader.Read()) { AuthorizedPerson oAuthorizedPerson = new AuthorizedPerson(); oAuthorizedPerson.ID = oReader.GetInt32("PersonID").Value; oAuthorizedPerson.Name = oReader.GetString("Name"); oAuthorizedPerson.Sequence = oReader.GetInt32("Position").Value; oAuthorizedPerson.Designation = oReader.GetString("DESIGNATION"); oAuthorizedPerson.Email = oReader.GetString("Email", null); oAuthorizedPerson.Extension = oReader.GetString("Extension", null); oAuthorizedPerson.Phone = oReader.GetString("Phone", null); oAuthorizedPerson.FileName = oReader.GetString("FileName", null); oAuthorizedPerson.EmployeeId = oReader.GetInt32("EmployeeId", 0); oAuthorizedPerson.Signature = oReader.GetLob("SignatureData"); oAuthorizedPerson.CreatedBy = oReader.GetInt32("CreatedBy", 0); oAuthorizedPerson.CreatedDate = oReader.GetDateTime("CreatedDate").HasValue ? oReader.GetDateTime("CreatedDate").Value : DateTime.MinValue; oAuthorizedPerson.ModifiedBy = oReader.GetInt32("ModifiedBy", 0); oAuthorizedPerson.ModifiedDate = oReader.GetDateTime("ModifiedDate").HasValue ? oReader.GetDateTime("ModifiedDate").Value : (DateTime?)null; oAuthorizedPersons.Add(oAuthorizedPerson); } oReader.Close(); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return oAuthorizedPersons; } public bool IsExist(string Name) { TransactionContext tc = null; bool IsExist = false; try { tc = TransactionContext.Begin(true); IsExist = AuthorizedPersonDA.IsExist(tc, Name); return IsExist; } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } public List GetByReportID(int reportID) { List oAuthorizedPersons = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(AuthorizedPersonDA.GetByReportID(tc, reportID)); oAuthorizedPersons = this.CreateObjects(dr); dr.Close(); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return oAuthorizedPersons; } public void SaveFile(TransactionContext tc, int nPersonID, object Object, FileStream stream) { //string ConnString = tc.Connection.ConnectionString; //SqlConnection connection = new SqlConnection(ConnString); //try //{ // tc.End(); // BinaryReader reader = new BinaryReader(stream); // byte[] signature = reader.ReadBytes((int)stream.Length); // reader.Close(); // stream.Close(); // AuthorizedPersonDA.UpdateData(connection, nPersonID, signature); // connection.Close(); //} //catch (Exception e) //{ // #region Handle Exception // if (tc != null) // tc.HandleError(); // connection.Close(); // throw new ServiceException(e.Message, e); // #endregion //} } public int Save(AuthorizedPerson oAuthorizedPerson, FileStream stream) { try { TransactionContext tc = null; try { #region Saving data tc = TransactionContext.Begin(true); if (oAuthorizedPerson.IsNew) { #region Insert int id = tc.GenerateID("AuthorizedPerson", "PersonID"); base.SetObjectID(oAuthorizedPerson, (id)); oAuthorizedPerson.Sequence = this.GetMaxPosition(tc); AuthorizedPersonDA.Insert(tc, oAuthorizedPerson); if (stream != null) { SaveFile(tc, id, oAuthorizedPerson.Signature, stream); stream.Close(); } #endregion } else { #region Edit AuthorizedPersonDA.Update(tc, oAuthorizedPerson); if (stream != null) { SaveFile(tc, oAuthorizedPerson.ID, oAuthorizedPerson.Signature, stream); stream.Close(); } #endregion } tc.End(); #endregion } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); throw new ServiceException(e.Message, e); #endregion } } catch (Exception e) { throw new ServiceException(e.Message, e); } return oAuthorizedPerson.ID; } public DataTable GetImage(int PersonID) { TransactionContext tc = null; DataSet dSet = null; DataTable dTable = null; try { tc = TransactionContext.Begin(true); string ConnString = tc.Connection.ConnectionString; SqlConnection connection = new SqlConnection(ConnString); dSet = AuthorizedPersonDA.GetImage(tc, PersonID); if (dSet != null && dSet.Tables[0].Rows.Count > 0) { dTable = dSet.Tables[0]; } tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return dTable; } public int Save(AuthorizedPerson oAuthorizedPerson) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); if (oAuthorizedPerson.IsNew) { int id = tc.GenerateID("AuthorisedPerson", "PersonID"); base.SetObjectID(oAuthorizedPerson, (id)); AuthorizedPersonDA.Insert(tc, oAuthorizedPerson); } else { AuthorizedPersonDA.Update(tc, oAuthorizedPerson); } tc.End(); if (oAuthorizedPerson.Signature != null) { AuthorizedPersonDA.UpdateData(oAuthorizedPerson.ConnectionString, oAuthorizedPerson); } return oAuthorizedPerson.ID; } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } public void Delete(int id) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); AuthorizedPersonDA.Delete(tc, id); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } #endregion } }