KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mvnforum > db > jdbc > RankDAOImplJDBC


1 /*
2  * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/db/jdbc/RankDAOImplJDBC.java,v 1.8 2006/04/14 17:05:26 minhnn Exp $
3  * $Author: minhnn $
4  * $Revision: 1.8 $
5  * $Date: 2006/04/14 17:05:26 $
6  *
7  * ====================================================================
8  *
9  * Copyright (C) 2002-2006 by MyVietnam.net
10  *
11  * All copyright notices regarding mvnForum MUST remain
12  * intact in the scripts and in the outputted HTML.
13  * The "powered by" text/logo with a link back to
14  * http://www.mvnForum.com and http://www.MyVietnam.net in
15  * the footer of the pages MUST remain visible when the pages
16  * are viewed on the internet or intranet.
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 2 of the License, or
21  * any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31  *
32  * Support can be obtained from support forums at:
33  * http://www.mvnForum.com/mvnforum/index
34  *
35  * Correspondence and Marketing Questions can be sent to:
36  * info at MyVietnam net
37  *
38  * @author: Minh Nguyen
39  * @author: Mai Nguyen
40  */

41 package com.mvnforum.db.jdbc;
42
43 import java.sql.*;
44 import java.util.ArrayList JavaDoc;
45 import java.util.Collection JavaDoc;
46
47 import com.mvnforum.db.RankBean;
48 import com.mvnforum.db.RankDAO;
49 import net.myvietnam.mvncore.db.DBUtils;
50 import net.myvietnam.mvncore.exception.*;
51 import org.apache.commons.logging.Log;
52 import org.apache.commons.logging.LogFactory;
53
54 public class RankDAOImplJDBC implements RankDAO {
55
56     private static Log log = LogFactory.getLog(RankDAOImplJDBC.class);
57
58     // this variable will support caching if cache for this class is needed
59
private static boolean m_dirty = true;
60
61     public RankDAOImplJDBC() {
62     }
63
64     public static boolean isDirty() {
65         return m_dirty;
66     }
67
68     public static void setDirty(boolean dirty) {
69         m_dirty = dirty;
70     }
71
72     public void findByAlternateKey_RankTitle(String JavaDoc rankTitle)
73         throws ObjectNotFoundException, DatabaseException {
74
75         Connection connection = null;
76         PreparedStatement statement = null;
77         ResultSet resultSet = null;
78         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
79         sql.append("SELECT RankTitle");
80         sql.append(" FROM " + TABLE_NAME);
81         sql.append(" WHERE RankTitle = ?");
82         try {
83             connection = DBUtils.getConnection();
84             statement = connection.prepareStatement(sql.toString());
85             statement.setString(1, rankTitle);
86             resultSet = statement.executeQuery();
87             if (!resultSet.next()) {
88                 throw new ObjectNotFoundException("Cannot find the alternate key [RankTitle] (" + rankTitle + ") in table 'Rank'.");
89             }
90         } catch(SQLException sqle) {
91             log.error("Sql Execution Error!", sqle);
92             throw new DatabaseException("Error executing SQL in RankDAOImplJDBC.findByAlternateKey_RankTitle.");
93         } finally {
94             DBUtils.closeResultSet(resultSet);
95             DBUtils.closeStatement(statement);
96             DBUtils.closeConnection(connection);
97         }
98     }
99
100     public void findByAlternateKey_RankMinPosts(int rankMinPosts)
101         throws ObjectNotFoundException, DatabaseException {
102
103         Connection connection = null;
104         PreparedStatement statement = null;
105         ResultSet resultSet = null;
106         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
107         sql.append("SELECT RankMinPosts");
108         sql.append(" FROM " + TABLE_NAME);
109         sql.append(" WHERE RankMinPosts = ?");
110         try {
111             connection = DBUtils.getConnection();
112             statement = connection.prepareStatement(sql.toString());
113             statement.setInt(1, rankMinPosts);
114             resultSet = statement.executeQuery();
115             if (!resultSet.next()) {
116                 throw new ObjectNotFoundException("Cannot find the alternate key [RankMinPosts] (" + rankMinPosts + ") in table 'Rank'.");
117             }
118         } catch(SQLException sqle) {
119             log.error("Sql Execution Error!", sqle);
120             throw new DatabaseException("Error executing SQL in RankDAOImplJDBC.findByAlternateKey_RankMinPosts.");
121         } finally {
122             DBUtils.closeResultSet(resultSet);
123             DBUtils.closeStatement(statement);
124             DBUtils.closeConnection(connection);
125         }
126     }
127
128     /*
129      * Included columns: RankMinPosts, RankLevel, RankTitle, RankImage, RankType,
130      * RankOption
131      * Excluded columns: RankID
132      */

133     public void create(int rankMinPosts, int rankLevel, String JavaDoc rankTitle,
134                        String JavaDoc rankImage, int rankType, int rankOption)
135         throws CreateException, DatabaseException, DuplicateKeyException {
136
137         // @todo: Comment this try-catch block if the needed columns dont have attribute 'include'
138
// If this is the case, then it is highly recommended that you regenerate this method with the attribute 'include' turned on
139
try {
140             //Check if alternate key already exists
141
findByAlternateKey_RankTitle(rankTitle);
142             //If so, then we have to throw an exception
143
throw new DuplicateKeyException("Alternate key already exists. Cannot create new Rank with the same [RankTitle] (" + rankTitle + ").");
144         } catch(ObjectNotFoundException e) {
145             //Otherwise we can go ahead
146
}
147
148         // @todo: Comment this try-catch block if the needed columns dont have attribute 'include'
149
// If this is the case, then it is highly recommended that you regenerate this method with the attribute 'include' turned on
150
try {
151             //Check if alternate key already exists
152
findByAlternateKey_RankMinPosts(rankMinPosts);
153             //If so, then we have to throw an exception
154
throw new DuplicateKeyException("Alternate key already exists. Cannot create new Rank with the same [RankMinPosts] (" + rankMinPosts + ").");
155         } catch(ObjectNotFoundException e) {
156             //Otherwise we can go ahead
157
}
158
159         Connection connection = null;
160         PreparedStatement statement = null;
161         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
162         sql.append("INSERT INTO " + TABLE_NAME + " (RankMinPosts, RankLevel, RankTitle, RankImage, RankType, RankOption)");
163         sql.append(" VALUES (?, ?, ?, ?, ?, ?)");
164         try {
165             connection = DBUtils.getConnection();
166             statement = connection.prepareStatement(sql.toString());
167
168             statement.setInt(1, rankMinPosts);
169             statement.setInt(2, rankLevel);
170             statement.setString(3, rankTitle);
171             statement.setString(4, rankImage);
172             statement.setInt(5, rankType);
173             statement.setInt(6, rankOption);
174
175             if (statement.executeUpdate() != 1) {
176                 throw new CreateException("Error adding a row into table 'Rank'.");
177             }
178             m_dirty = true;
179         } catch(SQLException sqle) {
180             log.error("Sql Execution Error!", sqle);
181             throw new DatabaseException("Error executing SQL in RankDAOImplJDBC.create.");
182         } finally {
183             DBUtils.closeStatement(statement);
184             DBUtils.closeConnection(connection);
185         }
186     }
187
188     /*
189      * Included columns: RankMinPosts, RankLevel, RankTitle, RankImage, RankType,
190      * RankOption
191      * Excluded columns: RankID
192      */

193     public void update(int rankID, // primary key
194
int rankMinPosts, int rankLevel, String JavaDoc rankTitle,
195                        String JavaDoc rankImage, int rankType, int rankOption)
196         throws ObjectNotFoundException, DatabaseException, DuplicateKeyException {
197
198         RankBean bean = getRank(rankID);
199
200         if ( !rankTitle.equals(bean.getRankTitle()) ) {
201             // Rank tries to change its alternate key <RankTitle>, so we must check if it already exist
202
try {
203                 findByAlternateKey_RankTitle(rankTitle);
204                 throw new DuplicateKeyException("Alternate key [RankTitle] (" + rankTitle + ")already exists. Cannot update Rank.");
205             } catch(ObjectNotFoundException e) {
206                 //Otherwise we can go ahead
207
}
208         }
209
210         if ( (rankMinPosts != bean.getRankMinPosts()) ) {
211             // Rank tries to change its alternate key <RankMinPosts>, so we must check if it already exist
212
try {
213                 findByAlternateKey_RankMinPosts(rankMinPosts);
214                 throw new DuplicateKeyException("Alternate key [RankMinPosts] (" + rankMinPosts + ")already exists. Cannot update Rank.");
215             } catch(ObjectNotFoundException e) {
216                 //Otherwise we can go ahead
217
}
218         }
219
220         Connection connection = null;
221         PreparedStatement statement = null;
222         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
223         sql.append("UPDATE " + TABLE_NAME + " SET RankMinPosts = ?, RankLevel = ?, RankTitle = ?, RankImage = ?, RankType = ?, RankOption = ?");
224         sql.append(" WHERE RankID = ?");
225         try {
226             connection = DBUtils.getConnection();
227             statement = connection.prepareStatement(sql.toString());
228
229             // // column(s) to update
230
statement.setInt(1, rankMinPosts);
231             statement.setInt(2, rankLevel);
232             statement.setString(3, rankTitle);
233             statement.setString(4, rankImage);
234             statement.setInt(5, rankType);
235             statement.setInt(6, rankOption);
236
237             // primary key column(s)
238
statement.setInt(7, rankID);
239
240             if (statement.executeUpdate() != 1) {
241                 throw new ObjectNotFoundException("Cannot update table Rank where primary key = (" + rankID + ").");
242             }
243             m_dirty = true;
244         } catch(SQLException sqle) {
245             log.error("Sql Execution Error!", sqle);
246             throw new DatabaseException("Error executing SQL in RankDAOImplJDBC.update.");
247         } finally {
248             DBUtils.closeStatement(statement);
249             DBUtils.closeConnection(connection);
250         }
251     }
252
253     public void delete(int rankID)
254         throws DatabaseException, ObjectNotFoundException {
255
256         Connection connection = null;
257         PreparedStatement statement = null;
258         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
259         sql.append("DELETE FROM " + TABLE_NAME);
260         sql.append(" WHERE RankID = ?");
261
262         try {
263             connection = DBUtils.getConnection();
264             statement = connection.prepareStatement(sql.toString());
265             statement.setInt(1, rankID);
266             if (statement.executeUpdate() != 1) {
267                 throw new ObjectNotFoundException("Cannot delete a row in table Rank where primary key = (" + rankID + ").");
268             }
269             m_dirty = true;
270         } catch(SQLException sqle) {
271             log.error("Sql Execution Error!", sqle);
272             throw new DatabaseException("Error executing SQL in RankDAOImplJDBC.delete.");
273         } finally {
274             DBUtils.closeStatement(statement);
275             DBUtils.closeConnection(connection);
276         }
277     }
278
279     /*
280      * Included columns: RankMinPosts, RankLevel, RankTitle, RankImage, RankType,
281      * RankOption
282      * Excluded columns: RankID
283      */

284     public RankBean getRank(int rankID)
285         throws ObjectNotFoundException, DatabaseException {
286
287         Connection connection = null;
288         PreparedStatement statement = null;
289         ResultSet resultSet = null;
290         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
291         sql.append("SELECT RankMinPosts, RankLevel, RankTitle, RankImage, RankType, RankOption");
292         sql.append(" FROM " + TABLE_NAME);
293         sql.append(" WHERE RankID = ?");
294         try {
295             connection = DBUtils.getConnection();
296             statement = connection.prepareStatement(sql.toString());
297             statement.setInt(1, rankID);
298             resultSet = statement.executeQuery();
299             if(!resultSet.next()) {
300                 throw new ObjectNotFoundException("Cannot find the row in table Rank where primary key = (" + rankID + ").");
301             }
302
303             RankBean bean = new RankBean();
304             // @todo: uncomment the following line(s) as needed
305
bean.setRankID(rankID);
306             bean.setRankMinPosts(resultSet.getInt("RankMinPosts"));
307             bean.setRankLevel(resultSet.getInt("RankLevel"));
308             bean.setRankTitle(resultSet.getString("RankTitle"));
309             bean.setRankImage(resultSet.getString("RankImage"));
310             bean.setRankType(resultSet.getInt("RankType"));
311             bean.setRankOption(resultSet.getInt("RankOption"));
312             return bean;
313         } catch(SQLException sqle) {
314             log.error("Sql Execution Error!", sqle);
315             throw new DatabaseException("Error executing SQL in RankDAOImplJDBC.getRank(pk).");
316         } finally {
317             DBUtils.closeResultSet(resultSet);
318             DBUtils.closeStatement(statement);
319             DBUtils.closeConnection(connection);
320         }
321     }
322
323     /*
324      * Included columns: RankID, RankMinPosts, RankLevel, RankTitle, RankImage,
325      * RankType, RankOption
326      * Excluded columns:
327      */

328     public Collection JavaDoc getRanks()
329         throws DatabaseException {
330
331         Connection connection = null;
332         PreparedStatement statement = null;
333         ResultSet resultSet = null;
334         Collection JavaDoc retValue = new ArrayList JavaDoc();
335         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
336         sql.append("SELECT RankID, RankMinPosts, RankLevel, RankTitle, RankImage, RankType, RankOption");
337         sql.append(" FROM " + TABLE_NAME);
338         //sql.append(" WHERE "); // @todo: uncomment as needed
339
sql.append(" ORDER BY RankMinPosts ASC ");
340         try {
341             connection = DBUtils.getConnection();
342             statement = connection.prepareStatement(sql.toString());
343             resultSet = statement.executeQuery();
344             while (resultSet.next()) {
345                 RankBean bean = new RankBean();
346                 bean.setRankID(resultSet.getInt("RankID"));
347                 bean.setRankMinPosts(resultSet.getInt("RankMinPosts"));
348                 bean.setRankLevel(resultSet.getInt("RankLevel"));
349                 bean.setRankTitle(resultSet.getString("RankTitle"));
350                 bean.setRankImage(resultSet.getString("RankImage"));
351                 bean.setRankType(resultSet.getInt("RankType"));
352                 bean.setRankOption(resultSet.getInt("RankOption"));
353                 retValue.add(bean);
354             }
355             return retValue;
356         } catch(SQLException sqle) {
357             log.error("Sql Execution Error!", sqle);
358             throw new DatabaseException("Error executing SQL in RankDAOImplJDBC.getRanks.");
359         } finally {
360             DBUtils.closeResultSet(resultSet);
361             DBUtils.closeStatement(statement);
362             DBUtils.closeConnection(connection);
363         }
364     }
365
366     public int getRankIDFromRankTitle(String JavaDoc rankTitle)
367         throws ObjectNotFoundException, DatabaseException {
368
369         Connection connection = null;
370         PreparedStatement statement = null;
371         ResultSet resultSet = null;
372         String JavaDoc sql = "SELECT RankID FROM " + TABLE_NAME +
373                      " WHERE RankTitle = ?";
374         try {
375             connection = DBUtils.getConnection();
376             statement = connection.prepareStatement(sql);
377             statement.setString(1, rankTitle);
378             resultSet = statement.executeQuery();
379             if (!resultSet.next()) {
380                 throw new ObjectNotFoundException("Cannot find the alternate key [RankTitle] (" + rankTitle + ") in table 'Rank'.");
381             }
382             return resultSet.getInt(1);
383         } catch (SQLException sqle) {
384             log.error("Sql Execution Error!", sqle);
385             throw new DatabaseException("Error executing SQL in RankDAOImplJDBC.getRankIDFromRankTitle.");
386         } finally {
387             DBUtils.closeResultSet(resultSet);
388             DBUtils.closeStatement(statement);
389             DBUtils.closeConnection(connection);
390         }
391     }
392
393 }// end of class RankDAOImplJDBC
394
Popular Tags