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 dd59f5a9a73eda0d3b419b51a82bac85b02359e0..2515eb6bc75f9d532cfda1adec384aa028a2cdcf 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 @@ -1,8 +1,10 @@ package es.uvigo.esei.xcs.jsf; import javax.annotation.PostConstruct; +import javax.ejb.EJBException; import javax.inject.Inject; import javax.inject.Named; +import javax.faces.application.FacesMessage; import javax.faces.context.FacesContext; import javax.faces.view.ViewScoped; @@ -50,7 +52,7 @@ public class PetDetailsManagedBean implements Serializable{ if (id != null) { this.pet = petService.get(Long.parseLong(id)); } - this.allVaccines = vaccineService.list(0, 100); // ajustar paginación + this.allVaccines = vaccineService.list(0, 100); } public Pet getPet() { @@ -77,28 +79,28 @@ public class PetDetailsManagedBean implements Serializable{ this.vaccinationDate = vaccinationDate; } - // Método para vacunar public String vaccinate() { - if (selectedVaccineId == null || vaccinationDate == null /*|| vaccinationDate.isEmpty()*/) { + if (selectedVaccineId == null || vaccinationDate == null) { FacesContext.getCurrentInstance().addMessage(null, new javax.faces.application.FacesMessage("Debe seleccionar vacuna y fecha")); return null; } - + try { - //Date date = new SimpleDateFormat("yyyy-MM-dd").parse(vaccinationDate); Vaccination v = vaccinationService.create(pet.getId(), selectedVaccineId, vaccinationDate); - // refrescar la mascota para mostrar la nueva vacunación this.pet = petService.get(pet.getId()); FacesContext.getCurrentInstance().addMessage(null, - new javax.faces.application.FacesMessage("Vacunación creada correctamente")); - } /*catch (ParseException e) { - FacesContext.getCurrentInstance().addMessage(null, - new javax.faces.application.FacesMessage("Formato de fecha inválido")); - }*/ catch (IllegalArgumentException e) { - FacesContext.getCurrentInstance().addMessage(null, - new javax.faces.application.FacesMessage(e.getMessage())); + new FacesMessage("Vacunación creada correctamente")); + } catch (EJBException e) { + Throwable cause = e.getCause(); + if (cause instanceof IllegalArgumentException) { + FacesContext.getCurrentInstance().addMessage(null, + new FacesMessage(cause.getMessage())); + } else { + FacesContext.getCurrentInstance().addMessage(null, + new FacesMessage("Error inesperado al crear vacunación")); + } } - return null; // recargar la misma página + return null; } } 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 d22c2c1e5a9f40cdd2a55e1c0a4a1c43ce21e76c..2fc22413437aa5cff8899bc099a019b0f337117a 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 @@ -1,21 +1,32 @@ package es.uvigo.esei.xcs.jsf; +import java.io.Serializable; import java.util.Date; import java.util.List; +import java.util.Map; import java.util.Optional; +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.AnimalType; import es.uvigo.esei.xcs.domain.entities.Pet; import es.uvigo.esei.xcs.service.PetService; @Named("pet") -@RequestScoped -public class PetManagedBean { +//@RequestScoped +@ViewScoped +public class PetManagedBean implements Serializable{ + private static final long serialVersionUID = 1L; + @Inject private PetService service; @@ -27,6 +38,29 @@ public class PetManagedBean { 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.getAll(first, pageSize); + } + + @Override + public int count(Map filterBy) { + return service.countAll(); + } + }; + } + + + public LazyDataModel getPets() { + return pets; + } + public String getName() { return name; } @@ -73,11 +107,11 @@ public class PetManagedBean { this.id = id; } - public List getPets() { + /*public List getPets() { List list = this.service.getAll(0, 100); System.out.println(list); return list; - } + }*/ public String edit(Long petId) { final Pet pet = this.service.get(petId); diff --git a/jsf/src/main/webapp/vet/petDetails.xhtml b/jsf/src/main/webapp/vet/petDetails.xhtml index 50c2a0776e490dc9053fb293f9e266cfb9f3dec3..d5bc8ffbda46ea835103720965f16b59c1e71402 100644 --- a/jsf/src/main/webapp/vet/petDetails.xhtml +++ b/jsf/src/main/webapp/vet/petDetails.xhtml @@ -42,7 +42,8 @@

Registrar nueva vacunación

- + + diff --git a/jsf/src/main/webapp/vet/pets.xhtml b/jsf/src/main/webapp/vet/pets.xhtml index dbb5030531af6d348da24fb6301055a84a211979..107f69eefcb7723a5a9eb83346cb7e945062bec7 100644 --- a/jsf/src/main/webapp/vet/pets.xhtml +++ b/jsf/src/main/webapp/vet/pets.xhtml @@ -1,30 +1,23 @@ Mis Mascotas - - - Nombre - #{p.name} - - - Nacimiento - #{p.birth} - - - Tipo - #{p.animal} - - - Acción - - - + + + #{p.name} + #{p.birth} + #{p.animal} + + + + + 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 b4591cb0caa5b5020e90aa2d57f1c8bb33c2cd4e..47bd9eb0c5682d1e6e3114d340ef34c00efcb018 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 @@ -55,12 +55,18 @@ public class PetService { throw new EJBAccessException("Pet's owner is not the current principal"); } }*/ + public int countAll() { + Long count = em.createQuery("SELECT COUNT(p) FROM Pet p", Long.class) + .getSingleResult(); + return count.intValue(); + } + public Pet get(Long id) { return em.find(Pet.class, id); } - public List getAll(int page, int pageSize) { + /*public List getAll(int page, int pageSize) { if (page < 0) { throw new IllegalArgumentException("The page can't be negative"); } @@ -71,8 +77,19 @@ public class PetService { .setFirstResult(page * pageSize) .setMaxResults(pageSize) .getResultList(); + }*/ + + public List getAll(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 Pet p", Pet.class) + .setFirstResult(first) // no multiplicar por pageSize + .setMaxResults(pageSize) + .getResultList(); } + /** * Returns the complete list of pets of the current owner.