001package edu.pdx.cs.joy.grader;
002
003import edu.pdx.cs.joy.ParserException;
004import edu.pdx.cs.joy.grader.gradebook.*;
005
006import java.io.File;
007import java.io.PrintStream;
008import java.util.ArrayList;
009import java.util.Iterator;
010import java.util.List;
011
012/**
013 * This program imports a bunch of students into a grade book.  It is
014 * used at the beginning of the term when students submit their XML
015 * files using the {@link Survey} program.
016 *
017 * @author David Whitlock
018 * @version $Revision: 1.2 $
019 */
020public class ImportStudents {
021  private static final PrintStream out = System.out;
022  private static final PrintStream err = System.err;
023
024  /**
025   * Prints usage information for this program
026   */
027  private static void usage(String s) {
028    err.println("\n** " + s + "\n");
029    err.println("usage: java ImportStudents xmlFile (studentXml)+");
030    err.println("  xmlFile     The grade book's XML file");
031    err.println("  studentXml  A student's XML file");
032    err.println("");
033    err.println("This program imports student XML files into a ");
034    err.println("grade book.");
035    err.println("\n");
036
037    System.exit(1);
038  }
039
040  public static void main(String[] args) throws Throwable {
041    String xmlFileName = null;
042    List<String> studentXmlNames = new ArrayList<String>();
043
044    for (int i = 0; i < args.length; i++) {
045      if (xmlFileName == null) {
046        xmlFileName = args[i];
047
048      } else {
049        studentXmlNames.add(args[i]);
050      }
051    }
052
053    if (xmlFileName == null) {
054      usage("Missing grade book XML file");
055
056    } else if (studentXmlNames.isEmpty()) {
057      usage("Missing student XML file");
058    }
059
060    File xmlFile = new File(xmlFileName);
061    if (!xmlFile.exists()) {
062      usage("Grade book file \"" + xmlFile + "\" does not exist");
063    }
064
065    XmlGradeBookParser parser = new XmlGradeBookParser(xmlFile);
066    GradeBook book = parser.parse();
067    
068    Iterator iter = studentXmlNames.iterator();
069    while (iter.hasNext()) {
070      File studentFile = new File((String) iter.next());
071      if (!studentFile.exists()) {
072        err.println("** Student file \"" + studentFile +
073                    "\" does not exist");
074      }
075
076      XmlStudentParser sp = new XmlStudentParser(studentFile);
077      Student student;
078      try {
079        student = sp.parseStudent();
080
081      } catch (ParserException ex) {
082        err.println("** Could not parse student file \"" + studentFile
083                    + "\"");
084        continue;
085      }
086
087      String id = student.getId();
088      if (book.containsStudent(id)) {
089        err.println("** Grade book already contains \"" + id + "\"");
090
091      } else {
092        out.println("Importing " + student);
093        book.addStudent(student);
094      }
095    }
096
097    if (book.isDirty()) {
098      XmlDumper dumper = new XmlDumper(xmlFile);
099      dumper.dump(book);
100    }
101  }
102
103}