using System; using System.Collections.Generic; using System.Linq; using System.Text; using Ease.CoreV35; using Ease.CoreV35.Model; using Ease.CoreV35.DataAccess; using Payroll.BO; using Ease.CoreV35.Caching; namespace Payroll.Service { #region Asset Service [Serializable] public class AssetService : ServiceTemplate, IAssetService { #region Private functions and declaration Cache _cache = new Cache(typeof(Asset)); #endregion public AssetService() { } #region MapObject For Asset private void MapObject(Asset oAsset, DataReader oReader) { base.SetObjectID(oAsset, oReader.GetID("AssetID")); oAsset.Code = oReader.GetString("Code"); oAsset.Name = oReader.GetString("Name"); oAsset.Description = oReader.GetString("Description"); oAsset.Section = oReader.GetString("Section",string.Empty); oAsset.AssignUserId1 = oReader.GetID("AssignUserId1"); oAsset.AssignUserId2 = oReader.GetID("AssignUserId2"); //oAsset.BuyTime = oReader.GetDateTime("BuyTime").Value; //oAsset.Remarks = oReader.GetString("Remarks"); oAsset.IsLinemanager = oReader.GetBoolean("IsLinemanager").GetValueOrDefault(); oAsset.BuyPrice = oReader.GetDouble("BuyPrice").Value; oAsset.DefaultItem =(bool) oReader.GetBoolean("DefaultItem"); oAsset.AssetType =(EnumAssetInventoryType) oReader.GetInt32("AssetType"); //oAsset.CreatedBy = oReader.GetID("CreatedBy"); //oAsset.CreatedDate = oReader.GetDateTime("CreationDate").Value; //oAsset.ModifiedBy = oReader.GetID("ModifiedBy"); //oAsset.ModifiedDate = oReader.GetDateTime("ModifiedDate"); this.SetObjectState(oAsset, Ease.CoreV35.ObjectState.Saved); } protected override T CreateObject(DataReader oReader) { Asset oAsset = new Asset(); MapObject(oAsset, oReader); return oAsset as T; } protected Asset CreateObject(DataReader oReader) { Asset oAsset = new Asset(); MapObject(oAsset, oReader); return oAsset; } #endregion #region Service Implemantation public Asset Get(ID id) { Asset oAsset = new Asset(); #region Cache Header oAsset = _cache["Get", id] as Asset; if (oAsset != null) return oAsset; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(AssetDA.Get(tc, id)); if (oreader.Read()) { oAsset = 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(oAsset, "Get", id); #endregion return oAsset; } public Asset Get(string name) { Asset oAsset = new Asset(); #region Cache Header oAsset = _cache["Get", name] as Asset; if (oAsset != null) return oAsset; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(AssetDA.Get(tc, name)); if (oreader.Read()) { oAsset = 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(oAsset, "Get", name); #endregion return oAsset; } public ObjectsTemplate Get(Employee oEmployee, EnumAssetInventoryType assetType, bool defaultItem) { ObjectsTemplate Assets = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(AssetDA.Get(tc, oEmployee, assetType, defaultItem)); Assets = 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 Assets; } public ObjectsTemplate Get(Employee oEmployee, EnumAssetInventoryType assetType) { ObjectsTemplate Assets = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(AssetDA.Get(tc, oEmployee, assetType)); Assets = 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 Assets; } public ObjectsTemplate Get(EnumAssetInventoryType assetType) { ObjectsTemplate Assets = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(AssetDA.Get(tc, assetType)); Assets = 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 Assets; } public ObjectsTemplate GetLineManagerItem() { ObjectsTemplate Assets = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(AssetDA.GetLineManagerItem(tc)); Assets = 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 Assets; } public ObjectsTemplate Get() { #region Cache Header ObjectsTemplate Assets = _cache["Get"] as ObjectsTemplate; if (Assets != null) return Assets; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(AssetDA.Get(tc)); Assets = 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(Assets, "Get"); #endregion return Assets; } public ID Save(Asset oAsset) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); if (oAsset.IsNew) { int id = tc.GenerateID("Asset", "AssetID"); oAsset.Code = "00" + id; base.SetObjectID(oAsset, ID.FromInteger(id)); AssetDA.Insert(tc, oAsset); } else { AssetDA.Update(tc, oAsset); } return oAsset.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(ID id) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); AssetDA.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 } #endregion }