Esempio n. 1
0
        private void UpdateParsingStatus(EligibilityUpload eligibilityUploadDomain, long statusId, bool isCompleted = true)
        {
            eligibilityUploadDomain.StatusId     = statusId;;
            eligibilityUploadDomain.ParseEndTime = DateTime.Now;
            eligibilityUploadDomain.ParseEndTime = isCompleted ? DateTime.Now : (DateTime?)null;

            _eligibilityUploadRepository.Save(eligibilityUploadDomain);
        }
        public ActionResult EligibilityUpload(HttpPostedFileBase eligibilityUploadFile, CustomerEligibilityUploadEditModel model)
        {
            try
            {
                if (Request.Files.Count < 1 || eligibilityUploadFile == 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));
                }

                try
                {
                    var organization = _organizationRepository.GetOrganizationbyId(model.CorporateAccountId);
                }
                catch (Exception ex)
                {
                    _logger.Error(string.Format("EligibilityUpload\nException occurred while retrieving account by accountId: {0}", model.CorporateAccountId));
                    _logger.Error(string.Format("Exception message: {0}\n\tStackTrace:{1}", ex.Message, ex.StackTrace));
                    model.FeedbackMessage = FeedbackMessageModel.CreateFailureMessage("Select valid corporate Account");
                    return(View(model));
                }

                var uploadMediaLocation = _mediaRepository.GetEligibilityUploadMediaFileLocation();

                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("EligibilityUpload\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 csvReader     = IoC.Resolve <ICsvReader>();
                var customerTable = csvReader.ReadWithTextQualifier(fullPath);
                if (customerTable.Rows.Count == 0)
                {
                    model.FeedbackMessage = FeedbackMessageModel.CreateFailureMessage("Uploaded file has no data.");

                    return(View(model));
                }

                var columns            = customerTable.Columns.Cast <DataColumn>().Select(x => x.ColumnName).ToArray();
                var missingColumnNames = _corporateUploadHelper.CheckAllEligibilityColumnExist(columns);
                if (!string.IsNullOrEmpty(missingColumnNames))
                {
                    model.FeedbackMessage = FeedbackMessageModel.CreateFailureMessage("Missing Column Name(s) : " + missingColumnNames);

                    return(View(model));
                }
                var files = new Core.Application.Domain.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("EligibilityUpload\nException occurred while saving info in TblFile."));
                    _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 eligibilityUpload = new EligibilityUpload
                {
                    FileId     = files.Id,
                    UploadTime = DateTime.Now,
                    UploadedBy = _sessionContext.UserSession.CurrentOrganizationRole.OrganizationRoleUserId,
                    AccountId  = model.CorporateAccountId,
                    StatusId   = (long)EligibilityUploadStatus.Uploaded
                };

                try
                {
                    eligibilityUpload = _eligibilityUploadRepository.Save(eligibilityUpload);
                }
                catch (Exception ex)
                {
                    _logger.Error(string.Format("EligibilityUpload\nException occurred while saving info in EligibilityUpload."));
                    _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 (eligibilityUpload != null && eligibilityUpload.Id > 0)
                {
                    ModelState.Clear();
                    //const string message = "File uploaded successfully";
                    return(RedirectToAction("EligibilityUpload", "MassRegistration", new { message = "File uploaded successfully" }));
                }

                model.FeedbackMessage = FeedbackMessageModel.CreateFailureMessage("File upload failed");
                return(View(model));
            }
            catch (Exception ex)
            {
                _logger.Error(string.Format("EligibilityUpload\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));
            }
        }