public void GetPeriodsForAktuellLeverans(AdmFilkrav filkrav, RegisterFilkrav regFilkrav) { string period = String.Empty; DateTime startDate; DateTime endDate; DateTime dagensDatum = DateTime.Now.Date; var perioder = new List <string>(); //hämta varje förväntad leverans och sätt rätt period utifrån dagens datum foreach (var item in filkrav.AdmForvantadleverans) { if (item != null) { startDate = item.Rapporteringsstart; endDate = item.Rapporteringsslut; if (dagensDatum >= startDate && dagensDatum <= endDate) { //regInfo.Period = item.Period; perioder.Add(item.Period); //regInfo.ForvantadLevransId = item.Id; } } regFilkrav.Perioder = perioder; } }
private bool CompleteDelivery(RegisterFilkrav filkrav, List <FileInfo> fileList) { var complete = false; var numberOfFiles = filkrav.AntalFiler; var numberOfRequiredFiles = filkrav.AntalObligatoriskaFiler; var numberOfNotRequiredFiles = filkrav.AntalEjObligatoriskaFiler; var numberOfFilesInList = fileList.Count(); var numberOfRequriredFilesinList = 0; var numberOfNotRequiredFilesinList = 0; //TODO - check time foreach (var file in fileList) { foreach (var forvantadFil in filkrav.ForvantadeFiler) { Regex expression = new Regex(forvantadFil.Regexp, RegexOptions.IgnoreCase); Match match = expression.Match(file.Name); if (match.Success) { if (forvantadFil.Obligatorisk) { numberOfRequriredFilesinList++; } else { numberOfNotRequiredFiles++; } } } } if (numberOfRequiredFiles == numberOfRequriredFilesinList) { complete = true; } return(complete); }
private RegisterInfo CreateRegisterInfoObj(AdmDelregister delReg) { var regInfo = new RegisterInfo { Id = delReg.Id, Namn = delReg.Delregisternamn, Kortnamn = delReg.Kortnamn, InfoText = delReg.AdmRegister.Beskrivning + "<br>" + delReg.Beskrivning, Slussmapp = delReg.Slussmapp }; var filkravList = new List <RegisterFilkrav>(); var i = 1; foreach (var filkrav in delReg.AdmFilkrav) { var regFilkrav = new RegisterFilkrav(); var filmaskList = new List <string>(); var regExpList = new List <string>(); if (filkrav.Namn != null) { regFilkrav.Namn = filkrav.Namn; } else { regFilkrav.Namn = ""; } //Sök forväntad fil för varje filkrav istället för alla forvantade filer för registret!! //var forvantadFil = delReg.AdmFilkrav.Select(x => x.AdmForvantadfil); var forvantadeFiler = filkrav.AdmForvantadfil.ToList(); regFilkrav.AntalFiler = forvantadeFiler.Count(); foreach (var forvFil in forvantadeFiler) { filmaskList.Add(forvFil.Filmask); regExpList.Add(forvFil.Regexp); regFilkrav.InfoText = regFilkrav.InfoText + "<br> Filformat: " + forvFil.Filmask; regFilkrav.Obligatorisk = forvFil.Obligatorisk; } //get period och forvantadleveransId GetPeriodsForAktuellLeverans(filkrav, regFilkrav); regFilkrav.InfoText = regFilkrav.InfoText + "<br> Antal filer: " + regFilkrav.AntalFiler; regFilkrav.Id = i; regFilkrav.FilMasker = filmaskList; regFilkrav.RegExper = regExpList; //Om inga aktuella perioder finns för filkravet ska det inte läggas med i RegInfo if (regFilkrav.Perioder != null) { if (regFilkrav.Perioder.ToList().Count != 0) { filkravList.Add(regFilkrav); i++; } } } regInfo.Filkrav = filkravList; return(regInfo); }
private void HandleFileCheckResult(List <FileInfo> okFilesForSubDirList, RegisterFilkrav filkrav, string unitCode, RegisterInfo delregInfo, string period, List <RegisterInfo> delregisterInfoList, string folder, List <FileInfo> incorrectFileCodeList, List <FileInfo> incorrectPeriodList) { if (okFilesForSubDirList.Count > 0) { if (CompleteDelivery(filkrav, okFilesForSubDirList)) { try { var resultList = new List <ViewDataUploadFilesResult>(); //If complete delivery of approved files, tag and upload _filesHelper.UploadSFTPFilesAndShowResults(okFilesForSubDirList, resultList, _ftpAccount, delregInfo.Id, unitCode, period, delregisterInfoList); //Delete uploaded files from incoming filearea foreach (var file in okFilesForSubDirList) { if (file.Exists) { file.Delete(); } } HandleErrorLists(incorrectFileCodeList, incorrectPeriodList, folder); incorrectFileCodeList.Clear(); incorrectPeriodList.Clear(); DirectoryInfo dir = new DirectoryInfo(folder); _filesInFolder = dir.GetFiles().OrderByDescending(p => p.CreationTime).ToList(); //Rekursivt anrop, hantera ev kvarvarande filer CheckFiles(folder, incorrectPeriodList, incorrectFileCodeList); } catch (ApplicationException e) { ErrorManager.WriteToErrorLog("SFTPWatcher", "Upload approved files", e.ToString(), e.HResult, folder); } catch (Exception e) { //Todo - send mail? Console.WriteLine("Sending email-alert. Upload files aborted."); ErrorManager.WriteToErrorLog("SFTPWatcher", "Upload approved files", e.ToString(), e.HResult, folder); } } else { //If not complete, check if enough time elapsed var sortedFilesList = okFilesForSubDirList .OrderByDescending(p => p.CreationTime).ToList(); //Check youngest file if (sortedFilesList[0].CreationTime.AddMinutes(_timeToWaitForCompleteDelivery) < DateTime.Now) { //Not complete delivery - email user and remove files try { IncompleteDeliveryHandler(sortedFilesList, folder); HandleErrorLists(incorrectFileCodeList, incorrectPeriodList, folder); DirectoryInfo dir = new DirectoryInfo(folder); _filesInFolder = dir.GetFiles().OrderByDescending(p => p.CreationTime).ToList(); //Rekursivt anrop, hantera ev kvarvarande filer CheckFiles(folder, incorrectPeriodList, incorrectFileCodeList); } catch (System.Net.Mail.SmtpException e) { Console.WriteLine(e); //throw new ArgumentException(e.Message); ErrorManager.WriteToErrorLog("SFTPWatcher", "SendEmail/Incomplete delivery", e.ToString(), e.HResult, _folderName); } catch (Exception e) { Console.WriteLine(e); ErrorManager.WriteToErrorLog("SFTPWatcher", "Moving not approved files aborted", e.ToString(), e.HResult, _folderName); } } //If not compelete delivery WITHIN TIMEINTERVAL => files can not be deleted from in-folder because there might come files to complete the delivery later. //These files shall not be tested against another subdirectory because that will cause them to be handled as files with incorrect filenames. //Copy these files to exclude-list so that they will not be checked against another subdirectory in this execution. //clear errorlists. else { foreach (var file in okFilesForSubDirList) { _filesToExcludeFromCheckList.Add(file); } _inCorrectFilenameList.Clear(); incorrectFileCodeList.Clear(); incorrectPeriodList.Clear(); } } } HandleErrorLists(incorrectFileCodeList, incorrectPeriodList, folder); }