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 8cdf1152493d0e343a2f149017910eb94191bf03..a27ee7d3426db94cd199446f8bb8f2d3a8cde7dd 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 2fc22413437aa5cff8899bc099a019b0f337117a..7f0faba8924631b042d6c19d4fb307b11dc7b7c8 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 050be53cc1617a8745817b408035cf4c546e951c..a676b39ee0a914dc730c10ec93b8aabfbc4e7ba8 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 489e0aa0b298f96dca709a472957bf7e60d31b0c..1531d0e3c303502ae6fe87db8c1c3a2b6259b8b2 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 7434bda02834ba5ea3aabe2104b427cb995df30a..a5bf9afe5dc5538052efac4b4ee52ac576bdf7b9 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(); }