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; namespace Payroll.Service { #region Location Service [Serializable] public class LocationService : ServiceTemplate, ILocationService { #region Private functions and declaration Cache _cache = new Cache(typeof(Location)); #endregion public LocationService() { } private void MapObject(Location oLocation, DataReader oReader) { base.SetObjectID(oLocation, oReader.GetID("LocationID")); oLocation.Code = oReader.GetString("code"); oLocation.Name = oReader.GetString("description"); oLocation.ParentID = oReader.GetID("parentID"); // oLocation.ParentsID = oReader.GetString("parentsID"); oLocation.Sequence = oReader.GetInt32("SequenceNo").Value; oLocation.Status = (EnumStatus)oReader.GetInt32("Status").Value; oLocation.Tier = oReader.GetInt32("TIRE").Value; oLocation.CreatedBy = oReader.GetID("CreatedBy"); oLocation.CreatedDate = oReader.GetDateTime("CreationDate").Value; oLocation.ModifiedBy = oReader.GetID("ModifiedBy"); oLocation.ModifiedDate = oReader.GetDateTime("ModifiedDate"); this.SetObjectState(oLocation, Ease.CoreV35.ObjectState.Saved); } protected override T CreateObject(DataReader oReader) { Location oLocation = new Location(); MapObject(oLocation, oReader); return oLocation as T; } protected Location CreateObject(DataReader oReader) { Location oLocation = new Location(); MapObject(oLocation, oReader); return oLocation; } #region Service implementation public Location Get(ID id) { Location oLocation = new Location(); #region Cache Header oLocation = _cache["Get", id] as Location; if (oLocation != null) return oLocation; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(LocationDA.Get(tc, id)); if (oreader.Read()) { oLocation = 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(oLocation, "Get", id); #endregion return oLocation; } public string GetNextCode(int tier) { TransactionContext tc = null; string _code = ""; try { tc = TransactionContext.Begin(); _code = GlobalFunctionService.GetMaxCode(tc, "location", "codeautogenerate", "location", "Code", tier); 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 string GetNextCode(int _tier, string _parentCode) { TransactionContext tc = null; string _code = ""; try { tc = TransactionContext.Begin(); _code = GlobalFunctionService.GetMaxCodeofTree(tc, "location", "codeautogenerate", "location", "Code", _parentCode, _tier); 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 Location Get(string code) { Location oLocation = new Location(); #region Cache Header oLocation = _cache["Get", code] as Location; if (oLocation != null) return oLocation; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(LocationDA.Get(tc, code)); if (oreader.Read()) { oLocation = 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(oLocation, "Get", code); #endregion return oLocation; } public ObjectsTemplate Get(EnumStatus sts) { ObjectsTemplate allLocations = new ObjectsTemplate(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(LocationDA.Get(tc, sts)); if (oreader.Read()) { allLocations = this.CreateObjects(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 allLocations; } public ObjectsTemplate GetForJV(DateTime dSalaryMonth, ID PayrollTypeID) { ObjectsTemplate allLocations = new ObjectsTemplate(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(LocationDA.GetForJV(tc, dSalaryMonth, PayrollTypeID)); if (oreader.Read()) { allLocations = this.CreateObjects(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 allLocations; } public ObjectsTemplate Get() { #region Cache Header ObjectsTemplate locations = _cache["Get"] as ObjectsTemplate; if (locations != null) return locations; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(LocationDA.Get(tc)); locations = 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(locations, "Get"); #endregion return locations; } public ObjectsTemplate GetChield(ID parentID) { #region Cache Header ObjectsTemplate locations = _cache["GetChield", parentID] as ObjectsTemplate; if (locations != null) return locations; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(LocationDA.GetChilds(tc, parentID)); locations = 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(locations, "GetChield", parentID); #endregion return locations; } public ObjectsTemplate GetParents(EnumStatus status) { #region Cache Header ObjectsTemplate locations = _cache["GetParents", status] as ObjectsTemplate; if (locations != null) return locations; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(LocationDA.GetParents(tc, status)); locations = 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(locations, "GetParents", status); #endregion return locations; } public ObjectsTemplate GetByTier(int tier) { #region Cache Header ObjectsTemplate locations = _cache["GetByTier", tier] as ObjectsTemplate; if (locations != null) return locations; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(LocationDA.GetByTier(tc, tier)); locations = 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(locations, "GetByTier", tier); #endregion return locations; } public ID Save(Location oLocation) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); if (oLocation.IsNew) { int id = tc.GenerateID("Location", "LocationID"); int sequenceId = tc.GenerateID("Location", "SequenceNO"); oLocation.Sequence = sequenceId; base.SetObjectID(oLocation, ID.FromInteger(id)); LocationDA.Insert(tc, oLocation); } else { LocationDA.Update(tc, oLocation); } tc.End(); return oLocation.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); LocationDA.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 public static void SaveForUpload(TransactionContext tc, ObjectsTemplate locations) { try { string parentcode = ""; foreach (Location oLocation in locations) { if (oLocation.IsNew) { int seqNo = tc.GenerateID("Location", "SequenceNo"); oLocation.Sequence = seqNo; parentcode = string.Empty; bool isAutoGenerated = ConfigurationManager.GetBoolValue("location", "codeautogenerate", EnumConfigurationType.Logic); if (oLocation.ParentID != null) parentcode = locations.GetItem(oLocation.ParentID).Code; if (isAutoGenerated == true) oLocation.Code = GlobalFunctionService.GetMaxCodeofTree(tc, "location", "codeautogenerate", "Location", "Code", parentcode, oLocation.Tier); oLocation.CreatedBy = User.CurrentUser.ID; oLocation.CreatedDate = DateTime.Now; LocationDA.Insert(tc, oLocation); } else { oLocation.ModifiedBy = User.CurrentUser.ID; oLocation.ModifiedDate = DateTime.Now; LocationDA.Update(tc, oLocation); } } } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } } #endregion }