Exemple #1
0
        private void UpdateParsingStatus(StaffEventScheduleUpload domain, long statusId, bool isCompleted = true)
        {
            domain.StatusId     = statusId;;
            domain.ParseEndTime = DateTime.Now;
            domain.ParseEndTime = isCompleted ? DateTime.Now : (DateTime?)null;

            _staffEventScheduleUploadRepository.Save(domain);
        }
Exemple #2
0
        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));
            }
        }