diff --git a/src/main/java/es/uvigo/esei/daa/dao/PetsDAO.java b/src/main/java/es/uvigo/esei/daa/dao/PetsDAO.java new file mode 100644 index 0000000000000000000000000000000000000000..4bc62f895d2e94266b49a618ea87dcafa1bebe5b --- /dev/null +++ b/src/main/java/es/uvigo/esei/daa/dao/PetsDAO.java @@ -0,0 +1,165 @@ +package es.uvigo.esei.daa.dao; + +import java.util.logging.Logger; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.LinkedList; +import java.util.List; +import java.util.logging.Level; + +import es.uvigo.esei.daa.entities.Pet; + +public class PetsDAO extends DAO { + private final static Logger LOG = Logger.getLogger(PetsDAO.class.getName()); + + + public Pet get(int id) + throws DAOException, IllegalArgumentException { + try (final Connection conn = this.getConnection()) { + final String query = "SELECT * FROM pets WHERE id=?"; + + try (final PreparedStatement statement = conn.prepareStatement(query)) { + statement.setInt(1, id); + + try (final ResultSet result = statement.executeQuery()) { + if (result.next()) { + return rowToEntity(result); + } else { + throw new IllegalArgumentException("Invalid id"); + } + } + } + } catch (SQLException e) { + LOG.log(Level.SEVERE, "Error getting a pet", e); + throw new DAOException(e); + } + } + + public List list() throws DAOException { + try (final Connection conn = this.getConnection()) { + final String query = "SELECT * FROM pets"; + + try (final PreparedStatement statement = conn.prepareStatement(query)) { + try (final ResultSet result = statement.executeQuery()) { + final List pets = new LinkedList<>(); + + while (result.next()) { + pets.add(rowToEntity(result)); + } + + return pets; + } + } + } catch (SQLException e) { + LOG.log(Level.SEVERE, "Error listing pets", e); + throw new DAOException(e); + } + } + + public List peoplePets(int peopleId) throws DAOException { + try(final Connection conn = this.getConnection()){ + final String query = "SELECT * FROM pets WHERE owner= ?"; + + try(final PreparedStatement stmt = conn.prepareStatement(query)) { + stmt.setInt(1,peopleId); + try(final ResultSet result = stmt.executeQuery()) { + final List pets = new LinkedList<>(); + while (result.next()) { + pets.add(rowToEntity(result)); + } + return pets; + } + } + } catch (SQLException e){ + LOG.log(Level.SEVERE, "Error peoplePets", e); + throw new DAOException(e); + } + } + + public Pet add(Pet pet) + throws DAOException, IllegalArgumentException { + if (pet==null) { + throw new IllegalArgumentException("Pet is null!"); + } + + try (Connection conn = this.getConnection()) { + final String query = "INSERT INTO pets(name, kind, breed, owner) VALUES (?,?,?,?)"; + + try (PreparedStatement statement = conn.prepareStatement(query, Statement.RETURN_GENERATED_KEYS)) { + statement.setString(1, pet.getName()); + statement.setString(2, pet.getKind()); + statement.setString(3, pet.getBreed()); + statement.setInt(4, pet.getOwner()); + + if (statement.executeUpdate() == 1) { + return pet; + } else { + LOG.log(Level.SEVERE, "Error inserting value"); + throw new SQLException("Error inserting value"); + } + } + } catch (SQLException e) { + LOG.log(Level.SEVERE, "Error adding a pet", e); + throw new DAOException(e); + } + } + + + public void modify(Pet pet) + throws DAOException, IllegalArgumentException { + if (pet == null) { + throw new IllegalArgumentException("pet can't be null"); + } + + try (Connection conn = this.getConnection()) { + final String query = "UPDATE pets SET name=?, kind=?, breed=?, owner=? WHERE id=?"; + + try (PreparedStatement statement = conn.prepareStatement(query)) { + statement.setString(1, pet.getName()); + statement.setString(2, pet.getKind()); + statement.setString(3, pet.getBreed()); + statement.setInt(4,pet.getOwner()); + statement.setInt(5,pet.getId()); + + if (statement.executeUpdate() != 1) { + throw new IllegalArgumentException("name, kind and breed can't be null"); + } + } + } catch (SQLException e) { + LOG.log(Level.SEVERE, "Error modifying a pet", e); + throw new DAOException(); + } + } + + + public void delete(int id) + throws DAOException, IllegalArgumentException { + try (final Connection conn = this.getConnection()) { + final String query = "DELETE FROM pets 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 pet: "+ id, e); + throw new DAOException(e); + } + } + + private Pet rowToEntity(ResultSet row) throws SQLException { + return new Pet( + row.getInt("id"), + row.getString("name"), + row.getString("kind"), + row.getString("breed"), + row.getInt("owner") + ); + } +} diff --git a/src/main/java/es/uvigo/esei/daa/entities/Pet.java b/src/main/java/es/uvigo/esei/daa/entities/Pet.java new file mode 100644 index 0000000000000000000000000000000000000000..aafe4fc7209dcec9f334d18882f0cc41f7013b6b --- /dev/null +++ b/src/main/java/es/uvigo/esei/daa/entities/Pet.java @@ -0,0 +1,82 @@ +package es.uvigo.esei.daa.entities; + +public class Pet { + + private int id; + private String name; + private String kind; + private String breed; + private int owner; + + Pet(){} + + public Pet(int id, String name, String kind, String breed, int owner) { + this.setName(name); + this.setKind(kind); + this.setBreed(breed); + this.setOwner(owner); + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getKind() { + return kind; + } + + public void setKind(String kind) { + this.kind = kind; + } + + public String getBreed() { + return breed; + } + + public void setBreed(String breed) { + this.breed = breed; + } + + public int getOwner() { + return owner; + } + + public void setOwner(int owner) { + this.owner = owner; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = (int) (prime * result + id); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (!(obj instanceof Pet)) + return false; + Pet other = (Pet) obj; + if (id != other.id) + return false; + return true; + } +} +