Commit 6a37b255 authored by Breixo Senra's avatar Breixo Senra

Tabla de /owner/pets.xhtml con paginación

parent a92ee916
......@@ -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,6 +15,10 @@ 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 {
......@@ -21,49 +27,41 @@ public class OwnerManagedBean {
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;
}
private LazyDataModel<Owner> owners;
public String getPassword() {
return password;
@PostConstruct
public void init() {
owners = new LazyDataModel<Owner>() {
@Override
public List<Owner> load(int first, int pageSize, Map<String, SortMeta> sortBy, Map<String, FilterMeta> filterBy) {
return service.list(first, pageSize);
}
public void setPassword(String password) {
this.password = password;
@Override
public int count(Map<String, FilterMeta> filterBy) {
return service.countAll();
}
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 LazyDataModel<Owner> getOwners() {
return owners;
}
public List<Owner> getOwners() {
return this.service.list(0, 100);
}
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(0, 100).stream()
return this.service.getPets(login, 0, 100).stream()
.map(Pet::getName)
.collect(joining(", "));
}
......@@ -71,40 +69,33 @@ public class OwnerManagedBean {
public String edit(String login) {
this.editing = true;
this.login = login;
return this.getViewId();
return getViewId();
}
public String cancelEditing() {
this.clear();
return this.getViewId();
clear();
return getViewId();
}
public String remove(String login) {
this.service.remove(login);
return redirectTo(this.getViewId());
service.remove(login);
return redirectTo(getViewId());
}
public String store() {
try {
if (this.isEditing()) {
final Owner owner = this.service.get(this.login);
if (isEditing()) {
Owner owner = service.get(this.login);
owner.changePassword(this.password);
this.service.update(owner);
service.update(owner);
} else {
this.service.create(new Owner(login, password));
service.create(new Owner(login, password));
}
this.clear();
return redirectTo(this.getViewId());
clear();
return redirectTo(getViewId());
} catch (Throwable t) {
this.errorMessage = t.getMessage();
return this.getViewId();
return getViewId();
}
}
......@@ -115,11 +106,6 @@ public class OwnerManagedBean {
this.editing = false;
}
private String redirectTo(String url) {
return url + "?faces-redirect=true";
}
private String getViewId() {
return FacesContext.getCurrentInstance().getViewRoot().getViewId();
}
private String redirectTo(String url) { return url + "?faces-redirect=true"; }
private String getViewId() { return FacesContext.getCurrentInstance().getViewRoot().getViewId(); }
}
......@@ -53,6 +53,18 @@ public class PetManagedBean implements Serializable{
public int count(Map<String, FilterMeta> 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));
}
};
}
......
......@@ -2,6 +2,7 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:a="http://xmlns.jcp.org/jsf/passthrough">
......@@ -31,30 +32,20 @@
</div>
</ui:define>
<ui:define name="content">
<h:dataTable id="owners-table"
value="#{owner.owners}" var="ownerEntity"
<p:dataTable id="owners-table" value="#{owner.owners}" var="ownerEntity"
lazy="true" paginator="true" rows="10"
styleClass="table table-striped table-bordered"
columnClasses="owners-table-login,owners-table-password,owners-table-pets,owners-table-options"
>
<h:column>
<f:facet name="header">Login</f:facet>
#{ownerEntity.login}
</h:column>
<h:column>
<f:facet name="header">Password</f:facet>
#{ownerEntity.password}
</h:column>
<h:column>
<f:facet name="header">Pets</f:facet>
#{owner.getPetNames(ownerEntity.login)}
</h:column>
<h:column>
columnClasses="owners-table-login,owners-table-password,owners-table-pets,owners-table-options">
<p:column headerText="Login">#{ownerEntity.login}</p:column>
<p:column headerText="Password">#{ownerEntity.password}</p:column>
<p:column headerText="Pets">#{owner.getPetNames(ownerEntity.login)}</p:column>
<p:column headerText="Opciones">
<h:form>
<h:commandButton class="owners-table-remove" value="Remove" action="#{owner.remove(ownerEntity.login)}"/>
<h:commandButton class="owners-table-edit" value="Edit" action="#{owner.edit(ownerEntity.login)}"/>
</h:form>
</h:column>
</h:dataTable>
</p:column>
</p:dataTable>
</ui:define>
</ui:composition>
</body>
......
......@@ -2,6 +2,7 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:a="http://xmlns.jcp.org/jsf/passthrough">
......@@ -36,28 +37,24 @@
</div>
</ui:define>
<ui:define name="content">
<h:dataTable value="#{pet.pets}" var="petEntity" styleClass="table table-striped table-bordered">
<h:column>
<f:facet name="header">Name</f:facet>
#{petEntity.name}
</h:column>
<h:column>
<f:facet name="header">Birth</f:facet>
<p:dataTable id="pets-table" value="#{pet.pets}" var="petEntity"
paginator="true" rows="10" lazy="true"
styleClass="table table-striped table-bordered">
<p:column headerText="Name">#{petEntity.name}</p:column>
<p:column headerText="Birth">
<h:outputText value="#{petEntity.birth}">
<f:convertDateTime pattern="yyyy-M-d hh:mm:ss" />
</h:outputText>
</h:column>
<h:column>
<f:facet name="header">Type</f:facet>
#{petEntity.animal}
</h:column>
<h:column>
</p:column>
<p:column headerText="Type">#{petEntity.animal}</p:column>
<p:column headerText="Opciones">
<h:form>
<h:commandButton value="Remove" type="submit" action="#{pet.remove(petEntity.id)}"/>
<h:commandButton value="Remove" action="#{pet.remove(petEntity.id)}"/>
<h:commandButton value="Edit" action="#{pet.edit(petEntity.id)}"/>
</h:form>
</h:column>
</h:dataTable>
</p:column>
</p:dataTable>
</ui:define>
</ui:composition>
</body>
......
......@@ -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<Owner> 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<Owner> 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<Pet> getPets(int first, int pageSize) {
public List<Pet> 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();
}
......
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