Commit d596a8e3 authored by Breixo Senra's avatar Breixo Senra

Paginación funcionando

parent 25685e70
package es.uvigo.esei.xcs.jsf; package es.uvigo.esei.xcs.jsf;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.ejb.EJBException;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext; import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped; import javax.faces.view.ViewScoped;
...@@ -50,7 +52,7 @@ public class PetDetailsManagedBean implements Serializable{ ...@@ -50,7 +52,7 @@ public class PetDetailsManagedBean implements Serializable{
if (id != null) { if (id != null) {
this.pet = petService.get(Long.parseLong(id)); 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() { public Pet getPet() {
...@@ -77,28 +79,28 @@ public class PetDetailsManagedBean implements Serializable{ ...@@ -77,28 +79,28 @@ public class PetDetailsManagedBean implements Serializable{
this.vaccinationDate = vaccinationDate; this.vaccinationDate = vaccinationDate;
} }
// Método para vacunar
public String vaccinate() { public String vaccinate() {
if (selectedVaccineId == null || vaccinationDate == null /*|| vaccinationDate.isEmpty()*/) { if (selectedVaccineId == null || vaccinationDate == null) {
FacesContext.getCurrentInstance().addMessage(null, FacesContext.getCurrentInstance().addMessage(null,
new javax.faces.application.FacesMessage("Debe seleccionar vacuna y fecha")); new javax.faces.application.FacesMessage("Debe seleccionar vacuna y fecha"));
return null; return null;
} }
try { try {
//Date date = new SimpleDateFormat("yyyy-MM-dd").parse(vaccinationDate);
Vaccination v = vaccinationService.create(pet.getId(), selectedVaccineId, vaccinationDate); Vaccination v = vaccinationService.create(pet.getId(), selectedVaccineId, vaccinationDate);
// refrescar la mascota para mostrar la nueva vacunación
this.pet = petService.get(pet.getId()); this.pet = petService.get(pet.getId());
FacesContext.getCurrentInstance().addMessage(null, FacesContext.getCurrentInstance().addMessage(null,
new javax.faces.application.FacesMessage("Vacunación creada correctamente")); new FacesMessage("Vacunación creada correctamente"));
} /*catch (ParseException e) { } catch (EJBException e) {
FacesContext.getCurrentInstance().addMessage(null, Throwable cause = e.getCause();
new javax.faces.application.FacesMessage("Formato de fecha inválido")); if (cause instanceof IllegalArgumentException) {
}*/ catch (IllegalArgumentException e) { FacesContext.getCurrentInstance().addMessage(null,
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(cause.getMessage()));
new javax.faces.application.FacesMessage(e.getMessage())); } else {
FacesContext.getCurrentInstance().addMessage(null,
new FacesMessage("Error inesperado al crear vacunación"));
}
} }
return null; // recargar la misma página return null;
} }
} }
package es.uvigo.esei.xcs.jsf; package es.uvigo.esei.xcs.jsf;
import java.io.Serializable;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Optional; import java.util.Optional;
import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped; import javax.enterprise.context.RequestScoped;
import javax.faces.context.FacesContext; import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; 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.AnimalType;
import es.uvigo.esei.xcs.domain.entities.Pet; import es.uvigo.esei.xcs.domain.entities.Pet;
import es.uvigo.esei.xcs.service.PetService; import es.uvigo.esei.xcs.service.PetService;
@Named("pet") @Named("pet")
@RequestScoped //@RequestScoped
public class PetManagedBean { @ViewScoped
public class PetManagedBean implements Serializable{
private static final long serialVersionUID = 1L;
@Inject @Inject
private PetService service; private PetService service;
...@@ -27,6 +38,29 @@ public class PetManagedBean { ...@@ -27,6 +38,29 @@ public class PetManagedBean {
private String errorMessage; private String errorMessage;
private LazyDataModel<Pet> pets;
@PostConstruct
public void init() {
pets = new LazyDataModel<Pet>() {
@Override
public List<Pet> load(int first, int pageSize, Map<String, SortMeta> sortBy, Map<String, FilterMeta> filterBy) {
return service.getAll(first, pageSize);
}
@Override
public int count(Map<String, FilterMeta> filterBy) {
return service.countAll();
}
};
}
public LazyDataModel<Pet> getPets() {
return pets;
}
public String getName() { public String getName() {
return name; return name;
} }
...@@ -73,11 +107,11 @@ public class PetManagedBean { ...@@ -73,11 +107,11 @@ public class PetManagedBean {
this.id = id; this.id = id;
} }
public List<Pet> getPets() { /*public List<Pet> getPets() {
List<Pet> list = this.service.getAll(0, 100); List<Pet> list = this.service.getAll(0, 100);
System.out.println(list); System.out.println(list);
return list; return list;
} }*/
public String edit(Long petId) { public String edit(Long petId) {
final Pet pet = this.service.get(petId); final Pet pet = this.service.get(petId);
......
...@@ -42,7 +42,8 @@ ...@@ -42,7 +42,8 @@
<p:commandButton value="Open Dialog" /> <p:commandButton value="Open Dialog" />
<h3>Registrar nueva vacunación</h3> <h3>Registrar nueva vacunación</h3>
<h:form> <h:form>
<!--<h:messages globalOnly="true" layout="table" style="color:red;" />--> <h:messages globalOnly="true" style="color:red;" />
<h:panelGrid columns="2"> <h:panelGrid columns="2">
<h:outputLabel value="Vacuna:" for="vaccine"/> <h:outputLabel value="Vacuna:" for="vaccine"/>
<h:selectOneMenu value="#{petDetails.selectedVaccineId}" id="vaccine"> <h:selectOneMenu value="#{petDetails.selectedVaccineId}" id="vaccine">
......
<html xmlns="http://www.w3.org/1999/xhtml" <html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://xmlns.jcp.org/jsf/core"> xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head> <h:head>
<title>Mis Mascotas</title> <title>Mis Mascotas</title>
</h:head> </h:head>
<h:body> <h:body>
<h:form> <h:form>
<h:dataTable value="#{pet.pets}" var="p" border="1"> <!--NUEVA TABLA-->
<h:column> <p:dataTable value="#{pet.pets}" var="p" paginator="true" rows="10" lazy="true">
<f:facet name="header">Nombre</f:facet> <p:column headerText="Nombre">#{p.name}</p:column>
#{p.name} <p:column headerText="Nacimiento">#{p.birth}</p:column>
</h:column> <p:column headerText="Tipo">#{p.animal}</p:column>
<h:column> <p:column headerText="Acción">
<f:facet name="header">Nacimiento</f:facet> <p:commandButton value="#{pet.isAssignedToMe(p) ? 'Desasignar' : 'Asignar'}"
#{p.birth} action="#{pet.toggleAssignment(p)}" update="@this"/>
</h:column> </p:column>
<h:column> </p:dataTable>
<f:facet name="header">Tipo</f:facet>
#{p.animal}
</h:column>
<h:column>
<f:facet name="header">Acción</f:facet>
<h:commandButton value="#{pet.isAssignedToMe(p) ? 'Desasignar' : 'Asignar'}"
action="#{pet.toggleAssignment(p)}"/>
</h:column>
</h:dataTable>
<h:messages globalOnly="true"/> <h:messages globalOnly="true"/>
</h:form> </h:form>
......
...@@ -55,12 +55,18 @@ public class PetService { ...@@ -55,12 +55,18 @@ public class PetService {
throw new EJBAccessException("Pet's owner is not the current principal"); 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) { public Pet get(Long id) {
return em.find(Pet.class, id); return em.find(Pet.class, id);
} }
public List<Pet> getAll(int page, int pageSize) { /*public List<Pet> getAll(int page, int pageSize) {
if (page < 0) { if (page < 0) {
throw new IllegalArgumentException("The page can't be negative"); throw new IllegalArgumentException("The page can't be negative");
} }
...@@ -71,8 +77,19 @@ public class PetService { ...@@ -71,8 +77,19 @@ public class PetService {
.setFirstResult(page * pageSize) .setFirstResult(page * pageSize)
.setMaxResults(pageSize) .setMaxResults(pageSize)
.getResultList(); .getResultList();
}*/
public List<Pet> 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. * Returns the complete list of pets of the current owner.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment