1 5 package org.exoplatform.services.database; 6 7 import java.util.List ; 8 import org.exoplatform.commons.utils.PageList; 9 import net.sf.hibernate.Query; 10 import net.sf.hibernate.Session; 11 16 public class DBObjectPageList extends PageList { 17 18 private String findQuery_ ; 19 private String countQuery_ ; 20 private HibernateService service_ ; 21 22 public DBObjectPageList(HibernateService service, Class objectType) throws Exception { 23 super(20) ; 24 service_ = service ; 25 findQuery_ = "from o in class " + objectType.getName() ; 26 countQuery_ = "select count(o) from " + objectType.getName() + " o" ; 27 Session session = service_.openSession() ; 28 List l = session.find(countQuery_) ; 29 Integer count = (Integer ) l.get(0) ; 30 setAvailablePage(count.intValue()) ; 31 } 32 33 public DBObjectPageList(HibernateService service, ObjectQuery oq) throws Exception { 34 super(20) ; 35 service_ = service ; 36 findQuery_ = oq.getHibernateQuery() ; 37 countQuery_ = oq.getHibernateCountQuery() ; 38 Session session = service_.openSession() ; 39 List l = session.find(countQuery_) ; 40 Integer count = (Integer ) l.get(0) ; 41 setAvailablePage(count.intValue()) ; 42 } 43 44 public DBObjectPageList(HibernateService service, int pageSize, 45 String query, String countQuery) throws Exception { 46 super(pageSize) ; 47 service_ = service ; 48 findQuery_ = query ; 49 countQuery_ = countQuery ; 50 Session session = service_.openSession() ; 51 List l = session.find(countQuery_) ; 52 Integer count = (Integer ) l.get(0) ; 53 setAvailablePage(count.intValue()) ; 54 } 55 56 protected void populateCurrentPage(int page) throws Exception { 57 Session session = service_.openSession() ; 58 Query query = session.createQuery(findQuery_); 59 int from = getFrom() ; 60 query.setFirstResult(from); 61 query.setMaxResults(getTo()- from) ; 62 currentListPage_ = query.list() ; 63 } 64 65 public List getAll() throws Exception { 66 Session session = service_.openSession() ; 67 Query query = session.createQuery(findQuery_); 68 return query.list() ; 69 } 70 } | Popular Tags |