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; ...@@ -3,7 +3,9 @@ package es.uvigo.esei.xcs.jsf;
import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.joining;
import java.util.List; import java.util.List;
import java.util.Map;
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.inject.Inject; import javax.inject.Inject;
...@@ -13,6 +15,10 @@ import es.uvigo.esei.xcs.domain.entities.Owner; ...@@ -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.domain.entities.Pet;
import es.uvigo.esei.xcs.service.OwnerService; import es.uvigo.esei.xcs.service.OwnerService;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortMeta;
import org.primefaces.model.FilterMeta;
@Named("owner") @Named("owner")
@RequestScoped @RequestScoped
public class OwnerManagedBean { public class OwnerManagedBean {
...@@ -21,49 +27,41 @@ public class OwnerManagedBean { ...@@ -21,49 +27,41 @@ public class OwnerManagedBean {
private String login; private String login;
private String password; private String password;
private boolean editing; private boolean editing;
private String errorMessage; private String errorMessage;
public String getLogin() { private LazyDataModel<Owner> owners;
return login;
}
public void setLogin(String name) {
this.login = name;
}
public String getPassword() { @PostConstruct
return password; 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) { @Override
this.password = password; 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) { public LazyDataModel<Owner> getOwners() {
this.editing = editing; return owners;
} }
public List<Owner> getOwners() { public String getLogin() { return login; }
return this.service.list(0, 100); 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) { public String getPetNames(String login) {
return this.service.getPets(0, 100).stream() return this.service.getPets(login, 0, 100).stream()
.map(Pet::getName) .map(Pet::getName)
.collect(joining(", ")); .collect(joining(", "));
} }
...@@ -71,40 +69,33 @@ public class OwnerManagedBean { ...@@ -71,40 +69,33 @@ public class OwnerManagedBean {
public String edit(String login) { public String edit(String login) {
this.editing = true; this.editing = true;
this.login = login; this.login = login;
return getViewId();
return this.getViewId();
} }
public String cancelEditing() { public String cancelEditing() {
this.clear(); clear();
return getViewId();
return this.getViewId();
} }
public String remove(String login) { public String remove(String login) {
this.service.remove(login); service.remove(login);
return redirectTo(getViewId());
return redirectTo(this.getViewId());
} }
public String store() { public String store() {
try { try {
if (this.isEditing()) { if (isEditing()) {
final Owner owner = this.service.get(this.login); Owner owner = service.get(this.login);
owner.changePassword(this.password); owner.changePassword(this.password);
service.update(owner);
this.service.update(owner);
} else { } else {
this.service.create(new Owner(login, password)); service.create(new Owner(login, password));
} }
clear();
this.clear(); return redirectTo(getViewId());
return redirectTo(this.getViewId());
} catch (Throwable t) { } catch (Throwable t) {
this.errorMessage = t.getMessage(); this.errorMessage = t.getMessage();
return getViewId();
return this.getViewId();
} }
} }
...@@ -115,11 +106,6 @@ public class OwnerManagedBean { ...@@ -115,11 +106,6 @@ public class OwnerManagedBean {
this.editing = false; this.editing = false;
} }
private String redirectTo(String url) { private String redirectTo(String url) { return url + "?faces-redirect=true"; }
return url + "?faces-redirect=true"; private String getViewId() { return FacesContext.getCurrentInstance().getViewRoot().getViewId(); }
}
private String getViewId() {
return FacesContext.getCurrentInstance().getViewRoot().getViewId();
}
} }
...@@ -53,6 +53,18 @@ public class PetManagedBean implements Serializable{ ...@@ -53,6 +53,18 @@ public class PetManagedBean implements Serializable{
public int count(Map<String, FilterMeta> filterBy) { public int count(Map<String, FilterMeta> filterBy) {
return service.countAll(); 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 @@ ...@@ -2,6 +2,7 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <!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" <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"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets" xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:a="http://xmlns.jcp.org/jsf/passthrough"> xmlns:a="http://xmlns.jcp.org/jsf/passthrough">
...@@ -31,30 +32,20 @@ ...@@ -31,30 +32,20 @@
</div> </div>
</ui:define> </ui:define>
<ui:define name="content"> <ui:define name="content">
<h:dataTable id="owners-table" <p:dataTable id="owners-table" value="#{owner.owners}" var="ownerEntity"
value="#{owner.owners}" var="ownerEntity" lazy="true" paginator="true" rows="10"
styleClass="table table-striped table-bordered" styleClass="table table-striped table-bordered"
columnClasses="owners-table-login,owners-table-password,owners-table-pets,owners-table-options" columnClasses="owners-table-login,owners-table-password,owners-table-pets,owners-table-options">
> <p:column headerText="Login">#{ownerEntity.login}</p:column>
<h:column> <p:column headerText="Password">#{ownerEntity.password}</p:column>
<f:facet name="header">Login</f:facet> <p:column headerText="Pets">#{owner.getPetNames(ownerEntity.login)}</p:column>
#{ownerEntity.login} <p:column headerText="Opciones">
</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>
<h:form> <h:form>
<h:commandButton class="owners-table-remove" value="Remove" action="#{owner.remove(ownerEntity.login)}"/> <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:commandButton class="owners-table-edit" value="Edit" action="#{owner.edit(ownerEntity.login)}"/>
</h:form> </h:form>
</h:column> </p:column>
</h:dataTable> </p:dataTable>
</ui:define> </ui:define>
</ui:composition> </ui:composition>
</body> </body>
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <!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" <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"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets" xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:a="http://xmlns.jcp.org/jsf/passthrough"> xmlns:a="http://xmlns.jcp.org/jsf/passthrough">
...@@ -36,28 +37,24 @@ ...@@ -36,28 +37,24 @@
</div> </div>
</ui:define> </ui:define>
<ui:define name="content"> <ui:define name="content">
<h:dataTable value="#{pet.pets}" var="petEntity" styleClass="table table-striped table-bordered"> <p:dataTable id="pets-table" value="#{pet.pets}" var="petEntity"
<h:column> paginator="true" rows="10" lazy="true"
<f:facet name="header">Name</f:facet> styleClass="table table-striped table-bordered">
#{petEntity.name} <p:column headerText="Name">#{petEntity.name}</p:column>
</h:column> <p:column headerText="Birth">
<h:column>
<f:facet name="header">Birth</f:facet>
<h:outputText value="#{petEntity.birth}"> <h:outputText value="#{petEntity.birth}">
<f:convertDateTime pattern="yyyy-M-d hh:mm:ss" /> <f:convertDateTime pattern="yyyy-M-d hh:mm:ss" />
</h:outputText> </h:outputText>
</h:column> </p:column>
<h:column> <p:column headerText="Type">#{petEntity.animal}</p:column>
<f:facet name="header">Type</f:facet> <p:column headerText="Opciones">
#{petEntity.animal}
</h:column>
<h:column>
<h:form> <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:commandButton value="Edit" action="#{pet.edit(petEntity.id)}"/>
</h:form> </h:form>
</h:column> </p:column>
</h:dataTable> </p:dataTable>
</ui:define> </ui:define>
</ui:composition> </ui:composition>
</body> </body>
......
...@@ -35,6 +35,13 @@ public class OwnerService { ...@@ -35,6 +35,13 @@ public class OwnerService {
private Principal currentUser; 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 * Returns the owner identified by {@code login}. If there is no owner with
* the specified login, {@code null} will be returned. * the specified login, {@code null} will be returned.
...@@ -54,15 +61,12 @@ public class OwnerService { ...@@ -54,15 +61,12 @@ public class OwnerService {
* *
* @return the complete list of owners. * @return the complete list of owners.
*/ */
public List<Owner> list(int page, int pageSize) { public List<Owner> list(int first, int pageSize) {
if (page < 0) { if (first < 0) throw new IllegalArgumentException("First can't be negative");
throw new IllegalArgumentException("The page can't be negative"); if (pageSize <= 0) throw new IllegalArgumentException("Page size must be positive");
}
if (pageSize <= 0) {
throw new IllegalArgumentException("The page size can't be negative or zero");
}
return em.createQuery("SELECT o FROM Owner o", Owner.class) return em.createQuery("SELECT o FROM Owner o", Owner.class)
.setFirstResult(page * pageSize) .setFirstResult(first)
.setMaxResults(pageSize) .setMaxResults(pageSize)
.getResultList(); .getResultList();
} }
...@@ -140,7 +144,7 @@ public class OwnerService { ...@@ -140,7 +144,7 @@ public class OwnerService {
* @throws IllegalArgumentException if {@code login} is {@code null} or it * @throws IllegalArgumentException if {@code login} is {@code null} or it
* does not identifies a valid owner. * 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 (first < 0) throw new IllegalArgumentException("First can't be negative");
if (pageSize <= 0) throw new IllegalArgumentException("Page size must be positive"); if (pageSize <= 0) throw new IllegalArgumentException("Page size must be positive");
...@@ -151,7 +155,7 @@ public class OwnerService { ...@@ -151,7 +155,7 @@ public class OwnerService {
Pet.class) Pet.class)
.setFirstResult(first) .setFirstResult(first)
.setMaxResults(pageSize) .setMaxResults(pageSize)
.setParameter("login", currentUser.getName()) .setParameter("login", login)
.getResultList(); .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