1 24 package org.riotfamily.website.generic.model.hibernate; 25 26 import java.util.List ; 27 import java.util.Map ; 28 29 import javax.servlet.http.HttpServletRequest ; 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 43 public class HqlPagedListModelBuilder extends HqlListModelBuilder { 44 45 private String pagerModelKey = "pager"; 46 47 private String countHql; 48 49 private String pageParam = "page"; 50 51 private String pageSizeParam; 52 53 private int pageSize = 10; 54 55 private int padding = 3; 56 57 public void afterPropertiesSet() throws Exception { 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 77 public void setCountHql(String countHql) { 78 this.countHql = countHql; 79 } 80 81 85 public void setPageParam(String pageParam) { 86 this.pageParam = pageParam; 87 } 88 89 93 public void setPagerModelKey(String pagerModelKey) { 94 this.pagerModelKey = pagerModelKey; 95 } 96 97 100 public void setPageSize(int pageSize) { 101 this.pageSize = pageSize; 102 } 103 104 109 public void setPageSizeParam(String pageSizeParam) { 110 this.pageSizeParam = pageSizeParam; 111 } 112 113 119 public void setPadding(int padding) { 120 this.padding = padding; 121 } 122 123 126 protected int getPage(HttpServletRequest request) { 127 return ServletRequestUtils.getIntParameter(request, pageParam, 1); 128 } 129 130 133 protected int getPageSize(HttpServletRequest request) { 134 if (pageSizeParam == null) { 135 return pageSize; 136 } 137 return ServletRequestUtils.getIntParameter(request, pageSizeParam, 138 pageSize); 139 } 140 141 public Map buildModel(final HttpServletRequest 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 items = query.list(); 151 152 Query countQuery = createQuery(countHql); 153 setParameters(countQuery, request); 154 Number count = (Number )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 key, HttpServletRequest 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 |