<commit_before>package edu.pdx.cs410J.grader;

import edu.pdx.cs410J.ParserException;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

public class StudentFileMailer extends EmailSender {

  private final GradeBook gradeBook;
  private final String subject;
  private final Session session;

  public StudentFileMailer(GradeBook gradeBook, String subject, Session session) {
    this.gradeBook = gradeBook;
    this.subject = subject;
    this.session = session;
  }

  public static void main(String[] args) {
    String gradeBookFileName = null;
    String subject = null;
    List<String> fileNames = new ArrayList<>();

    for (String arg : args) {
      if (gradeBookFileName == null) {
        gradeBookFileName = arg;

      } else if (subject == null) {
        subject = arg;

      } else {
        fileNames.add(arg);
      }
    }

    usageIfNull(gradeBookFileName, "Missing grade book file name");
    usageIfNull(subject, "Missing email subject");
    usageIfEmpty(fileNames, "Missing file names");
    List<File> files = getFiles(fileNames);
    GradeBook gradeBook = getGradeBook(gradeBookFileName);
    List<Student> students = getStudentsFromFiles(files, gradeBook);

    assert files.size() == students.size();

    StudentFileMailer mailer = new StudentFileMailer(gradeBook, subject, newEmailSession(false));
    for (int i = 0; i < files.size(); i++) {
      File file = files.get(i);
      Student student = students.get(i);
      try {
        mailer.mailFileToStudent(file, student);

      } catch (MessagingException ex) {
        System.err.println("While mailing \"" + file + "\" to " + student);
        ex.printStackTrace(System.err);
      }
    }
  }

  private void mailFileToStudent(File file, Student student) throws MessagingException {
    String studentEmail = getEmailAddress(student);

    System.out.println("Mailing \"" + file + "\" to \"" + studentEmail + "\"");

    MimeMessage message = newEmailTo(this.session, studentEmail, this.subject);
    message.setText(readTextFromFile(file));

    sendEmailMessage(message);
  }

  private String readTextFromFile(File file) {
    return "The text from " + file;
  }

  private void sendEmailMessage(MimeMessage message) throws MessagingException {
    Transport.send(message);
  }

  private String getEmailAddress(Student student) {
    StringBuilder sb = new StringBuilder();
    sb.append(student.getFirstName());
    sb.append(" ");
    if (student.getNickName() != null) {
      sb.append("\"");
      sb.append(student.getNickName());
      sb.append("\" ");
    }
    sb.append(student.getLastName());
    sb.append(" <");
    sb.append(student.getEmail());
    sb.append(">");

    return sb.toString();
  }

  private static List<Student> getStudentsFromFiles(List<File> files, GradeBook gradeBook) {
    List<Student> students = new ArrayList<>(files.size());<commit_msg>Email the contents of the file.<commit_after>package edu.pdx.cs410J.grader;

import com.google.common.io.Files;
import edu.pdx.cs410J.ParserException;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

public class StudentFileMailer extends EmailSender {

  private final GradeBook gradeBook;
  private final String subject;
  private final Session session;

  public StudentFileMailer(GradeBook gradeBook, String subject, Session session) {
    this.gradeBook = gradeBook;
    this.subject = subject;
    this.session = session;
  }

  public static void main(String[] args) {
    String gradeBookFileName = null;
    String subject = null;
    List<String> fileNames = new ArrayList<>();

    for (String arg : args) {
      if (gradeBookFileName == null) {
        gradeBookFileName = arg;

      } else if (subject == null) {
        subject = arg;

      } else {
        fileNames.add(arg);
      }
    }

    usageIfNull(gradeBookFileName, "Missing grade book file name");
    usageIfNull(subject, "Missing email subject");
    usageIfEmpty(fileNames, "Missing file names");
    List<File> files = getFiles(fileNames);
    GradeBook gradeBook = getGradeBook(gradeBookFileName);
    List<Student> students = getStudentsFromFiles(files, gradeBook);

    assert files.size() == students.size();

    StudentFileMailer mailer = new StudentFileMailer(gradeBook, subject, newEmailSession(false));
    for (int i = 0; i < files.size(); i++) {
      File file = files.get(i);
      Student student = students.get(i);
      try {
        mailer.mailFileToStudent(file, student);

      } catch (MessagingException | IOException ex) {
        System.err.println("While mailing \"" + file + "\" to " + student);
        ex.printStackTrace(System.err);
      }
    }
  }

  private void mailFileToStudent(File file, Student student) throws MessagingException, IOException {
    String studentEmail = getEmailAddress(student);

    System.out.println("Mailing \"" + file + "\" to \"" + studentEmail + "\"");

    MimeMessage message = newEmailTo(this.session, studentEmail, this.subject);
    message.setText(readTextFromFile(file));

    sendEmailMessage(message);
  }

  private String readTextFromFile(File file) throws IOException {
    StringBuilder sb = new StringBuilder();
    Files.copy(file, Charset.defaultCharset(), sb);
    return sb.toString();
  }

  private void sendEmailMessage(MimeMessage message) throws MessagingException {
    Transport.send(message);
  }

  private String getEmailAddress(Student student) {
    StringBuilder sb = new StringBuilder();
    sb.append(student.getFirstName());
    sb.append(" ");
    if (student.getNickName() != null) {
      sb.append("\"");
      sb.append(student.getNickName());
      sb.append("\" ");
    }
    sb.append(student.getLastName());
    sb.append(" <");
    sb.append(student.getEmail());
    sb.append(">");

    return sb.toString();
  }

  private static List<Student> getStudentsFromFiles(List<File> files, GradeBook gradeBook) {
    List<Student> students = new ArrayList<>(files.size());
{'fraction_non_alphanumeric': 0.12245855489521426, 'fraction_numerical': 0.002189552705661558, 'mean_word_length': 3.6240057845263918, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3435860', 'n_tokens_mistral': 1887, 'n_tokens_neox': 1825, 'n_words': 442}