public ActionResult ImportStep1(TeamOutOfOfficeGroupImportStep1WithFileVM csvfile)
        {
            if (!ModelState.IsValid)
            {
                return(View(csvfile));
            }
            string fileExtension = Path.GetExtension(csvfile.File.FileName);

            if (fileExtension != ".csv")
            {
                ModelState.AddModelError("file", "This is not a valid entry");
                return(View(csvfile));
            }

            if (csvfile.File.ContentLength > 0)
            {
                TeamOutOfOfficeGroupImportStep2VM preImportCheckResult = new TeamOutOfOfficeGroupImportStep2VM();
                List <string> returnMessages = new List <string>();

                preImportCheckResult = teamOutOfOfficeGroupRepository.PreImportCheck(csvfile.File, csvfile.ClientSubUnitGuid);

                TeamOutOfOfficeGroupImportStep1VM preImportCheckResultVM = new TeamOutOfOfficeGroupImportStep1VM();
                preImportCheckResultVM.ImportStep2VM     = preImportCheckResult;
                preImportCheckResultVM.ClientSubUnitGuid = csvfile.ClientSubUnitGuid;

                TempData["PreImportCheckResultVM"] = preImportCheckResultVM;
                return(RedirectToAction("ImportStep2"));
            }

            return(View());
        }
        public ActionResult ImportStep2(TeamOutOfOfficeGroupImportStep1VM 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)
            TeamOutOfOfficeGroupImportStep2VM preImportCheckResult = new TeamOutOfOfficeGroupImportStep2VM();

            preImportCheckResult = preImportCheckResultVM.ImportStep2VM;

            //Do the Import, return results
            TeamOutOfOfficeGroupImportStep3VM postImportResult = new TeamOutOfOfficeGroupImportStep3VM();

            postImportResult = teamOutOfOfficeGroupRepository.Import(
                preImportCheckResult.FileBytes
                );

            TempData["PostImportResult"] = postImportResult;

            //Pass Results to Next Page
            return(RedirectToAction("ImportStep3"));
        }
Example #3
0
        public TeamOutOfOfficeGroupImportStep2VM PreImportCheck(HttpPostedFileBase file, string clientSubUnitGuid)
        {
            //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();

            //Prepare a list of error messages
            List <string> returnMessages = new List <string>();

            //Split the CSV into lines
            string[] lines = fileToText.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

            //Check Headers
            bool validHeaders = true;

            if (lines[0] != null)
            {
                Regex    csvParser = new Regex(",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))");
                string[] headers   = csvParser.Split(lines[0]);

                if (headers.Length < 2)
                {
                    string returnMessage = "File contains incorrect number of columns. File must contain columns for ClientSubUnitGuid, PrimaryBackupTeamId and optionally SecondaryBackupTeamId and TertiaryBackupTeamId";
                    returnMessages.Add(returnMessage);
                    validHeaders = false;
                }
            }

            //Validate CSV Lines
            if (validHeaders)
            {
                ValidateLines(ref doc, lines, ref returnMessages);
            }

            TeamOutOfOfficeGroupImportStep2VM preImportCheckResult = new TeamOutOfOfficeGroupImportStep2VM();

            preImportCheckResult.ReturnMessages = returnMessages;

            if (returnMessages.Count != 0)
            {
                preImportCheckResult.IsValidData = false;
            }
            else
            {
                //DB Check
                string adminUserGuid = HttpContext.Current.User.Identity.Name.Split(new[] { '|' })[0];

                var output = (
                    from n in db.spDesktopDataAdmin_UpdateTeamOutOfOfficeGroupCount_v1(
                        System.Xml.Linq.XElement.Parse(doc.OuterXml),
                        adminUserGuid
                        )
                    select n).ToList();

                foreach (spDesktopDataAdmin_UpdateTeamOutOfOfficeGroupCount_v1Result message in output)
                {
                    returnMessages.Add(message.MessageText.ToString());
                }

                preImportCheckResult.FileBytes = array;

                preImportCheckResult.IsValidData = true;
            }

            return(preImportCheckResult);
        }