001package edu.pdx.cs410J.grader; 002 003import edu.pdx.cs410J.ParserException; 004import edu.pdx.cs410J.grader.gradebook.GradeBook; 005import edu.pdx.cs410J.grader.gradebook.Student; 006import edu.pdx.cs410J.grader.gradebook.XmlGradeBookParser; 007 008import java.io.*; 009import java.util.Comparator; 010import java.util.Iterator; 011import java.util.SortedSet; 012import java.util.TreeSet; 013 014/** 015 * This program generates a .mailrc file containing a mail alias for 016 * every student in a given grade book. 017 */ 018public class GenerateMailrc { 019 020 /** 021 * Generates a mailrc file for the students in a given grade book. 022 */ 023 static void generateMailrc(GradeBook book, PrintWriter mailrc) { 024 mailrc.println("# Mail aliases for " + book.getClassName()); 025 026 // Sort all of the students by their id 027 SortedSet<Student> sorted = new TreeSet<Student>(new Comparator<Student>() { 028 public int compare(Student o1, Student o2) { 029 String id1 = o1.getId(); 030 String id2 = o2.getId(); 031 return id1.compareTo(id2); 032 } 033 }); 034 035 book.forEachStudent(sorted::add); 036 037 Iterator iter = sorted.iterator(); 038 while (iter.hasNext()) { 039 // alias id "email (firstName LastName)" 040 Student student = (Student) iter.next(); 041 String email = student.getEmail(); 042 if (email != null) { 043 mailrc.print("alias " + student.getId() + " \""); 044 StringBuffer name = new StringBuffer(); 045 046 if (student.getNickName() != null) { 047 name.append(student.getNickName()); 048 name.append(" "); 049 050 } else if (student.getFirstName() != null) { 051 name.append(student.getFirstName()); 052 name.append(" "); 053 } 054 if(student.getLastName() != null) { 055 name.append(student.getLastName()); 056 } 057 if(!name.toString().equals("")) { 058 mailrc.print(name.toString().trim() + " <" + email + ">"); 059 } 060 mailrc.println("\""); 061 } 062 } 063 } 064 065 /** 066 * Displays usage information for this program 067 */ 068 private static void usage() { 069 PrintStream err = System.err; 070 err.println("usage: java GenerateMailrc gradeBookFile mailrcFile"); 071 System.exit(1); 072 } 073 074 /** 075 * Main program. 076 */ 077 public static void main(String[] args) { 078 if (args.length < 2) { 079 System.err.println("** Missing arguments"); 080 usage(); 081 } 082 083 String bookName = args[0]; 084 String mailrcName = args[1]; 085 086 GradeBook book = null; 087 PrintWriter mailrc = null; 088 try { 089 XmlGradeBookParser parser = new XmlGradeBookParser(bookName); 090 book = parser.parse(); 091 mailrc = new PrintWriter(new FileWriter(mailrcName), true); 092 093 } catch (FileNotFoundException ex) { 094 System.err.println("** Could not find file: " + ex.getMessage()); 095 System.exit(1); 096 097 } catch (IOException ex) { 098 System.err.println("** IOException during parsing: " + ex.getMessage()); 099 System.exit(1); 100 101 } catch (ParserException ex) { 102 System.err.println("** Error during parsing: " + ex); 103 System.exit(1); 104 } 105 106 generateMailrc(book, mailrc); 107 } 108 109}