public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification) { ComplianceProcessDTO inputParam = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <ComplianceProcessDTO>(); notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; Module.Framework.DAL.DataFactory fwFactory = new Module.Framework.DAL.DataFactory(); try { using (ComplianceMngEntities context = CreateContext()) { ComplianceProcess dbItem = null; if (id == 0) { dbItem = new ComplianceProcess(); context.ComplianceProcess.Add(dbItem); dbItem.ComplianceProcessUD = context.ComplianceMng_function_GenerateCode().FirstOrDefault(); } else { //get db item dbItem = context.ComplianceProcess.FirstOrDefault(o => o.ComplianceProcessID == id); } if (dbItem == null) { notification.Message = "Comliance not found!"; return(false); } else { dbItem.UpdatedBy = userId; dbItem.UpdatedDate = DateTime.Now; // process Detail foreach (DTO.ComplianceAttachedFileDTO dtoDetail in inputParam.ComplianceAttachedFileDTOs) { // FileUD if (dtoDetail.FileUDHasChange) { if (string.IsNullOrEmpty(dtoDetail.NewFileUD)) { if (dtoDetail.FileUD == dtoDetail.NewFileUD) { dtoDetail.FileUD = null; } else { fwFactory.RemoveFile(dtoDetail.FileUD); } } else { dtoDetail.FileUD = fwFactory.CreateNoneImageFilePointer(FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", dtoDetail.NewFileUD, dtoDetail.FileUD, dtoDetail.FileUDFriendlyName); } } } foreach (DTO.CompliancePICDTO dtoDetail in inputParam.CompliancePICDTOs) { // FileUD if (dtoDetail.FileUDHasChange) { if (string.IsNullOrEmpty(dtoDetail.NewFileUD)) { if (dtoDetail.FileUD == dtoDetail.NewFileUD) { dtoDetail.FileUD = null; } else { fwFactory.RemoveFile(dtoDetail.FileUD); } } else { dtoDetail.FileUD = fwFactory.CreateNoneImageFilePointer(FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", dtoDetail.NewFileUD, dtoDetail.FileUD, dtoDetail.FileUDFriendlyName); } } } //read dto to db converter.DTO2DB_ComplianceProcess(inputParam, ref dbItem, userId); //delete offerline is null context.ComplianceAttachedFile.Local.Where(o => o.ComplianceProcess == null).ToList().ForEach(o => context.ComplianceAttachedFile.Remove(o)); context.CompliancePIC.Local.Where(o => o.ComplianceProcess == null).ToList().ForEach(o => context.CompliancePIC.Remove(o)); //save data context.SaveChanges(); //reload data if (id > 0) { dtoItem = new ComplianceProcessDTO { ComplianceProcessID = id }; } else { dtoItem = new ComplianceProcessDTO { ComplianceProcessID = dbItem.ComplianceProcessID }; } return(true); } } } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = Library.Helper.HandleExceptionSingleLine(ex); } return(false); }
public void DTO2DB_ComplianceProcess(ComplianceProcessDTO dtoItem, ref ComplianceProcess dbItem, int userId) { /* * MAP & CHECK ATTACHED FILE */ List <ComplianceAttachedFile> toBeDeletedItems = new List <ComplianceAttachedFile>(); if (dtoItem.ComplianceAttachedFileDTOs != null) { //CHECK foreach (ComplianceAttachedFile dbSample in dbItem.ComplianceAttachedFile.Where(o => !dtoItem.ComplianceAttachedFileDTOs.Select(s => s.ComplianceAttachedFileID).Contains(o.ComplianceAttachedFileID)).ToArray()) { dbItem.ComplianceAttachedFile.Remove(dbSample); } //MAP foreach (ComplianceAttachedFileDTO dtoSample in dtoItem.ComplianceAttachedFileDTOs) { ComplianceAttachedFile dbSample = null; if (dtoSample.ComplianceAttachedFileID <= 0) { dbSample = new ComplianceAttachedFile(); dbItem.ComplianceAttachedFile.Add(dbSample); } else { dbSample = dbItem.ComplianceAttachedFile.FirstOrDefault(o => o.ComplianceAttachedFileID == dtoSample.ComplianceAttachedFileID); if (dbSample == null) { throw new Exception("Sample item not found!"); } } AutoMapper.Mapper.Map <ComplianceAttachedFileDTO, ComplianceAttachedFile>(dtoSample, dbSample); } } /* * MAP & CHECK PIC */ List <CompliancePIC> toBeDeletedCompliancePICItems = new List <CompliancePIC>(); if (dtoItem.CompliancePICDTOs != null) { //CHECK foreach (CompliancePIC dbSample in dbItem.CompliancePIC.Where(o => !dtoItem.CompliancePICDTOs.Select(s => s.CompliancePICID).Contains(o.CompliancePICID)).ToArray()) { dbItem.CompliancePIC.Remove(dbSample); } //MAP foreach (CompliancePICDTO dtoSample in dtoItem.CompliancePICDTOs) { CompliancePIC dbSample = null; if (dtoSample.CompliancePICID <= 0) { dbSample = new CompliancePIC(); dbItem.CompliancePIC.Add(dbSample); } else { dbSample = dbItem.CompliancePIC.FirstOrDefault(o => o.CompliancePICID == dtoSample.CompliancePICID); if (dbSample == null) { throw new Exception("Sample item not found!"); } } AutoMapper.Mapper.Map <CompliancePICDTO, CompliancePIC>(dtoSample, dbSample); } } /* * SETUP FORMATED FIELD */ //dbItem.ExpiredDate = Library.Helper.ConvertStringToDateTime(dtoItem.ExpiredDate, new System.Globalization.CultureInfo("vi-VN")); //dbItem.RenewDate = Library.Helper.ConvertStringToDateTime(dtoItem.RenewDate, new System.Globalization.CultureInfo("vi-VN")); //dbItem.FictiveDate = Library.Helper.ConvertStringToDateTime(dtoItem.FictiveDate, new System.Globalization.CultureInfo("vi-VN")); Mapper.Map(dtoItem, dbItem); }