KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > database > DBObjectPageList


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.services.database;
6
7 import java.util.List JavaDoc;
8 import org.exoplatform.commons.utils.PageList;
9 import net.sf.hibernate.Query;
10 import net.sf.hibernate.Session;
11 /**
12  * @author Tuan Nguyen (tuan08@users.sourceforge.net)
13  * @since Oct 21, 2004
14  * @version $Id: DBObjectPageList.java,v 1.1 2004/10/22 14:22:27 tuan08 Exp $
15  */

16 public class DBObjectPageList extends PageList {
17  
18   private String JavaDoc findQuery_ ;
19   private String JavaDoc countQuery_ ;
20   private HibernateService service_ ;
21   
22   public DBObjectPageList(HibernateService service, Class JavaDoc objectType) throws Exception JavaDoc {
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 JavaDoc l = session.find(countQuery_) ;
29     Integer JavaDoc count = (Integer JavaDoc) l.get(0) ;
30     setAvailablePage(count.intValue()) ;
31   }
32   
33   public DBObjectPageList(HibernateService service, ObjectQuery oq) throws Exception JavaDoc {
34     super(20) ;
35     service_ = service ;
36     findQuery_ = oq.getHibernateQuery() ;
37     countQuery_ = oq.getHibernateCountQuery() ;
38     Session session = service_.openSession() ;
39     List JavaDoc l = session.find(countQuery_) ;
40     Integer JavaDoc count = (Integer JavaDoc) l.get(0) ;
41     setAvailablePage(count.intValue()) ;
42   }
43   
44   public DBObjectPageList(HibernateService service, int pageSize,
45                           String JavaDoc query, String JavaDoc countQuery) throws Exception JavaDoc {
46     super(pageSize) ;
47     service_ = service ;
48     findQuery_ = query ;
49     countQuery_ = countQuery ;
50     Session session = service_.openSession() ;
51     List JavaDoc l = session.find(countQuery_) ;
52     Integer JavaDoc count = (Integer JavaDoc) l.get(0) ;
53     setAvailablePage(count.intValue()) ;
54   }
55   
56   protected void populateCurrentPage(int page) throws Exception JavaDoc {
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 JavaDoc getAll() throws Exception JavaDoc {
66     Session session = service_.openSession() ;
67     Query query = session.createQuery(findQuery_);
68     return query.list() ;
69   }
70 }
Popular Tags