001package edu.pdx.cs410J.grader.poa;
002
003import com.google.common.eventbus.EventBus;
004import com.google.common.eventbus.Subscribe;
005import com.google.inject.Inject;
006import edu.pdx.cs410J.ParserException;
007import edu.pdx.cs410J.grader.gradebook.GradeBook;
008import edu.pdx.cs410J.grader.gradebook.XmlDumper;
009import edu.pdx.cs410J.grader.gradebook.XmlGradeBookParser;
010
011import java.io.File;
012import java.io.IOException;
013
014public class GradeBookFileManager {
015  private final EventBus bus;
016  private File file;
017
018  @Inject
019  public GradeBookFileManager(EventBus bus) {
020    this.bus = bus;
021    this.bus.register(this);
022  }
023
024  @Subscribe
025  public void loadGradeBookFromFile(LoadGradeBook event) throws IOException, ParserException {
026    file = event.getFile();
027    XmlGradeBookParser parser = new XmlGradeBookParser(file);
028    GradeBook book = parser.parse();
029
030    this.bus.post(new GradeBookLoaded(book));
031  }
032
033  @Subscribe
034  public void writeGradeBookToFile(SaveGradeBook event) throws IOException {
035    XmlDumper dumper = new XmlDumper(this.file);
036    dumper.dump(event.getGradeBook());
037  }
038}