using System; using System.Data; using System.Linq; using Ease.CoreV35; using Ease.CoreV35.Model; using Ease.CoreV35.DataAccess; using System.Collections.Generic; using Payroll.BO; using Ease.CoreV35.Caching; using System.IO; using System.Data.SqlClient; namespace Payroll.Service { [Serializable] public class AuthorizedPersonService : ServiceTemplate, IAuthorizedPersonService { #region Private functions and declaration Cache _cache = new Cache(typeof(AuthorizedPerson)); #endregion public AuthorizedPersonService() { } private void MapObject(AuthorizedPerson oAuthorizedPerson, DataReader oReader) { base.SetObjectID(oAuthorizedPerson, oReader.GetID("PERSONID")); oAuthorizedPerson.Name = oReader.GetString("Name"); oAuthorizedPerson.Sequence = oReader.GetInt32("Position").Value; oAuthorizedPerson.Designation = oReader.GetString("DESIGNATION"); oAuthorizedPerson.CreatedBy = oReader.GetID("CreatedBy"); oAuthorizedPerson.CreatedDate = oReader.GetDateTime("CreatedDate").Value; oAuthorizedPerson.ModifiedBy = oReader.GetID("ModifiedBy"); oAuthorizedPerson.ModifiedDate = oReader.GetDateTime("ModifiedDate"); this.SetObjectState(oAuthorizedPerson, Ease.CoreV35.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(ID id) { AuthorizedPerson oAuthorizedPerson = new AuthorizedPerson(); #region Cache Header oAuthorizedPerson = _cache["Get", id] as AuthorizedPerson; if (oAuthorizedPerson != null) return oAuthorizedPerson; #endregion 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 } #region Cache Footer _cache.Add(oAuthorizedPerson, "Get", id); #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 ObjectsTemplate Get() { #region Cache Header ObjectsTemplate oAuthorizedPersons = _cache["Get"] as ObjectsTemplate; if (oAuthorizedPersons != null) return oAuthorizedPersons; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(AuthorizedPersonDA.Get(tc)); 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 } #region Cache Footer _cache.Add(oAuthorizedPersons, "Get"); #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 ObjectsTemplate GetByReportID(ID reportID) { #region Cache Header ObjectsTemplate oAuthorizedPersons = _cache["GetByReportID", reportID] as ObjectsTemplate; if (oAuthorizedPersons != null) return oAuthorizedPersons; #endregion 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 } #region Cache Footer _cache.Add(oAuthorizedPersons, "GetByReportID", reportID); #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 ID Save(AuthorizedPerson oAuthorizedPerson, FileStream stream) { try { TransactionContext tc = null; try { #region Saving data tc = TransactionContext.Begin(true); if (oAuthorizedPerson.IsNew) { #region Save int id = tc.GenerateID("AuthorizedPerson", "PersonID"); base.SetObjectID(oAuthorizedPerson, ID.FromInteger(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.Integer, 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+";Password=cel1@3$56;"; SqlConnection connection = new SqlConnection(ConnString); tc.End(); dSet = AuthorizedPersonDA.GetImage(connection, PersonID); if (dSet != null && dSet.Tables[0].Rows.Count > 0) { dTable = dSet.Tables[0]; } } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return dTable; } public ID Save(AuthorizedPerson oAuthorizedPerson) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); if (oAuthorizedPerson.IsNew) { int id = tc.GenerateID("AuthorizedPerson", "PersonID"); base.SetObjectID(oAuthorizedPerson, ID.FromInteger(id)); AuthorizedPersonDA.Insert(tc, oAuthorizedPerson); } else { AuthorizedPersonDA.Update(tc, oAuthorizedPerson); } tc.End(); 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(ID 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 } }