1 /*** $Id: AbstractPageByPageAction.java,v 1.1.1.1 2004/05/18 10:50:17 mochoa Exp $ */
2 package org.j2ee.dao.util;
3
4 //sql
5 import java.sql.SQLException;
6
7 // Java
8 import java.util.ArrayList;
9 import java.util.Iterator;
10 import java.util.List;
11
12 //dao
13 import org.j2ee.dao.AbstractHibernateDAOAction;
14 import org.j2ee.dao.HibernateDAOAction;
15
16 /***
17 * Limit—with offset
18 * Objective: Want to only get n rows in the result set,
19 * and we want the first y rows in the result set discarded.
20 * Usually only makes sense in connection with an ORDER BY expression.
21 * see http://troels.arvin.dk/db/rdbms/#select-limit-offset
22 *
23 * Last modified $Date: 2004/05/18 10:50:17 $
24 * @version $Revision: 1.1.1.1 $
25 * @author mochoa (mochoa@users.sourceforge.net) */
26 public abstract class AbstractPageByPageAction extends AbstractHibernateDAOAction {
27
28 private ArrayList dtoList;
29 private int from = 1;
30 private int to = 5;
31 protected HibernateDAOAction action = null;
32
33 public AbstractPageByPageAction() {
34 super(AbstractHibernateDAOAction.SQL_QUERY);
35 dtoList = new ArrayList(to-from+1);
36 }
37
38 public AbstractPageByPageAction(int fromNo, int toNo) {
39 super(AbstractHibernateDAOAction.SQL_QUERY);
40 from = fromNo;
41 to = toNo;
42 dtoList = new ArrayList(to-from+1);
43 }
44
45
46 public HibernateDAOAction getAction() { return action; }
47 public void setAction(HibernateDAOAction action) throws Exception {
48 if (!action.isSqlQuery()) throw new Exception("Page By Page Action is only valid with SQL Queries");
49 this.action = action;
50 }
51
52 public List getList() {
53 return dtoList;
54 }
55
56 /***
57 * This method must return a valid SQL query, it will receive all the named
58 * parameters defined by the action object plus two aditionals parameters
59 * named firstRow and lastRow representing the values for the window to be select.
60 * @return an String database dependant syntax for Limit—with offset querys.
61 * http://troels.arvin.dk/db/rdbms/ for more information.
62 */
63 public abstract String getSQL();
64
65 public Object[] getParameters() {
66 Object[] qryParam = action.getParameters();
67 int size = 2;
68 if (qryParam!=null)
69 size = qryParam.length+2;
70 Object[] nqryParam = new Object[size];
71 if (qryParam!=null)
72 for(int i=0;i<size-2;i++)
73 nqryParam[i]=qryParam[i];
74 nqryParam[size-2] = new Integer(this.from);
75 nqryParam[size-1] = new Integer(this.to);
76 return nqryParam;
77 }
78
79 public String[] getNamesForParams() {
80 String[] qryNames = action.getNamesForParams();
81 int size = 2;
82 if (qryNames!=null)
83 size = qryNames.length+2;
84 String[] nqryNames = new String[size];
85 if (qryNames!=null)
86 for(int i=0;i<size-2;i++)
87 nqryNames[i]=qryNames[i];
88 nqryNames[size-2] = "firstRow";
89 nqryNames[size-1] = "lastRow";
90 return nqryNames;
91 }
92
93 public Class [] getReturnClassForSqlQuery() {
94 return action.getReturnClassForSqlQuery();
95 }
96
97 public String [] getReturnAliasForSqlQuery() {
98 return action.getReturnAliasForSqlQuery();
99 }
100
101 public final void handleResults(List rs) throws SQLException {
102 for(int i=0;i<rs.size();i++) {
103 Object dto = rs.get(i);
104 dtoList.add(i,dto);
105 }
106 }
107
108 public void print() {
109 Iterator ite = dtoList.iterator();
110 while(ite.hasNext()) {
111 Object dto = ite.next();
112 System.out.println(dto.toString());
113 }
114 }
115
116
117 public void setFrom(int from) {
118 this.from = from;
119 if (dtoList.size()<(to-from))
120 dtoList = new ArrayList(to-from+1);
121 }
122
123
124 public int getFrom() {
125 return from;
126 }
127
128
129 public void setTo(int to) {
130 this.to = to;
131 if (dtoList.size()<(to-from))
132 dtoList = new ArrayList(to-from+1);
133 }
134
135
136 public int getTo() {
137 return to;
138 }
139 }
This page was automatically generated by Maven