KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > contrib > sam > models > IterativeResultSetModel


1 /*
2  * Copyright (C) 2003 Stefan Armbruster [stefan@armbruster-it.de]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: IterativeResultSetModel.java,v 1.2 2004/02/01 05:16:27 christianc Exp $
19  */

20 package org.enhydra.barracuda.contrib.sam.models;
21
22 import java.sql.*;
23 import org.enhydra.barracuda.core.comp.*;
24 import org.apache.log4j.*;
25
26 /** A specific ListModel implementation for java.sql.ResultSets.
27  * To use this class, simply pass a ResultSet to the constructor,
28  * the model iterates over all rows of the underlying SQL query.
29  * A sample HTML fragment might look like
30  * <code>
31  * <table>
32  * <tr>
33  * <th>Username</th>
34  * <th>Password</th>
35  * </tr>
36  * <tr class="Dir::Iterate_Start.ResultSet Dir::Iterate_Next.ResultSet">
37  * <td><span class="Dir::Get_Data.ResultSet.name">abc</SPAN></td>
38  * <td><span class="Dir::Get_Data.ResultSet.password">abc</SPAN></td>
39  * </tr>
40  * <tr class="Dir::Iterate_End.ResultSet Dir::Discard">
41  * </tr>
42  * </table>
43  * </code>
44  * @author Stefan Armbruster
45  * @version $Id: IterativeResultSetModel.java,v 1.2 2004/02/01 05:16:27 christianc Exp $
46  */

47 public class IterativeResultSetModel extends AbstractIterativeTemplateModel {
48
49     protected static Logger logger = Logger.getLogger(IterativeResultSetModel.class.getName());
50
51     /** ResultSet representing the SQL query */
52     protected ResultSet resultSet;
53     /** name of the model */
54     protected String JavaDoc name;
55
56     /** initialize the model
57      * @param name Name of the model
58      * @param rs ResultSet to iterate
59      */

60     public IterativeResultSetModel(String JavaDoc name, ResultSet rs) {
61         this.resultSet = rs;
62         setName(name);
63     }
64
65     /** get the name of the model
66      * @return name of the model
67      */

68     public String JavaDoc getName() {
69         return name;
70     }
71
72     /** set the name of the model
73      * @param name name of the model
74      */

75     public void setName(String JavaDoc name) {
76         this.name = name;
77     }
78
79
80     /** returns resultSet.next()
81      * @return resultSet.next()
82      */

83     public boolean hasNext() {
84         try {
85             return resultSet.next();
86         } catch (Exception JavaDoc ex) {
87             logger.error(ex.getMessage(),ex);
88             return false;
89         }
90     }
91
92     /** get the model's value for a specific key
93      * @return result of ResultSet's getString(colname)
94      * @param key must match the columns of the SQL query
95      */

96     public Object JavaDoc getItem(String JavaDoc key) {
97         try {
98             return resultSet.getString(key);
99         }
100         catch (SQLException e) {
101             logger.error(e.getMessage());
102             return super.getItem(key);
103         }
104     }
105
106     /** empty implementation, does nothing. Only reason for being here is to fully
107      * implement IterativeModel.
108      */

109     public void loadNext() {
110     }
111
112 }
113
Popular Tags