private void MoveFileToArchive(string sourceFolder, string destinationFolder) { _logger.Info("Moving directory to archive folder."); var directories = DirectoryOperationsHelper.GetDirectories(sourceFolder); if (directories == null || !directories.Any()) { _logger.Info("No directory found to move into archive."); return; } directories = directories.Where(x => !x.Contains(DateTime.Today.Year.ToString())).ToArray(); if (directories.Any()) { foreach (var dir in directories) { var files = DirectoryOperationsHelper.GetFiles(dir, "*.pdf"); if (!files.Any()) { _logger.Info("No files found in Directory: " + dir + "to move into archive."); continue; } _logger.Info(string.Format("{0} files found in directory: {1}.", files.Count(), dir)); var folderName = Path.GetFileName(dir.TrimEnd(Path.DirectorySeparatorChar)); var destinationPath = Path.Combine(destinationFolder, folderName); DirectoryOperationsHelper.CreateDirectoryIfNotExist(destinationPath); foreach (var file in files) { if (DirectoryOperationsHelper.IsFileExist(file)) { var fileName = Path.GetFileName(file); var destinationFileName = Path.Combine(destinationPath, fileName); try { DirectoryOperationsHelper.DeleteFileIfExist(destinationFileName); DirectoryOperationsHelper.Move(file, destinationFileName); _logger.Info(string.Format("{0} file moved into {1} from directory: {2}.", fileName, destinationPath, dir)); } catch (Exception ex) { _logger.Error(string.Format("Exception occurred while moving file {0} to {1}. \nException Message: {2}\n\tStackTrace:{3}", file, destinationFileName, ex.Message, ex.StackTrace)); } } } DirectoryOperationsHelper.DeleteDirectory(dir); } } }
private static string GetFolderPathfor(string toFindinFolder) { // Need to provide a better pattern if (DirectoryOperationsHelper.GetFiles(toFindinFolder).Any(file => Path.GetExtension(file).ToLower().IndexOf("pdf") > 0)) { return(toFindinFolder); } foreach (string directory in DirectoryOperationsHelper.GetDirectories(toFindinFolder)) { var path = GetFolderPathfor(directory); if (!string.IsNullOrEmpty(path)) { return(path); } } return(string.Empty); }
public void PollForParsing() { try { var timeOfDay = DateTime.Now; if (_isDevEnvironment || timeOfDay.TimeOfDay > new TimeSpan(4, 0, 0)) { _logger.Info("Service started..."); var directoryInfo = DirectoryOperationsHelper.GetDirectories(_hkynParsePdfPath); if (directoryInfo.IsNullOrEmpty()) { _logger.Info("No Directory found for Parsing"); return; } foreach (var dirPath in directoryInfo) { try { var dirInfo = DirectoryOperationsHelper.GetDirectoryInfo(dirPath); if (dirInfo != null) { var files = DirectoryOperationsHelper.GetFiles(dirPath, "*.Pdf"); long eventid = 0; long.TryParse(dirInfo.Name, out eventid); if (eventid > 0) { var eventData = _eventRepository.GetById(eventid); if (files.IsNullOrEmpty()) { _logger.Info("No file found inside " + dirInfo.Name); if (eventData.EventDate > DateTime.Today.AddDays(-DeleteEmptyFolderAfter)) { DirectoryOperationsHelper.DeleteDirectory(dirPath, true); } continue; } ParseFilesForCustomer(dirPath, eventid); } else { _logger.Info("folder does not contain valid Name: " + dirInfo.Name); } } } catch (Exception ex) { _logger.Error("Some error occurred for Dir Path: " + dirPath); _logger.Error("Message: " + ex.Message); _logger.Error("Stack Trace: " + ex.StackTrace); } } } } catch (Exception ex) { _logger.Error("Some error occurred while parsing"); _logger.Error("Message: " + ex.Message); _logger.Error("Stack Trace: " + ex.StackTrace); } }
public IEnumerable <EventCustomerScreeningAggregate> Parse() { var eventCustomerAggregates = new List <EventCustomerScreeningAggregate>(); var directoryPath = GetFolderPathfor(_microalbuminResultOutputPath); if (string.IsNullOrEmpty(directoryPath) && string.IsNullOrEmpty(directoryPath)) { return(null); } List <string> pdfFiles = null; if (!string.IsNullOrEmpty(directoryPath)) { pdfFiles = GetPdfFiles(directoryPath); } var eventList = DirectoryOperationsHelper.GetDirectories(directoryPath); if (eventList != null && eventList.Any()) { _logger.Info("Number of Events : " + eventList.Count()); foreach (var events in eventList) { long customerId = 0; long eventId = 0; string errorMessage = string.Empty; if (!long.TryParse(events, out eventId)) { _logger.Info("Event Id not found : " + events); continue; } List <string> pdfResults = null; var eventFolder = Path.Combine(directoryPath, events); if (!string.IsNullOrEmpty(eventFolder)) { pdfResults = GetPdfFiles(eventFolder); if (pdfResults != null && pdfResults.Any()) { _logger.Info("Number of Files to Parse : " + pdfFiles.Count() + " Event Id is :" + events); foreach (var filePath in pdfResults) { var fileNameWithExtention = Path.GetFileName(filePath); if (!string.IsNullOrEmpty(fileNameWithExtention)) { fileNameWithExtention = fileNameWithExtention.ToLower(); } var fileName = Path.GetFileNameWithoutExtension(filePath); if (!string.IsNullOrEmpty(fileName)) { fileName = fileName.ToLower(); } _logger.Info("=============== Parsing Started for file: " + fileName + " ================="); TestType testTypeId; string testName = ""; try { var temp = fileName.Split('_'); var memberIdString = temp[0]; testName = Convert.ToString(temp[1]); testTypeId = ((TestType)Enum.Parse(typeof(TestType), testName)); IEnumerable <long> customerIds = _customerRepository.GetCustomerIdByInsuranceId(memberIdString); if (!customerIds.IsNullOrEmpty()) { errorMessage = "Invalid member Id: " + memberIdString + " File Path" + filePath; _logger.Info(errorMessage); continue; } var eventCustomers = _eventCustomerRepository.GetbyEventId(eventId); eventCustomers = eventCustomers.Where(x => customerIds.Contains(x.CustomerId) && x.AppointmentId.HasValue); if (eventCustomers.IsNullOrEmpty()) { errorMessage = "customer has not been booked for event with member Id: " + memberIdString + " EventId : " + eventId; _logger.Info(errorMessage); continue; } if (eventCustomers.Count() > 1) { errorMessage = "more than one customer has been booked for event with member Id: " + memberIdString + " EventId : " + eventId; _logger.Info(errorMessage); continue; } customerId = eventCustomers.First().CustomerId; } catch (Exception) { errorMessage = "File name is not in correct format. "; _logger.Error(errorMessage); continue; } var isTestPurchased = _testResultService.IsTestPurchasedByCustomer(eventId, customerId, (long)testTypeId); if (!isTestPurchased) { errorMessage = string.Format("Customer Id :" + customerId + " Test: " + testName + " not purchased "); _logger.Info(errorMessage); continue; } try { string folderToSavePdf = _mediaRepository.GetResultMediaFileLocation(customerId, eventId).PhysicalPath; var resultMedia = GetMediaFromPdfFile(filePath, folderToSavePdf, testTypeId); MovedParsedFile(filePath, eventId); //if (resultMedia != null) //{ // TestResult testResult = new DiabeticRetinopathyTestResult { ResultImage = resultMedia }; // _resultParserHelper.AddTestResulttoEventCustomerAggregate(eventCustomerAggregates, eventId, customerId, testResult); // _resultParserHelper.AddResultArchiveLog(string.Empty, TestType.DiabeticRetinopathy, customerId, MedicalEquipmentTag.Vatica); // _logger.Info(string.Concat("\nParsing succeeded for Customer Id: ", customerId, "\n")); //} } catch (Exception ex) { errorMessage = " System Failure! Message: " + ex.Message + "\n\t" + ex.StackTrace; _logger.Error(errorMessage); // _resultParserHelper.AddResultArchiveLog(ex.Message, TestType.DiabeticRetinopathy, customerId, MedicalEquipmentTag.Vatica, false); } } } } } } return(eventCustomerAggregates); }