KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > website > generic > model > hibernate > HqlPagedListModelBuilder


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.website.generic.model.hibernate;
25
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30
31 import org.hibernate.Query;
32 import org.riotfamily.common.collection.FlatMap;
33 import org.riotfamily.riot.hibernate.support.HibernateUtils;
34 import org.riotfamily.website.generic.view.Pager;
35 import org.springframework.beans.factory.BeanCreationException;
36 import org.springframework.web.bind.ServletRequestUtils;
37
38 /**
39  * HqlModelBuilder that supports large lists and provides pagination. In
40  * addition to the HQL query that returns the list items, a second query must be
41  * specified, that returns the total number of items.
42  */

43 public class HqlPagedListModelBuilder extends HqlListModelBuilder {
44
45     private String JavaDoc pagerModelKey = "pager";
46
47     private String JavaDoc countHql;
48
49     private String JavaDoc pageParam = "page";
50
51     private String JavaDoc pageSizeParam;
52
53     private int pageSize = 10;
54
55     private int padding = 3;
56
57     public void afterPropertiesSet() throws Exception JavaDoc {
58         super.afterPropertiesSet();
59         if (countHql == null) {
60             if (getHql().startsWith("from")) {
61                 countHql = "select count(*) " + getHql();
62                 int i = countHql.indexOf(" order by ");
63                 if (i != -1) {
64                     countHql = countHql.substring(0, i);
65                 }
66             }
67             else {
68                 throw new BeanCreationException("The property 'countHql' must "
69                         + "be set if hql doesn't starts with 'from'");
70             }
71         }
72     }
73
74     /**
75      * Sets the HQL query that returns the total number of items.
76      */

77     public void setCountHql(String JavaDoc countHql) {
78         this.countHql = countHql;
79     }
80
81     /**
82      * Sets the name of the HTTP parameter that contains the current page
83      * number. Please note that page numbers start at <code>1</code>.
84      */

85     public void setPageParam(String JavaDoc pageParam) {
86         this.pageParam = pageParam;
87     }
88
89     /**
90      * Sets the key under which the {@link Pager pager bean} will be placed in
91      * the model. Defaults to <code>pager</code>
92      */

93     public void setPagerModelKey(String JavaDoc pagerModelKey) {
94         this.pagerModelKey = pagerModelKey;
95     }
96
97     /**
98      * Sets the page size, i.e. the number of items displayed on each page.
99      */

100     public void setPageSize(int pageSize) {
101         this.pageSize = pageSize;
102     }
103
104     /**
105      * Sets the name of the HTTP parameter that determines the page size.
106      * Default is <code>null</code>, which disables the variable page size
107      * feature.
108      */

109     public void setPageSizeParam(String JavaDoc pageSizeParam) {
110         this.pageSizeParam = pageSizeParam;
111     }
112
113     /**
114      * Sets the number of pages that will be displayed before and after the
115      * current page.
116      *
117      * @see Pager
118      */

119     public void setPadding(int padding) {
120         this.padding = padding;
121     }
122
123     /**
124      * Returns the current page number for the given request.
125      */

126     protected int getPage(HttpServletRequest JavaDoc request) {
127         return ServletRequestUtils.getIntParameter(request, pageParam, 1);
128     }
129
130     /**
131      * Returns the page-size for the given request.
132      */

133     protected int getPageSize(HttpServletRequest JavaDoc request) {
134         if (pageSizeParam == null) {
135             return pageSize;
136         }
137         return ServletRequestUtils.getIntParameter(request, pageSizeParam,
138                 pageSize);
139     }
140
141     public Map JavaDoc buildModel(final HttpServletRequest JavaDoc request) {
142         HibernateUtils.enableLiveModeFilterIfNecessary(getSession());
143         final int page = getPage(request);
144         final int pageSize = getPageSize(request);
145
146         Query query = createQuery(getHql());
147         setParameters(query, request);
148         query.setFirstResult((page - 1) * pageSize);
149         query.setMaxResults(pageSize);
150         List JavaDoc items = query.list();
151
152         Query countQuery = createQuery(countHql);
153         setParameters(countQuery, request);
154         Number JavaDoc count = (Number JavaDoc)countQuery.uniqueResult();
155
156         Pager pager = new Pager(page, pageSize, count.longValue());
157         pager.initialize(request, padding, pageParam);
158
159         tagResult(query, items, request);
160         
161         FlatMap model = new FlatMap();
162         model.put(getModelKey(query), items);
163         model.put(pagerModelKey, pager);
164         return model;
165     }
166
167     public void appendCacheKey(StringBuffer JavaDoc key, HttpServletRequest JavaDoc request) {
168         super.appendCacheKey(key, request);
169         key.append("?page=").append(getPage(request));
170         key.append("&pageSize=").append(getPageSize(request));
171     }
172 }
173
Popular Tags