/// <summary> /// Parse incoming keys from CSV file. /// </summary> public ParsedCsvData <Tuple <string, string> > ParseKeys() { var parsedKeys = new ParsedCsvData <Tuple <string, string> >(); int currentLineNumber = 0; using (var reader = new StreamReader(FileData)) { while (!reader.EndOfStream) { var line = reader.ReadLine(); var values = line.Split(Delimiter); try { var key = Tuple.Create(values[0], values[1]); parsedKeys.ValidList.Add(key); } catch (Exception e) { parsedKeys.InvalidList.Add(new InvalidCsvEntry(currentLineNumber, line)); } currentLineNumber++; } } return(parsedKeys); }
/// <summary> /// Parse product names from file /// </summary> public ParsedCsvData <ProductName> ParseProducts() { var parsedProducts = new ParsedCsvData <ProductName>(); int currentLineNumber = 0; using (var reader = new StreamReader(FileData)) { while (!reader.EndOfStream) { var line = reader.ReadLine(); var productName = new ProductName() { QuantityLimit = 1, Name = line, ActiveStatus = "Actived" }; parsedProducts.ValidList.Add(productName); currentLineNumber++; //TODO: How should we check for validation? } return(parsedProducts); } }
/// <summary> /// Parse keys. If a key is in the format 'productName;key' (or with a different delimiter) /// it'll be added to the ValidList. If there are too many or too few values, the values /// will be added to the invalid list. /// </summary> /// <returns>ParsedCsvData with valid keys as tuples to be added to db in controller</returns> public ParsedCsvData <Tuple <string, string> > ParseKeys() { var parsedKeys = new ParsedCsvData <Tuple <string, string> >(); int currentLineNumber = 0; using (var reader = new StreamReader(FileData)) { while (!reader.EndOfStream) { var line = reader.ReadLine(); var values = line.Split(Delimiter); if (values.Length == 2) { var key = Tuple.Create(values[0], values[1]); parsedKeys.ValidList.Add(currentLineNumber.ToString(), key); } else { parsedKeys.InvalidList.Add(currentLineNumber.ToString(), line); } currentLineNumber++; } } return(parsedKeys); }
/// <summary> /// Parse incoming students from CSV file. /// </summary> /// <returns>Parsed data with successful results in form of 'eligible student'</returns> public ParsedCsvData <EligibleStudent> ParseStudents() { using (var reader = new StreamReader(FileData)) { //SETUP ParsedCsvData <EligibleStudent> parsedStudents = new ParsedCsvData <EligibleStudent>(); int currentLineNumber = 0; while (!reader.EndOfStream) { var line = reader.ReadLine(); var values = line.Split(Delimiter); try { //Validate student number by attempting parse var studentNumber = Int32.Parse(values[0]); //Validate student has proper Mohawk email var studentEmail = values[3].ToLower(); var emailValidated = Regex.Match(studentEmail, patterns["email"]); //First&Last name are not check vs email; some students may have different names the one in their email //However, names should not include numbers var firstNameValidated = Regex.Match(values[1], patterns["name"]); var lastNameValidated = Regex.Match(values[2], patterns["name"]); //Student number should be < 9 digits and > 0, other fields must match regex. if (studentNumber < 1000000000 && studentNumber > 0 && emailValidated.Success && firstNameValidated.Success && lastNameValidated.Success) { var eligible = new EligibleStudent() { StudentID = studentNumber, FirstName = values[1], LastName = values[2], StudentEmail = values[3] }; parsedStudents.ValidList.Add(currentLineNumber.ToString(), eligible); } else { throw new Exception("Invalid format."); } } catch (Exception e) { parsedStudents.InvalidList.Add(currentLineNumber.ToString(), line); } currentLineNumber++; } return(parsedStudents); } }
/// <summary> /// Parse incoming students from CSV file. /// </summary> public ParsedCsvData <EligibleStudent> ParseStudents() { using (var reader = new StreamReader(FileData)) { //SETUP ParsedCsvData <EligibleStudent> parsedStudents = new ParsedCsvData <EligibleStudent>(); int currentLineNumber = 0; while (!reader.EndOfStream) { var line = reader.ReadLine(); var values = line.Split(Delimiter); try { //Validate student number by attempting parse var studentNumber = Int32.Parse(values[0]); //Validate student has proper Mohawk email var studentEmail = values[3].ToLower(); var emailPattern = "[a-z]*.[a-z0-9]*\\@mohawkcollege.ca"; var emailValidated = Regex.Match(studentEmail, emailPattern); //First&Last name are not validated; some students may have different names the one in their email //Student number should be < 9 digits, email must pass regex if (studentNumber < 1000000000 && emailValidated.Success) { var eligible = new EligibleStudent() { StudentID = studentNumber, FirstName = values[1], LastName = values[2], StudentEmail = values[3] }; parsedStudents.ValidList.Add(eligible); } } catch (Exception e) { parsedStudents.InvalidList.Add(new InvalidCsvEntry(currentLineNumber, line)); } currentLineNumber++; } return(parsedStudents); } }
/// <summary> /// Parses products based on list. /// </summary> /// <returns>Parsed data containing list of valid and invalid Product names </returns> public ParsedCsvData <Product> ParseProducts() { var parsedProducts = new ParsedCsvData <Product>(); int currentLineNumber = 0; using (var reader = new StreamReader(FileData)) { while (!reader.EndOfStream) { var line = reader.ReadLine(); var validatedProductName = Regex.Match(line, patterns["productName"]); bool foundDelimiter = line.IndexOf(Delimiter) > -1; //Quick check to ensure we're not uploading the wrong file... if (line.Length > 0 && validatedProductName.Success && !foundDelimiter) { var product = new Product() { QuantityLimit = 1, Name = line, ActiveStatus = "Active" }; parsedProducts.ValidList.Add(currentLineNumber.ToString(), product); } else { parsedProducts.InvalidList.Add(currentLineNumber.ToString(), line); } currentLineNumber++; } return(parsedProducts); } }