Exemplo n.º 1
0
        /// <summary>
        /// Import a CSV file existing on disk, the path of the file to import could be provided by the console call as an argument or could be read from the program. Once validated that we can access the file all students are validated and verified if they don't already exists on the database, the if any student could be imported is saved if the users wants.
        /// </summary>
        /// <param name="path">Path of the file to import provided as an argument at execution time</param>
        private static void ImportCSVFile(string path = "")
        {
            if (string.IsNullOrEmpty(path))
            {
                Console.WriteLine("Please introduce the path of the CSV file which contains the students to load: ");
                path = Console.ReadLine();
            }

            if (File.Exists(path) && Path.GetExtension(path) == ".csv")
            {
                var content = File.ReadAllLines(path);
                if (content != null && content.Length > 0)
                {
                    bool hasErrors       = false;
                    bool hasDuplicates   = false;
                    var  currentStudents = StudentMapper.GetAllWhere(string.Empty);
                    currentStudents.Sort();
                    var studentsToImport = new List <Student>();
                    int errorCount       = 0;
                    for (int position = 0; position < content.Length; position++)
                    {
                        string[] parts = content[position].Split(',');
                        if (parts.Length == 4)
                        {
                            StudentType type          = StudentType.Elementary;
                            bool        validType     = Enum.TryParse(parts[0].ToString(), out type);
                            DateTime    updatedOn     = DateTime.Now;
                            bool        validDateTime = DateTime.TryParseExact(parts[3], "yyyyMMddHHmmss", CultureInfo.InvariantCulture, DateTimeStyles.None, out updatedOn);
                            string      name          = null;
                            if (parts[1] != null)
                            {
                                name = parts[1].Trim();
                            }
                            string gender = null;
                            if (parts[2] != null)
                            {
                                gender = parts[2].Trim().ToUpper();
                            }

                            if (validType && validDateTime && !string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(gender) && (gender == "M" || gender == "F"))
                            {
                                Student student = new Student();
                                student.Type      = type;
                                student.Name      = name;
                                student.Gender    = gender == "M" ? "Male" : "Female";
                                student.Enabled   = true;
                                student.UpdatedOn = updatedOn;
                                if (!currentStudents.Contains(student, new StudentsComparer()))
                                {
                                    studentsToImport.Add(student);
                                }
                                else
                                {
                                    string msg = string.Format("There is a duplicated student in row {0}. Please verify the data if you want before import.", position + 1);
                                    if (errorCount < MaxImportErrorsToShow)
                                    {
                                        Console.WriteLine(msg);
                                        Logger.Create().Message(msg);
                                    }

                                    hasDuplicates = true;
                                    errorCount++;
                                }
                            }
                            else
                            {
                                string msg = string.Format("There is an error with your data in row {0}. Please verify that the Type is (Kinder, Elementary, High, University), The name is not empty, Gender is M(Male) or F(Female) and you placed a valid time stamp in the following format <year><month><day><hour><minute><second> for example the representation for December 31st, 2013 14:59:34 is 20131231145934. All this FOUR values must be in a valid CSV format.", position + 1);
                                if (errorCount < MaxImportErrorsToShow)
                                {
                                    Console.WriteLine(msg);
                                    Logger.Create().Message(msg);
                                }

                                hasErrors = true;
                                errorCount++;
                            }
                        }
                        else
                        {
                            string msg = string.Format("There is an error with your data in row {0}. Please verify that the Type is (Kinder, Elementary, High, University), The name is not empty, Gender is M(Male) or F(Female) and you placed a valid time stamp in the following format <year><month><day><hour><minute><second> for example the representation for December 31st, 2013 14:59:34 is 20131231145934. All this FOUR values must be in a valid CSV format.", position + 1);
                            if (errorCount < MaxImportErrorsToShow)
                            {
                                Console.WriteLine(msg);
                                Logger.Create().Message(msg);
                            }

                            hasErrors = true;
                            errorCount++;
                        }

                        if (position % 10000 == 0)
                        {
                            Console.WriteLine("We are processing your data, please wait ...");
                        }
                    } //End of the processing CSV file

                    string message = string.Empty;
                    if (hasErrors)
                    {
                        message += "We found some errors processing your file, above we show some of them. ";
                    }
                    if (hasDuplicates)
                    {
                        message += "We found some duplicated students when processing your file, above we show some of them.";
                    }

                    if (studentsToImport.Count > 0)
                    {
                        if (!string.IsNullOrEmpty(message))
                        {
                            Console.WriteLine(message + string.Format(" The total of errors/duplicates found was {0}. Even if you have errors and duplicates we found some students which can be imported.", errorCount));
                        }

                        Console.WriteLine(string.Format("You have {0} students to import, do you wish to insert them to the database? (Y/N) [Y]: ", studentsToImport.Count));
                        string answer = Console.ReadLine();

                        if (string.IsNullOrEmpty(answer) || answer[0].ToString().ToUpper() == "Y")
                        {
                            int count = 0;
                            foreach (var student in studentsToImport)
                            {
                                if (StudentMapper.InsertFull(student))
                                {
                                    count++;
                                    if (count % 10000 == 0)
                                    {
                                        Console.WriteLine("We are saving your data, please wait ...");
                                    }
                                }
                                else
                                {
                                    Logger.Create().Message("We couldn't save the student: " + student.ToString());
                                }
                            }

                            ShowContinueMessage(string.Format("{0} students were saved of {1}. {2}", count, studentsToImport.Count, (count < studentsToImport.Count ? "Some students were not saved, please review the log file to see more details." : "")));
                        }
                        else
                        {
                            if (!string.IsNullOrEmpty(answer) && !(answer[0].ToString().ToUpper() == "Y" || answer[0].ToString().ToUpper() == "N"))
                            {
                                ShowContinueMessage("You should choose Y for yes or N for no, please try again.");
                            }
                        }
                    }
                    else
                    {
                        ShowContinueMessage(message + " After the processing of your file we couldn't find any students to import, please review your data and try again.");
                    }
                }
                else
                {
                    ShowContinueMessage("We couldn't read anything from your file, please check it and try again");
                }
            }
            else
            {
                ShowContinueMessage("Please verify your path and try again, be sure to provide a valid CSV file!");
            }
        }