From 7fe077dce6c683665f5e0e36e72f26f117e5e5fd Mon Sep 17 00:00:00 2001 From: Breixo Senra Date: Fri, 24 Oct 2025 14:02:11 +0200 Subject: [PATCH] Plantillas de JSF funcionando --- .../esei/xcs/jsf/PetDetailsManagedBean.java | 35 +++++++++ .../es/uvigo/esei/xcs/jsf/PetManagedBean.java | 18 ++--- .../es/uvigo/esei/xcs/jsf/VetManagedBean.java | 8 +- jsf/src/main/webapp/vet/petDetails.xhtml | 43 +++++++++++ jsf/src/main/webapp/vet/supervisedPets.xhtml | 48 ++++++++++++ jsf/src/main/webapp/vet/vaccines.xhtml | 73 +++++++++++-------- .../es/uvigo/esei/xcs/rest/VetResource.java | 20 ++--- .../es/uvigo/esei/xcs/service/PetService.java | 6 +- .../esei/xcs/service/VaccineService.java | 2 + .../es/uvigo/esei/xcs/service/VetService.java | 17 +++-- 10 files changed, 208 insertions(+), 62 deletions(-) create mode 100644 jsf/src/main/java/es/uvigo/esei/xcs/jsf/PetDetailsManagedBean.java create mode 100644 jsf/src/main/webapp/vet/petDetails.xhtml create mode 100644 jsf/src/main/webapp/vet/supervisedPets.xhtml diff --git a/jsf/src/main/java/es/uvigo/esei/xcs/jsf/PetDetailsManagedBean.java b/jsf/src/main/java/es/uvigo/esei/xcs/jsf/PetDetailsManagedBean.java new file mode 100644 index 0000000..75f8651 --- /dev/null +++ b/jsf/src/main/java/es/uvigo/esei/xcs/jsf/PetDetailsManagedBean.java @@ -0,0 +1,35 @@ +package es.uvigo.esei.xcs.jsf; + +import javax.annotation.PostConstruct; +import javax.enterprise.context.RequestScoped; +import javax.inject.Inject; +import javax.inject.Named; +import javax.faces.context.FacesContext; +import java.util.Map; + +import es.uvigo.esei.xcs.domain.entities.Pet; +import es.uvigo.esei.xcs.service.PetService; + +@Named("petDetails") +@RequestScoped +public class PetDetailsManagedBean { + @Inject + private PetService service; + + private Pet pet; + + @PostConstruct + public void init() { + Map params = FacesContext.getCurrentInstance() + .getExternalContext() + .getRequestParameterMap(); + String id = params.get("id"); + if (id != null) { + this.pet = service.get(Long.parseLong(id)); + } + } + + public Pet getPet() { + return pet; + } +} 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 836ee76..e6854fd 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 @@ -16,14 +16,14 @@ import es.uvigo.esei.xcs.service.PetService; @Named("pet") @RequestScoped public class PetManagedBean { -/* @Inject + @Inject private PetService service; private String name; private Date birth; private AnimalType animal; - private Integer id; + private Long id; private String errorMessage; @@ -65,19 +65,19 @@ public class PetManagedBean { return this.id != null; } - public Integer getId() { + public Long getId() { return id; } - public void setId(Integer id) { + public void setId(Long id) { this.id = id; } public List getPets() { - return this.service.list(); + return this.service.list(0, 100); } - public String edit(int petId) { + public String edit(Long petId) { final Pet pet = this.service.get(petId); this.id = pet.getId(); @@ -94,7 +94,7 @@ public class PetManagedBean { return this.getViewId(); } - public String remove(int id) { + public String remove(Long id) { this.service.remove(id); return redirectTo(this.getViewId()); @@ -110,7 +110,7 @@ public class PetManagedBean { this.service.update(pet); } else { - this.service.create(new Pet(name, animal, birth)); + //this.service.create(new Pet(name, animal, birth)); } this.clear(); @@ -137,5 +137,5 @@ public class PetManagedBean { private String getViewId() { return FacesContext.getCurrentInstance().getViewRoot().getViewId(); - }*/ + } } diff --git a/jsf/src/main/java/es/uvigo/esei/xcs/jsf/VetManagedBean.java b/jsf/src/main/java/es/uvigo/esei/xcs/jsf/VetManagedBean.java index ab30c39..ab12b02 100644 --- a/jsf/src/main/java/es/uvigo/esei/xcs/jsf/VetManagedBean.java +++ b/jsf/src/main/java/es/uvigo/esei/xcs/jsf/VetManagedBean.java @@ -61,9 +61,13 @@ public class VetManagedBean { public List getVets() { return this.service.list(); } + + public List getPets() { + return this.service.getPets(0, 100); + } - public String getPetNames(String login) { - return this.service.getPets(login, 0, 100).stream() + public String getPetNames() { + return this.service.getPets(0, 100).stream() .map(Pet::getName) .collect(joining(", ")); } diff --git a/jsf/src/main/webapp/vet/petDetails.xhtml b/jsf/src/main/webapp/vet/petDetails.xhtml new file mode 100644 index 0000000..1874831 --- /dev/null +++ b/jsf/src/main/webapp/vet/petDetails.xhtml @@ -0,0 +1,43 @@ + + + + + Detalle de Mascota + + + + +

Detalles de Mascota

+ + + + + + + + + + + + + + + +

Vacunas

+ + + Fecha + #{v.date} + + + Vacuna + #{v.vaccine.name} + + +
+
+
+ diff --git a/jsf/src/main/webapp/vet/supervisedPets.xhtml b/jsf/src/main/webapp/vet/supervisedPets.xhtml new file mode 100644 index 0000000..1cd9acd --- /dev/null +++ b/jsf/src/main/webapp/vet/supervisedPets.xhtml @@ -0,0 +1,48 @@ + + + + + + Vet - Pets Supervised + + + + +

Mascotas Supervisadas

+

Listado de las mascotas que supervisas (nombre y tipo de animal).

+
+ + + + + ID + #{supervisedPet.id} + + + Nombre + #{supervisedPet.name} + + + Tipo de Animal + #{supervisedPet.animal} + + + + Acciones + + + + + + + + + Error: #{vet.errorMessage} + + +
+ + diff --git a/jsf/src/main/webapp/vet/vaccines.xhtml b/jsf/src/main/webapp/vet/vaccines.xhtml index 3f22e63..466243c 100644 --- a/jsf/src/main/webapp/vet/vaccines.xhtml +++ b/jsf/src/main/webapp/vet/vaccines.xhtml @@ -16,21 +16,20 @@ ID#{v.id} Nombre#{v.name} Tipo#{v.class.simpleName} + + Doses + #{v.class.simpleName eq 'MultidoseVaccine' ? v.doses : ''} + - - Doses - #{v.doses} - - - - Periode - #{v.periode} - + + Periode + #{v.class.simpleName eq 'PeriodicVaccine' ? v.periode : ''} + - - PeriodicType - #{v.periodicType} - + + Periodic Type + #{v.class.simpleName eq 'PeriodicVaccine' ? v.periodicType : ''} + Acciones @@ -61,30 +60,40 @@ - + - - - - - - - - - - - + --> + + + + + + + + + + + + + + + + + + + + +
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 9da343a..f053e04 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,15 +98,15 @@ public class VetResource { } @GET - @Path("{login}/pet") + @Path("pet") public Response listPets( - @PathParam("login") String login, + //@PathParam("login") String login, @QueryParam("page") @DefaultValue("0") int page, @QueryParam("pageSize") @DefaultValue("10") int pageSize ) { - requireNonNull(login, "login can't be null"); + //requireNonNull(login, "login can't be null"); - return Response.ok(this.vetService.getPets(login, page, pageSize)).build(); + return Response.ok(this.vetService.getPets(page, pageSize)).build(); } @@ -155,7 +155,7 @@ public class VetResource { } } - @Path("{login}/pet/{petIdentifierType}/{petIdentifierValue}/vaccination") + @Path("pet/{petIdentifierType}/{petIdentifierValue}/vaccination") @GET public Response listVaccinations( @PathParam("login") String login, @@ -167,7 +167,7 @@ public class VetResource { requireNonNull(login, "login can't be null"); return Response.ok(this.vetService.getVaccinationsFromOwnPet( - login, + //login, petIdentifierType, petIdentifierValue, page, @@ -218,16 +218,16 @@ public class VetResource { @DELETE @Path("{login}/unassign/pet/{petId}") public Response unassignVetFromPet( - @PathParam("login") String vetLogin, + //@PathParam("login") String vetLogin, @PathParam("petId") Long petId ) { - requireNonNull(vetLogin, "vetLogin can't be null"); + //requireNonNull(vetLogin, "vetLogin can't be null"); requireNonNull(petId, "petId can't be null"); try { - petService.unassignVetFromPet(petId, vetLogin); + petService.unassignVetFromPet(petId); return Response.ok() - .entity("Vet " + vetLogin + " unassigned from pet " + petId) + //.entity("Vet " + vetLogin + " unassigned from pet " + petId) .build(); } catch (IllegalArgumentException e) { return Response.status(Response.Status.NOT_FOUND) 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 16125eb..664a2d0 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 @@ -175,15 +175,15 @@ public class PetService { } - public void unassignVetFromPet(Long petId, String vetLogin) { + public void unassignVetFromPet(Long petId) { requireNonNull(petId, "Pet ID can't be null"); - requireNonNull(vetLogin, "Vet login can't be null"); + //requireNonNull(vetLogin, "Vet login can't be null"); Pet pet = em.find(Pet.class, petId); if (pet == null) throw new IllegalArgumentException("Pet not found"); - Vet vet = em.find(Vet.class, vetLogin); + Vet vet = em.find(Vet.class, currentUser.getName()); if (vet == null) throw new IllegalArgumentException("Vet not found"); diff --git a/service/src/main/java/es/uvigo/esei/xcs/service/VaccineService.java b/service/src/main/java/es/uvigo/esei/xcs/service/VaccineService.java index 0ad391c..1973d4b 100644 --- a/service/src/main/java/es/uvigo/esei/xcs/service/VaccineService.java +++ b/service/src/main/java/es/uvigo/esei/xcs/service/VaccineService.java @@ -5,6 +5,7 @@ package es.uvigo.esei.xcs.service; import java.util.List; import javax.annotation.security.PermitAll; +import javax.annotation.security.RolesAllowed; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; @@ -18,6 +19,7 @@ import es.uvigo.esei.xcs.domain.entities.Vaccine; @Stateless @PermitAll +//@RolesAllowed("VET") public class VaccineService { @PersistenceContext private EntityManager em; diff --git a/service/src/main/java/es/uvigo/esei/xcs/service/VetService.java b/service/src/main/java/es/uvigo/esei/xcs/service/VetService.java index d45d0ac..cdb9095 100644 --- a/service/src/main/java/es/uvigo/esei/xcs/service/VetService.java +++ b/service/src/main/java/es/uvigo/esei/xcs/service/VetService.java @@ -2,11 +2,13 @@ package es.uvigo.esei.xcs.service; import static java.util.Objects.requireNonNull; +import java.security.Principal; import java.util.List; import javax.annotation.security.PermitAll; import javax.annotation.security.RolesAllowed; import javax.ejb.Stateless; +import javax.inject.Inject; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; @@ -19,6 +21,9 @@ import es.uvigo.esei.xcs.domain.entities.Vet; //@RolesAllowed("VET") @PermitAll public class VetService { + @Inject + private Principal currentUser; + @PersistenceContext EntityManager em; @@ -68,8 +73,8 @@ public class VetService { } - public List getPets(String login, int page , int pageSize) { - requireNonNull(login, "Login can't be null"); + public List getPets(int page , int pageSize) { + //requireNonNull(login, "Login can't be null"); if (page < 0) { throw new IllegalArgumentException("The page can't be negative"); } @@ -80,18 +85,18 @@ public class VetService { + "WHERE v.login = :login", Pet.class) .setFirstResult(page * pageSize) .setMaxResults(pageSize) - .setParameter("login", login) + .setParameter("login", currentUser.getName()) .getResultList(); } public List getVaccinationsFromOwnPet( - String login, + //String login, IdentifierType identifierType, String identifierValue, int page, int pageSize ){ - requireNonNull(login, "login can't be null"); + //requireNonNull(login, "login can't be null"); requireNonNull(identifierType, "pet's identifier type can't be null"); requireNonNull(identifierValue, "pet's identifier value can't be null"); if (page < 0) { @@ -110,7 +115,7 @@ public class VetService { + "i.identifierType = :identifierType AND " + "i.identifierValue = :identifierValue", Vaccination.class) - .setParameter("login", login) + .setParameter("login", currentUser.getName()) .setParameter("identifierType", identifierType) .setParameter("identifierValue", identifierValue) .setFirstResult(page * pageSize) -- 2.18.1