private int ProcessUploadedFile(string fileName) { int idImport = ApplicationConstants.INT_NULL_VALUE; string cleanFileName = ApplicationUtils.GetCleanFileName(fileName); string shareFilePath = string.Empty; // get the directory from appsettings string dirUrl = ConfigurationManager.AppSettings["UploadFolderInitial"]; string dirPath = Server.MapPath(dirUrl); string shareName = @"\\" + Server.MachineName + @"\" + dirUrl; shareFilePath = shareName + @"\" + cleanFileName; ImportInitialBudget initialBudgetUpload = new ImportInitialBudget(SessionManager.GetSessionValueNoRedirect(this.Page, SessionStrings.CONNECTION_MANAGER)); try { idImport = initialBudgetUpload.WriteToInitialBudgetImportTable(shareFilePath, currentUser.IdAssociate); if (idImport > 0) { btnProcess.Enabled = true; ProcessIdHdn.Value = idImport.ToString(); MoveFileToDirectory(TargetDirectoryEnum.DIRECTORY_PROCESSED, shareFilePath); } return(idImport); } catch (Exception ex) { btnProcess.Enabled = false; MoveFileToDirectory(TargetDirectoryEnum.DIRECTORY_CANCELLED, shareFilePath); throw new IndException(ex); } }
private bool ProcessFiles(ref ArrayList processedMessages) { string dirUrl = ConfigurationManager.AppSettings["UploadFolderAnnual"]; string dirPath = Server.MapPath(@"..\..\" + dirUrl); string shareName = @"\\" + Server.MachineName + @"\" + dirUrl; string fileName = string.Empty; bool bResult = false; if (!Directory.Exists(dirPath)) { processedMessages.Add(string.Format(ApplicationMessages.IMPORT_FOLDER_DOES_NOT_EXIST, dirUrl)); return(false); } foreach (GridDataItem item in grdProcessInformation.MasterTableView.Items) { fileName = shareName + @"\" + item["InProcessRealFileName"].Text; if (!File.Exists(fileName)) { processedMessages.Add(string.Format(ApplicationMessages.IMPORT_FILE_NOT_EXIST, Path.GetFileName(fileName), shareName)); LogErrorToDataBase(string.Format(ApplicationMessages.IMPORT_FILE_NOT_EXIST, Path.GetFileName(fileName), shareName), fileName); continue; } FileInfo fi = new FileInfo(fileName); //log error if the file has 0 size if (fi.Length == 0) { processedMessages.Add(string.Format(ApplicationMessages.PROCESS_ZERO_SIZE, ApplicationUtils.GetCleanFileName(fileName))); LogErrorToDataBase(string.Format(ApplicationMessages.PROCESS_ZERO_SIZE, ApplicationUtils.GetCleanFileName(fileName)), fileName); MoveFileToDirectory(false, fileName); continue; } try { bResult = ProcessFile(fileName, ref processedMessages); MoveFileToDirectory(bResult, fileName); } catch (Exception ex) { processedMessages.Add(string.Format(ApplicationMessages.IMPORT_FILE_ERROR, ApplicationUtils.GetCleanFileName(fileName))); LogErrorToDataBase(ex.Message, ApplicationUtils.GetCleanFileName(fileName)); continue; //we continue with the next file } } return(bResult); }
protected void btnUpload_Click(object sender, EventArgs e) { if (filUpload.UploadedFiles.Count > 0) { try { if (VerifyRequiredFields()) { int idImport = DoUploadFile(); this.ShowError(new IndException(string.Format(ApplicationMessages.UPLOAD_SUCCESSFULY_BUDGET_INITIAL, idImport))); } else { this.ShowError(new IndException(string.Format(ApplicationMessages.IMPORT_FILE_INITIAL_BUDGET_ERROR, ApplicationUtils.GetCleanFileName(filUpload.UploadedFiles[0].FileName), errorMessages[0].ToString()))); } } catch (IndException ex) { string showError = string.Format(ApplicationMessages.IMPORT_FILE_INITIAL_BUDGET_ERROR, ApplicationUtils.GetCleanFileName(filUpload.UploadedFiles[0].FileName), ex.Message); this.ShowError(new IndException(showError)); } } else { this.ShowError(new IndException(ApplicationMessages.UPLOAD_ZERO_SIZE)); } }
protected void btnUpload_Click(object sender, EventArgs e) { ArrayList uploadMessages = new ArrayList(); String strLink = GetLogLink(Request.ApplicationPath); if (filUpload.UploadedFiles.Count > 0) { try { if (VerifyRequiredFields()) { DoUploadFile(); LoadDataGrid(); uploadMessages.Add(ApplicationMessages.UPLOAD_SUCCESFULLY); } else { LogErrorToDataBase(errorMessages[0].ToString(), filUpload.UploadedFiles[0].FileName); uploadMessages.Add(string.Format(ApplicationMessages.IMPORT_FILE_ERROR, ApplicationUtils.GetCleanFileName(filUpload.UploadedFiles[0].FileName))); uploadMessages.Add(strLink); } } catch (IndException ex) { LogErrorToDataBase(ex.Message, filUpload.UploadedFiles[0].FileName); uploadMessages.Add(string.Format(ApplicationMessages.IMPORT_FILE_ERROR, ApplicationUtils.GetCleanFileName(filUpload.UploadedFiles[0].FileName))); uploadMessages.Add(strLink); } } else { uploadMessages.Add(ApplicationMessages.UPLOAD_ZERO_SIZE); uploadMessages.Add(strLink); } this.ShowError(uploadMessages); }
/// <summary> /// put the data from imports table to actual data after constraints check /// </summary> private bool ProcessFile(string fileName, ref ArrayList processedMessages) { int idImport = ApplicationConstants.INT_NULL_VALUE; int result = ApplicationConstants.INT_NULL_VALUE; int resultFileExist = ApplicationConstants.INT_NULL_VALUE; string duplicateError = string.Empty; string cleanFileName = ApplicationUtils.GetCleanFileName(fileName); AnnualUpload annualUpload = new AnnualUpload(SessionManager.GetSessionValueNoRedirect(this.Page, SessionStrings.CONNECTION_MANAGER)); annualUpload.IdAssociate = currentUser.IdAssociate; try { annualUpload.FileName = fileName; resultFileExist = annualUpload.CheckFileAlreadyUploaded(); } catch (Exception ex) { processedMessages.Add(ApplicationMessages.IMPORT_CHECK_FILE_CRASH); LogErrorToDataBase(ex.Message, fileName); return(false); } if (resultFileExist < 0) { processedMessages.Add(string.Format(ApplicationMessages.IMPORT_FILE_ALREADY_UPLOADED, cleanFileName)); return(false); } try { idImport = annualUpload.WriteToAnnualImportTable(); } catch (Exception ex) { processedMessages.Add(string.Format(ApplicationMessages.IMPORT_FILE_ERROR, cleanFileName)); LogErrorToDataBase(ex.Message, fileName); return(false); } if (idImport < 0) { processedMessages.Add(string.Format(ApplicationMessages.IMPORT_FILE_ERROR, cleanFileName)); return(false); } try { annualUpload.IdImport = idImport; result = annualUpload.InsertIntoAnnualTable(); } catch (IndException ex) { result = -100; if (((IndException)ex).BaseException is SqlException) { SqlException sqlBaseException = ((SqlException)((IndException)ex).BaseException); if (sqlBaseException.Number == 2627) //The exception number for primary key violation { duplicateError = "At least one record from the file " + cleanFileName + " already exists in the database."; } } if (string.IsNullOrEmpty(duplicateError)) { LogProcessErrorToDataBase(idImport, ex.Message, fileName); } else { LogProcessErrorToDataBase(idImport, duplicateError, fileName); } } if (result >= 0) { processedMessages.Add(string.Format(ApplicationMessages.IMPORT_FILE_PROCESSED, cleanFileName)); return(true); } else { processedMessages.Add(string.Format(ApplicationMessages.IMPORT_FILE_ERROR, cleanFileName)); return(false); } }
/// <summary> /// Get the source of the grid from InProcess directory files /// </summary> /// <returns>generic list of UploadFile class type</returns> private List <UploadFile> GetSource() { List <UploadFile> uf = new List <UploadFile>(); string dirUrl = @"..\..\" + ConfigurationManager.AppSettings["UploadFolderAnnual"];//@"..\..\UploadDirectoriesAnnual\InProcess"; string filesPath = HttpContext.Current.Server.MapPath(dirUrl); string[] filesArr = Directory.GetFiles(filesPath); string fileName = string.Empty; string realFileName = string.Empty; for (int i = 0; i <= filesArr.Length - 1; i++) { //get only csv files if (Path.GetExtension(filesArr[i].ToString()) != ".csv") { continue; } //only BA and TA are allowed to see all files if ((currentUser.UserRole.Id == ApplicationConstants.ROLE_FINANCIAL_TEAM) && (ApplicationUtils.GetCleanFileName(filesArr[i].ToString()).Substring(0, 3) != currentUser.CountryCode)) { continue; } realFileName = Path.GetFileName(filesArr[i]); string[] arr = Path.GetFileNameWithoutExtension(filesArr[i]).Split('_'); fileName = arr[0] + Path.GetExtension(filesArr[i]); uf.Add(new UploadFile(File.GetLastWriteTime(filesArr[i]), fileName, realFileName)); } return(uf); }
/// <summary> /// put the data from imports table to actual data after constraints check /// </summary> private int ProcessFile(string fileName, ref ArrayList processedMessages, ref int idImport) { idImport = ApplicationConstants.INT_NULL_VALUE; int resultChronologicalOrder = ApplicationConstants.INT_NULL_VALUE; int NonExistingAssociates = ApplicationConstants.INT_NULL_VALUE; string fileNameNonExistingAssociates = string.Empty; //check if file is not already imported string[] str = Path.GetFileName(fileName).Split('_'); string cleanFileName = str[0] + Path.GetExtension(fileName); Imports imp = new Imports(SessionManager.GetSessionValueNoRedirect(this.Page, SessionStrings.CONNECTION_MANAGER)); bool validationResult; validationResult = CheckFileAlreadyExists(imp, fileName, cleanFileName, processedMessages); if (validationResult == false) { return(-1); } try { resultChronologicalOrder = imp.CheckChronologicalImports(cleanFileName); } catch (IndException ex) { processedMessages.Add(ex.Message); ChronologicalErrorsToDB(ex.Message, fileName); return(-1); } //write in imports table try { idImport = imp.InsertToImportsTable(fileName, currentUser.IdAssociate); } catch (IndException ex) { processedMessages.Add(string.Format(ApplicationMessages.IMPORT_FILE_ERROR, cleanFileName)); LogErrorToDataBase(string.Format(ApplicationMessages.IMPORT_FILE_UPLOAD_ERROR, cleanFileName, ex.Message), fileName); return(-1); } if (idImport < 0) { processedMessages.Add(string.Format(ApplicationMessages.IMPORT_FILE_ERROR, cleanFileName)); return(-1); } int result = ApplicationConstants.INT_NULL_VALUE; string duplicateError = string.Empty; bool isConsistencyKRMError = false; //KRM stands for "key row(s) missing" String consistencyKRMError = String.Empty; String consistencyKRMMessage = "key row(s) were missing from import"; try { if (FilesWithCostCentersWithoutHR.Contains(fileName)) { //file is processed second time. Skip Cost Centers without Hourly Rate result = imp.InsertToActualTable(fileName, currentUser.IdAssociate, idImport, true); } else { result = imp.InsertToActualTable(fileName, currentUser.IdAssociate, idImport, false); } } catch (IndException ex) { result = -100; if (((IndException)ex).BaseException is SqlException) { SqlException sqlBaseException = ((SqlException)((IndException)ex).BaseException); if (sqlBaseException.Number == 2627) //The exception number for primary key violation { duplicateError = "At least one record from the file " + cleanFileName + " already exists in the database."; } if (sqlBaseException.Message.Contains(consistencyKRMMessage)) { consistencyKRMError = sqlBaseException.Message; isConsistencyKRMError = true; } } if (!isConsistencyKRMError) //for this we write the message below { if (string.IsNullOrEmpty(duplicateError)) { LogProcessErrorToDataBase(idImport, ex.Message, fileName); } else { LogProcessErrorToDataBase(idImport, duplicateError, fileName); } } } // consistency error treatment int rowsInserted = 0; String strError = String.Empty; try { if (isConsistencyKRMError) { strError += consistencyKRMError; rowsInserted = InsertKeyRowsMissingInDB(idImport); if (rowsInserted > 0) { DataTable dtRowsMissing = SelectKeyRowsMissingFromDB(idImport); LogWriteKeyrowsMissingToLogTable(idImport, strError, fileName, dtRowsMissing); } else { LogProcessErrorToDataBase(idImport, strError, fileName); } } } catch (Exception ex) { LogProcessErrorToDataBase(idImport, ex.Message, fileName); } ///////////////////////////// try { DataSet dsNonExistingAssociates = imp.SelectNonExistingAssociateNumbers(idImport); if (result >= 0) { fileNameNonExistingAssociates = GetServerPath(TargetDirectoryEnum.DIRECTORY_PROCESSED) + @"\" + ApplicationUtils.GetCleanFileName(Path.GetFileNameWithoutExtension(fileName)) + "_MissingAssociates.txt"; } else { fileNameNonExistingAssociates = GetServerPath(TargetDirectoryEnum.DIRECTORY_CANCELLED) + @"\" + ApplicationUtils.GetCleanFileName(Path.GetFileNameWithoutExtension(fileName)) + "_MissingAssociates.txt"; } NonExistingAssociates = ApplicationUtils.CreateAssociatesTextFile(dsNonExistingAssociates, fileNameNonExistingAssociates); } catch (Exception ex) { throw new IndException(ex); } string msg = string.Empty; if (result >= 0 && result != ApplicationConstants.COST_CENTER_WITHOUT_HOURLYRATE_CODE_FROM_SP) { msg = string.Format(ApplicationMessages.IMPORT_FILE_PROCESSED, cleanFileName); if (NonExistingAssociates >= 0) { fileNameNonExistingAssociates = ConfigurationManager.AppSettings["UploadFolderProcessed"] + @"\" + ApplicationUtils.GetCleanFileName(Path.GetFileNameWithoutExtension(fileName)) + "_MissingAssociates.txt"; msg += string.Format(ApplicationMessages.UPLOAD_MISSING_ASSOCIATES + ApplicationMessages.UPLOAD_ASSOCIATES_INSERTED, NonExistingAssociates + 1, fileNameNonExistingAssociates); } processedMessages.Add(msg); //If file was in this collection and it has been successfully processed, then remove it if (FilesWithCostCentersWithoutHR.Contains(fileName)) { FilesWithCostCentersWithoutHR.Remove(fileName); } return(1); } else { int returnValue = 0; if (result == ApplicationConstants.COST_CENTER_WITHOUT_HOURLYRATE_CODE_FROM_SP) { msg = string.Format(ApplicationMessages.IMPORT_FILE_CC_WITHOUT_HR, cleanFileName); msg += "<BR>" + GetLogLink(Request.ApplicationPath, 1, idImport) + "<BR>" + GetDismissFile(Request.ApplicationPath, Path.GetFileName(fileName)); if (!FilesWithCostCentersWithoutHR.Contains(fileName)) { FilesWithCostCentersWithoutHR.Add(fileName); } returnValue = 0; } else { msg = string.Format(ApplicationMessages.IMPORT_FILE_ERROR, cleanFileName); if (FilesWithCostCentersWithoutHR.Contains(fileName)) { FilesWithCostCentersWithoutHR.Remove(fileName); } returnValue = -1; } if (NonExistingAssociates >= 0) { fileNameNonExistingAssociates = ConfigurationManager.AppSettings["UploadFolderCancelled"] + @"\" + ApplicationUtils.GetCleanFileName(Path.GetFileNameWithoutExtension(fileName)) + "_MissingAssociates.txt"; msg += string.Format(ApplicationMessages.UPLOAD_MISSING_ASSOCIATES, NonExistingAssociates + 1, fileNameNonExistingAssociates); } processedMessages.Add(msg); return(returnValue); } }
private int ProcessFiles(ref ArrayList processedMessages, ref List <int> importsWithCostCentersWithoutHR) { string fileName = string.Empty; // get the directory from appsettings string dirUrl = ConfigurationManager.AppSettings["UploadFolder"]; string dirPath = Server.MapPath(dirUrl); string shareName = @"\\" + Server.MachineName + @"\" + dirUrl; int iResult; if (!Directory.Exists(dirPath)) { processedMessages.Add(string.Format(ApplicationMessages.IMPORT_FOLDER_DOES_NOT_EXIST, dirUrl)); return(0); } int CountFileFailed = 0; //loop trough all files in grid foreach (GridDataItem item in grdProcessInformation.MasterTableView.Items) { fileName = shareName + @"\" + item["InProcessRealFileName"].Text; if (!File.Exists(fileName)) { //put message in arraylist to be shown on page processedMessages.Add(string.Format(ApplicationMessages.IMPORT_FILE_NOT_EXIST, item["InProcessFileName"].Text, shareName)); LogErrorToDataBase(string.Format(ApplicationMessages.IMPORT_FILE_NOT_EXIST, item["InProcessFileName"].Text, shareName), fileName); CountFileFailed++; continue; } FileInfo fi = new FileInfo(fileName); //if the file has 0 size if (fi.Length == 0) { processedMessages.Add(string.Format(ApplicationMessages.PROCESS_ZERO_SIZE, ApplicationUtils.GetCleanFileName(fileName))); LogErrorToDataBase(string.Format(ApplicationMessages.PROCESS_ZERO_SIZE, ApplicationUtils.GetCleanFileName(fileName)), fileName); MoveFileToDirectory(TargetDirectoryEnum.DIRECTORY_CANCELLED, fileName); CountFileFailed++; continue; } try { int idImport = 0; iResult = ProcessFile(fileName, ref processedMessages, ref idImport); if (iResult > 0) { MoveFileToDirectory(TargetDirectoryEnum.DIRECTORY_PROCESSED, fileName); } else if (iResult < 0) { CountFileFailed++; MoveFileToDirectory(TargetDirectoryEnum.DIRECTORY_CANCELLED, fileName); } else if (iResult == 0) { importsWithCostCentersWithoutHR.Add(idImport); } } catch (IndException ex) { processedMessages.Add(ex.Message); LogErrorToDataBase(ex.Message, fileName); MoveFileToDirectory(TargetDirectoryEnum.DIRECTORY_CANCELLED, fileName); continue; //we continue with the next file. } } return(CountFileFailed); }
/// <summary> /// put the data from imports table to actual data after constraints check /// </summary> private bool ProcessFile(string fileName, ref ArrayList processedMessages) { int idImport = ApplicationConstants.INT_NULL_VALUE; int result = ApplicationConstants.INT_NULL_VALUE; int resultFileExist = ApplicationConstants.INT_NULL_VALUE; string duplicateError = string.Empty; string cleanFileName = ApplicationUtils.GetCleanFileName(fileName); AnnualUpload annualUpload = new AnnualUpload(SessionManager.GetSessionValueNoRedirect(this.Page, SessionStrings.CONNECTION_MANAGER)); annualUpload.IdAssociate = currentUser.IdAssociate; try { annualUpload.FileName = fileName; resultFileExist = annualUpload.CheckFileAlreadyUploaded(); } catch (Exception ex) { processedMessages.Add(ApplicationMessages.IMPORT_CHECK_FILE_CRASH); LogErrorToDataBase(ex.Message, fileName); return(false); } if (resultFileExist < 0) { processedMessages.Add(string.Format(ApplicationMessages.IMPORT_FILE_ALREADY_UPLOADED, cleanFileName)); return(false); } try { idImport = annualUpload.WriteToAnnualImportTable(); } catch (Exception ex) { processedMessages.Add(string.Format(ApplicationMessages.IMPORT_FILE_ERROR, cleanFileName)); LogErrorToDataBase(ex.Message, fileName); return(false); } if (idImport < 0) { processedMessages.Add(string.Format(ApplicationMessages.IMPORT_FILE_ERROR, cleanFileName)); return(false); } try { annualUpload.IdImport = idImport; bool skipStartEndPhaseErrorsHours = false; bool skipStartEndPhaseErrorsCosts = false; bool skipStartEndPhaseErrorsSales = false; if (FilesWithSkipStartEndPhaseErrors.Contains(cleanFileName + "_Hours")) { skipStartEndPhaseErrorsHours = true; } if (FilesWithSkipStartEndPhaseErrors.Contains(cleanFileName + "_Costs")) { skipStartEndPhaseErrorsCosts = true; } if (FilesWithSkipStartEndPhaseErrors.Contains(cleanFileName + "_Sales")) { skipStartEndPhaseErrorsSales = true; } result = annualUpload.InsertIntoAnnualTable(skipStartEndPhaseErrorsHours, skipStartEndPhaseErrorsCosts, skipStartEndPhaseErrorsSales); } catch (IndException ex) { result = -100; if (((IndException)ex).BaseException is SqlException) { SqlException sqlBaseException = ((SqlException)((IndException)ex).BaseException); if (sqlBaseException.Number == 2627) //The exception number for primary key violation { duplicateError = "At least one record from the file " + cleanFileName + " already exists in the database."; } } if (string.IsNullOrEmpty(duplicateError)) { LogProcessErrorToDataBase(idImport, ex.Message, fileName); } else { LogProcessErrorToDataBase(idImport, duplicateError, fileName); } } if (result >= 0) { processedMessages.Add(string.Format(ApplicationMessages.IMPORT_FILE_PROCESSED, cleanFileName)); //If file was in this collection and it has been successfully processed, then remove it if (FilesWithSkipStartEndPhaseErrors.Contains(cleanFileName + "_Hours")) { FilesWithSkipStartEndPhaseErrors.Remove(cleanFileName + "_Hours"); } if (FilesWithSkipStartEndPhaseErrors.Contains(cleanFileName + "_Costs")) { FilesWithSkipStartEndPhaseErrors.Remove(cleanFileName + "_Costs"); } if (FilesWithSkipStartEndPhaseErrors.Contains(cleanFileName + "_Sales")) { FilesWithSkipStartEndPhaseErrors.Remove(cleanFileName + "_Sales"); } return(true); } else { processedMessages.Add(string.Format(ApplicationMessages.IMPORT_FILE_ERROR, cleanFileName)); if (result == ApplicationConstants.SKIP_START_END_PHASE_VALIDATION_HOURS) { if (!FilesWithSkipStartEndPhaseErrors.Contains(cleanFileName + "_Hours")) { FilesWithSkipStartEndPhaseErrors.Add(cleanFileName + "_Hours"); } } else if (result == ApplicationConstants.SKIP_START_END_PHASE_VALIDATION_SALES) { if (!FilesWithSkipStartEndPhaseErrors.Contains(cleanFileName + "_Sales")) { FilesWithSkipStartEndPhaseErrors.Add(cleanFileName + "_Sales"); } } else if (result == ApplicationConstants.SKIP_START_END_PHASE_VALIDATION_COSTS) { if (!FilesWithSkipStartEndPhaseErrors.Contains(cleanFileName + "_Costs")) { FilesWithSkipStartEndPhaseErrors.Add(cleanFileName + "_Costs"); } } else { //if (FilesWithSkipStartEndPhaseErrors.Contains(cleanFileName + "_Hours")) FilesWithSkipStartEndPhaseErrors.Remove(cleanFileName + "_Hours"); //if (FilesWithSkipStartEndPhaseErrors.Contains(cleanFileName + "_Costs")) FilesWithSkipStartEndPhaseErrors.Remove(cleanFileName + "_Costs"); //if (FilesWithSkipStartEndPhaseErrors.Contains(cleanFileName + "_Sales")) FilesWithSkipStartEndPhaseErrors.Remove(cleanFileName + "_Sales"); } return(false); } }