<commit_before>package es.uvigo.esei.daa.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import es.uvigo.esei.daa.entities.Event;
import es.uvigo.esei.daa.entities.Person;

public class EventDAO extends DAO {
	private final static Logger LOG = Logger.getLogger("PeopleDAO");
	
	public Event get(int id)
	throws DAOException, IllegalArgumentException {
		try (final Connection conn = this.getConnection()) {
			final String query = "SELECT * FROM event WHERE id=?";
			
			try (final PreparedStatement statement = conn.prepareStatement(query)) {
				statement.setInt(1, id);
				
				try (final ResultSet result = statement.executeQuery()) {
					if (result.next()) {
						return new Event(
								result.getInt("id"),
								result.getString("nombre"),
								result.getTimestamp("fechaCreacion"),
								result.getTimestamp("fechaInicio"),
								result.getTimestamp("fechaFin"),
								result.getString("descripcion"),
								result.getString("categoria")
						);
					} else {
						throw new IllegalArgumentException("Invalid id");
					}
				}
			}
		} catch (SQLException e) {
			LOG.log(Level.SEVERE, "Error getting an event", e);
			throw new DAOException(e);
		}
	}
	
	public List<Event> list() throws DAOException {
		try (final Connection conn = this.getConnection()) {
			final String query = "SELECT * FROM event";
			
			try (final PreparedStatement statement = conn.prepareStatement(query)) {
				try (final ResultSet result = statement.executeQuery()) {
					final List<Event> events = new LinkedList<>();
//////Cambiar los tags a los atributos cuando este la BD
					while (result.next()) {
						events.add(new Event(
							result.getInt("id"),
							result.getString("nombre"),
							result.getTimestamp("fechaCreacion"),
							result.getTimestamp("fechaInicio"),
							result.getTimestamp("fechaFin"),
							result.getString("descripcion"),
							result.getString("categoria")
						));
					}
					
					return events;
				}
			}
		} catch (SQLException e) {
			LOG.log(Level.SEVERE, "Error listing events", e);
			throw new DAOException(e);
		}
	}
	
	public void delete(int id)
	throws DAOException, IllegalArgumentException {
		try (final Connection conn = this.getConnection()) {
			final String query = "DELETE FROM event WHERE id=?";
			
			try (final PreparedStatement statement = conn.prepareStatement(query)) {
				statement.setInt(1, id);
				
				if (statement.executeUpdate() != 1) {
					throw new IllegalArgumentException("Invalid id");
				}
			}
		} catch (SQLException e) {
			LOG.log(Level.SEVERE, "Error deleting an event", e);
			throw new DAOException(e);
		}
	}
	
	public Event modify(int id,String nombre, Timestamp fechaCreacion, Timestamp fechaInicio, Timestamp fechaFin, String descripcion, String categoria)
	throws DAOException, IllegalArgumentException {
		if (  nombre == null) {
			throw new IllegalArgumentException("name cannot be null");
		}
		
		try (Connection conn = this.getConnection()) {
			///Sentencia de modificacion, hacer cuando la bd este correcta
			final String query = "UPDATE event SET name=?, surname=? WHERE id=?";
			
			try (PreparedStatement statement = conn.prepareStatement(query)) {
				statement.setString(1, nombre);
				statement.setString(2, fechaCreacion.toString());
				statement.setString(3, fechaInicio.toString());
				statement.setString(4, fechaFin.toString());
				statement.setString(5, descripcion);
				statement.setString(6, categoria);
				
				if (statement.executeUpdate() == 1) {
					return new Event(id, nombre, fechaCreacion,fechaInicio,fechaFin,descripcion,categoria);
				} else {
					throw new IllegalArgumentException("id and name cannot be null");
				}
			}
		} catch (SQLException e) {
			LOG.log(Level.SEVERE, "Error modifying an event", e);
			throw new DAOException();
		}
	}
	
