001package edu.pdx.cs410J.grader; 002 003import com.google.common.annotations.VisibleForTesting; 004import edu.pdx.cs410J.ParserException; 005import edu.pdx.cs410J.grader.gradebook.GradeBook; 006import edu.pdx.cs410J.grader.gradebook.XmlDumper; 007import edu.pdx.cs410J.grader.gradebook.XmlGradeBookParser; 008 009import java.io.*; 010 011public class FetchAndProcessGraderEmail { 012 013 public static void main(String[] args) { 014 String passwordFile = null; 015 String directoryName = null; 016 String gradeBookFile = null; 017 String whatToFetch = null; 018 019 for (String arg : args) { 020 if (passwordFile == null) { 021 passwordFile = arg; 022 023 } else if (directoryName == null) { 024 directoryName = arg; 025 026 } else if (gradeBookFile == null) { 027 gradeBookFile = arg; 028 029 } else if (whatToFetch == null) { 030 whatToFetch = arg; 031 032 } else { 033 usage("Extraneous argument: " + arg); 034 } 035 } 036 037 if (passwordFile == null) { 038 usage("Missing password file"); 039 } 040 041 if (directoryName == null) { 042 usage("Missing directory name"); 043 } 044 045 if (gradeBookFile == null) { 046 usage("Missing grade book file"); 047 } 048 049 if (whatToFetch == null) { 050 usage("Missing kind of email to fetch"); 051 } 052 053 String password = readGraderEmailPasswordFromFile(passwordFile); 054 File directory = getDirectory(directoryName); 055 GradeBook gradeBook = getGradeBook(gradeBookFile); 056 057 GraderEmailAccount account = new GraderEmailAccount("sjavata", password, m -> { }); 058 fetchAndProcessGraderEmails(whatToFetch, account, directory, gradeBook); 059 060 saveGradeBookIfModified(gradeBook, gradeBookFile); 061 } 062 063 @VisibleForTesting 064 static void fetchAndProcessGraderEmails(String whatToFetch, GraderEmailAccount account, File directory, GradeBook gradeBook) { 065 StudentEmailAttachmentProcessor processor = getStudentEmailAttachmentProcessor(whatToFetch, directory, gradeBook); 066 account.fetchAttachmentsFromUnreadMessagesInFolder(processor.getEmailFolder(), processor); 067 } 068 069 private static void saveGradeBookIfModified(GradeBook gradeBook, String gradeBookFile) { 070 if (gradeBook.isDirty()) { 071 File file = new File(gradeBookFile); 072 try { 073 XmlDumper dumper = new XmlDumper(file); 074 dumper.dump(gradeBook); 075 076 } catch (IOException e) { 077 usage("Can't write grade book in \"" + gradeBookFile + "\""); 078 } 079 } 080 } 081 082 private static StudentEmailAttachmentProcessor getStudentEmailAttachmentProcessor(String whatToFetch, File directory, GradeBook gradeBook) { 083 if (whatToFetch.equalsIgnoreCase("projects")) { 084 return new ProjectSubmissionsProcessor(directory, gradeBook); 085 086 } else if (whatToFetch.equalsIgnoreCase("surveys")) { 087 return new SurveySubmissionsProcessor(directory, gradeBook); 088 089 } else if (whatToFetch.equalsIgnoreCase("androidProjects")) { 090 return new AndroidProjectSubmissionsProcessor(directory, gradeBook); 091 092 } else { 093 return usage("Cannot fetch \"" + whatToFetch + "\""); 094 } 095 } 096 097 private static GradeBook getGradeBook(String gradeBookFile) { 098 try { 099 XmlGradeBookParser parser = new XmlGradeBookParser(gradeBookFile); 100 return parser.parse(); 101 102 } catch (IOException ex) { 103 return usage("Couldn't read grade book file \"" + gradeBookFile + "\""); 104 105 } catch (ParserException ex) { 106 return usage("Couldn't parse grade book file \"" + gradeBookFile + "\""); 107 } 108 } 109 110 private static File getDirectory(String directoryName) { 111 File directory = new File(directoryName); 112 if (!directory.exists()) { 113 return usage("Download directory \"" + directoryName + "\" does not exist"); 114 } 115 116 if (!directory.isDirectory()) { 117 return usage("Download directory \"" + directoryName + "\" is not a directory"); 118 } 119 return directory; 120 } 121 122 private static String readGraderEmailPasswordFromFile(String passwordFile) { 123 File file = new File(passwordFile); 124 if (!file.exists()) { 125 return usage("Password file \"" + passwordFile + "\" does not exist"); 126 } 127 128 try { 129 BufferedReader br = new BufferedReader(new FileReader(file)); 130 return br.readLine(); 131 132 } catch (IOException e) { 133 return usage("Exception while reading password file"); 134 } 135 } 136 137 private static <T> T usage(String message) { 138 PrintStream err = System.err; 139 err.println("** " + message); 140 err.println(); 141 err.println("FetchAndProcessGraderEmail passwordFile directory gradeBookFile whatToFetch"); 142 err.println(" passwordFile File containing the password for the Grader's email account"); 143 err.println(" directory The directory in which to download attachments"); 144 err.println(" gradeBookFile Grade Book XML file related to this submission"); 145 err.println(" whatToFetch What kind of student emails should be fetched"); 146 err.println(" projects Project submissions"); 147 err.println(" surveys Student surveys"); 148 err.println(); 149 150 System.exit(1); 151 152 throw new IllegalStateException("Shouldn't reach here"); 153 } 154}