public ActionResult ImportStep2(PolicyCityImportStep1VM preImportCheckResultVM) { if (preImportCheckResultVM.ImportStep2VM.IsValidData == false) { //Check JSON for valid messages if (preImportCheckResultVM.ImportStep2VM.ReturnMessages[0] != null) { List <string> returnMessages = new List <string>(); var settings = new JsonSerializerSettings { StringEscapeHandling = StringEscapeHandling.EscapeHtml, }; List <string> returnMessagesJSON = JsonConvert.DeserializeObject <List <string> >(preImportCheckResultVM.ImportStep2VM.ReturnMessages[0], settings); foreach (string message in returnMessagesJSON) { string validMessage = Regex.Replace(message, @"[^À-ÿ\w\s&:._()\-]", ""); if (!string.IsNullOrEmpty(validMessage)) { returnMessages.Add(validMessage); } } preImportCheckResultVM.ImportStep2VM.ReturnMessages = returnMessages; } TempData["ErrorMessages"] = preImportCheckResultVM; return(RedirectToAction("ExportErrors")); } //PreImport Check Results (check has passed) PolicyCityImportStep2VM preImportCheckResult = new PolicyCityImportStep2VM(); preImportCheckResult = preImportCheckResultVM.ImportStep2VM; //Do the Import, return results PolicyCityImportStep3VM cdrPostImportResult = new PolicyCityImportStep3VM(); cdrPostImportResult = policyCityGroupItemRepository.Import( preImportCheckResult.FileBytes, preImportCheckResultVM.PolicyGroupId ); cdrPostImportResult.PolicyGroupId = preImportCheckResultVM.PolicyGroupId; TempData["CdrPostImportResult"] = cdrPostImportResult; //Pass Results to Next Page return(RedirectToAction("ImportStep3")); }
public ActionResult ImportStep1(PolicyCityImportStep1WithFileVM csvfile) { //used for return only PolicyGroup policyGroup = new PolicyGroup(); policyGroup = policyGroupRepository.GetGroup(csvfile.PolicyGroupId); csvfile.PolicyGroup = policyGroup; if (!ModelState.IsValid) { return(View(csvfile)); } string fileExtension = Path.GetExtension(csvfile.File.FileName); if (fileExtension != ".csv") // && fileExtension != ".xls" && fileExtension != ".xlsx") { ModelState.AddModelError("file", "This is not a valid entry"); return(View(csvfile)); } if (csvfile.File.ContentLength > 0) { PolicyCityImportStep2VM preImportCheckResult = new PolicyCityImportStep2VM(); List <string> returnMessages = new List <string>(); preImportCheckResult = policyCityGroupItemRepository.PreImportCheck(csvfile.File, csvfile.PolicyGroupId); PolicyCityImportStep1VM preImportCheckResultVM = new PolicyCityImportStep1VM(); preImportCheckResultVM.PolicyGroup = policyGroup; preImportCheckResultVM.ImportStep2VM = preImportCheckResult; preImportCheckResultVM.PolicyGroupId = csvfile.PolicyGroupId; TempData["PreImportCheckResultVM"] = preImportCheckResultVM; return(RedirectToAction("ImportStep2")); } return(View()); }
public PolicyCityImportStep2VM PreImportCheck(HttpPostedFileBase file, int policyGroupId) { //convert file to string so that we can parse int length = file.ContentLength; byte[] tempFile = new byte[length]; file.InputStream.Read(tempFile, 0, length); byte[] array = tempFile.ToArray(); System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding(); string fileToText = fileToText = enc.GetString(array); // Create the xml document container, this will be used to store the data after the checks XmlDocument doc = new XmlDocument(); XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", null, null); doc.AppendChild(dec); XmlElement root = doc.CreateElement("PAVIGs"); doc.AppendChild(root); List <string> returnMessages = new List <string>(); string returnMessage; int i = 0; //Split the CSV into lines string[] lines = fileToText.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries); //loop through CSV lines foreach (string line in lines) { i++; if (i > 1) //ignore first line with titles { string[] cells = line.Split(';'); //extract the data items from the file string CityCode = cells[0]; //Required string PolicyCityStatusDescription = cells[1]; //Required string enabledFlag = cells[2]; //(True / False)(1, 0) string enabledDate = cells[3]; //(YYYY / MM / DD) string expiryDate = cells[4]; //(YYYY / MM / DD) string travelDateValidFrom = cells[5]; //(YYYY / MM / DD) string travelDateValidTo = cells[6]; //(YYYY / MM / DD) //Build the XML Element for items XmlElement xmlPAVItem = doc.CreateElement("PAVIG"); XmlElement xmlCityCode = doc.CreateElement("CityCode"); xmlCityCode.InnerText = CityCode; xmlPAVItem.AppendChild(xmlCityCode); XmlElement xmlPolicyCityStatusDescription = doc.CreateElement("PolicyCityStatusDescription"); xmlPolicyCityStatusDescription.InnerText = PolicyCityStatusDescription; xmlPAVItem.AppendChild(xmlPolicyCityStatusDescription); XmlElement xmlEnabledDate = doc.CreateElement("EnabledDate"); xmlEnabledDate.InnerText = enabledDate; xmlPAVItem.AppendChild(xmlEnabledDate); XmlElement xmlExpiryDate = doc.CreateElement("ExpiryDate"); xmlExpiryDate.InnerText = expiryDate; xmlPAVItem.AppendChild(xmlExpiryDate); XmlElement xmlTravelDateValidFrom = doc.CreateElement("TravelDateValidFrom"); xmlTravelDateValidFrom.InnerText = travelDateValidFrom; xmlPAVItem.AppendChild(xmlTravelDateValidFrom); XmlElement xmlTravelDateValidTo = doc.CreateElement("TravelDateValidTo"); xmlTravelDateValidTo.InnerText = travelDateValidTo; xmlPAVItem.AppendChild(xmlTravelDateValidTo); //Validate data city status if (string.IsNullOrEmpty(PolicyCityStatusDescription) == true) { returnMessage = "Row " + i + ": Policy City Status Description is missing. Please provide a valid City Status"; if (!returnMessages.Contains(returnMessage)) { returnMessages.Add(returnMessage); } } PolicyCityStatusRepository policyCityStatusRepository = new PolicyCityStatusRepository(); PolicyCityStatus policyCity = policyCityStatusRepository.GetPolicyCityStatusByDescrition(PolicyCityStatusDescription); if (policyCity == null) { returnMessage = "Row " + i + ": City Status Description is invalid. Please provide a valid City Status"; if (!returnMessages.Contains(returnMessage)) { returnMessages.Add(returnMessage); } } else { PolicyCityStatusDescription = policyCity.PolicyCityStatusId.ToString(); } //Validate data city code if (string.IsNullOrEmpty(CityCode) == true) { returnMessage = "Row " + i + ": City Code is missing. Please provide a valid City Code"; if (!returnMessages.Contains(returnMessage)) { returnMessages.Add(returnMessage); } } else { CityRepository cityRepository = new CityRepository(); City city = cityRepository.GetCity(CityCode); if (city == null) { returnMessage = "Row " + i + ": City Code is invalid. Please provide a valid City Code"; if (!returnMessages.Contains(returnMessage)) { returnMessages.Add(returnMessage); } } } enabledFlag = enabledFlag.ToUpper(); if (enabledFlag != "TRUE" && enabledFlag != "FALSE" && enabledFlag != "1" && enabledFlag != "0" && enabledFlag != "") { returnMessage = "Row " + i + ": EnabledFlag value is not valid. Please provide a valid Enabled Flag"; if (!returnMessages.Contains(returnMessage)) { returnMessages.Add(returnMessage); } } if (enabledFlag == "TRUE" || string.IsNullOrEmpty(enabledFlag)) { enabledFlag = "1"; } if (enabledFlag == "FALSE") { enabledFlag = "0"; } XmlElement xmlEnabledFlag = doc.CreateElement("EnabledFlag"); xmlEnabledFlag.InnerText = enabledFlag; xmlPAVItem.AppendChild(xmlEnabledFlag); if (enabledDate != "" && enabledDate != "NULL") { DateTime enabledDateDT; if (DateTime.TryParseExact(enabledDate, "yyyy/MM/dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out enabledDateDT) == false) { returnMessage = "Row " + i + ": EnabledDate must be in the format YYYY/MM/DD. Please provide a valid date format"; if (!returnMessages.Contains(returnMessage)) { returnMessages.Add(returnMessage); } } } if (expiryDate != "" && expiryDate != "NULL") { DateTime enabledDateDT; if (DateTime.TryParseExact(expiryDate, "yyyy/MM/dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out enabledDateDT) == false) { returnMessage = "Row " + i + ": ExpiryDate must be in the format YYYY/MM/DD. Please provide a valid date format"; if (!returnMessages.Contains(returnMessage)) { returnMessages.Add(returnMessage); } } } if (travelDateValidFrom != "" && travelDateValidFrom != "NULL") { DateTime enabledDateDT; if (DateTime.TryParseExact(travelDateValidFrom, "yyyy/MM/dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out enabledDateDT) == false) { returnMessage = "Row " + i + ": TravelDateFrom must be in the format YYYY/MM/DD. Please provide a valid date format"; if (!returnMessages.Contains(returnMessage)) { returnMessages.Add(returnMessage); } } } if (travelDateValidTo != "" && travelDateValidTo != "NULL") { DateTime enabledDateDT; if (DateTime.TryParseExact(travelDateValidTo, "yyyy/MM/dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out enabledDateDT) == false) { returnMessage = "Row " + i + ": TravelDateTo must be in the format YYYY/MM/DD. Please provide a valid date format"; if (!returnMessages.Contains(returnMessage)) { returnMessages.Add(returnMessage); } } } //Attach the XML Element for an item to the Document root.AppendChild(xmlPAVItem); } } if (i == 0) { returnMessage = "There is no data in the file"; returnMessages.Add(returnMessage); } PolicyCityImportStep2VM preImportCheckResult = new PolicyCityImportStep2VM(); preImportCheckResult.ReturnMessages = returnMessages; if (returnMessages.Count != 0) { //preImportCheckResult.FileBytes = array; preImportCheckResult.IsValidData = false; } else { //DB Check string adminUserGuid = HttpContext.Current.User.Identity.Name.Split(new[] { '|' })[0]; var output = ( from n in dbHierarchyDC.spDesktopDataAdmin_UpdatePolicyCityGroupItemCount_v1( policyGroupId, System.Xml.Linq.XElement.Parse(doc.OuterXml), adminUserGuid ) select n).ToList(); foreach (spDesktopDataAdmin_UpdatePolicyCityGroupItemCount_v1Result message in output) { returnMessages.Add(message.MessageText.ToString()); } preImportCheckResult.FileBytes = array; preImportCheckResult.IsValidData = true; } return(preImportCheckResult); }