<commit_before>package de.unibremen.opensores.model;

import de.unibremen.opensores.util.mail.MailJob;
import de.unibremen.opensores.util.mail.Mail;

import com.github.mustachejava.MustacheFactory;
import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.Mustache;

import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.ManyToOne;
import javax.persistence.JoinColumn;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;

/**
 * Entity bean for the MailTemplate Class.
 *
 * @author <NAME>
 */
@Entity
@Table(name = "MAIL_TEMPLATES")
public class MailTemplate {

    @Id
    @GeneratedValue
    private Long mailTemplateId;

    @ManyToOne(optional = false)
    @JoinColumn(name = "courseId")
    private Course course;

    @Column(nullable = false)
    private String subject;

    @Column(name = "text", nullable = false, columnDefinition = "LONG VARCHAR")
    private String text;

    @Column(nullable = false)
    private String locale;

    @Column(nullable = true)
    private String name;

    @Column(nullable = true)
    private Boolean isDefault;

    public String getSubject() {
        return subject;
    }

    public String getText() {
        return text;
    }

    public String getLocale() {
        return locale;
    }

    public String getName() {
        return name;
    }

    public Long getMailTemplateId() {
        return mailTemplateId;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public void setText(String text) {
        this.text = text;
    }

    public void setLocale(String locale) {
        this.locale = locale;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Course getCourse() {
        return course;
    }

    public void setCourse(Course course) {
        this.course = course;
    }

    public void setMailTemplateId(Long mailTemplateId) {
        this.mailTemplateId = mailTemplateId;
    }

    public Boolean getIsDefault() {
        return isDefault;
    }

    public void setIsDefault(Boolean isDefault) {
        this.isDefault = isDefault;
    }

    /**
     * Generates the text from a template and a given map.
     *
     * @param map the map.
     * @return the filled template as a string.
     * @throws IOException If the string couldn't be created.
     */
    private String toString(Map<String, Object> map) throws IOException {
        MustacheFactory mf = new DefaultMustacheFactory();
        Mustache mustache = mf.compile(new StringReader(text), "template");
        StringWriter writer = new StringWriter();
        mustache.execute(writer, map);

        text = writer.toString();
        writer.close();

        return text;
    }

    /**
     * Retrieves the needed data fields from student.
     *
     * @param student the student in question.
     * @return a map with the student's fields or null.
     */
    private Map<String, Object> getTemplateData(Student student) {
        User user = student.getUser();

        ArrayList<String> gradeList = new ArrayList<>();
        for (Grading g : student.getGradings()) {
            gradeList.add(g.getGrade().getValue().toString());
        }

        String firstName = user.getFirstName();
        String lastName = user.getLastName();<commit_msg>Fix flushing of mustard writer<commit_after>package de.unibremen.opensores.model;

import de.unibremen.opensores.util.mail.MailJob;
import de.unibremen.opensores.util.mail.Mail;

import com.github.mustachejava.MustacheFactory;
import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.Mustache;

import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.ManyToOne;
import javax.persistence.JoinColumn;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;

/**
 * Entity bean for the MailTemplate Class.
 *
 * @author <NAME>
 */
@Entity
@Table(name = "MAIL_TEMPLATES")
public class MailTemplate {

    @Id
    @GeneratedValue
    private Long mailTemplateId;

    @ManyToOne(optional = false)
    @JoinColumn(name = "courseId")
    private Course course;

    @Column(nullable = false)
    private String subject;

    @Column(name = "text", nullable = false, columnDefinition = "LONG VARCHAR")
    private String text;

    @Column(nullable = false)
    private String locale;

    @Column(nullable = true)
    private String name;

    @Column(nullable = true)
    private Boolean isDefault;

    public String getSubject() {
        return subject;
    }

    public String getText() {
        return text;
    }

    public String getLocale() {
        return locale;
    }

    public String getName() {
        return name;
    }

    public Long getMailTemplateId() {
        return mailTemplateId;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public void setText(String text) {
        this.text = text;
    }

    public void setLocale(String locale) {
        this.locale = locale;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Course getCourse() {
        return course;
    }

    public void setCourse(Course course) {
        this.course = course;
    }

    public void setMailTemplateId(Long mailTemplateId) {
        this.mailTemplateId = mailTemplateId;
    }

    public Boolean getIsDefault() {
        return isDefault;
    }

    public void setIsDefault(Boolean isDefault) {
        this.isDefault = isDefault;
    }

    /**
     * Generates the text from a template and a given map.
     *
     * @param map the map.
     * @return the filled template as a string.
     */
    private String toString(Map<String, Object> map) {
        MustacheFactory mf = new DefaultMustacheFactory();
        Mustache mustache = mf.compile(new StringReader(text), "template");
        return mustache.execute(new StringWriter(), map).toString();
    }

    /**
     * Retrieves the needed data fields from student.
     *
     * @param student the student in question.
     * @return a map with the student's fields or null.
     */
    private Map<String, Object> getTemplateData(Student student) {
        User user = student.getUser();

        ArrayList<String> gradeList = new ArrayList<>();
        for (Grading g : student.getGradings()) {
            gradeList.add(g.getGrade().getValue().toString());
        }

        String firstName = user.getFirstName();
        String lastName = user.getLastName();
{'fraction_non_alphanumeric': 0.09697053534375433, 'fraction_numerical': 0.0, 'mean_word_length': 2.8704496788008567, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '1562672', 'n_tokens_mistral': 2072, 'n_tokens_neox': 2031, 'n_words': 546}