<commit_before>package serviceResources;

import db.DBConnector;
import serviceRepresentations.Question;

import java.sql.*;
import java.util.List;

public class QuestionDAO {

    private DBConnector dbConnector;

    public QuestionDAO(DBConnector dbConnector) {
        this.dbConnector = dbConnector;
    }

    /**
     * Returns a new `Question` object populated to the associated values in
     * the database.
     * @param id The question is selected based on it's id.
     * @return The question with id `id`.
     */
    public Question getQuestion(Long id) {
        Question question = null;

        try {
            question = new Question();
            Connection connection = dbConnector.getDBConnection();
            PreparedStatement statement =
                    connection.prepareStatement("SELECT " +
                            "ENUNCIATION, " +
                            "REWARD, " +
                            "CHARCTERS_ID, " +
                            "CHAPTER FROM QUESTIONS WHERE QUESTION_ID = ?");
            statement.setLong(1, id);

            ResultSet rs = statement.executeQuery();
            question.setId(id);
            question.setEnunciation(rs.getString("ENUNCIATION"));
            question.setChapterId(rs.getLong("CHAPTER"));
            question.setReward(rs.getLong("REWARD"));
            question.setCharacterId(rs.getLong("CHARACTERS_ID"));

            return question;

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return question;
    }

    /**
     *
     * @return
     */
    public List<Question> getNPCQuestions(Long npcId) {
        return null;
    }

    /**
     * Inserts a new entry in the database with the attributes of `question`.
     * @param question The new question to be inserted in the database.<commit_msg>Created skeleton for getting list of questions of a NPC<commit_after>package serviceResources;

import db.DBConnector;
import serviceRepresentations.Question;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class QuestionDAO {

    private DBConnector dbConnector;

    public QuestionDAO(DBConnector dbConnector) {
        this.dbConnector = dbConnector;
    }

    /**
     * Returns a new `Question` object populated to the associated values in
     * the database.
     * @param id The question is selected based on it's id.
     * @return The question with id `id`.
     */
    public Question getQuestion(Long id) {
        Question question = null;

        try {
            question = new Question();
            Connection connection = dbConnector.getDBConnection();
            PreparedStatement statement =
                    connection.prepareStatement("SELECT " +
                            "ENUNCIATION, " +
                            "REWARD, " +
                            "CHARCTERS_ID, " +
                            "CHAPTER FROM QUESTIONS WHERE QUESTION_ID = ?");
            statement.setLong(1, id);

            ResultSet rs = statement.executeQuery();
            question.setId(id);
            question.setEnunciation(rs.getString("ENUNCIATION"));
            question.setChapterId(rs.getLong("CHAPTER"));
            question.setReward(rs.getLong("REWARD"));
            question.setCharacterId(rs.getLong("CHARACTERS_ID"));

            return question;

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return question;
    }

    /**
     * Returns the list with all the questions registered to a particular NPC.
     * @param npcId The NPC whose questions we want.
     * @return Returns a list with all the questions.
     */
    public List<Question> getNPCQuestions(Long npcId) {

        // TODO: Security and edge cases.

        List<Question> questions = null;

        try {

            questions = new ArrayList<>();

            Connection connection = dbConnector.getDBConnection();
            PreparedStatement statement =
                    connection.prepareStatement("SELECT QUESTION_ID, " +
                            "ENUNCIATION, " +
                            "REWARD, " +
                            "CHARACTERS_ID, " +
                            "CHAPTER FROM QUESTIONS WHERE CHARACTERS_ID = ?");

            statement.setLong(1, npcId);

            ResultSet rs = statement.executeQuery();

            while (rs.next()) {
                Question question = new Question();

                question.setId(rs.getLong("QUESTION_ID"));
                question.setCharacterId(rs.getLong("CHARACTERS_ID"));
                question.setEnunciation(rs.getString("ENUNCIATION"));
                question.setChapterId(rs.getLong("CHAPTER"));

                questions.add(question);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return questions;
    }

    /**
     * Inserts a new entry in the database with the attributes of `question`.
     * @param question The new question to be inserted in the database.
{'fraction_non_alphanumeric': 0.09148640603294304, 'fraction_numerical': 0.0005953562214725144, 'mean_word_length': 1.847457627118644, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '5213303', 'n_tokens_mistral': 1295, 'n_tokens_neox': 1236, 'n_words': 330}