<commit_before>package Dao;

import BulletinBoard.Database;
import Domain.Message;
import Domain.Thread;
import Domain.User;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MessageDao {

    private final Database database;
    private final ThreadDao threadDao;
    private final UserDao userDao;

    public MessageDao(Database database) {
        this.database = database;
        threadDao = new ThreadDao(database); //Unnecessary objects, could set in setter
        userDao = new UserDao(database);
    }

    public Message findOne(int messageId) throws SQLException {
        List<Message> row = database.queryAndCollect(
                "SELECT * FROM Message WHERE messageId = ?;", rs -> {
                    return new Message(
                            rs.getInt("messageId"),
                            threadDao.findOne(rs.getInt("threadId")),
                            userDao.findOne(rs.getInt("sender")),
                            rs.getInt("order"),
                            rs.getTimestamp("dateTime"),
                            rs.getString("content")
                    );
                }, messageId);

        return !row.isEmpty() ? row.get(0) : null;
    }

    public List<Message> findAll() throws SQLException {
        List<Message> messages = new ArrayList<>();
        Map<Integer, List<Message>> threadRefs = new HashMap<>();
        Map<Integer, List<Message>> senderRefs = new HashMap<>();

        try (ResultSet rs = database.query("SELECT * FROM Message;")) {
            while (rs.next()) {
                int id = rs.getInt("messageId");
                int thread = rs.getInt("threadId");
                int sender = rs.getInt("sender");
                int order = rs.getInt("order");
                Timestamp date = rs.getTimestamp("dateTime");
                String content = rs.getString("content");

                Message msg = new Message(id, order, date, content);
                messages.add(msg);

                threadRefs.putIfAbsent(thread, new ArrayList<>());
                threadRefs.get(thread).add(msg);
                senderRefs.putIfAbsent(sender, new ArrayList<>());
                senderRefs.get(sender).add(msg);
            }
        }

        for (Thread thread : threadDao.findAllIn(threadRefs.keySet())) {
            for (Message message : threadRefs.get(thread.getThreadId())) {
                message.setThread(thread);
            }
        }

        for (User user : userDao.findAllIn(senderRefs.keySet())) {
            for (Message message : senderRefs.get(user.getUserId())) {
                message.setSender(user);
            }
        }

        return messages;
    }

    public List<Message> findAllIn(int threadId) throws SQLException {
        List<Message> messages = new ArrayList<>();
        Map<Integer, List<Message>> threadRefs = new HashMap<>();
        Map<Integer, List<Message>> senderRefs = new HashMap<>();

        try (ResultSet rs = database.query("SELECT * FROM Message WHERE threadId = ?;", threadId)) {
            while (rs.next()) {
                int id = rs.getInt("messageId");
                int thread = rs.getInt("threadId");
                int sender = rs.getInt("sender");
                int order = rs.getInt("order");
                Timestamp date = rs.getTimestamp("dateTime");
                String content = rs.getString("content");

                Message msg = new Message(id, order, date, content);
                messages.add(msg);

                threadRefs.putIfAbsent(thread, new ArrayList<>());
                threadRefs.get(thread).add(msg);
                senderRefs.putIfAbsent(sender, new ArrayList<>());
                senderRefs.get(sender).add(msg);
            }
        }

        Thread thread = threadDao.findOne(threadId);
        for (Message message : messages) {
            message.setThread(thread);
        }

        for (User user : userDao.findAllIn(senderRefs.keySet())) {
            for (Message message : senderRefs.get(user.getUserId())) {<commit_msg>MessageDoa<commit_after>package Dao;

import BulletinBoard.Database;
import Domain.Message;
import Domain.Thread;
import Domain.User;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MessageDao {

    private final Database database;
    private final ThreadDao threadDao;
    private final UserDao userDao;

    public MessageDao(Database database) {
        this.database = database;
        threadDao = new ThreadDao(database); //Unnecessary objects, could set in setter
        userDao = new UserDao(database);
    }

    public Message findOne(int messageId) throws SQLException {
        List<Message> row = database.queryAndCollect(
                "SELECT * FROM Message WHERE messageId = ?;", rs -> {
                    return new Message(
                            rs.getInt("messageId"),
                            threadDao.findOne(rs.getInt("threadId")),
                            userDao.findOne(rs.getInt("sender")),
                            rs.getInt("order"),
                            rs.getTimestamp("dateTime"),
                            rs.getString("content")
                    );
                }, messageId);

        return !row.isEmpty() ? row.get(0) : null;
    }

    public List<Message> findAll() throws SQLException {
        List<Message> messages = new ArrayList<>();
        Map<Integer, List<Message>> threadRefs = new HashMap<>();
        Map<Integer, List<Message>> senderRefs = new HashMap<>();

        try (ResultSet rs = database.query("SELECT * FROM Message;")) {
            while (rs.next()) {
                int id = rs.getInt("messageId");
                int thread = rs.getInt("threadId");
                int sender = rs.getInt("sender");
                int order = rs.getInt("order");
                Timestamp date = rs.getTimestamp("dateTime");
                String content = rs.getString("content");

                Message msg = new Message(id, order, date, content);
                messages.add(msg);

                threadRefs.putIfAbsent(thread, new ArrayList<>());
                threadRefs.get(thread).add(msg);
                senderRefs.putIfAbsent(sender, new ArrayList<>());
                senderRefs.get(sender).add(msg);
            }
        }

        for (Thread thread : threadDao.findAllIn(threadRefs.keySet())) {
            for (Message message : threadRefs.get(thread.getThreadId())) {
                message.setThread(thread);
            }
        }

        for (User user : userDao.findAllIn(senderRefs.keySet())) {
            for (Message message : senderRefs.get(user.getUserId())) {
                message.setSender(user);
            }
        }

        return messages;
    }

    public List<Message> findAllIn(int threadId) throws SQLException {
        List<Message> messages = new ArrayList<>();
        Map<Integer, List<Message>> senderRefs = new HashMap<>();

        try (ResultSet rs = database.query("SELECT * FROM Message WHERE threadId = ?;", threadId)) {
            while (rs.next()) {
                int id = rs.getInt("messageId");
                int thread = rs.getInt("threadId");
                int sender = rs.getInt("sender");
                int order = rs.getInt("order");
                Timestamp date = rs.getTimestamp("dateTime");
                String content = rs.getString("content");

                Message msg = new Message(id, order, date, content);
                messages.add(msg);

                senderRefs.putIfAbsent(sender, new ArrayList<>());
                senderRefs.get(sender).add(msg);
            }
        }

        Thread thread = threadDao.findOne(threadId);
        for (Message message : messages) {
            message.setThread(thread);
        }

        for (User user : userDao.findAllIn(senderRefs.keySet())) {
            for (Message message : senderRefs.get(user.getUserId())) {
{'fraction_non_alphanumeric': 0.11888794049506157, 'fraction_numerical': 0.00024387269845140838, 'mean_word_length': 1.9271948608137044, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 47, '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': '2360330', 'n_tokens_mistral': 2149, 'n_tokens_neox': 2104, 'n_words': 485}