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 com.google.inject.Singleton;
007
008import java.time.LocalDateTime;
009import java.time.format.DateTimeFormatter;
010
011import static edu.pdx.cs410J.grader.poa.POASubmissionView.CouldNotParseContent;
012import static edu.pdx.cs410J.grader.poa.POASubmissionView.POAContentType;
013
014@Singleton
015public class POASubmissionPresenter {
016  private final POASubmissionView view;
017  private final EventBus bus;
018
019  @Inject
020  public POASubmissionPresenter(EventBus bus, POASubmissionView view) {
021    this.view = view;
022
023    bus.register(this);
024    this.bus = bus;
025  }
026
027  @Subscribe
028  public void displayPOASubmissionInformation(POASubmissionSelected event) {
029    POASubmission submission = event.getSubmission();
030
031    this.view.setSubmissionSubject(submission.getSubject());
032    this.view.setSubmissionSubmitter(submission.getSubmitter());
033    this.view.setSubmissionTime(formatSubmissionTime(submission.getSubmitTime()));
034    String contentType = submission.getContentType();
035    boolean isHtml = contentType.toLowerCase().contains("text/html");
036
037    try {
038      this.view.setContent(submission.getContent(), isHtml ? POAContentType.HTML : POAContentType.TEXT);
039
040    } catch (CouldNotParseContent ex) {
041      this.bus.post(new UnhandledExceptionEvent(ex));
042    }
043  }
044
045  static String formatSubmissionTime(LocalDateTime submitTime) {
046    return submitTime.format(DateTimeFormatter.ofPattern("E M/d/yyyy h:mm a"));
047  }
048}