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