From f2341cbc51dc4dc235f261ff77ee17c5379be29e Mon Sep 17 00:00:00 2001 From: cyanide4all Date: Wed, 15 Feb 2017 03:12:26 +0100 Subject: [PATCH] Pet page not working yet, but progress is made --- .../java/es/uvigo/esei/daa/dao/PetDAO.java | 200 +++++++++++++++++ .../java/es/uvigo/esei/daa/entities/Pet.java | 32 +-- .../es/uvigo/esei/daa/rest/PetResource.java | 207 ++++++++++++++++++ src/main/webapp/js/dao/pet.js | 2 +- src/main/webapp/pet.html | 10 +- 5 files changed, 429 insertions(+), 22 deletions(-) create mode 100644 src/main/java/es/uvigo/esei/daa/dao/PetDAO.java create mode 100644 src/main/java/es/uvigo/esei/daa/rest/PetResource.java diff --git a/src/main/java/es/uvigo/esei/daa/dao/PetDAO.java b/src/main/java/es/uvigo/esei/daa/dao/PetDAO.java new file mode 100644 index 0000000..59dc68e --- /dev/null +++ b/src/main/java/es/uvigo/esei/daa/dao/PetDAO.java @@ -0,0 +1,200 @@ +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.util.LinkedList; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; + +import es.uvigo.esei.daa.entities.Pet; + +/** + * DAO class for the {@link Pet} entities. + * + * @author Cya + * + */ +public class PetDAO extends DAO { + private final static Logger LOG = Logger.getLogger(PetDAO.class.getName()); + //TODO junto con lo de justo debajo + private int ownerID = 1; + + public int getOwnerID(){ + return ownerID; + } + /*//TODO ESTO IGUAL NO FUNCIONA LO TESTEO LUEGO + public PetDAO(int ownerID){ + this.ownerID = ownerID; + } + */ + /** + * Returns a pet stored persisted in the system. + * + * @param id identifier of the pet. + * @return a pet with the provided identifier. + * @throws DAOException if an error happens while retrieving the pet. + * @throws IllegalArgumentException if the provided id does not corresponds + * with any persisted pet. + */ + public Pet get(int id) + throws DAOException, IllegalArgumentException { + try (final Connection conn = this.getConnection()) { + final String query = "SELECT * FROM pet 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); + } + } + + /** + * Returns a list with all the pets persisted in the system. + * + * @return a list with all the pet persisted in the system. + * @throws DAOException if an error happens while retrieving the pets. + */ + public List list() throws DAOException { + try (final Connection conn = this.getConnection()) { + final String query = "SELECT * FROM pet"; + + try (final PreparedStatement statement = conn.prepareStatement(query)) { + try (final ResultSet result = statement.executeQuery()) { + final List petList = new LinkedList<>(); + + while (result.next()) { + petList.add(rowToEntity(result)); + } + + return petList; + } + } + } catch (SQLException e) { + LOG.log(Level.SEVERE, "Error listing pets", e); + throw new DAOException(e); + } + } + + /** + * Persists a new pet in the system. An identifier will be assigned + * automatically to the new pet. + * + * @param name name of the new pet. Can't be {@code null}. + * @return a {@link Pet} entity representing the persisted pet. + * @throws DAOException if an error happens while persisting the new pet. + * @throws IllegalArgumentException if the name is {@code null}. + */ + public Pet add(String name) + throws DAOException, IllegalArgumentException { + if (name == null) { + throw new IllegalArgumentException("name can't be null"); + } + + try (Connection conn = this.getConnection()) { + final String query = "INSERT INTO pet VALUES(null, ?)"; + + try (PreparedStatement statement = conn.prepareStatement(query, Statement.RETURN_GENERATED_KEYS)) { + statement.setString(1, name); + + if (statement.executeUpdate() == 1) { + try (ResultSet resultKeys = statement.getGeneratedKeys()) { + if (resultKeys.next()) { + return new Pet(resultKeys.getInt(1), name,ownerID); + } else { + LOG.log(Level.SEVERE, "Error retrieving inserted id"); + throw new SQLException("Error retrieving inserted id"); + } + } + } 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); + } + } + + /** + * Modifies a pet previously persisted in the system. The pet will be + * retrieved by the provided id and its current name will be + * replaced with the provided. + * + * @param pet a {@link Pet} entity with the new data. + * @throws DAOException if an error happens while modifying the pet. + * @throws IllegalArgumentException if the pet is {@code null}. + */ + 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 pet SET name=? WHERE id=?"; + + try (PreparedStatement statement = conn.prepareStatement(query)) { + statement.setString(1, pet.getName()); + statement.setInt(2, pet.getId()); + + if (statement.executeUpdate() != 1) { + throw new IllegalArgumentException("name and surname can't be null"); + } + } + } catch (SQLException e) { + LOG.log(Level.SEVERE, "Error modifying a pet", e); + throw new DAOException(); + } + } + + /** + * Removes a persisted pet from the system. + * + * @param id identifier of the pet to be deleted. + * @throws DAOException if an error happens while deleting the pet. + * @throws IllegalArgumentException if the provided id does not corresponds + * with any persisted pet. + */ + public void delete(int id) + throws DAOException, IllegalArgumentException { + try (final Connection conn = this.getConnection()) { + final String query = "DELETE FROM pet 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 a pet", e); + throw new DAOException(e); + } + } + + private Pet rowToEntity(ResultSet row) throws SQLException { + return new Pet( + row.getInt("id"), + row.getString("name"), + //TODO EXTREMO ESTO ES PROVISIONAL + ownerID + //TODO REPITO PROVISIONAL MECA + ); + } +} diff --git a/src/main/java/es/uvigo/esei/daa/entities/Pet.java b/src/main/java/es/uvigo/esei/daa/entities/Pet.java index b42ef44..87be65b 100644 --- a/src/main/java/es/uvigo/esei/daa/entities/Pet.java +++ b/src/main/java/es/uvigo/esei/daa/entities/Pet.java @@ -3,14 +3,14 @@ package es.uvigo.esei.daa.entities; import static java.util.Objects.requireNonNull; /** - * Entity that represents a pet from a person + * Entity that represents a pet from a int * * Created by cya on 2/9/17. */ public class Pet { private int id; private String name; - private Person owner; + private int ownerID; // Constructor needed for the JSON conversion Pet() {} @@ -20,12 +20,12 @@ public class Pet { * * @param id identifier of the pet. * @param name name of the pet. - * @param owner person who owns the pet. + * @param ownerID int who owns the pet. */ - public Pet(int id, String name, Person owner) { + public Pet(int id, String name, int ownerID) { this.id = id; this.setName(name); - this.setOwner(owner); + this.setownerID(ownerID); } /** @@ -47,30 +47,30 @@ public class Pet { } /** - * Set the name of this person. + * Set the name of this int. * - * @param name the new name of the person. + * @param name the new name of the int. * @throws NullPointerException if the {@code name} is {@code null}. */ public void setName(String name) { this.name = requireNonNull(name, "Name can't be null"); } /** - * Returns the owner of the pet. + * Returns the ownerID of the pet. * - * @return the owner of the pet. + * @return the ownerID of the pet. */ - public Person getOwner() { - return owner; + public int getownerID() { + return ownerID; } /** - * Set the owner of this pet. + * Set the ownerID of this pet. * - * @param owner the owner of the pet. - * @throws NullPointerException if the {@code owner} is {@code null}. + * @param ownerID the ownerID of the pet. + * @throws NullPointerException if the {@code ownerID} is {@code null}. */ - public void setOwner(Person owner) { - this.owner = requireNonNull(owner, "Owner can't be a null reference"); + public void setownerID(int ownerID) { + this.ownerID = requireNonNull(ownerID, "ownerID can't be a null reference"); } } diff --git a/src/main/java/es/uvigo/esei/daa/rest/PetResource.java b/src/main/java/es/uvigo/esei/daa/rest/PetResource.java new file mode 100644 index 0000000..1d5fccf --- /dev/null +++ b/src/main/java/es/uvigo/esei/daa/rest/PetResource.java @@ -0,0 +1,207 @@ +package es.uvigo.esei.daa.rest; + +import java.util.logging.Level; +import java.util.logging.Logger; + +import javax.ws.rs.DELETE; +import javax.ws.rs.FormParam; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import es.uvigo.esei.daa.dao.DAOException; +import es.uvigo.esei.daa.dao.PetDAO; +import es.uvigo.esei.daa.entities.Pet; + +/** + * REST resource for managing pets. + * + * @author Martín Vázquez Torres + */ +@Path("/pet") +@Produces(MediaType.APPLICATION_JSON) +public class PetResource { + private final static Logger LOG = Logger.getLogger(PetResource.class.getName()); + + private final PetDAO dao; + + /** + * Constructs a new instance of {@link PetResource}. + */ + public PetResource() { + this(new PetDAO()); + } + + // Needed for testing purposes (parece ser) + PetResource(PetDAO dao) { + this.dao = dao; + } + + /** + * Returns a pet with the provided identifier. + * + * @param id the identifier of the pet to retrieve. + * @return a 200 OK response with a pet that has the provided identifier. + * If the identifier does not corresponds with any user, a 400 Bad Request + * response with an error message will be returned. If an error happens + * while retrieving the list, a 500 Internal Server Error response with an + * error message will be returned. + */ + @GET + @Path("/{id}") + public Response get( + @PathParam("id") int id + ) { + try { + final Pet pet = this.dao.get(id); + + return Response.ok(pet).build(); + } catch (IllegalArgumentException iae) { + LOG.log(Level.FINE, "Invalid pet id in get method", iae); + + return Response.status(Response.Status.BAD_REQUEST) + .entity(iae.getMessage()) + .build(); + } catch (DAOException e) { + LOG.log(Level.SEVERE, "Error getting a pet", e); + + return Response.serverError() + .entity(e.getMessage()) + .build(); + } + } + + /** + * Returns the complete list of the pets stored in the system. + * + * @return a 200 OK response with the complete list of pets stored in the + * system. If an error happens while retrieving the list, a 500 Internal + * Server Error response with an error message will be returned. + */ + @GET + public Response list() { + try { + return Response.ok(this.dao.list()).build(); + } catch (DAOException e) { + LOG.log(Level.SEVERE, "Error listing pets", e); + return Response.serverError().entity(e.getMessage()).build(); + } + } + + /** + * Creates a new pet in the system. + * + * @param name the name of the new pet. + * @return a 200 OK response with a pet that has been created. If the + * name is not provided, a 400 Bad Request response with an + * error message will be returned. If an error happens while retrieving the + * list, a 500 Internal Server Error response with an error message will be + * returned. + */ + @POST + public Response add( + @FormParam("name") String name + ) { + try { + final Pet newPet = this.dao.add(name); + + return Response.ok(newPet).build(); + } catch (IllegalArgumentException iae) { + LOG.log(Level.FINE, "Invalid pet id in add method", iae); + + return Response.status(Response.Status.BAD_REQUEST) + .entity(iae.getMessage()) + .build(); + } catch (DAOException e) { + LOG.log(Level.SEVERE, "Error adding a pet", e); + + return Response.serverError() + .entity(e.getMessage()) + .build(); + } + } + + /** + * Modifies the data of a pet. + * + * @param id identifier of the pet to modify. + * @param name the new name of the pet. + * @return a 200 OK response with a pet that has been modified. If the + * identifier does not corresponds with any user or the name is + * not provided, a 400 Bad Request response with an error message will be + * returned. If an error happens while retrieving the list, a 500 Internal + * Server Error response with an error message will be returned. + */ + @PUT + @Path("/{id}") + public Response modify( + @PathParam("id") int id, + @FormParam("name") String name + ) { + try { + final Pet modifiedPet = new Pet(id, name, dao.getOwnerID()); + this.dao.modify(modifiedPet); + + return Response.ok(modifiedPet).build(); + } catch (NullPointerException npe) { + final String message = String.format("Invalid data for pet (name: %s)", name); + + LOG.log(Level.FINE, message); + + return Response.status(Response.Status.BAD_REQUEST) + .entity(message) + .build(); + } catch (IllegalArgumentException iae) { + LOG.log(Level.FINE, "Invalid pet id in modify method", iae); + + return Response.status(Response.Status.BAD_REQUEST) + .entity(iae.getMessage()) + .build(); + } catch (DAOException e) { + LOG.log(Level.SEVERE, "Error modifying a pet", e); + + return Response.serverError() + .entity(e.getMessage()) + .build(); + } + } + + /** + * Deletes a pet from the system. + * + * @param id the identifier of the pet to be deleted. + * @return a 200 OK response with the identifier of the pet that has + * been deleted. If the identifier does not corresponds with any user, a 400 + * Bad Request response with an error message will be returned. If an error + * happens while retrieving the list, a 500 Internal Server Error response + * with an error message will be returned. + */ + @DELETE + @Path("/{id}") + public Response delete( + @PathParam("id") int id + ) { + try { + this.dao.delete(id); + + return Response.ok(id).build(); + } catch (IllegalArgumentException iae) { + LOG.log(Level.FINE, "Invalid pet id in delete method", iae); + + return Response.status(Response.Status.BAD_REQUEST) + .entity(iae.getMessage()) + .build(); + } catch (DAOException e) { + LOG.log(Level.SEVERE, "Error deleting a pet", e); + + return Response.serverError() + .entity(e.getMessage()) + .build(); + } + } +} diff --git a/src/main/webapp/js/dao/pet.js b/src/main/webapp/js/dao/pet.js index 08ae3dd..a60921d 100644 --- a/src/main/webapp/js/dao/pet.js +++ b/src/main/webapp/js/dao/pet.js @@ -12,7 +12,7 @@ var PetDAO = (function() { }; function PetDAO() { - this.listPet = function(done, fail, always) { + this.listPets = function(done, fail, always) { requestByAjax({ url: resourcePath, type: 'GET' diff --git a/src/main/webapp/pet.html b/src/main/webapp/pet.html index ac81ccd..4328875 100644 --- a/src/main/webapp/pet.html +++ b/src/main/webapp/pet.html @@ -14,11 +14,11 @@ + view.init(); + }); + \ No newline at end of file -- 2.18.1