Revision: 16233
Updated Code
at July 29, 2009 16:45 by ronaldtm
Updated Code
//CriteriaDataProvider.java package dbapp.persistence.hibernate; import static dbapp.persistence.hibernate.Orders.*; import java.util.Iterator; import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider; import org.apache.wicket.model.CompoundPropertyModel; import org.apache.wicket.model.IModel; import org.hibernate.Criteria; import org.hibernate.criterion.Projections; public abstract class CriteriaDataProvider<T> extends SortableDataProvider<T> { private String countProperty; public CriteriaDataProvider() { this("id"); } public CriteriaDataProvider(String countProperty) { this.countProperty = countProperty; } protected abstract Criteria createCriteria(); public int size() { Criteria criteria = createCriteria(); criteria.setProjection(Projections.count(countProperty)); return ((Number) criteria.uniqueResult()).intValue(); } @SuppressWarnings("unchecked") public Iterator<? extends T> iterator(int first, int count) { Criteria criteria = createCriteria(); addOrders(criteria); criteria.setFirstResult(first).setMaxResults(count); return criteria.list().iterator(); } protected void addOrders(Criteria criteria) { if (getSort() != null) criteria.addOrder(orderBy(getSort())); } public IModel<T> model(T object) { return new CompoundPropertyModel<T>(object); } } //ContatosPage.java package dbapp.ui.page; import static org.hibernate.criterion.Restrictions.*; import org.apache.wicket.authorization.strategies.role.annotations.AuthorizeInstantiation; import org.apache.wicket.extensions.markup.html.repeater.data.sort.OrderByBorder; import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider; import org.apache.wicket.markup.html.basic.Label; import org.apache.wicket.markup.html.form.Button; import org.apache.wicket.markup.html.form.Form; import org.apache.wicket.markup.html.form.TextField; import org.apache.wicket.markup.html.link.Link; import org.apache.wicket.markup.html.navigation.paging.PagingNavigator; import org.apache.wicket.markup.repeater.Item; import org.apache.wicket.markup.repeater.data.DataView; import org.apache.wicket.model.CompoundPropertyModel; import org.apache.wicket.spring.injection.annot.SpringBean; import org.hibernate.Criteria; import org.hibernate.Session; import org.wicketstuff.annotation.mount.MountPath; import org.wicketstuff.annotation.strategy.MountHybrid; import dbapp.domain.entity.Contato; import dbapp.persistence.hibernate.CriteriaDataProvider; import dbapp.ui.BasePage; @AuthorizeInstantiation("ADMIN") @MountPath(path = "contatos") @MountHybrid @SuppressWarnings("unchecked") public class ContatosPage extends BasePage { @SpringBean private Session session; private String keywords = ""; SortableDataProvider<Contato> dataProvider = new CriteriaDataProvider<Contato>() { protected Criteria createCriteria() { Criteria criteria = session.createCriteria(Contato.class, "contato"); if (keywords != null) { for (String word : keywords.split(" +")) { String arg = "%" + word + "%"; criteria.add(or(ilike("contato.nome", arg), ilike("contato.telefone", arg))); } } return criteria; } }; public ContatosPage() { super("Contatos - Pesquisa"); Form form = new Form("form", new CompoundPropertyModel(this)); form.add(new TextField("keywords")); form.add(new Button("pesquisar")); form.add(new Button("novo") { public void onSubmit() { setResponsePage(new EditContatoPage(new Contato(), true)); } }); add(form); DataView<Contato> dataView = new DataView<Contato>("contatos", dataProvider, 2) { protected void populateItem(Item<Contato> item) { item.add(new Label("nome")); item.add(new Label("telefone")); item.add(new Link<Contato>("editar", item.getModel()) { public void onClick() { setResponsePage(new EditContatoPage(getModelObject(), true)); } }); } }; add(dataView); add(new PagingNavigator("footerPaginator", dataView)); add(new PagingNavigator("headerPaginator", dataView)); add(new OrderByBorder("orderByNome", "contato.nome", dataProvider)); add(new OrderByBorder("orderByTelefone", "contato.telefone", dataProvider)); } } //ContatosPage.html <html xmlns:wicket="http://wicket.apache.org"> <body> <wicket:extend> <div class="span-24 last"> <form wicket:id="form"> <fieldset> <legend>Pesquisa por Contatos</legend> <div class="last"> <label>Palavras-chave <input wicket:id="keywords" style="width: 100%;"> </label> </div> <div class="last"> <button wicket:id="pesquisar">Pesquisar</button> <button wicket:id="novo">Novo Contato...</button> </div> </fieldset> </form> </div> <div class="span-24 last"> <table> <caption>Registro(s) encontrado(s):</caption> <thead> <tr> <td colspan="3" wicket:id="headerPaginator"></td> </tr> </thead> <thead> <tr> <th wicket:id="orderByNome">Nome</th> <th wicket:id="orderByTelefone">Telefone</th> <th></th> </tr> </thead> <tbody> <tr wicket:id="contatos"> <td wicket:id="nome"></td> <td wicket:id="telefone"></td> <td><a wicket:id="editar">editar</a></td> </tr> </tbody> <tfoot> <tr> <td colspan="3" wicket:id="footerPaginator"></td> </tr> </tfoot> </table> </div> </wicket:extend> </body> </html>
Revision: 16232
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at July 29, 2009 13:20 by ronaldtm
Initial Code
//CriteriaDataProvider.java package dbapp.persistence.hibernate; import static dbapp.persistence.hibernate.Orders.*; import java.util.Iterator; import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider; import org.apache.wicket.model.CompoundPropertyModel; import org.apache.wicket.model.IModel; import org.hibernate.Criteria; import org.hibernate.criterion.Projections; public abstract class CriteriaDataProvider<T> extends SortableDataProvider<T> { private String countProperty; public CriteriaDataProvider() { this("id"); } public CriteriaDataProvider(String countProperty) { this.countProperty = countProperty; } protected abstract Criteria createCriteria(); public int size() { Criteria criteria = createCriteria(); criteria.setProjection(Projections.count(countProperty)); return ((Number) criteria.uniqueResult()).intValue(); } @SuppressWarnings("unchecked") public Iterator<? extends T> iterator(int first, int count) { Criteria criteria = createCriteria(); addOrders(criteria); criteria.setFirstResult(first).setMaxResults(count); return criteria.list().iterator(); } protected void addOrders(Criteria criteria) { if (getSort() != null) criteria.addOrder(orderBy(getSort())); } public IModel<T> model(T object) { return new CompoundPropertyModel<T>(object); } } //ContatosPage.java package dbapp.ui.page; import static org.hibernate.criterion.Restrictions.ilike; import static org.hibernate.criterion.Restrictions.or; import org.apache.wicket.authorization.strategies.role.annotations.AuthorizeInstantiation; import org.apache.wicket.extensions.markup.html.repeater.data.sort.OrderByBorder; import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider; import org.apache.wicket.markup.html.basic.Label; import org.apache.wicket.markup.html.form.Button; import org.apache.wicket.markup.html.form.Form; import org.apache.wicket.markup.html.form.TextField; import org.apache.wicket.markup.html.link.Link; import org.apache.wicket.markup.repeater.Item; import org.apache.wicket.markup.repeater.data.DataView; import org.apache.wicket.model.CompoundPropertyModel; import org.apache.wicket.spring.injection.annot.SpringBean; import org.hibernate.Criteria; import org.hibernate.Session; import org.wicketstuff.annotation.mount.MountPath; import org.wicketstuff.annotation.strategy.MountHybrid; import dbapp.domain.entity.Contato; import dbapp.persistence.hibernate.CriteriaDataProvider; import dbapp.ui.BasePage; @AuthorizeInstantiation("ADMIN") @MountPath(path = "contatos") @MountHybrid @SuppressWarnings("unchecked") public class ContatosPage extends BasePage { @SpringBean private Session session; private String keywords = ""; SortableDataProvider<Contato> dataProvider = new CriteriaDataProvider<Contato>() { protected Criteria createCriteria() { Criteria criteria = session.createCriteria(Contato.class, "contato"); if (keywords != null) { for (String word : keywords.split(" +")) { String arg = "%" + word + "%"; criteria.add(or(ilike("contato.nome", arg), ilike("contato.telefone", arg))); } } return criteria; } }; public ContatosPage() { super("Contatos - Pesquisa"); Form form = new Form("form", new CompoundPropertyModel(this)); form.add(new TextField("keywords")); form.add(new Button("pesquisar")); form.add(new Button("novo") { public void onSubmit() { setResponsePage(new EditContatoPage(new Contato(), true)); } }); add(form); add(new OrderByBorder("orderByNome", "contato.nome", dataProvider)); add(new OrderByBorder("orderByTelefone", "contato.telefone", dataProvider)); add(new DataView<Contato>("contatos", dataProvider) { protected void populateItem(Item<Contato> item) { item.add(new Label("nome")); item.add(new Label("telefone")); item.add(new Link<Contato>("editar", item.getModel()) { public void onClick() { setResponsePage(new EditContatoPage(getModelObject(), true)); } }); } }); } } //ContatosPage.html <html xmlns:wicket="http://wicket.apache.org"> <body> <wicket:extend> <div class="span-24 last"> <form wicket:id="form"> <fieldset> <legend>Pesquisa por Contatos</legend> <div class="last"> <label>Palavras-chave <input wicket:id="keywords" style="width: 100%;"> </label> </div> <div class="last"> <button wicket:id="pesquisar">Pesquisar</button> <button wicket:id="novo">Novo Contato...</button> </div> </fieldset> </form> </div> <div class="span-24 last"> <table> <caption>Registro(s) encontrado(s):</caption> <thead> <tr> <th wicket:id="orderByNome">Nome</th> <th wicket:id="orderByTelefone">Telefone</th> <th></th> </tr> </thead> <tbody> <tr wicket:id="contatos"> <td wicket:id="nome"></td> <td wicket:id="telefone"></td> <td><a wicket:id="editar">editar</a></td> </tr> </tbody> </table> </div> </wicket:extend> </body> </html>
Initial URL
Initial Description
Initial Title
Wicket Pagination example with Hibernate Criteria + DataView
Initial Tags
Initial Language
Java