using System; using System.Collections.Generic; using HRM.BO; using Ease.Core.DataAccess; using Ease.Core.Utility; using System.Data; using Ease.Core.Model; namespace HRM.DA { public class FMFundUserPermissionService : ServiceTemplate, IFundUserPermissionService { public FMFundUserPermissionService() { } private void MapObject(FMFundUserPermission item, DataReader dr) { // Map ID using ServiceTemplate helper base.SetObjectID(item, dr.GetInt32("ID").Value); item.UserID = dr.GetInt32("UserID", 0); item.FundTypeID = dr.GetInt32("FundTypeID", 0); // map audit fields if present item.CreatedBy = dr.GetInt32("CreatedBy", 0); item.CreatedDate = dr.GetDateTime("CreatedDate").Value; item.ModifiedBy = dr.GetInt32("ModifiedBy", 0); item.ModifiedDate = dr.GetDateTime("ModifiedDate"); this.SetObjectState(item, Ease.Core.ObjectState.Saved); } protected override T CreateObject(DataReader dr) { FMFundUserPermission item = new FMFundUserPermission(); MapObject(item, dr); return item as T; } public List GetByUserID(int userID) { List list = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(FMFundUserPermissionDA.GetByUserID(tc, userID)); list = this.CreateObjects(dr); dr.Close(); tc.End(); } catch (Exception e) { if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); } return list; } public void SavePermissions(List permissions, int performedBy) { if (permissions == null) throw new ArgumentNullException(nameof(permissions)); // If empty, delete existing entries for the user and return if (permissions.Count == 0) { return; } TransactionContext tc = null; try { tc = TransactionContext.Begin(true); int userId = permissions[0].UserID; // Remove existing permissions for the user FMFundUserPermissionDA.DeleteByUserID(tc, userId); // Insert each permission foreach (var p in permissions) { // set audit fields p.CreatedBy = performedBy; p.CreatedDate = DateTime.Now; FMFundUserPermissionDA.Insert(tc, p); } tc.End(); } catch (Exception e) { if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); } } public void DeleteByUserID(int userID) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); FMFundUserPermissionDA.DeleteByUserID(tc, userID); tc.End(); } catch (Exception e) { if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); } } } }