<commit_before>/* JDBCInfoSessionDAO.java
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * Copyright Ⓒ 2014-2015 Universiteit Gent
 * 
 * This file is part of the Degage Web Application
 * 
 * Corresponding author (see also AUTHORS.txt)
 * 
 * <NAME>
 * Department of Applied Mathematics, Computer Science and Statistics
 * Ghent University 
 * Krijgslaan 281-S9
 * B-9000 GENT Belgium
 * 
 * The Degage Web Application is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * The Degage Web Application is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with the Degage Web Application (file LICENSE.txt in the
 * distribution).  If not, see <http://www.gnu.org/licenses/>.
 */

package be.ugent.degage.db.jdbc;

import be.ugent.degage.db.DataAccessException;
import be.ugent.degage.db.dao.InfoSessionDAO;
import be.ugent.degage.db.models.*;

import java.sql.*;
import java.time.Instant;
import java.util.Map;
import java.util.HashMap;

import static be.ugent.degage.db.jdbc.JDBCUserDAO.USER_HEADER_FIELDS;

/**
 * JDBC implementation of {@link be.ugent.degage.db.dao.InfoSessionDAO}
 */
class JDBCInfoSessionDAO extends AbstractDAO implements InfoSessionDAO {

    JDBCInfoSessionDAO(JDBCDataAccessContext context) {
        super(context);
    }

    // TODO: check what kind of 'partial' is really needed in each case


    private static InfoSession populateInfoSession(ResultSet rs) throws SQLException {
        Address address = JDBCAddressDAO.populateAddress(rs);

        InfoSession infoSession = new InfoSession(
                rs.getInt("infosession_id"),
                InfoSessionType.valueOf(rs.getString("infosession_type")),
                rs.getTimestamp("infosession_timestamp").toInstant(),
                address,
                rs.getInt("user_id"),
                rs.getString("user_firstname") + " " + rs.getString("user_lastname"),
                rs.getInt("infosession_max_enrollees"),
                rs.getString("infosession_comments")
        );
        infoSession.setEnrolleeCount(rs.getInt("enrollee_count"));
        return infoSession;
    }

    // TODO: code below is only used from ApprovalDAO. Check whether it can be cleaned up
    static InfoSession populateInfoSessionPartial(ResultSet rs) throws SQLException {
        return new InfoSession(
                rs.getInt("ses.infosession_id"),
                InfoSessionType.valueOf(rs.getString("infosession_type")),
                rs.getTimestamp("infosession_timestamp").toInstant(),
                null,
                0, null,
                rs.getInt("infosession_max_enrollees"),
                rs.getString("infosession_comments")
        );
    }

    @Override
    public int createInfoSession(InfoSessionType type, int hostId, Address address,
                                         Instant time, int maxEnrollees, String comments) throws DataAccessException {
        try (PreparedStatement ps = prepareStatement(
                "INSERT INTO infosessions(infosession_type, infosession_timestamp, " +
                        "infosession_host_user_id, infosession_max_enrollees, infosession_comments) " +
                        "VALUES (?,?,?,?,?)",
                "infosession_id"
        )) {
            ps.setString(1, type.name());
            ps.setTimestamp(2, Timestamp.from(time));
            ps.setInt(3, hostId);
            ps.setInt(4, maxEnrollees);
            ps.setString(5, comments);
            ps.executeUpdate();

            try (ResultSet keys = ps.getGeneratedKeys()) {
                keys.next(); //if this fails we want an exception anyway

                int sessionId = keys.getInt(1);
                updateLocation(sessionId, address);
                return sessionId;
            }
        } catch (SQLException ex) {
            throw new DataAccessException("Could not create infosession.", ex);
        }
    }

    private void updateLocation(int sessionId, Address location) {
        JDBCAddressDAO.updateLocation(
                getConnection(),
                "JOIN infosessions ON infosession_address_id=address_id", "infosession_id",
                sessionId, location
        );
    }

    @Override
    public InfoSession getInfoSession(int id) throws DataAccessException {
        try (PreparedStatement ps = prepareStatement(
                "SELECT " + INFOSESSION_EXTENDED_FIELDS + "FROM infosessions_extended WHERE infosession_id = ?"
        )) {
            ps.setInt(1, id);
            return toSingleObject(ps, JDBCInfoSessionDAO::populateInfoSession);
        } catch (SQLException ex) {
            throw new DataAccessException("Could not fetch infosession by id.", ex);
        }
    }


    @Override
    public int getAmountOfAttendees(int id) throws DataAccessException {
        try (PreparedStatement ps = prepareStatement(
                "SELECT COUNT(*) FROM infosessionenrollees WHERE infosession_id = ? and infosession_enrollment_status = "
        )) {
            ps.setInt(1, id);
            return toSingleInt(ps);
        } catch (SQLException ex) {
            throw new DataAccessException("Could not get amount of attendees for infosession.", ex);
        }
    }

