Posted By

ronaldtm on 07/29/09


Tagged

pagination Hibernate criteria wicket


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

ronaldtm
duthied


Wicket Pagination example with Hibernate Criteria + DataView


 / Published in: Java
 

  1. //CriteriaDataProvider.java
  2. package dbapp.persistence.hibernate;
  3.  
  4. import static dbapp.persistence.hibernate.Orders.*;
  5.  
  6. import java.util.Iterator;
  7.  
  8. import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
  9. import org.apache.wicket.model.CompoundPropertyModel;
  10. import org.apache.wicket.model.IModel;
  11. import org.hibernate.Criteria;
  12. import org.hibernate.criterion.Projections;
  13.  
  14. public abstract class CriteriaDataProvider<T> extends SortableDataProvider<T> {
  15.  
  16. private String countProperty;
  17.  
  18. public CriteriaDataProvider() {
  19. this("id");
  20. }
  21.  
  22. public CriteriaDataProvider(String countProperty) {
  23. this.countProperty = countProperty;
  24. }
  25.  
  26. protected abstract Criteria createCriteria();
  27. public int size() {
  28. Criteria criteria = createCriteria();
  29. criteria.setProjection(Projections.count(countProperty));
  30. return ((Number) criteria.uniqueResult()).intValue();
  31. }
  32. @SuppressWarnings("unchecked")
  33. public Iterator<? extends T> iterator(int first, int count) {
  34. Criteria criteria = createCriteria();
  35. addOrders(criteria);
  36. criteria.setFirstResult(first).setMaxResults(count);
  37. return criteria.list().iterator();
  38. }
  39. protected void addOrders(Criteria criteria) {
  40. if (getSort() != null)
  41. criteria.addOrder(orderBy(getSort()));
  42. }
  43. public IModel<T> model(T object) {
  44. return new CompoundPropertyModel<T>(object);
  45. }
  46. }
  47.  
  48. //ContatosPage.java
  49. package dbapp.ui.page;
  50.  
  51. import static org.hibernate.criterion.Restrictions.*;
  52.  
  53. import org.apache.wicket.authorization.strategies.role.annotations.AuthorizeInstantiation;
  54. import org.apache.wicket.extensions.markup.html.repeater.data.sort.OrderByBorder;
  55. import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
  56. import org.apache.wicket.markup.html.basic.Label;
  57. import org.apache.wicket.markup.html.form.Button;
  58. import org.apache.wicket.markup.html.form.Form;
  59. import org.apache.wicket.markup.html.form.TextField;
  60. import org.apache.wicket.markup.html.link.Link;
  61. import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
  62. import org.apache.wicket.markup.repeater.Item;
  63. import org.apache.wicket.markup.repeater.data.DataView;
  64. import org.apache.wicket.model.CompoundPropertyModel;
  65. import org.apache.wicket.spring.injection.annot.SpringBean;
  66. import org.hibernate.Criteria;
  67. import org.hibernate.Session;
  68. import org.wicketstuff.annotation.mount.MountPath;
  69. import org.wicketstuff.annotation.strategy.MountHybrid;
  70.  
  71. import dbapp.domain.entity.Contato;
  72. import dbapp.persistence.hibernate.CriteriaDataProvider;
  73. import dbapp.ui.BasePage;
  74.  
  75. @AuthorizeInstantiation("ADMIN")
  76. @MountPath(path = "contatos")
  77. @MountHybrid
  78. @SuppressWarnings("unchecked")
  79. public class ContatosPage extends BasePage {
  80.  
  81. @SpringBean
  82. private Session session;
  83.  
  84. private String keywords = "";
  85.  
  86. SortableDataProvider<Contato> dataProvider = new CriteriaDataProvider<Contato>() {
  87. protected Criteria createCriteria() {
  88. Criteria criteria = session.createCriteria(Contato.class, "contato");
  89. if (keywords != null) {
  90. for (String word : keywords.split(" +")) {
  91. String arg = "%" + word + "%";
  92. criteria.add(or(ilike("contato.nome", arg), ilike("contato.telefone", arg)));
  93. }
  94. }
  95. return criteria;
  96. }
  97. };
  98.  
  99. public ContatosPage() {
  100. super("Contatos - Pesquisa");
  101.  
  102. Form form = new Form("form", new CompoundPropertyModel(this));
  103. form.add(new TextField("keywords"));
  104. form.add(new Button("pesquisar"));
  105. form.add(new Button("novo") {
  106. public void onSubmit() {
  107. setResponsePage(new EditContatoPage(new Contato(), true));
  108. }
  109. });
  110. add(form);
  111.  
  112. DataView<Contato> dataView = new DataView<Contato>("contatos", dataProvider, 2) {
  113. protected void populateItem(Item<Contato> item) {
  114. item.add(new Label("nome"));
  115. item.add(new Label("telefone"));
  116. item.add(new Link<Contato>("editar", item.getModel()) {
  117. public void onClick() {
  118. setResponsePage(new EditContatoPage(getModelObject(), true));
  119. }
  120. });
  121. }
  122. };
  123. add(dataView);
  124. add(new PagingNavigator("footerPaginator", dataView));
  125. add(new PagingNavigator("headerPaginator", dataView));
  126. add(new OrderByBorder("orderByNome", "contato.nome", dataProvider));
  127. add(new OrderByBorder("orderByTelefone", "contato.telefone", dataProvider));
  128. }
  129. }
  130.  
  131.  
  132.  
  133. //ContatosPage.html
  134. <html xmlns:wicket="http://wicket.apache.org">
  135. <body>
  136. <wicket:extend>
  137. <div class="span-24 last">
  138. <form wicket:id="form">
  139. <fieldset>
  140. <legend>Pesquisa por Contatos</legend>
  141. <div class="last">
  142. <label>Palavras-chave
  143. <input wicket:id="keywords" style="width: 100%;">
  144. </label>
  145. </div>
  146. <div class="last">
  147. <button wicket:id="pesquisar">Pesquisar</button>
  148. <button wicket:id="novo">Novo Contato...</button>
  149. </div>
  150. </fieldset>
  151. </form>
  152. </div>
  153. <div class="span-24 last">
  154. <table>
  155. <caption>Registro(s) encontrado(s):</caption>
  156. <thead>
  157. <tr>
  158. <td colspan="3" wicket:id="headerPaginator"></td>
  159. </tr>
  160. </thead>
  161. <thead>
  162. <tr>
  163. <th wicket:id="orderByNome">Nome</th>
  164. <th wicket:id="orderByTelefone">Telefone</th>
  165. <th></th>
  166. </tr>
  167. </thead>
  168. <tbody>
  169. <tr wicket:id="contatos">
  170. <td wicket:id="nome"></td>
  171. <td wicket:id="telefone"></td>
  172. <td><a wicket:id="editar">editar</a></td>
  173. </tr>
  174. </tbody>
  175. <tfoot>
  176. <tr>
  177. <td colspan="3" wicket:id="footerPaginator"></td>
  178. </tr>
  179. </tfoot>
  180. </table>
  181. </div>
  182. </wicket:extend>
  183. </body>
  184. </html>

Report this snippet  

You need to login to post a comment.