From f8cb9edccb150e7a245b1bf0c003e575e7557de7 Mon Sep 17 00:00:00 2001 From: Breixo Senra Date: Fri, 24 Oct 2025 23:56:08 +0200 Subject: [PATCH] =?UTF-8?q?Asignaci=C3=B3n=20de=20mascotas=20funcionando?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../uvigo/esei/xcs/domain/entities/Pet.java | 4 +-- jsf/pom.xml | 10 ++++++ .../es/uvigo/esei/xcs/jsf/PetManagedBean.java | 34 +++++++++++++++++- jsf/src/main/webapp/WEB-INF/template.xhtml | 2 +- jsf/src/main/webapp/WEB-INF/web.xml | 5 +++ jsf/src/main/webapp/index.xhtml | 2 ++ jsf/src/main/webapp/vet/pets.xhtml | 32 +++++++++++++++++ jsf/src/main/webapp/vet/vaccines.xhtml | 18 +++------- pom.xml | 18 ++++++++-- rest/pom.xml | 14 ++++++++ .../esei/xcs/rest/AdministratorResource.java | 12 +++++++ .../es/uvigo/esei/xcs/rest/PetResource.java | 16 +++++++-- .../es/uvigo/esei/xcs/rest/VetResource.java | 12 +++---- .../uvigo/esei/xcs/service/EmailService.java | 2 ++ .../es/uvigo/esei/xcs/service/PetService.java | 35 ++++++++++++++++++- 15 files changed, 186 insertions(+), 30 deletions(-) create mode 100644 jsf/src/main/webapp/vet/pets.xhtml diff --git a/domain/src/main/java/es/uvigo/esei/xcs/domain/entities/Pet.java b/domain/src/main/java/es/uvigo/esei/xcs/domain/entities/Pet.java index 567707a..2a8ca46 100644 --- a/domain/src/main/java/es/uvigo/esei/xcs/domain/entities/Pet.java +++ b/domain/src/main/java/es/uvigo/esei/xcs/domain/entities/Pet.java @@ -35,7 +35,7 @@ import javax.xml.bind.annotation.XmlTransient; @Entity(name = "Pet") -@XmlRootElement(name = "pet", namespace = "http://entities.domain.xcs.esei.uvigo.es") +@XmlRootElement(name = "pett", namespace = "http://entities.domain.xcs.esei.uvigo.es") @XmlAccessorType(XmlAccessType.FIELD) public class Pet implements Serializable { private static final long serialVersionUID = 1L; @@ -69,7 +69,7 @@ public class Pet implements Serializable { private Owner owner; @ManyToMany( - fetch = FetchType.LAZY, + fetch = FetchType.LAZY, //es LAZY cascade = { CascadeType.PERSIST, CascadeType.MERGE } ) @JoinTable( diff --git a/jsf/pom.xml b/jsf/pom.xml index 90c38e2..029093b 100644 --- a/jsf/pom.xml +++ b/jsf/pom.xml @@ -13,6 +13,16 @@ XCS Sample - JSF + + + org.primefaces + primefaces + + + org.primefaces.themes + bootstrap + + javax diff --git a/jsf/src/main/java/es/uvigo/esei/xcs/jsf/PetManagedBean.java b/jsf/src/main/java/es/uvigo/esei/xcs/jsf/PetManagedBean.java index e6854fd..d22c2c1 100644 --- a/jsf/src/main/java/es/uvigo/esei/xcs/jsf/PetManagedBean.java +++ b/jsf/src/main/java/es/uvigo/esei/xcs/jsf/PetManagedBean.java @@ -74,7 +74,9 @@ public class PetManagedBean { } public List getPets() { - return this.service.list(0, 100); + List list = this.service.getAll(0, 100); + System.out.println(list); + return list; } public String edit(Long petId) { @@ -138,4 +140,34 @@ public class PetManagedBean { private String getViewId() { return FacesContext.getCurrentInstance().getViewRoot().getViewId(); } + + public void assignToMe(Long petId) { + try { + service.assignVetToPet(petId); + } catch (Exception e) { + this.errorMessage = e.getMessage(); + } + } + + public void unassignFromMe(Long petId) { + try { + service.unassignVetFromPet(petId); + } catch (Exception e) { + this.errorMessage = e.getMessage(); + } + } + + public boolean isAssignedToMe(Pet pet) { + return this.service.isAssignedToCurrentVet(pet.getId()); + } + + public void toggleAssignment(Pet pet) { + if (isAssignedToMe(pet)) { + unassignFromMe(pet.getId()); + } else { + assignToMe(pet.getId()); + } + } + + } diff --git a/jsf/src/main/webapp/WEB-INF/template.xhtml b/jsf/src/main/webapp/WEB-INF/template.xhtml index 85c07dd..01fc39d 100644 --- a/jsf/src/main/webapp/WEB-INF/template.xhtml +++ b/jsf/src/main/webapp/WEB-INF/template.xhtml @@ -41,7 +41,7 @@ - + \ No newline at end of file diff --git a/jsf/src/main/webapp/WEB-INF/web.xml b/jsf/src/main/webapp/WEB-INF/web.xml index b79c6b3..c3e2b88 100644 --- a/jsf/src/main/webapp/WEB-INF/web.xml +++ b/jsf/src/main/webapp/WEB-INF/web.xml @@ -14,6 +14,11 @@ Development + + primefaces.THEME + bootstrap + + Faces Servlet diff --git a/jsf/src/main/webapp/index.xhtml b/jsf/src/main/webapp/index.xhtml index d8d9f5c..1e31f7b 100644 --- a/jsf/src/main/webapp/index.xhtml +++ b/jsf/src/main/webapp/index.xhtml @@ -4,6 +4,7 @@ xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:f="http://xmlns.jcp.org/jsf/core" xmlns:ui="http://xmlns.jcp.org/jsf/facelets" + xmlns:p="http://primefaces.org/ui" xmlns:a="http://xmlns.jcp.org/jsf/passthrough"> Pet Store - Index @@ -13,6 +14,7 @@

Welcome!

This is the Pet Store web page, where you can manage your pets. Please, login to continue.
+
diff --git a/jsf/src/main/webapp/vet/pets.xhtml b/jsf/src/main/webapp/vet/pets.xhtml new file mode 100644 index 0000000..dbb5030 --- /dev/null +++ b/jsf/src/main/webapp/vet/pets.xhtml @@ -0,0 +1,32 @@ + + + Mis Mascotas + + + + + + Nombre + #{p.name} + + + Nacimiento + #{p.birth} + + + Tipo + #{p.animal} + + + Acción + + + + + + + + diff --git a/jsf/src/main/webapp/vet/vaccines.xhtml b/jsf/src/main/webapp/vet/vaccines.xhtml index 466243c..1f9c4a2 100644 --- a/jsf/src/main/webapp/vet/vaccines.xhtml +++ b/jsf/src/main/webapp/vet/vaccines.xhtml @@ -56,20 +56,10 @@ - + - - - - - @@ -77,7 +67,7 @@ - @@ -85,7 +75,7 @@ - diff --git a/pom.xml b/pom.xml index d051f3d..17e6b00 100644 --- a/pom.xml +++ b/pom.xml @@ -79,8 +79,18 @@ - - + + + org.primefaces + primefaces + 13.0.10 + + + org.primefaces.themes + bootstrap + 1.1.0 + + @@ -744,7 +754,9 @@ => "required", "module-options" => [("dsJndiName" => "java:jboss/datasources/xcs"),("principalsQuery" => "SELECT password FROM User WHERE login=?"),("rolesQuery" => "SELECT role, 'Roles' FROM User WHERE login=?"),("hashAlgorithm" => "MD5"),("hashEncoding" => "hex"),("ignorePasswordCase" => "true")]}]) - + /socket-binding-group=standard-sockets/remote-destination-outbound-socket-binding=mail-smtp:write-attribute(name=port,value=2525) + :reload + diff --git a/rest/pom.xml b/rest/pom.xml index 34c97b3..9dc514b 100644 --- a/rest/pom.xml +++ b/rest/pom.xml @@ -13,6 +13,20 @@ XCS Sample - REST + + + + + org.primefaces + primefaces + + + org.primefaces.themes + bootstrap + + + + javax diff --git a/rest/src/main/java/es/uvigo/esei/xcs/rest/AdministratorResource.java b/rest/src/main/java/es/uvigo/esei/xcs/rest/AdministratorResource.java index a0a78fa..2835e35 100644 --- a/rest/src/main/java/es/uvigo/esei/xcs/rest/AdministratorResource.java +++ b/rest/src/main/java/es/uvigo/esei/xcs/rest/AdministratorResource.java @@ -4,6 +4,7 @@ package es.uvigo.esei.xcs.rest; import javax.ejb.EJB; import javax.ws.rs.Consumes; import javax.ws.rs.GET; +import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; @@ -11,6 +12,7 @@ import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import es.uvigo.esei.xcs.service.AdministratorService; +import es.uvigo.esei.xcs.service.EmailService; @Path("administrator") @Consumes(MediaType.APPLICATION_JSON) @@ -20,9 +22,19 @@ public class AdministratorResource { @EJB AdministratorService service; + @EJB + EmailService emailService; + @GET public Response list(@QueryParam("page") int page, @QueryParam("pageSize") int pageSize) { return Response.ok(this.service.list(page, pageSize)).build(); } + @POST + public Response sendEmail() { + this.emailService.send("email@fake.email", "Topic", "Text Message"); + return Response.ok().build(); + } + + } diff --git a/rest/src/main/java/es/uvigo/esei/xcs/rest/PetResource.java b/rest/src/main/java/es/uvigo/esei/xcs/rest/PetResource.java index 20db74c..7d1b6a1 100644 --- a/rest/src/main/java/es/uvigo/esei/xcs/rest/PetResource.java +++ b/rest/src/main/java/es/uvigo/esei/xcs/rest/PetResource.java @@ -7,6 +7,7 @@ import javax.ejb.EJBAccessException; import javax.persistence.EntityExistsException; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; +import javax.ws.rs.DefaultValue; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.PUT; @@ -28,7 +29,7 @@ import es.uvigo.esei.xcs.service.PetService; * * @author Miguel Reboiro Jato */ -@Path("pet") +@Path("pettt") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public class PetResource { @@ -67,8 +68,19 @@ public class PetResource { * @return an {@code OK} response containing the complete list of pets of * the current owner. */ + + @GET + @Path("lista") + public Response getAll( + @QueryParam("page") @DefaultValue("0") int page, + @QueryParam("pageSize") @DefaultValue("10") int pageSize) { + return Response.ok(this.service.getAll(page, pageSize)).build(); + } + @GET - public Response list(@QueryParam("page") int page, @QueryParam("pageSize") int pageSize) { + public Response list( + @QueryParam("page") @DefaultValue("0") int page, + @QueryParam("pageSize") @DefaultValue("10") int pageSize) { return Response.ok(this.service.list(page, pageSize)).build(); } diff --git a/rest/src/main/java/es/uvigo/esei/xcs/rest/VetResource.java b/rest/src/main/java/es/uvigo/esei/xcs/rest/VetResource.java index f053e04..17a70c2 100644 --- a/rest/src/main/java/es/uvigo/esei/xcs/rest/VetResource.java +++ b/rest/src/main/java/es/uvigo/esei/xcs/rest/VetResource.java @@ -98,7 +98,7 @@ public class VetResource { } @GET - @Path("pet") + @Path("pets") public Response listPets( //@PathParam("login") String login, @QueryParam("page") @DefaultValue("0") int page, @@ -111,7 +111,7 @@ public class VetResource { @GET - @Path("pet/{petId}") + @Path("pets/{petId}") public Response getPet(@PathParam("petId") Long petId) { return Response.ok(this.petService.get(petId)).build(); } @@ -155,7 +155,7 @@ public class VetResource { } } - @Path("pet/{petIdentifierType}/{petIdentifierValue}/vaccination") + @Path("pets/{petIdentifierType}/{petIdentifierValue}/vaccination") @GET public Response listVaccinations( @PathParam("login") String login, @@ -175,7 +175,7 @@ public class VetResource { )).build(); } - @Path("/pet/{petIdentifierType}/{petIdentifierValue}/vaccination") + @Path("pets/{petIdentifierType}/{petIdentifierValue}/vaccination") @POST public Response registerVaccination( @QueryParam("date") String date, @@ -195,7 +195,7 @@ public class VetResource { @POST - @Path("/assign/pet/{petId}") + @Path("/assign/pets/{petId}") public Response assignVetToPet( //@PathParam("login") String vetLogin, @PathParam("petId") Long petId @@ -216,7 +216,7 @@ public class VetResource { } @DELETE - @Path("{login}/unassign/pet/{petId}") + @Path("{login}/unassign/pets/{petId}") public Response unassignVetFromPet( //@PathParam("login") String vetLogin, @PathParam("petId") Long petId diff --git a/service/src/main/java/es/uvigo/esei/xcs/service/EmailService.java b/service/src/main/java/es/uvigo/esei/xcs/service/EmailService.java index 8bd0c92..119c739 100644 --- a/service/src/main/java/es/uvigo/esei/xcs/service/EmailService.java +++ b/service/src/main/java/es/uvigo/esei/xcs/service/EmailService.java @@ -7,12 +7,14 @@ import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.annotation.Resource; +import javax.annotation.security.PermitAll; import javax.mail.internet.MimeMessage; import java.util.logging.Level; import java.util.logging.Logger; @Stateless +@PermitAll public class EmailService { @Resource(name = "java:jboss/mail/Default") private Session session; diff --git a/service/src/main/java/es/uvigo/esei/xcs/service/PetService.java b/service/src/main/java/es/uvigo/esei/xcs/service/PetService.java index 664a2d0..b4591cb 100644 --- a/service/src/main/java/es/uvigo/esei/xcs/service/PetService.java +++ b/service/src/main/java/es/uvigo/esei/xcs/service/PetService.java @@ -2,6 +2,7 @@ package es.uvigo.esei.xcs.service; import static java.util.Objects.requireNonNull; +import java.io.Console; import java.security.Principal; import java.util.List; @@ -25,7 +26,7 @@ import es.uvigo.esei.xcs.domain.entities.Vet; * @author Miguel Reboiro Jato */ @Stateless -//@RolesAllowed("OWNER") +//@RolesAllowed("VET") @PermitAll public class PetService { @Inject @@ -59,6 +60,20 @@ public class PetService { return em.find(Pet.class, id); } + public List getAll(int page, int pageSize) { + if (page < 0) { + throw new IllegalArgumentException("The page can't be negative"); + } + if (pageSize <= 0) { + throw new IllegalArgumentException("The page size can't be negative or zero"); + } + return em.createQuery("SELECT p FROM Pet p", Pet.class) + .setFirstResult(page * pageSize) + .setMaxResults(pageSize) + .getResultList(); + } + + /** * Returns the complete list of pets of the current owner. * @@ -192,6 +207,24 @@ public class PetService { em.merge(pet); } + public Principal getCurrentUser() { + return this.currentUser; + } + public boolean isAssignedToCurrentVet(Long petId) { + requireNonNull(petId, "Pet ID can't be null"); + + + Long count = em.createQuery( + "SELECT COUNT(p) FROM Pet p JOIN p.vets v WHERE p.id = :petId AND v.login = :login", + Long.class + ) + .setParameter("petId", petId) + .setParameter("login", currentUser.getName()) + .getSingleResult(); + + return count > 0; + } + } -- 2.18.1