    @Override
    public void deleteInfoSession(int id) throws DataAccessException {
        try (PreparedStatement ps = prepareStatement(
                "DELETE FROM infosessions WHERE infosession_id = ?"
        )) {
            ps.setInt(1, id);<commit_msg>Bad query for info session attendee count<commit_after>/* JDBCInfoSessionDAO.java
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * Copyright Ⓒ 2014-2015 Universiteit Gent
 * 
 * This file is part of the Degage Web Application
 * 
 * Corresponding author (see also AUTHORS.txt)
 * 
 * <NAME>
 * Department of Applied Mathematics, Computer Science and Statistics
 * Ghent University 
 * Krijgslaan 281-S9
 * B-9000 GENT Belgium
 * 
 * The Degage Web Application is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * The Degage Web Application is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with the Degage Web Application (file LICENSE.txt in the
 * distribution).  If not, see <http://www.gnu.org/licenses/>.
 */

package be.ugent.degage.db.jdbc;

import be.ugent.degage.db.DataAccessException;
import be.ugent.degage.db.dao.InfoSessionDAO;
import be.ugent.degage.db.models.*;

import java.sql.*;
import java.time.Instant;
import java.util.Map;
import java.util.HashMap;

import static be.ugent.degage.db.jdbc.JDBCUserDAO.USER_HEADER_FIELDS;

/**
 * JDBC implementation of {@link be.ugent.degage.db.dao.InfoSessionDAO}
 */
class JDBCInfoSessionDAO extends AbstractDAO implements InfoSessionDAO {

    JDBCInfoSessionDAO(JDBCDataAccessContext context) {
        super(context);
    }

    // TODO: check what kind of 'partial' is really needed in each case


    private static InfoSession populateInfoSession(ResultSet rs) throws SQLException {
        Address address = JDBCAddressDAO.populateAddress(rs);

        InfoSession infoSession = new InfoSession(
                rs.getInt("infosession_id"),
                InfoSessionType.valueOf(rs.getString("infosession_type")),
                rs.getTimestamp("infosession_timestamp").toInstant(),
                address,
                rs.getInt("user_id"),
                rs.getString("user_firstname") + " " + rs.getString("user_lastname"),
                rs.getInt("infosession_max_enrollees"),
                rs.getString("infosession_comments")
        );
        infoSession.setEnrolleeCount(rs.getInt("enrollee_count"));
        return infoSession;
    }

    // TODO: code below is only used from ApprovalDAO. Check whether it can be cleaned up
    static InfoSession populateInfoSessionPartial(ResultSet rs) throws SQLException {
        return new InfoSession(
                rs.getInt("ses.infosession_id"),
                InfoSessionType.valueOf(rs.getString("infosession_type")),
                rs.getTimestamp("infosession_timestamp").toInstant(),
                null,
                0, null,
                rs.getInt("infosession_max_enrollees"),
                rs.getString("infosession_comments")
        );
    }

    @Override
    public int createInfoSession(InfoSessionType type, int hostId, Address address,
                                         Instant time, int maxEnrollees, String comments) throws DataAccessException {
        try (PreparedStatement ps = prepareStatement(
                "INSERT INTO infosessions(infosession_type, infosession_timestamp, " +
                        "infosession_host_user_id, infosession_max_enrollees, infosession_comments) " +
                        "VALUES (?,?,?,?,?)",
                "infosession_id"
        )) {
            ps.setString(1, type.name());
            ps.setTimestamp(2, Timestamp.from(time));
            ps.setInt(3, hostId);
            ps.setInt(4, maxEnrollees);
            ps.setString(5, comments);
            ps.executeUpdate();

            try (ResultSet keys = ps.getGeneratedKeys()) {
                keys.next(); //if this fails we want an exception anyway

                int sessionId = keys.getInt(1);
                updateLocation(sessionId, address);
                return sessionId;
            }
        } catch (SQLException ex) {
            throw new DataAccessException("Could not create infosession.", ex);
        }
    }

    private void updateLocation(int sessionId, Address location) {
        JDBCAddressDAO.updateLocation(
                getConnection(),
                "JOIN infosessions ON infosession_address_id=address_id", "infosession_id",
                sessionId, location
        );
    }

    @Override
    public InfoSession getInfoSession(int id) throws DataAccessException {
        try (PreparedStatement ps = prepareStatement(
                "SELECT " + INFOSESSION_EXTENDED_FIELDS + "FROM infosessions_extended WHERE infosession_id = ?"
        )) {
            ps.setInt(1, id);
            return toSingleObject(ps, JDBCInfoSessionDAO::populateInfoSession);
        } catch (SQLException ex) {
            throw new DataAccessException("Could not fetch infosession by id.", ex);
        }
    }


    @Override
    public int getAmountOfAttendees(int id) throws DataAccessException {
        try (PreparedStatement ps = prepareStatement(
                "SELECT COUNT(*) FROM infosessionenrollees WHERE infosession_id = ? and infosession_enrollment_status = 'PRESENT'"
        )) {
            ps.setInt(1, id);
            return toSingleInt(ps);
        } catch (SQLException ex) {
            throw new DataAccessException("Could not get amount of attendees for infosession.", ex);
        }
    }

    @Override
    public void deleteInfoSession(int id) throws DataAccessException {
        try (PreparedStatement ps = prepareStatement(
                "DELETE FROM infosessions WHERE infosession_id = ?"
        )) {
            ps.setInt(1, id);
{'fraction_non_alphanumeric': 0.09566315789473684, 'fraction_numerical': 0.0045473684210526315, 'mean_word_length': 2.533472180898542, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6719915', 'n_tokens_mistral': 3292, 'n_tokens_neox': 3134, 'n_words': 862}