public void SaveMedications(IEnumerable <MedicationEditModel> medications, long customerId, long orgRoleUserId) { if (medications.IsNullOrEmpty()) { return; } var updateList = medications.Where(x => x.Id != 0).ToArray(); var insertList = medications.Where(x => x.Id == 0).ToArray(); var medicationIdNotToMarkAsDelete = updateList.Select(x => x.Id).ToArray(); _medicationRepository.MarkForDelete(customerId, orgRoleUserId, medicationIdNotToMarkAsDelete); if (!insertList.IsNullOrEmpty()) { var model = _medicationFactory.CreateModel(insertList, customerId); _medicationRepository.SaveMedications(model, orgRoleUserId); } updateList = updateList.Where(x => x.IsEdited).ToArray(); if (!updateList.IsNullOrEmpty()) { var model = _medicationFactory.CreateModel(updateList, customerId); _medicationRepository.UpdateMedications(model, orgRoleUserId); } if (insertList.Any() || updateList.Any()) { _syncMedicationPollingAgent.SyncMedicationData(_logger, orgRoleUserId, false, customerId); } }
public void PollForParsingMedicationUpload() { try { _logger.Info("Entering Medication upload File parser"); var timeOfDay = DateTime.Now; if (_isDevEnvironment || (DateTime.Now.TimeOfDay < new TimeSpan(23, 0, 0) && DateTime.Now.TimeOfDay > new TimeSpan(4, 0, 0))) //condition to restrict run time here { var filesToParse = (IReadOnlyCollection <MedicationUpload>)_medicationUploadRepository.GetFilesToParse(); if (filesToParse.IsNullOrEmpty()) { _logger.Info("No file found for Parsing"); return; } _logger.Info("Number Of File Found For parsing: " + filesToParse.Count()); var uploadedFileIds = filesToParse.Select(x => x.FileId); var uploadedFiles = _fileRepository.GetByIds(uploadedFileIds); var location = _mediaRepository.GetMedicationUploadMediaFileLocation(); foreach (var medicationUploadDomain in filesToParse) { try { medicationUploadDomain.ParseStartTime = DateTime.Now; var fileDomain = uploadedFiles.FirstOrDefault(x => x.Id == medicationUploadDomain.FileId); if (fileDomain == null) { UpdateParsingStatus(medicationUploadDomain, (long)MedicationUploadStatus.FileNotFound); _logger.Info("Parsing Failed: FileNotFound MedicationUploadId: " + medicationUploadDomain.Id); continue; } var file = location.PhysicalPath + fileDomain.Path; _logger.Info("\t Beginning parsing for File : " + file); UpdateParsingStatus(medicationUploadDomain, (long)MedicationUploadStatus.Parsing, false); _csvReader.Delimiter = _csvReader.GetFileDelimiter(file).ToString(); var medicationTable = _csvReader.ReadWithTextQualifier(file); var totalMedication = medicationTable.Rows.Count; var totalPages = totalMedication / _pageSize + (totalMedication % _pageSize != 0 ? 1 : 0); _logger.Info("Total No. Of Pages: " + totalPages + " Total No. of Records " + totalMedication); var pageNumber = 1; var failedRecordsList = new List <MedicationUploadLog>(); var units = _unitRepository.GetAll(); while (totalPages >= pageNumber) { var query = medicationTable.AsEnumerable(); _logger.Info("Parsing For Page Number: " + pageNumber); var rows = query.Skip(_pageSize * (pageNumber - 1)).Take(_pageSize); ParseMedicationUploaded(rows, failedRecordsList, medicationUploadDomain, units); pageNumber++; } medicationUploadDomain.SuccessfullUploadCount = (totalMedication - failedRecordsList.Count(s => !s.IsSuccessFull)); medicationUploadDomain.ParseEndTime = DateTime.Now; medicationUploadDomain.StatusId = (long)MedicationUploadStatus.Parsed; UpdateMedicationUploadDetail(medicationUploadDomain, failedRecordsList, file); _logger.Info("Passing control to Sync Service"); _syncMedicationPollingAgent.SyncMedicationData(_logger, medicationUploadDomain.UploadedBy, true); _logger.Info("Control returned from Sync Service"); _logger.Info("\n\t Medication Upload Parsing succeeded for file: " + file); } catch (Exception ex) { _logger.Error(string.Format("Error while parsing FileId {0} Message: {1} \n\t StackTrace: {2}", medicationUploadDomain.FileId, ex.Message, ex.StackTrace)); } } } else { _logger.Info(string.Format("Medication Upload Parser can not be called as time of day is {0}", timeOfDay.TimeOfDay)); } _logger.Info(string.Format("Exiting Medication Upload Parser : {0}", timeOfDay.TimeOfDay)); } catch (Exception ex) { _logger.Error("all Upload Parser Exception: " + ex + "\n Stack Trace:" + ex.StackTrace); } }