From a92ee916a02d46cd1868efdf10359a01b1593249 Mon Sep 17 00:00:00 2001 From: Breixo Senra Date: Sun, 26 Oct 2025 02:56:37 +0200 Subject: [PATCH] Restricciones de URL /vet a users VET --- .../uvigo/esei/xcs/jsf/OwnerManagedBean.java | 8 +- .../esei/xcs/jsf/PetDetailsManagedBean.java | 2 - .../es/uvigo/esei/xcs/jsf/VetManagedBean.java | 42 +++- jsf/src/main/webapp/WEB-INF/web.xml | 18 ++ jsf/src/main/webapp/admin/owners.xhtml | 2 +- jsf/src/main/webapp/vet/petDetails.xhtml | 22 +- jsf/src/main/webapp/vet/pets.xhtml | 37 ++-- jsf/src/main/webapp/vet/supervisedPets.xhtml | 32 +-- jsf/src/main/webapp/vet/vaccines.xhtml | 196 +++++++++--------- .../uvigo/esei/xcs/service/OwnerService.java | 25 ++- .../es/uvigo/esei/xcs/service/PetService.java | 4 +- .../esei/xcs/service/VaccinationService.java | 5 + .../es/uvigo/esei/xcs/service/VetService.java | 22 +- 13 files changed, 246 insertions(+), 169 deletions(-) diff --git a/jsf/src/main/java/es/uvigo/esei/xcs/jsf/OwnerManagedBean.java b/jsf/src/main/java/es/uvigo/esei/xcs/jsf/OwnerManagedBean.java index 6b2bece..8cdf115 100644 --- a/jsf/src/main/java/es/uvigo/esei/xcs/jsf/OwnerManagedBean.java +++ b/jsf/src/main/java/es/uvigo/esei/xcs/jsf/OwnerManagedBean.java @@ -16,7 +16,7 @@ import es.uvigo.esei.xcs.service.OwnerService; @Named("owner") @RequestScoped public class OwnerManagedBean { - /*@Inject + @Inject private OwnerService service; private String login; @@ -59,11 +59,11 @@ public class OwnerManagedBean { } public List getOwners() { - return this.service.list(); + return this.service.list(0, 100); } public String getPetNames(String login) { - return this.service.getPets(login).stream() + return this.service.getPets(0, 100).stream() .map(Pet::getName) .collect(joining(", ")); } @@ -121,5 +121,5 @@ public class OwnerManagedBean { private String getViewId() { return FacesContext.getCurrentInstance().getViewRoot().getViewId(); - }*/ + } } 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 index beb9948..9f8aba3 100644 --- a/jsf/src/main/java/es/uvigo/esei/xcs/jsf/PetDetailsManagedBean.java +++ b/jsf/src/main/java/es/uvigo/esei/xcs/jsf/PetDetailsManagedBean.java @@ -38,8 +38,6 @@ public class PetDetailsManagedBean implements Serializable{ @Inject private VaccinationService vaccinationService; - @Inject - private EmailService emailService; private Pet pet; 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 ab12b02..0fa9841 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 @@ -2,22 +2,32 @@ package es.uvigo.esei.xcs.jsf; import static java.util.stream.Collectors.joining; +import java.io.Serializable; import java.util.List; +import java.util.Map; +import javax.annotation.PostConstruct; import javax.enterprise.context.RequestScoped; import javax.faces.context.FacesContext; +import javax.faces.view.ViewScoped; import javax.inject.Inject; import javax.inject.Named; +import org.primefaces.model.FilterMeta; +import org.primefaces.model.LazyDataModel; +import org.primefaces.model.SortMeta; + import es.uvigo.esei.xcs.domain.entities.Pet; import es.uvigo.esei.xcs.domain.entities.Vet; import es.uvigo.esei.xcs.service.VetService; @Named("vet") -@RequestScoped -public class VetManagedBean { +//@RequestScoped +@ViewScoped +public class VetManagedBean implements Serializable{ + private static final long serialVersionUID = 1L; - @Inject + @Inject private VetService service; private String login; @@ -25,6 +35,28 @@ public class VetManagedBean { private boolean editing; private String errorMessage; + + private LazyDataModel pets; + + @PostConstruct + public void init() { + pets = new LazyDataModel() { + @Override + public List load(int first, int pageSize, Map sortBy, Map filterBy) { + return service.getPets(first, pageSize); + } + + @Override + public int count(Map filterBy) { + return service.countPets(); + } + }; + } + + public LazyDataModel getPets() { + return pets; + } + public String getLogin() { return login; @@ -62,9 +94,9 @@ public class VetManagedBean { return this.service.list(); } - public List getPets() { + /*public List getPets() { return this.service.getPets(0, 100); - } + }*/ public String getPetNames() { return this.service.getPets(0, 100).stream() diff --git a/jsf/src/main/webapp/WEB-INF/web.xml b/jsf/src/main/webapp/WEB-INF/web.xml index c3e2b88..dd9146c 100644 --- a/jsf/src/main/webapp/WEB-INF/web.xml +++ b/jsf/src/main/webapp/WEB-INF/web.xml @@ -60,6 +60,20 @@ OWNER + + + + vet + /vet/* + POST + GET + PUT + DELETE + + + VET + + @@ -70,5 +84,9 @@ OWNER + + + VET + \ No newline at end of file diff --git a/jsf/src/main/webapp/admin/owners.xhtml b/jsf/src/main/webapp/admin/owners.xhtml index 77f57f5..050be53 100644 --- a/jsf/src/main/webapp/admin/owners.xhtml +++ b/jsf/src/main/webapp/admin/owners.xhtml @@ -1,4 +1,4 @@ -w + + +

Detalles de #{petDetails.pet.name}

+
-

Detalles de Mascota

@@ -28,10 +30,22 @@ + +

Identificadores

+ + + Valor + #{id.value} + + + Tipo + #{id.type} + +

Vacunas

- + Fecha #{v.date} @@ -42,7 +56,6 @@ -

Registrar nueva vacunación

@@ -61,9 +74,6 @@ - - -
diff --git a/jsf/src/main/webapp/vet/pets.xhtml b/jsf/src/main/webapp/vet/pets.xhtml index 107f69e..9e4b7e8 100644 --- a/jsf/src/main/webapp/vet/pets.xhtml +++ b/jsf/src/main/webapp/vet/pets.xhtml @@ -1,25 +1,34 @@ + xmlns:ui="http://xmlns.jcp.org/jsf/facelets" + xmlns:f="http://xmlns.jcp.org/jsf/core"> Mis Mascotas - - - #{p.name} - #{p.birth} - #{p.animal} - - - - - - - + + +

Mascotas

+

Listado de las mascotas

+
+ + + + #{p.name} + #{p.birth} + #{p.animal} + + + + + + + + +
diff --git a/jsf/src/main/webapp/vet/supervisedPets.xhtml b/jsf/src/main/webapp/vet/supervisedPets.xhtml index 1cd9acd..7e32a4e 100644 --- a/jsf/src/main/webapp/vet/supervisedPets.xhtml +++ b/jsf/src/main/webapp/vet/supervisedPets.xhtml @@ -3,6 +3,7 @@ @@ -16,27 +17,16 @@ - - - ID - #{supervisedPet.id} - - - Nombre - #{supervisedPet.name} - - - Tipo de Animal - #{supervisedPet.animal} - - - - Acciones - - - - - + + #{supervisedPet.id} + #{supervisedPet.name} + #{supervisedPet.animal} + + + + + + diff --git a/jsf/src/main/webapp/vet/vaccines.xhtml b/jsf/src/main/webapp/vet/vaccines.xhtml index 062d31b..280f9af 100644 --- a/jsf/src/main/webapp/vet/vaccines.xhtml +++ b/jsf/src/main/webapp/vet/vaccines.xhtml @@ -9,102 +9,106 @@ -

CRUD de Vacunas

- - - - ID#{v.id} - Nombre#{v.name} - Tipo#{v.class.simpleName} - - Doses - #{v.class.simpleName eq 'MultidoseVaccine' ? v.doses : ''} - - - - Periode - #{v.class.simpleName eq 'PeriodicVaccine' ? v.periode : ''} - - - - Periodic Type - #{v.class.simpleName eq 'PeriodicVaccine' ? v.periodicType : ''} - - - - Acciones - - - - - - - -
- - - -

Añadir nueva vacuna

- - - - - - - - - - - - - - - - - - - + + +

Gestión de Vacunas

+
+ + + + ID#{v.id} + Nombre#{v.name} + Tipo#{v.class.simpleName} + + Doses + #{v.class.simpleName eq 'MultidoseVaccine' ? v.doses : ''} + + + + Periode + #{v.class.simpleName eq 'PeriodicVaccine' ? v.periode : ''} + + + + Periodic Type + #{v.class.simpleName eq 'PeriodicVaccine' ? v.periodicType : ''} + + + + Acciones + + + + + + + +
+ + + +

Añadir nueva vacuna

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
- - - - - - - - - - - - - -
- -
- -
- - - -

Editar vacuna #{vaccine.selectedVaccine.name}

- - - - - - -
- - -
- + + +

Editar vacuna #{vaccine.selectedVaccine.name}

+ + + + + + +
+ + +
+
+ diff --git a/service/src/main/java/es/uvigo/esei/xcs/service/OwnerService.java b/service/src/main/java/es/uvigo/esei/xcs/service/OwnerService.java index b2418be..7434bda 100644 --- a/service/src/main/java/es/uvigo/esei/xcs/service/OwnerService.java +++ b/service/src/main/java/es/uvigo/esei/xcs/service/OwnerService.java @@ -2,9 +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.EntityExistsException; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; @@ -21,11 +25,16 @@ import es.uvigo.esei.xcs.domain.entities.Vaccination; * @author Miguel Reboiro Jato */ @Stateless -@RolesAllowed("ADMIN") +//@RolesAllowed("ADMIN") +@PermitAll public class OwnerService { @PersistenceContext private EntityManager em; + @Inject + private Principal currentUser; + + /** * Returns the owner identified by {@code login}. If there is no owner with * the specified login, {@code null} will be returned. @@ -131,20 +140,18 @@ public class OwnerService { * @throws IllegalArgumentException if {@code login} is {@code null} or it * does not identifies a valid owner. */ - public List getPets(String login, 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"); - } + public List getPets(int first, int pageSize) { + if (first < 0) throw new IllegalArgumentException("First can't be negative"); + if (pageSize <= 0) throw new IllegalArgumentException("Page size must be positive"); + return em.createQuery("SELECT p FROM Owner o " + "JOIN o.pets p " + "WHERE " + "o.login = :login", Pet.class) - .setFirstResult(page * pageSize) + .setFirstResult(first) .setMaxResults(pageSize) + .setParameter("login", currentUser.getName()) .getResultList(); } 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 6ea6125..9c113fa 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 @@ -17,6 +17,7 @@ import javax.persistence.PersistenceContext; import es.uvigo.esei.xcs.domain.entities.Owner; import es.uvigo.esei.xcs.domain.entities.Pet; +import es.uvigo.esei.xcs.domain.entities.Vaccination; import es.uvigo.esei.xcs.domain.entities.Vaccine; import es.uvigo.esei.xcs.domain.entities.Vet; @@ -176,8 +177,6 @@ public class PetService { em.remove(pet); } - - public List getVaccinesByPetId(Long id, int page, int pageSize){ if (page < 0) { throw new IllegalArgumentException("The page can't be negative"); @@ -192,7 +191,6 @@ public class PetService { } - public void assignVetToPet(Long petId) { requireNonNull(petId, "Pet ID can't be null"); //requireNonNull(vetLogin, "Vet login can't be null"); diff --git a/service/src/main/java/es/uvigo/esei/xcs/service/VaccinationService.java b/service/src/main/java/es/uvigo/esei/xcs/service/VaccinationService.java index 70b5c98..3819f0b 100644 --- a/service/src/main/java/es/uvigo/esei/xcs/service/VaccinationService.java +++ b/service/src/main/java/es/uvigo/esei/xcs/service/VaccinationService.java @@ -3,6 +3,7 @@ package es.uvigo.esei.xcs.service; import java.util.Date; import static java.util.Objects.requireNonNull; +import java.security.Principal; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.List; @@ -10,6 +11,7 @@ import java.util.List; import javax.annotation.security.PermitAll; import javax.ejb.EJB; import javax.ejb.Stateless; +import javax.inject.Inject; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; @@ -22,6 +24,9 @@ import es.uvigo.esei.xcs.domain.entities.Vaccine; @Stateless @PermitAll public class VaccinationService { + @Inject + private Principal currentUser; + @PersistenceContext 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 cdb9095..7d1e825 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 @@ -27,6 +27,15 @@ public class VetService { @PersistenceContext EntityManager em; + public int countPets() { + Long count = em.createQuery( + "SELECT COUNT(p) FROM Vet v JOIN v.pets p WHERE v.login = :login", Long.class) + .setParameter("login", currentUser.getName()) + .getSingleResult(); + return count.intValue(); + } + + public Vet get(String login) { return em.find(Vet.class, login); } @@ -73,17 +82,14 @@ public class VetService { } - public List getPets(int page , int pageSize) { + public List getPets(int first , int pageSize) { //requireNonNull(login, "Login can't be null"); - 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"); - } + if (first < 0) throw new IllegalArgumentException("First can't be negative"); + if (pageSize <= 0) throw new IllegalArgumentException("Page size must be positive"); + return this.em.createQuery("SELECT DISTINCT p FROM Vet v JOIN v.pets p " + "WHERE v.login = :login", Pet.class) - .setFirstResult(page * pageSize) + .setFirstResult(first) .setMaxResults(pageSize) .setParameter("login", currentUser.getName()) .getResultList(); -- 2.18.1