public StaffEventScheduleUploadListModel Create(IEnumerable <FileModel> fileModels, IEnumerable <StaffEventScheduleUpload> staffEventScheduleUpload, IEnumerable <OrderedPair <long, string> > uploadedbyAgentNameIdPair) { var model = new StaffEventScheduleUploadListModel(); var collection = new List <StaffEventScheduleUploadViewModel>(); staffEventScheduleUpload.ToList().ForEach(eu => { var agentName = "N/A"; if (uploadedbyAgentNameIdPair != null && uploadedbyAgentNameIdPair.Any()) { agentName = uploadedbyAgentNameIdPair.Single(ap => ap.FirstValue == eu.UploadedByOrgRoleUserId).SecondValue; } var successFile = (from f in fileModels where f.Id == eu.FileId select f).Single(); FileModel failedFile = null; if (eu.LogFileId.HasValue) { failedFile = (from f in fileModels where f.Id == eu.LogFileId select f).Single(); } var eligibilityUploadViewModel = new StaffEventScheduleUploadViewModel { File = successFile, SuccessfullCustomer = eu.SuccessUploadCount, FailedCustomer = eu.FailedUploadCount, UploadTime = eu.UploadTime, FailedFile = failedFile, Status = ((StaffEventScheduleParseStatus)eu.StatusId).GetDescription(), UploadedBy = agentName, }; collection.Add(eligibilityUploadViewModel); }); model.Collection = collection; return(model); }
public ActionResult ScheduleUpload(string message) { var model = new StaffEventScheduleUploadViewModel { SampleCsvMediaUrl = _mediaRepository.GetSamplesLocation().Url + "StaffEventScheduleUploadSample.csv", SampleRoleUrl = _mediaRepository.GetSamplesLocation().Url + "StaffEventScheduleRoleSample.csv" }; if (message != null) { model.FeedbackMessage = FeedbackMessageModel.CreateSuccessMessage(message); } return(View(model)); }
public ActionResult ScheduleUpload(HttpPostedFileBase scheduleUploadFile) { var model = new StaffEventScheduleUploadViewModel { FeedbackMessage = new FeedbackMessageModel(), SampleCsvMediaUrl = _mediaRepository.GetSamplesLocation().Url + "StaffEventScheduleUploadSample.csv" }; try { if (Request.Files.Count < 1 || scheduleUploadFile == null) { model.FeedbackMessage = FeedbackMessageModel.CreateFailureMessage("No file has been uploaded. Please upload a csv file."); return(View(model)); } HttpPostedFileBase file = Request.Files[0]; var fileExtension = file.FileName.Split('.'); if ((fileExtension.Length >= 2 && fileExtension[fileExtension.Length - 1].ToLower() != "csv") || fileExtension.Length < 2) { model.FeedbackMessage = FeedbackMessageModel.CreateFailureMessage("File uploaded is not a CSV"); return(View(model)); } var uploadMediaLocation = _mediaRepository.GetStaffScheduleUploadMediaFileLocation(); var physicalPath = uploadMediaLocation.PhysicalPath; var fileUploadedName = (Path.GetFileNameWithoutExtension(file.FileName) + Path.GetExtension(file.FileName)).Replace("'", "").Replace("&", ""); var fileName = (Path.GetFileNameWithoutExtension(fileUploadedName) + "_" + DateTime.Now.ToString("MMddyyyyhhmmss") + Path.GetExtension(fileUploadedName)).Replace("'", "").Replace("&", ""); var fullPath = physicalPath + fileName; try { file.SaveAs(fullPath); } catch (Exception ex) { _logger.Error(string.Format("Staff Event Schedule Upload\nException occurred while saving file on server. FileName:{0} Path: {1}", fileUploadedName, fullPath)); _logger.Error(string.Format("Exception message: {0}\n\tStackTrace:{1}", ex.Message, ex.StackTrace)); model.FeedbackMessage = FeedbackMessageModel.CreateFailureMessage("Some internal error occurred"); return(View(model)); } var staffScheduleTable = _csvReader.ReadWithTextQualifier(fullPath); if (staffScheduleTable.Rows.Count == 0) { model.FeedbackMessage = FeedbackMessageModel.CreateFailureMessage("Uploaded file has no data."); return(View(model)); } var columns = staffScheduleTable.Columns.Cast <DataColumn>().Select(x => x.ColumnName).ToArray(); var missingColumnNames = CheckAllScheduleUploadColumnExist(columns); if (!string.IsNullOrEmpty(missingColumnNames)) { model.FeedbackMessage = FeedbackMessageModel.CreateFailureMessage("Missing Column Name(s) : " + missingColumnNames); return(View(model)); } var files = new File { Path = fileName, FileSize = file.ContentLength, Type = FileType.Csv, UploadedBy = new OrganizationRoleUser(_sessionContext.UserSession.CurrentOrganizationRole.OrganizationRoleUserId), UploadedOn = DateTime.Now }; try { files = _fileRepository.Save(files); } catch (Exception ex) { _logger.Error(string.Format("Staff Event Schedule Upload\nException occurred while saving info in File table.")); _logger.Error(string.Format("Exception message: {0}\n\tStackTrace:{1}", ex.Message, ex.StackTrace)); model.FeedbackMessage = FeedbackMessageModel.CreateFailureMessage("Some internal error occurred"); return(View(model)); } var staffAssignmentUpload = new StaffEventScheduleUpload { FileId = files.Id, UploadTime = DateTime.Now, UploadedByOrgRoleUserId = _sessionContext.UserSession.CurrentOrganizationRole.OrganizationRoleUserId, StatusId = (long)StaffEventScheduleParseStatus.Uploaded }; try { staffAssignmentUpload = _staffEventScheduleUploadRepository.Save(staffAssignmentUpload); } catch (Exception ex) { _logger.Error(string.Format("Staff Event Schedule Upload\nException occurred while saving info in StaffEventScheduleUpload.")); _logger.Error(string.Format("Exception message: {0}\n\tStackTrace:{1}", ex.Message, ex.StackTrace)); model.FeedbackMessage = FeedbackMessageModel.CreateFailureMessage("Some internal error occurred"); return(View(model)); } if (staffAssignmentUpload != null && staffAssignmentUpload.Id > 0) { ModelState.Clear(); return(RedirectToAction("ScheduleUpload", "Schedule", new { message = "File uploaded successfully" })); } model.FeedbackMessage = FeedbackMessageModel.CreateFailureMessage("File upload failed"); return(View(model)); } catch (Exception ex) { _logger.Error(string.Format("Staff Event Schedule Upload\nException occurred")); _logger.Error(string.Format("Exception message: {0}\n\tStackTrace:{1}", ex.Message, ex.StackTrace)); model.FeedbackMessage = FeedbackMessageModel.CreateFailureMessage("Some internal error occurred"); return(View(model)); } }