//Import a csv and add its values to a new object public static void importCSV(string path) { StreamReader CSVReader = new StreamReader(path); while (!CSVReader.EndOfStream) { List <string> tokens = new List <string>(); string lineString = CSVReader.ReadLine(); string[] tokenArray = lineString.Split(new char[] { ',' }); for (int i = 0; i < tokenArray.Length; i++) { tokens.Add(tokenArray[i]); } if (tokens.ElementAt(0).Equals("")) { } else { //If the path is the employees path, gather the employee tokens, make a new employee, and add it to the main employees ArrayList. if (path.Contains("Employees")) { string empID = tokens.ElementAt(0); string fName = tokens.ElementAt(1); string lName = tokens.ElementAt(2); string empEmail = tokens.ElementAt(3); string hPhone = tokens.ElementAt(4); string cPhone = tokens.ElementAt(5); string empAddress = tokens.ElementAt(6); string empAddress2 = tokens.ElementAt(7); string empCity = tokens.ElementAt(8); string empState = tokens.ElementAt(9); string empZip = tokens.ElementAt(10); Employee newEmp = new Employee(empID, fName, lName, empEmail, hPhone, cPhone, empAddress, empAddress2, empCity, empState, empZip); MainForm.Employees.Add(newEmp); } //If the path is the employers path, gather the employer tokens, make a new employer, and add it to the main employers ArrayList. else if (path.Contains("Employer")) { string emplrID = tokens.ElementAt(0); string compName = tokens.ElementAt(1); string compEmail = tokens.ElementAt(2); string compPhone = tokens.ElementAt(3); string compAddress = tokens.ElementAt(4); string compAddress2 = tokens.ElementAt(5); string compCity = tokens.ElementAt(6); string compState = tokens.ElementAt(7); string compZip = tokens.ElementAt(8); string compRep = tokens.ElementAt(9); Employer newEmplr = new Employer(emplrID, compName, compEmail, compPhone, compAddress, compAddress2, compCity, compState, compZip, compRep); MainForm.Employers.Add(newEmplr); } //If the path is the field placements path, gather the tokens, make a new F.P., and add it to the respective employee. else if (path.Contains("Field Placement")) { string employerID = tokens.ElementAt(0); string employerName = tokens.ElementAt(1); string employer = employerID + " " + employerName; string employeeID = tokens.ElementAt(2); string employeeName = tokens.ElementAt(3); string employee = employeeID + " " + employeeName; FieldPlacement newFP = new FieldPlacement(employer, employee); MainForm.FieldPlacements.Add(newFP); for (int i = 0; i < MainForm.Employees.Count; i++) { Employee currentEmployee = MainForm.Employees.ElementAt(i); if (currentEmployee.name.Equals(employeeName)) { currentEmployee.company = employer; } } } //If the path is the evaluation path, gather the evaluation tokens, make a new evaluation, and add it to the respective employee. else if (path.Contains("Evaluation")) { string evalDate = tokens.ElementAt(0).Replace('_', '/'); string evalIdent = tokens.ElementAt(1); string employeeID = tokens.ElementAt(2); string employeeName = tokens.ElementAt(3); string employerID = tokens.ElementAt(4); string employerName = tokens.ElementAt(5); string nextEval = tokens.ElementAt(6); string workQualityScore = tokens.ElementAt(7); string workQualityComments = tokens.ElementAt(8); string workHabitScore = tokens.ElementAt(9); string workHabitComments = tokens.ElementAt(10); string jobKnowledgeScore = tokens.ElementAt(11); string jobKnowledgeComments = tokens.ElementAt(12); string behavior_Score = tokens.ElementAt(13); string behavior_Comments = tokens.ElementAt(14); string averageScore = tokens.ElementAt(15); string overallProgressScore = tokens.ElementAt(16); string overallProgComments = tokens.ElementAt(17); string recommended = tokens.ElementAt(18); for (int i = 0; i < MainForm.Employees.Count; i++) { Employee currentEmp = MainForm.Employees.ElementAt(i); string checkResult = employeeName; string matchResult = currentEmp.name; if (checkResult.Equals(matchResult)) { MainForm.Employees.ElementAt(i).employeeEvaluations.Add( new Evaluation(evalDate, evalIdent, employeeID, employeeName, employerID, employerName, nextEval, workQualityScore, workQualityComments, workHabitScore, workHabitComments, jobKnowledgeScore, jobKnowledgeComments, behavior_Score, behavior_Comments, averageScore, overallProgressScore, overallProgComments, recommended)); } } } } } CSVReader.Close(); }
//END OF SHORTCUTS //REPORT METHODS AND EVENTS //Method for saving the edited file public void saveFile(string path, string name) { if (!System.IO.Directory.Exists(mmtMainPath + @"/Backups")) { System.IO.Directory.CreateDirectory(mmtMainPath + @"/Backups"); } if (path.Contains("Employee")) { Employee foundEmployee = Tools.findEmployee(name); MainForm.Employees.Remove(foundEmployee); MainForm.Employees.Add(new Employee(empIDBox.Text, empFNameBox.Text, empLNameBox.Text, empEmailBox.Text, empHPhoneBox.Text, empCPhoneBox.Text, empAddressBox.Text, empAddressbox2.Text, empCityBox.Text, empStateBox.Text, empZipBox.Text)); /* The following sequence of code reads the employees CSV, finds the old line for the edited employee, * and if the old ID's match, don't write that csv line to the new csv file, otherwise write each employee * line to the new csv. Then, replace the contents of the original Employees.txt file with the contents * of the new employees CSV (The one sans unedited employee), delete the "new" employee csv and then * write the edited employee to the csv. Essentially, this sequence replaces the unedited employee with the edited employee. */ string backupPath = mmtMainPath + @"Backups\Employees_Backup.txt"; string newPath = mmtMainPath + "Employees1.txt"; string next = null; using (StreamReader reader = new StreamReader(path)) { using (StreamWriter writer = new StreamWriter(newPath)) { while ((next = reader.ReadLine()) != null) { string[] tokenArray = next.Split(new char[] { ',' }); if (String.Compare(tokenArray[0], foundEmployee.empID) == 0) { } else { if (tokenArray[0].Equals("")) { } else { writer.WriteLine(next); } } } } } string[] empData = { empIDBox.Text, empFNameBox.Text, empLNameBox.Text, empEmailBox.Text, empHPhoneBox.Text, empCPhoneBox.Text, empAddressBox.Text, empAddressbox2.Text, empCityBox.Text, empStateBox.Text, empZipBox.Text }; string finalEmpData = Tools.formatToCSV(empData); System.IO.File.AppendAllText(newPath, finalEmpData); System.IO.File.Replace(newPath, path, backupPath); empRepList.Items.Clear(); Tools.readAndBindEmpCustomObject(path, empRepList); } else if (path.Contains("Employer")) { Employer foundEmployer = Tools.findEmployer(name); MainForm.Employers.Remove(foundEmployer); MainForm.Employers.Add(new Employer(emplrIDBox.Text, compNameBox.Text, compEmailBox.Text, compPhoneBox.Text, compAddressBox.Text, compAddress2Box.Text, compCityBox.Text, compStateBox.Text, compZipBox.Text, compRepBox.Text)); string backupPath = mmtMainPath + @"Backups\Employers_Backup.txt"; string newPath = mmtMainPath + "Employers1.txt"; string next = null; using (StreamReader reader = new StreamReader(path)) { using (StreamWriter writer = new StreamWriter(newPath)) { while ((next = reader.ReadLine()) != null) { string[] tokenArray = next.Split(new char[] { ',' }); if (String.Compare(tokenArray[0], foundEmployer.emplrID) == 0) { } else { if (tokenArray[0].Equals("")) { } else { writer.WriteLine(next); } } } } } string[] emplrData = { emplrIDBox.Text, compNameBox.Text, compEmailBox.Text, compPhoneBox.Text, compAddressBox.Text, compAddress2Box.Text, compCityBox.Text, compStateBox.Text, compZipBox.Text, compRepBox.Text }; string finalEmplrData = Tools.formatToCSV(emplrData); System.IO.File.AppendAllText(newPath, finalEmplrData); System.IO.File.Replace(newPath, path, backupPath); emplerRepList.Items.Clear(); Tools.readAndBindEmplerCustomObject(path, emplerRepList); } }