/// <summary> /// Callback method that is called when a file is successfully downloaded. This /// method extracts the ZIP file and produces the final output file. /// </summary> /// <param name="result"></param> private void FileDownloaded(FileDownloadedResult result) { try { if (result.Filename.ToLower().Trim().EndsWith(".zip")) { Logger.Log("INFO", string.Format("Extracting {0}", result.Filename)); ZipFile.ExtractToDirectory(result.Filename, downloadFolder + result.OrganizationID + "-" + result.FileIdentifier.ToString()); System.IO.File.Delete(result.Filename); FileLocator locator = new FileLocator(downloadFolder + result.OrganizationID + "-" + result.FileIdentifier.ToString()); var filenames = locator.FindHIDFiles(); using (IUnitOfWorkDataProvider dp = AppStorage.GetUnitOfWorkDataProvider()) { int oldLineCount = 0; Organization org = dp.Organizations.FindSingle(x => x.RemoteID == result.OrganizationID); int currentFileCount = 1; foreach (var filename in filenames) { //instantiate parser which will read file CSVFileParser parser = new CSVFileParser(filename); //if no data in file then no reason to do anything if (!string.IsNullOrEmpty(parser.FirstLineText) && parser.LineCount > 1 && !string.IsNullOrWhiteSpace(parser.MachineID)) { //look to see if we have read another file with the same data appearing at the start of the file //new files may get uploaded that are really the same data with additional modules //appended. var files = dp.SourceFiles.FindMatching(x => x.FirstLineText == parser.FirstLineText && org.Id == x.OrganizationId); SourceFile file = null; //get source file that had the highest line count if (files != null && files.Count() > 0) { file = files.OrderByDescending(x => x.LineCount).ToList()[0]; } var shortName = filename.Replace(downloadFolder, ""); OutputFile outputFile = new OutputFile(); outputFile.Filename = downloadFolder.Replace("\\temp", "") + parser.MachineID.ToUpper().Trim() + "_" + DateTime.Now.ToString("yyyyMMddHHmmss_fff") + ".TXT"; outputFile.Created = DateTime.Now; oldLineCount = 0; if (file != null) { oldLineCount = file.LineCount; } //add a record for this file id - file will not be downloaded in //subsequent downloads SourceFile newFile = new SourceFile(); newFile.BatchNumber = batchNumber; newFile.FileIdentifer = result.FileIdentifier; newFile.SourceFilename = shortName; newFile.FirstDownload = DateTime.Now; newFile.LastDownload = DateTime.Now; newFile.OrganizationId = org.Id; newFile.LineCount = parser.LineCount; newFile.FirstLineText = parser.FirstLineText.ToLower().Trim(); if (parser.LineCount > oldLineCount) { newFile.OutputFiles.Add(outputFile); parser.WriteFile(outputFile.Filename, oldLineCount); } dp.SourceFiles.Add(newFile); dp.SaveChanges(); currentFileCount++; } else { Logger.Log("WARNING", "Empty file downloaded ignoring."); } } } } } catch (Exception exc) { Logger.Log(exc); extractErrors++; } }