package dao; import com.google.gson.Gson; import model.Event; import model.Person; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.Reader; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import javax.xml.crypto.Data; /** * Person data access object that performs operations associated with persons */ public class PersonDAO { private final Connection conn; public PersonDAO(Connection conn) { this.conn = conn; } /** * Creates a Person * @param person */ public void createPerson(Person person) throws DataAccessException { String sql = "INSERT INTO Persons (personID, associatedUsername, firstName, lastName, gender, " + "fatherID, motherID, spouseID) VALUES(?,?,?,?,?,?,?,?)"; try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setString(1, person.getPersonID()); stmt.setString(2, person.getAssociatedUsername()); stmt.setString(3, person.getFirstName()); stmt.setString(4, person.getLastName()); stmt.setString(5, person.getGender()); stmt.setString(6, person.getFatherID()); stmt.setString(7, person.getMotherID()); stmt.setString(8, person.getSpouseID()); stmt.executeUpdate(); } catch(SQLException e) { e.printStackTrace(); throw new DataAccessException("Error encountered while inserting into the database"); } } /** * Finds and returns a person associated with a specific personID * @param personID * @return person associated with the personID */ public Person getPersonByID(String personID) throws DataAccessException { Person person; ResultSet rs = null; String sql = "SELECT * FROM Persons WHERE personID = ?;"; try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setString(1, personID); rs = stmt.executeQuery(); if (rs.next()) { person = new Person(rs.getString("personID"), rs.getString("associatedUsername"), rs.getString("firstName"), rs.getString("lastName"), rs.getString("gender"), rs.getString("fatherID"), rs.getString("motherID"), rs.getString("spouseID")); return person; } } catch (SQLException e) { e.printStackTrace(); throw new DataAccessException("Error encountered while finding person"); } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } } return null; } /** * Finds and removes a person associated with a specific personID * @param personID * @return person associated with the personID */ public void removePersonByID(String personID) throws DataAccessException { String sql = "DELETE FROM Persons WHERE personID = ?;"; try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setString(1, personID); stmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); throw new DataAccessException("Error encountered while removing person"); } } /** * * @param username * @return a list of Person objects associated with a given User */ public List<Person> findPersonsForUser(String username) throws DataAccessException { List<Person> personList = new ArrayList<>(); Person person; ResultSet rs = null; String sql = "SELECT * FROM Persons WHERE associatedUsername = ?;"; try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setString(1, username); rs = stmt.executeQuery(); while (rs.next()) { person = new Person(rs.getString("personID"), rs.getString("associatedUsername"), rs.getString("firstName"), rs.getString("lastName"), rs.getString("gender"), rs.getString("fatherID"), rs.getString("motherID"), rs.getString("spouseID")); personList.add(person); } return personList; } catch (SQLException e) { e.printStackTrace(); throw new DataAccessException("Error encountered while finding person"); } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } } } /** * Removes all persons associated with a user */ public void removeUserPersons(String username) throws DataAccessException { String sql = "DELETE FROM Persons WHERE associatedUsername = ?;"; try(PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setString(1, username); stmt.executeUpdate(); } catch(SQLException e) { e.printStackTrace(); throw new DataAccessException("Error encountered while deleting user's persons from the database"); } } /** * Removes all the persons from the database */ public void removeAllPersons() throws DataAccessException { String sql = "DELETE FROM Persons"; try(PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.executeUpdate(); } catch(SQLException e) { e.printStackTrace(); throw new DataAccessException("Error encountered while deleting from the database"); } } }