KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mapper > storage > PersistentDocumentInfo


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.mapper.storage;
24
25 import java.sql.*;
26
27 import org.xquark.mapper.dbms.AbstractConnection;
28 import org.xquark.mapper.dbms.TableInfo;
29 import org.xquark.mapper.metadata.DocumentInfo;
30
31 /** Data structure that corresponds to the storage table for XML document
32  * information used by the CollectionMetadata object.
33  * <p>This object contains all its JDBC calls and owns a connection
34  * in order to perform database access on his own</p>
35  */

36
37 public class PersistentDocumentInfo
38 {
39     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
40     private static final String JavaDoc RCSName = "$Name: $";
41     
42     private DocumentInfo wDocInfo;
43     private TableInfo docTable;
44     private String JavaDoc docInfoStmt;
45
46     /////////////////////////////////////
47
// Storage JDBC variables
48
/////////////////////////////////////
49

50     /** Connection object used by this data object. */
51     private AbstractConnection connection;
52
53     /** java.sql objects used to fetch the whole data set from the database. */
54     private Statement stmt = null;
55     private ResultSet rs = null;
56
57     public PersistentDocumentInfo(TableInfo docTable, AbstractConnection connection)
58     {
59         this.connection = connection;
60         this.docTable = docTable;
61         initStatements();
62     }
63
64     private void initStatements()
65     {
66         StringBuffer JavaDoc sql = new StringBuffer JavaDoc();
67         sql.append(docTable.getSelectAllStatement());
68         sql.append(" WHERE ");
69         sql.append(docTable.getColumns()[0].getName());
70         sql.append("=?");
71         docInfoStmt = sql.toString();
72     }
73
74     public DocumentInfo getDocumentInfo()
75     {
76         return wDocInfo;
77     }
78
79      /**
80      * Save this object data into a config table row with batch style.
81      * Automatic flush is proceeded when max size is reached
82      * @throws SQLException if a database access error occurs.
83      */

84     public void save()
85     throws SQLException
86     {
87         // preparation of PreparedStatement
88
PreparedStatement pStmt = connection.getConnection().prepareStatement(docTable.getInsertStatement());
89         try
90         {
91             pStmt.setString(1, wDocInfo.docID);
92             pStmt.setLong(2, wDocInfo.udid);
93             pStmt.setTimestamp(3, wDocInfo.creationDate);
94             pStmt.setLong(4, wDocInfo.nodeCount);
95             pStmt.setInt(5, wDocInfo.avgNodeLen);
96             pStmt.executeUpdate();
97         }
98         finally
99         {
100             pStmt.close();
101         }
102     }
103
104     /** Free all resources (JDBC)
105      * @throws RepositoryException application error
106      */

107     public void close()
108     {
109         // release statement
110
try {
111             if (rs != null)
112                 rs.close();
113             if (stmt != null)
114                 stmt.close();
115             connection = null;
116         }
117         catch(SQLException e) {
118             // no op
119
}
120     }
121     
122     public void retrieveDataSet()
123     throws SQLException
124     {
125         stmt = connection.getConnection().createStatement();
126         try {
127             rs = stmt.executeQuery(docTable.getSelectAllStatement());
128         }
129         catch (SQLException e) {
130             stmt.close();
131             throw e;
132         }
133     }
134
135     /** get the next row from database and store it in the current object.
136      * @throws SQLException if a database access error occurs.
137      * @return true, if a row was fetched, false if no more rows.
138      */

139     public boolean fetchNextRow()
140     throws SQLException
141     {
142         if (stmt == null)
143             return false;
144         try {
145             if (fetchNextResult(rs))
146                 return true;
147             else
148             {
149                 rs.close();
150                 stmt.close();
151                 stmt = null; // prevent closing 2 times
152
return false; // If no more row found
153
}
154         }
155         catch (SQLException e) {
156             rs.close();
157             stmt.close();
158             throw e;
159         }
160     }
161
162     public boolean fetchRow(String JavaDoc docID)
163     throws SQLException
164     {
165         PreparedStatement stmt = null;
166         ResultSet rs = null;
167         try {
168             stmt = connection.getConnection().prepareStatement(docInfoStmt);
169             stmt.setString(1, docID);
170             rs = stmt.executeQuery();
171             
172             if (fetchNextResult(rs))
173                 return true; // Take first row encountered
174
else
175                 return false; // If no row found
176
}
177         finally
178         {
179             if (rs != null)
180                 rs.close();
181             if (stmt != null)
182                 stmt.close();
183         }
184     }
185
186     private boolean fetchNextResult(ResultSet rs)
187     throws SQLException
188     {
189         if(rs.next())
190         {
191             wDocInfo = new DocumentInfo(
192                                 rs.getString(1),
193                                 rs.getLong(2),
194                                 rs.getTimestamp(3),
195                                 rs.getLong(4),
196                                 rs.getInt(5)
197                                 );
198             return true;
199         }
200         else
201             return false; // If no more row found
202
}
203 }
204
Popular Tags