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 HRM.BO; using System.IO; using System.Diagnostics; using Microsoft.AspNetCore.Routing.Matching; namespace HRM.DA { public class CandidateService : ServiceTemplate, ICandidateService { #region Candidate Object Mapping private void MapCandidateObject(Candidate ob, DataReader oReader) { this.SetObjectID(ob, oReader.GetInt32("CandidateID").Value); ob.ProcessId = oReader.GetString("ProcessId") != null ? oReader.GetInt32("ProcessId").Value : 0; ob.IsEmployee = oReader.GetString("IsEmployee") != null ? oReader.GetBoolean("IsEmployee").Value : false; ob.IsSelected = oReader.GetString("IsSelected") != null ? oReader.GetBoolean("IsSelected").Value : false; ob.StartDate = oReader.GetString("StartDate") != null ? oReader.GetDateTime("StartDate").Value : DateTime.MinValue; ob.StartTime = oReader.GetString("StartTime") != null ? oReader.GetString("StartTime") : string.Empty; ob.CvID = oReader.GetString("CvId") != null ? oReader.GetInt32("CvId").Value : 0; ob.EmployeeId = oReader.GetString("EmployeeId") != null ? oReader.GetInt32("EmployeeId").Value : 0; ob.PrimarySelected = oReader.GetString("PrimarySelected") != null ? oReader.GetBoolean("PrimarySelected").Value : false; ob.DeptSelected = oReader.GetString("DeptSelected") != null ? oReader.GetBoolean("DeptSelected").Value : false; ob.CandidateLastStatus = oReader.GetString("CandidateLastStatus",true,null); this.SetObjectState(ob, ObjectState.Saved); } protected override T CreateObject(DataReader oReader) { Candidate ob = new Candidate(); MapCandidateObject(ob, oReader); return ob as T; } #endregion #region CVAttachment protected List CreateirFileAttachmentObjects(DataReader oReader) { List oIRFileAttachments = new List(); while (oReader.Read()) { FileAttachment oIRFileAtachment = new FileAttachment(); MapIRFileAttachmentObject(oIRFileAtachment, oReader); oIRFileAttachments.Add(oIRFileAtachment); } return oIRFileAttachments; } private void MapIRFileAttachmentObject(FileAttachment oIRFileAttachment, DataReader oReader) { base.SetObjectID(oIRFileAttachment, (oReader.GetInt32("FILEATTACHMENTID").Value)); oIRFileAttachment.ReferenceID = oReader.GetInt32("ReferenceID", 0); oIRFileAttachment.FileAsByteArray = oReader.GetLob("FileData"); oIRFileAttachment.OriginalFileName = oReader.GetString("OriginalFileName"); oIRFileAttachment.FileType = (EnumFileType)oReader.GetInt32("FileType").GetValueOrDefault(); this.SetObjectState(oIRFileAttachment, Ease.Core.ObjectState.Saved); } #endregion #region Service Implementation #region Delete(CV obCv) public void Delete(Candidate ob) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); CVDA.DeleteAttachment(tc, ob.CvID, EnumFileType.CV); CVDA.Delete(ob.ID, tc); tc.End(); } catch (Exception ex) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(ex); throw new ServiceException(ex.Message, ex); #endregion } } public void DeleteCandidateCVCollection(int candidateID) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); CandidateDA.DeleteCandidateCVCollection(tc, candidateID); tc.End(); } catch (Exception ex) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(ex); throw new ServiceException(ex.Message, ex); #endregion } } #endregion #region Insert(CV obCv) public void Save(Candidate ob, string connectionString) { TransactionContext tc = null; int oID = 0; try { tc = TransactionContext.Begin(true); if (ob.IsNew) { int id = tc.GenerateID("Candidate", "CandidateID"); oID = (id); base.SetObjectID(ob, (id)); CandidateDA.Insert(ob, tc); } else { oID = ob.ID; CandidateDA.Update(ob, tc); } CVDA.DeleteAttachment(tc, ob.CvID, EnumFileType.CV); tc.End(); if (ob.IRFileAttacments != null && ob.IRFileAttacments.Count > 0) { foreach (var uploadfile in ob.IRFileAttacments) { if (uploadfile.ID > 0 && uploadfile.FileTobase64 != null) { byte[] newBytes = Convert.FromBase64String(uploadfile.FileTobase64); uploadfile.FileAsByteArray = newBytes; uploadfile.ReferenceID = ob.ID; uploadfile.FileType = EnumFileType.CV; FileAttachmentDA.Insert(uploadfile, connectionString); } else { FileStream stream = new FileStream(uploadfile.FilePath, FileMode.Open, FileAccess.Read); BinaryReader reader = new BinaryReader(stream); byte[] buff = reader.ReadBytes((int)stream.Length); reader.Close(); stream.Close(); uploadfile.FileAsByteArray = buff; uploadfile.ReferenceID = ob.ID; uploadfile.FileType = EnumFileType.CV; FileAttachmentDA.Insert(uploadfile, connectionString); } } } } catch (Exception ex) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(ex); throw new ServiceException(ex.Message, ex); #endregion } } #endregion #region Get() public List GetCanditates(int processID) { List allCVs = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(true); DataReader oreader = new DataReader(CandidateDA.GetCanditates(tc, processID)); allCVs = this.CreateObjects(oreader); oreader.Close(); //foreach (Candidate cv in allCVs) //{ // if (cv != null) // { // cv.IRFileAttacments = GetAllAttachments(tc, cv.CvId, EnumFileType.CV); // } //} tc.End(); } catch (Exception ex) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(ex); throw new ServiceException(ex.Message, ex); #endregion } return allCVs; } #endregion #region Get(int cVID) public Candidate Get(int cVID) { Candidate cv = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(true); DataReader oreader = new DataReader(CandidateDA.Get(tc)); if (oreader.Read()) { cv = this.CreateObject(oreader); } oreader.Close(); if (cv != null) { cv.IRFileAttacments = GetAllAttachments(tc, cv.ID, EnumFileType.CV); } tc.End(); } catch (Exception ex) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(ex); throw new ServiceException(ex.Message, ex); #endregion } return cv; } private List GetAllAttachments(TransactionContext tc, int cvID, EnumFileType type) { List allExps = null; try { DataReader oreader = new DataReader(FileAttachmentDA.GetByReferenceId(tc, cvID, type)); allExps = this.CreateirFileAttachmentObjects(oreader); oreader.Close(); } catch (Exception ex) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(ex); throw new ServiceException(ex.Message, ex); #endregion } return allExps; } public void Save(List items, string connectionString) { TransactionContext tc = null; int oID = 0; try { tc = TransactionContext.Begin(true); foreach (Candidate ob in items) { CV oCV = new CV(); int cvID = tc.GenerateID("CV", "CVID"); base.SetObjectID(oCV, (cvID)); oCV.FirstName = "Candidate"; oCV.LastName = cvID.ToString(); oCV.Name = oCV.FirstName + " " + oCV.LastName; ob.CvID = cvID; CVDA.InsertCVBase(oCV, tc); } foreach (Candidate ob in items) { if (ob.IsNew) { int id = tc.GenerateID("RECRUITEMENTCANDIDATE", "CandidateID"); oID = (id); base.SetObjectID(ob, (id)); CandidateDA.Insert(ob, tc); } else { oID = ob.ID; CandidateDA.Update(ob, tc); } CVDA.DeleteAttachment(tc, ob.CvID, EnumFileType.CV); } tc.End(); foreach (Candidate ob in items) { if (ob.IRFileAttacments != null && ob.IRFileAttacments.Count > 0) { foreach (var uploadfile in ob.IRFileAttacments) { if (uploadfile.ID > 0 && uploadfile.FileTobase64 != null) { byte[] newBytes = Convert.FromBase64String(uploadfile.FileTobase64); uploadfile.FileAsByteArray = newBytes; uploadfile.ReferenceID = ob.ID; uploadfile.FileType = EnumFileType.CV; FileAttachmentDA.Insert(uploadfile, connectionString); } else { FileStream stream = new FileStream(uploadfile.FilePath, FileMode.Open, FileAccess.Read); BinaryReader reader = new BinaryReader(stream); byte[] buff = reader.ReadBytes((int)stream.Length); reader.Close(); stream.Close(); uploadfile.FileAsByteArray = buff; uploadfile.ReferenceID = ob.CvID; uploadfile.FileType = EnumFileType.CV; FileAttachmentDA.Insert(uploadfile, connectionString); } } } } } catch (Exception ex) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(ex); throw new ServiceException(ex.Message, ex); #endregion } } public void SaveCandidateAnDUpdateCV(List candidates) { TransactionContext tc = null; int oID = 0; int requisitionID = 0; try { tc = TransactionContext.Begin(true); foreach (Candidate ob in candidates) { requisitionID = ob.ProcessId; if (ob.IsNew) { int id = tc.GenerateID("RECRUITEMENTCANDIDATE", "CandidateID"); oID = (id); base.SetObjectID(ob, (id)); CandidateDA.Insert(ob, tc); } else { oID = ob.ID; CandidateDA.Update(ob, tc); } //if(ob.CV != null) //{ // CVDA.UpdateCVRecruitment(ob.CV, tc); //} } if(requisitionID > 0) { CVDA.UpdateRequisitionStatus(requisitionID, EnumOnBoradStatus.CVCollection, tc); } tc.End(); } catch (Exception ex) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(ex); throw new ServiceException(ex.Message, ex); #endregion } } public void UpdateCVOnInitiateDone(List candidates) { TransactionContext tc = null; int oID = 0; int requisitionID = 0; try { tc = TransactionContext.Begin(true); foreach (Candidate ob in candidates) { requisitionID = ob.ProcessId; //if (ob.IsNew) //{ // int id = tc.GenerateID("RECRUITEMENTCANDIDATE", "CandidateID"); // oID = (id); // base.SetObjectID(ob, (id)); // CandidateDA.Insert(ob, tc); //} //else //{ // oID = ob.ID; // CandidateDA.Update(ob, tc); //} if (ob.CV != null) { CVDA.UpdateCVRecruitment(ob.CV, tc); } } if (requisitionID > 0) { CVDA.UpdateRequisitionStatus(requisitionID, EnumOnBoradStatus.CVCollection, tc); } tc.End(); } catch (Exception ex) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(ex); throw new ServiceException(ex.Message, ex); #endregion } } public void UpdateSelection(Candidate candidate) { TransactionContext tc = null; int oID = 0; int requisitionID = 0; try { tc = TransactionContext.Begin(true); if (candidate.ID > 0) { CandidateDA.UpdateSelection(candidate, tc); } tc.End(); } catch (Exception ex) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(ex); throw new ServiceException(ex.Message, ex); #endregion } } public void UpdatePrimarySelection(int candidateId) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); if (candidateId > 0) { CandidateDA.DiscardCandidate(candidateId, tc); } tc.End(); } catch (Exception ex) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(ex); throw new ServiceException(ex.Message, ex); #endregion } } public Candidate GetCanditatebyCvID(int cvID, int recruitmentId) { Candidate cv = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(true); DataReader oreader = new DataReader(CandidateDA.GetCanditatebyCvID(tc, cvID, recruitmentId)); if (oreader.Read()) { cv = this.CreateObject(oreader); } oreader.Close(); tc.End(); } catch (Exception ex) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(ex); throw new ServiceException(ex.Message, ex); #endregion } return cv; } public List GetCanditatebyCvID(string cvIDs) { List candidates= new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(true); DataReader oreader = new DataReader(CandidateDA.GetCanditatesbyCvID(tc, cvIDs)); candidates = this.CreateObjects(oreader); oreader.Close(); tc.End(); } catch (Exception ex) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(ex); throw new ServiceException(ex.Message, ex); #endregion } return candidates; } public List GetCanditatebyCvId(TransactionContext tc,int cvID,int currentRecruitmentID) { List candidates = new List(); //TransactionContext tc = null; try { tc = TransactionContext.Begin(true); DataReader oreader = new DataReader(CandidateDA.GetCanditateCvID(tc, cvID, currentRecruitmentID)); candidates = this.CreateObjects(oreader); oreader.Close(); //tc.End(); } catch (Exception ex) { #region Handle Exception //if (tc != null) // tc.HandleError(); ExceptionLog.Write(ex); throw new ServiceException(ex.Message, ex); #endregion } return candidates; } public void SaveMultipleCandidates(TransactionContext tc,List candidates) { //TransactionContext tc = null; int oID = 0; int requisitionID = 0; try { // tc = TransactionContext.Begin(true); foreach (Candidate ob in candidates) { requisitionID = ob.ProcessId; if (ob.IsNew) { int id = tc.GenerateID("RECRUITEMENTCANDIDATE", "CandidateID"); oID = (id); base.SetObjectID(ob, (id)); CandidateDA.Insert(ob, tc); } else { oID = ob.ID; CandidateDA.Update(ob, tc); } if (ob.CV != null) { CVDA.UpdateCVRecruitment(ob.CV, tc); } } if (requisitionID > 0) { CVDA.UpdateRequisitionStatus(requisitionID, EnumOnBoradStatus.CVCollection, tc); } // tc.End(); } catch (Exception ex) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(ex); throw new ServiceException(ex.Message, ex); #endregion } } #endregion #endregion } }