From 6a37b2558fdf88a0daeb4b035c94e1b8bebe598d Mon Sep 17 00:00:00 2001 From: Breixo Senra Date: Sun, 26 Oct 2025 03:19:18 +0100 Subject: [PATCH] =?UTF-8?q?Tabla=20de=20/owner/pets.xhtml=20con=20paginaci?= =?UTF-8?q?=C3=B3n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../uvigo/esei/xcs/jsf/OwnerManagedBean.java | 198 ++++++++---------- .../es/uvigo/esei/xcs/jsf/PetManagedBean.java | 12 ++ jsf/src/main/webapp/admin/owners.xhtml | 41 ++-- jsf/src/main/webapp/owner/pets.xhtml | 43 ++-- .../uvigo/esei/xcs/service/OwnerService.java | 24 ++- 5 files changed, 154 insertions(+), 164 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 8cdf115..a27ee7d 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 @@ -3,7 +3,9 @@ package es.uvigo.esei.xcs.jsf; import static java.util.stream.Collectors.joining; import java.util.List; +import java.util.Map; +import javax.annotation.PostConstruct; import javax.enterprise.context.RequestScoped; import javax.faces.context.FacesContext; import javax.inject.Inject; @@ -13,113 +15,97 @@ import es.uvigo.esei.xcs.domain.entities.Owner; import es.uvigo.esei.xcs.domain.entities.Pet; import es.uvigo.esei.xcs.service.OwnerService; +import org.primefaces.model.LazyDataModel; +import org.primefaces.model.SortMeta; +import org.primefaces.model.FilterMeta; + @Named("owner") @RequestScoped public class OwnerManagedBean { - @Inject - private OwnerService service; - - private String login; - private String password; - - private boolean editing; - - private String errorMessage; - - public String getLogin() { - return login; - } - - public void setLogin(String name) { - this.login = name; - } - - public String getPassword() { - return password; - } - - public void setPassword(String password) { - this.password = password; - } - - public String getErrorMessage() { - return errorMessage; - } - - public boolean isError() { - return this.errorMessage != null; - } - - public boolean isEditing() { - return this.editing; - } - - public void setEditing(boolean editing) { - this.editing = editing; - } - - public List getOwners() { - return this.service.list(0, 100); - } - - public String getPetNames(String login) { - return this.service.getPets(0, 100).stream() - .map(Pet::getName) - .collect(joining(", ")); - } - - public String edit(String login) { - this.editing = true; - this.login = login; - - return this.getViewId(); - } - - public String cancelEditing() { - this.clear(); - - return this.getViewId(); - } - - public String remove(String login) { - this.service.remove(login); - - return redirectTo(this.getViewId()); - } - - public String store() { - try { - if (this.isEditing()) { - final Owner owner = this.service.get(this.login); - owner.changePassword(this.password); - - this.service.update(owner); - } else { - this.service.create(new Owner(login, password)); - } - - this.clear(); - - return redirectTo(this.getViewId()); - } catch (Throwable t) { - this.errorMessage = t.getMessage(); - - return this.getViewId(); - } - } - - private void clear() { - this.login = null; - this.password = null; - this.errorMessage = null; - this.editing = false; - } - - private String redirectTo(String url) { - return url + "?faces-redirect=true"; - } - - private String getViewId() { - return FacesContext.getCurrentInstance().getViewRoot().getViewId(); - } + @Inject + private OwnerService service; + + private String login; + private String password; + private boolean editing; + private String errorMessage; + + private LazyDataModel owners; + + @PostConstruct + public void init() { + owners = new LazyDataModel() { + @Override + public List load(int first, int pageSize, Map sortBy, Map filterBy) { + return service.list(first, pageSize); + } + + @Override + public int count(Map filterBy) { + return service.countAll(); + } + }; + } + + public LazyDataModel getOwners() { + return owners; + } + + public String getLogin() { return login; } + public void setLogin(String login) { this.login = login; } + public String getPassword() { return password; } + public void setPassword(String password) { this.password = password; } + public boolean isEditing() { return editing; } + public void setEditing(boolean editing) { this.editing = editing; } + public String getErrorMessage() { return errorMessage; } + public boolean isError() { return errorMessage != null; } + + public String getPetNames(String login) { + return this.service.getPets(login, 0, 100).stream() + .map(Pet::getName) + .collect(joining(", ")); + } + + public String edit(String login) { + this.editing = true; + this.login = login; + return getViewId(); + } + + public String cancelEditing() { + clear(); + return getViewId(); + } + + public String remove(String login) { + service.remove(login); + return redirectTo(getViewId()); + } + + public String store() { + try { + if (isEditing()) { + Owner owner = service.get(this.login); + owner.changePassword(this.password); + service.update(owner); + } else { + service.create(new Owner(login, password)); + } + clear(); + return redirectTo(getViewId()); + } catch (Throwable t) { + this.errorMessage = t.getMessage(); + return getViewId(); + } + } + + private void clear() { + this.login = null; + this.password = null; + this.errorMessage = null; + this.editing = false; + } + + private String redirectTo(String url) { return url + "?faces-redirect=true"; } + private String getViewId() { return FacesContext.getCurrentInstance().getViewRoot().getViewId(); } } 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 2fc2241..7f0faba 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 @@ -53,6 +53,18 @@ public class PetManagedBean implements Serializable{ public int count(Map filterBy) { return service.countAll(); } + + @Override + public String getRowKey(Pet pet) { + return pet.getId() != null ? pet.getId().toString() : null; + } + + @Override + public Pet getRowData(String rowKey) { + if (rowKey == null) return null; + return service.get(Long.valueOf(rowKey)); + } + }; } diff --git a/jsf/src/main/webapp/admin/owners.xhtml b/jsf/src/main/webapp/admin/owners.xhtml index 050be53..a676b39 100644 --- a/jsf/src/main/webapp/admin/owners.xhtml +++ b/jsf/src/main/webapp/admin/owners.xhtml @@ -2,7 +2,8 @@ @@ -31,30 +32,20 @@ - - - Login - #{ownerEntity.login} - - - Password - #{ownerEntity.password} - - - Pets - #{owner.getPetNames(ownerEntity.login)} - - - - - - - - + + #{ownerEntity.login} + #{ownerEntity.password} + #{owner.getPetNames(ownerEntity.login)} + + + + + + + diff --git a/jsf/src/main/webapp/owner/pets.xhtml b/jsf/src/main/webapp/owner/pets.xhtml index 489e0aa..1531d0e 100644 --- a/jsf/src/main/webapp/owner/pets.xhtml +++ b/jsf/src/main/webapp/owner/pets.xhtml @@ -2,7 +2,8 @@ @@ -36,28 +37,24 @@ - - - Name - #{petEntity.name} - - - Birth - - - - - - Type - #{petEntity.animal} - - - - - - - - + + #{petEntity.name} + + + + + + #{petEntity.animal} + + + + + + + + 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 7434bda..a5bf9af 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 @@ -35,6 +35,13 @@ public class OwnerService { private Principal currentUser; + public int countAll() { + Long count = em.createQuery("SELECT COUNT(o) FROM Owner o", Long.class) + .getSingleResult(); + return count.intValue(); + } + + /** * Returns the owner identified by {@code login}. If there is no owner with * the specified login, {@code null} will be returned. @@ -54,15 +61,12 @@ public class OwnerService { * * @return the complete list of owners. */ - public List list(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 list(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 o FROM Owner o", Owner.class) - .setFirstResult(page * pageSize) + .setFirstResult(first) .setMaxResults(pageSize) .getResultList(); } @@ -140,7 +144,7 @@ public class OwnerService { * @throws IllegalArgumentException if {@code login} is {@code null} or it * does not identifies a valid owner. */ - public List getPets(int first, int pageSize) { + public List getPets(String login, 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"); @@ -151,7 +155,7 @@ public class OwnerService { Pet.class) .setFirstResult(first) .setMaxResults(pageSize) - .setParameter("login", currentUser.getName()) + .setParameter("login", login) .getResultList(); } -- 2.18.1