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,113 +15,97 @@ import es.uvigo.esei.xcs.domain.entities.Owner; ...@@ -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.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 {
@Inject @Inject
private OwnerService service; private OwnerService service;
private String login; private String login;
private String password; private String password;
private boolean editing;
private boolean editing; private String errorMessage;
private String errorMessage; private LazyDataModel<Owner> owners;
public String getLogin() { @PostConstruct
return login; public void init() {
} owners = new LazyDataModel<Owner>() {
@Override
public void setLogin(String name) { public List<Owner> load(int first, int pageSize, Map<String, SortMeta> sortBy, Map<String, FilterMeta> filterBy) {
this.login = name; return service.list(first, pageSize);
} }
public String getPassword() { @Override
return password; public int count(Map<String, FilterMeta> filterBy) {
} return service.countAll();
}
public void setPassword(String password) { };
this.password = password; }
}
public LazyDataModel<Owner> getOwners() {
public String getErrorMessage() { return owners;
return errorMessage; }
}
public String getLogin() { return login; }
public boolean isError() { public void setLogin(String login) { this.login = login; }
return this.errorMessage != null; public String getPassword() { return password; }
} public void setPassword(String password) { this.password = password; }
public boolean isEditing() { return editing; }
public boolean isEditing() { public void setEditing(boolean editing) { this.editing = editing; }
return this.editing; public String getErrorMessage() { return errorMessage; }
} public boolean isError() { return errorMessage != null; }
public void setEditing(boolean editing) { public String getPetNames(String login) {
this.editing = editing; return this.service.getPets(login, 0, 100).stream()
} .map(Pet::getName)
.collect(joining(", "));
public List<Owner> getOwners() { }
return this.service.list(0, 100);
} public String edit(String login) {
this.editing = true;
public String getPetNames(String login) { this.login = login;
return this.service.getPets(0, 100).stream() return getViewId();
.map(Pet::getName) }
.collect(joining(", "));
} public String cancelEditing() {
clear();
public String edit(String login) { return getViewId();
this.editing = true; }
this.login = login;
public String remove(String login) {
return this.getViewId(); service.remove(login);
} return redirectTo(getViewId());
}
public String cancelEditing() {
this.clear(); public String store() {
try {
return this.getViewId(); if (isEditing()) {
} Owner owner = service.get(this.login);
owner.changePassword(this.password);
public String remove(String login) { service.update(owner);
this.service.remove(login); } else {
service.create(new Owner(login, password));
return redirectTo(this.getViewId()); }
} clear();
return redirectTo(getViewId());
public String store() { } catch (Throwable t) {
try { this.errorMessage = t.getMessage();
if (this.isEditing()) { return getViewId();
final Owner owner = this.service.get(this.login); }
owner.changePassword(this.password); }
this.service.update(owner); private void clear() {
} else { this.login = null;
this.service.create(new Owner(login, password)); this.password = null;
} this.errorMessage = null;
this.editing = false;
this.clear(); }
return redirectTo(this.getViewId()); private String redirectTo(String url) { return url + "?faces-redirect=true"; }
} catch (Throwable t) { private String getViewId() { return FacesContext.getCurrentInstance().getViewRoot().getViewId(); }
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();
}
} }
...@@ -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,7 +2,8 @@ ...@@ -2,7 +2,8 @@
<!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:f="http://xmlns.jcp.org/jsf/core" xmlns:p="http://primefaces.org/ui"
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">
<head> <head>
...@@ -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:form>
<h:column> <h:commandButton class="owners-table-remove" value="Remove" action="#{owner.remove(ownerEntity.login)}"/>
<f:facet name="header">Password</f:facet> <h:commandButton class="owners-table-edit" value="Edit" action="#{owner.edit(ownerEntity.login)}"/>
#{ownerEntity.password} </h:form>
</h:column> </p:column>
<h:column> </p:dataTable>
<f:facet name="header">Pets</f:facet>
#{owner.getPetNames(ownerEntity.login)}
</h:column>
<h:column>
<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>
</ui:define> </ui:define>
</ui:composition> </ui:composition>
</body> </body>
......
...@@ -2,7 +2,8 @@ ...@@ -2,7 +2,8 @@
<!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:f="http://xmlns.jcp.org/jsf/core" xmlns:p="http://primefaces.org/ui"
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">
<head> <head>
...@@ -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> <h:outputText value="#{petEntity.birth}">
<f:facet name="header">Birth</f:facet> <f:convertDateTime pattern="yyyy-M-d hh:mm:ss" />
<h:outputText value="#{petEntity.birth}"> </h:outputText>
<f:convertDateTime pattern="yyyy-M-d hh:mm:ss" /> </p:column>
</h:outputText> <p:column headerText="Type">#{petEntity.animal}</p:column>
</h:column> <p:column headerText="Opciones">
<h:column> <h:form>
<f:facet name="header">Type</f:facet> <h:commandButton value="Remove" action="#{pet.remove(petEntity.id)}"/>
#{petEntity.animal} <h:commandButton value="Edit" action="#{pet.edit(petEntity.id)}"/>
</h:column> </h:form>
<h:column> </p:column>
<h:form> </p:dataTable>
<h:commandButton value="Remove" type="submit" action="#{pet.remove(petEntity.id)}"/>
<h:commandButton value="Edit" action="#{pet.edit(petEntity.id)}"/>
</h:form>
</h:column>
</h: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