	public Event add(int id,String nombre, Timestamp fechaCreacion, Timestamp fechaInicio, Timestamp fechaFin, String descripcion, String categoria)
	throws DAOException, IllegalArgumentException {
		if (nombre == null) {
			throw new IllegalArgumentException("name cannot be null");
		}
		
		try (Connection conn = this.getConnection()) {
			final String query = "INSERT INTO event VALUES(null, ?, ?,?,?,?,?)";
			
			try (PreparedStatement statement = conn.prepareStatement(query, Statement.RETURN_GENERATED_KEYS)) {
				///Comprobar que los tostring de los timestampo funcionen
				statement.setString(1, nombre);
				statement.setString(2, fechaCreacion.toString());
				statement.setString(3, fechaInicio.toString());
				statement.setString(4, fechaFin.toString());
				statement.setString(5, descripcion);
				statement.setString(6, categoria);
				
				if (statement.executeUpdate() == 1) {
					try (ResultSet resultKeys = statement.getGeneratedKeys()) {
						if (resultKeys.next()) {
							return new Event(resultKeys.getInt(1), nombre, fechaCreacion,fechaInicio,fechaFin,descripcion,categoria);
						} else {
							LOG.log(Level.SEVERE, "Error retrieving inserted id");
							throw new SQLException("Error retrieving inserted id");
						}
					}<commit_msg>Revert manual del revert erroneo<commit_after>package es.uvigo.esei.daa.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import es.uvigo.esei.daa.entities.Event;

public class EventDAO extends DAO {
	private final static Logger LOG = Logger.getLogger("EventDAO");
	
	public Event get(int id)
	throws DAOException, IllegalArgumentException {
		try (final Connection conn = this.getConnection()) {
			final String query = "SELECT * FROM event WHERE id=?";
			
			try (final PreparedStatement statement = conn.prepareStatement(query)) {
				statement.setInt(1, id);
				
				try (final ResultSet result = statement.executeQuery()) {
					if (result.next()) {
						return new Event(
								result.getInt("id"),
								result.getString("nameEvent"),
								result.getTimestamp("dateCreate"),
								result.getTimestamp("dateInit"),
								result.getTimestamp("dateFinal"),
								result.getString("description"),
								result.getString("category")
						);
					} else {
						throw new IllegalArgumentException("Invalid id");
					}
				}
			}
		} catch (SQLException e) {
			LOG.log(Level.SEVERE, "Error getting an event", e);
			throw new DAOException(e);
		}
	}
	
	public List<Event> list() throws DAOException {
		try (final Connection conn = this.getConnection()) {
			final String query = "SELECT * FROM event";
			
			try (final PreparedStatement statement = conn.prepareStatement(query)) {
				try (final ResultSet result = statement.executeQuery()) {
					final List<Event> events = new LinkedList<>();
					while (result.next()) {
						events.add(new Event(
								result.getInt("id"),
								result.getString("nameEvent"),
								result.getTimestamp("dateCreate"),
								result.getTimestamp("dateInit"),
								result.getTimestamp("dateFinal"),
								result.getString("description"),
								result.getString("category")
						));
					}
					
					return events;
				}
			}
		} catch (SQLException e) {
			LOG.log(Level.SEVERE, "Error listing events", e);
			throw new DAOException(e);
		}
	}
	
	public void delete(int id)
	throws DAOException, IllegalArgumentException {
		try (final Connection conn = this.getConnection()) {
			final String query = "DELETE FROM event WHERE id=?";
			
			try (final PreparedStatement statement = conn.prepareStatement(query)) {
				statement.setInt(1, id);
				
				if (statement.executeUpdate() != 1) {
					throw new IllegalArgumentException("Invalid id");
				}
			}
		} catch (SQLException e) {
			LOG.log(Level.SEVERE, "Error deleting an event", e);
			throw new DAOException(e);
		}
	}
	
	public Event modify(int id,String nameEvent, Timestamp dateCreate, Timestamp dateInit, Timestamp dateFinal, String description, String category)	throws DAOException, IllegalArgumentException {
		if (  nameEvent == null) {
			throw new IllegalArgumentException("name cannot be null");
		}
		
		try (Connection conn = this.getConnection()) {
			///Sentencia de modificacion, hacer cuando la bd este correcta
			final String query = "UPDATE event SET name=?, surname=? WHERE id=?";
			
			try (PreparedStatement statement = conn.prepareStatement(query)) {
				statement.setString(1, nameEvent);
				statement.setString(2, dateCreate.toString());
				statement.setString(3, dateInit.toString());
				statement.setString(4, dateFinal.toString());
				statement.setString(5, description);
				statement.setString(6, category);
				
				if (statement.executeUpdate() == 1) {
					return new Event(id, nameEvent, dateCreate,dateInit,dateFinal,description,category);
				} else {
					throw new IllegalArgumentException("id and name cannot be null");
				}
			}
		} catch (SQLException e) {
			LOG.log(Level.SEVERE, "Error modifying an event", e);
			throw new DAOException();
		}
	}
	
	public Event add(int id,String nameEvent, Timestamp dateCreate, Timestamp dateInit, Timestamp dateFinal, String description, String category)throws DAOException, IllegalArgumentException {
		if (nameEvent == null) {
			throw new IllegalArgumentException("name cannot be null");
		}
		
		try (Connection conn = this.getConnection()) {
			final String query = "INSERT INTO event VALUES(null, ?, ?,?,?,?,?)";
			
			try (PreparedStatement statement = conn.prepareStatement(query, Statement.RETURN_GENERATED_KEYS)) {
				///Comprobar que los tostring de los timestampo funcionen
				statement.setString(1, nameEvent);
				statement.setString(2, dateCreate.toString());
				statement.setString(3, dateInit.toString());
				statement.setString(4, dateFinal.toString());
				statement.setString(5, description);
				statement.setString(6, category);
				
				if (statement.executeUpdate() == 1) {
					try (ResultSet resultKeys = statement.getGeneratedKeys()) {
						if (resultKeys.next()) {
							return new Event(resultKeys.getInt(1), nameEvent, dateCreate,dateInit,dateFinal,description,category);
							} else {
							LOG.log(Level.SEVERE, "Error retrieving inserted id");
							throw new SQLException("Error retrieving inserted id");
						}
					}
{'fraction_non_alphanumeric': 0.1194849460329483, 'fraction_numerical': 0.0034084453701950387, 'mean_word_length': 3.7219490388913723, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '3140463', 'n_tokens_mistral': 3823, 'n_tokens_neox': 3012, 'n_words': 783}