001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/contrib/phpbb2mvnforum/src/org/mvnforum/phpbb2mvnforum/db/jdbc/RankDAOImplJDBC.java,v 1.7 2007/01/15 10:27:11 dungbtm Exp $
003: * $Author: dungbtm $
004: * $Revision: 1.7 $
005: * $Date: 2007/01/15 10:27:11 $
006: *
007: * ====================================================================
008: *
009: * Copyright (C) 2002-2007 by MyVietnam.net
010: *
011: * All copyright notices regarding mvnForum MUST remain
012: * intact in the scripts and in the outputted HTML.
013: * The "powered by" text/logo with a link back to
014: * http://www.mvnForum.com and http://www.MyVietnam.net in
015: * the footer of the pages MUST remain visible when the pages
016: * are viewed on the internet or intranet.
017: *
018: * This program is free software; you can redistribute it and/or modify
019: * it under the terms of the GNU General Public License as published by
020: * the Free Software Foundation; either version 2 of the License, or
021: * any later version.
022: *
023: * This program is distributed in the hope that it will be useful,
024: * but WITHOUT ANY WARRANTY; without even the implied warranty of
025: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
026: * GNU General Public License for more details.
027: *
028: * You should have received a copy of the GNU General Public License
029: * along with this program; if not, write to the Free Software
030: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
031: *
032: * Support can be obtained from support forums at:
033: * http://www.mvnForum.com/mvnforum/index
034: *
035: * Correspondence and Marketing Questions can be sent to:
036: * info at MyVietnam net
037: *
038: * @author:
039: */
040: package org.mvnforum.phpbb2mvnforum.db.jdbc;
041:
042: import java.sql.Connection;
043: import java.sql.PreparedStatement;
044: import java.sql.ResultSet;
045: import java.sql.SQLException;
046: import java.util.ArrayList;
047: import java.util.Collection;
048:
049: import net.myvietnam.mvncore.exception.CreateException;
050: import net.myvietnam.mvncore.exception.DatabaseException;
051: import net.myvietnam.mvncore.exception.DuplicateKeyException;
052: import net.myvietnam.mvncore.exception.ObjectNotFoundException;
053:
054: import org.mvnforum.util.DBUtils;
055:
056: import com.mvnforum.db.RankBean;
057: import com.mvnforum.db.RankDAO;
058:
059: public class RankDAOImplJDBC implements RankDAO {
060:
061: public void findByAlternateKey_RankTitle(String rankTitle)
062: throws ObjectNotFoundException, DatabaseException {
063: // TODO Auto-generated method stub
064: Connection connection = null;
065: PreparedStatement statement = null;
066: ResultSet resultSet = null;
067: StringBuffer sql = new StringBuffer(512);
068: sql.append("SELECT RankMinPosts");
069: sql.append(" FROM " + TABLE_NAME);
070: sql.append(" WHERE RankTitle = ?");
071: try {
072: connection = DBUtils.getMvnConnection();
073: statement = connection.prepareStatement(sql.toString());
074: statement.setString(1, rankTitle);
075: resultSet = statement.executeQuery();
076: if (!resultSet.next()) {
077: throw new ObjectNotFoundException(
078: "Cannot find the alternate key [RankTitle] ("
079: + rankTitle + ") in table 'Rank'.");
080: }
081: } catch (SQLException sqle) {
082: sqle.printStackTrace();
083: throw new DatabaseException(
084: "Error executing SQL in RankDAOImplJDBC.findByAlternateKey_RankTitle.");
085: } finally {
086: DBUtils.closeResultSet(resultSet);
087: DBUtils.closeStatement(statement);
088: DBUtils.closeConnection(connection);
089: }
090:
091: }
092:
093: public void findByAlternateKey_RankMinPosts(int rankMinPosts)
094: throws ObjectNotFoundException, DatabaseException {
095:
096: // -1 is meaningless
097: // TODO: check it again
098: if (rankMinPosts == -1) {
099: throw new IllegalArgumentException(
100: "Does not allow rankMinPosts < 0");
101: }
102:
103: Connection connection = null;
104: PreparedStatement statement = null;
105: ResultSet resultSet = null;
106: StringBuffer sql = new StringBuffer(512);
107: sql.append("SELECT RankTitle");
108: sql.append(" FROM " + TABLE_NAME);
109: sql.append(" WHERE RankMinPosts = ?");
110: try {
111: connection = DBUtils.getMvnConnection();
112: statement = connection.prepareStatement(sql.toString());
113: statement.setInt(1, rankMinPosts);
114: resultSet = statement.executeQuery();
115: if (!resultSet.next()) {
116: throw new ObjectNotFoundException(
117: "Cannot find the alternate key [RankMinPosts] ("
118: + rankMinPosts + ") in table 'Rank'.");
119: }
120: } catch (SQLException sqle) {
121: throw new DatabaseException(
122: "Error executing SQL in RankDAOImplJDBC.findByAlternateKey_RankMinPosts.");
123: } finally {
124: DBUtils.closeResultSet(resultSet);
125: DBUtils.closeStatement(statement);
126: DBUtils.closeConnection(connection);
127: }
128: }
129:
130: public void create(int rankMinPosts, int rankLevel,
131: String rankTitle, String rankImage, int rankType,
132: int rankOption) throws CreateException, DatabaseException,
133: DuplicateKeyException {
134:
135: // @todo: Comment this try-catch block if the needed columns dont have attribute 'include'
136: // If this is the case, then it is highly recommended that you regenerate this method with the attribute 'include' turned on
137: try {
138: //Check if alternate key already exists
139: findByAlternateKey_RankTitle(rankTitle);
140: //If so, then we have to throw an exception
141: throw new DuplicateKeyException(
142: "Alternate key already exists. Cannot create new Rank with the same [RankTitle] ("
143: + 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(
155: "Alternate key already exists. Cannot create new Rank with the same [RankMinPosts] ("
156: + rankMinPosts + ").");
157: } catch (ObjectNotFoundException e) {
158: //Otherwise we can go ahead
159: }
160:
161: Connection connection = null;
162: PreparedStatement statement = null;
163: StringBuffer sql = new StringBuffer(512);
164: sql
165: .append("INSERT INTO "
166: + TABLE_NAME
167: + " (RankMinPosts, RankLevel, RankTitle, RankImage, RankType, RankOption)");
168: sql.append(" VALUES (?, ?, ?, ?, ?, ?)");
169: try {
170: connection = DBUtils.getMvnConnection();
171: statement = connection.prepareStatement(sql.toString());
172:
173: statement.setInt(1, rankMinPosts);
174: statement.setInt(2, rankLevel);
175: statement.setString(3, rankTitle);
176: statement.setString(4, rankImage);
177: statement.setInt(5, rankType);
178: statement.setInt(6, rankOption);
179:
180: if (statement.executeUpdate() != 1) {
181: throw new CreateException(
182: "Error adding a row into table 'Rank'.");
183: }
184: } catch (SQLException sqle) {
185: throw new DatabaseException(
186: "Error executing SQL in RankDAOImplJDBC.create.");
187: } finally {
188: DBUtils.closeStatement(statement);
189: DBUtils.closeConnection(connection);
190: }
191: }
192:
193: public void update(int rankID, int rankMinPosts, int rankLevel,
194: String rankTitle, String rankImage, int rankType,
195: int rankOption) throws ObjectNotFoundException,
196: DatabaseException, DuplicateKeyException {
197: // TODO Auto-generated method stub
198:
199: }
200:
201: public void delete(int rankID) throws DatabaseException,
202: ObjectNotFoundException {
203: // TODO Auto-generated method stub
204:
205: }
206:
207: public RankBean getRank(int rankID) throws ObjectNotFoundException,
208: DatabaseException {
209: return null;
210: }
211:
212: public Collection getRanks() throws DatabaseException {
213: Connection connection = null;
214: PreparedStatement statement = null;
215: ResultSet resultSet = null;
216: Collection retValue = new ArrayList();
217: StringBuffer sql = new StringBuffer(512);
218: sql
219: .append("SELECT RankID, RankMinPosts, RankLevel, RankTitle, RankImage, RankType, RankOption");
220: sql.append(" FROM " + TABLE_NAME);
221: sql.append(" ORDER BY RankMinPosts ASC ");
222: try {
223: connection = DBUtils.getMvnConnection();
224: statement = connection.prepareStatement(sql.toString());
225: resultSet = statement.executeQuery();
226: while (resultSet.next()) {
227: RankBean bean = new RankBean();
228: bean.setRankID(resultSet.getInt("RankID"));
229: bean.setRankMinPosts(resultSet.getInt("RankMinPosts"));
230: bean.setRankLevel(resultSet.getInt("RankLevel"));
231: bean.setRankTitle(resultSet.getString("RankTitle"));
232: bean.setRankImage(resultSet.getString("RankImage"));
233: bean.setRankType(resultSet.getInt("RankType"));
234: bean.setRankOption(resultSet.getInt("RankOption"));
235: retValue.add(bean);
236: }
237: return retValue;
238: } catch (SQLException sqle) {
239: throw new DatabaseException(
240: "Error executing SQL in RankDAOImplJDBC.getRanks.");
241: } finally {
242: DBUtils.closeResultSet(resultSet);
243: DBUtils.closeStatement(statement);
244: DBUtils.closeConnection(connection);
245: }
246:
247: }
248:
249: public int getRankIDFromRankTitle(String rankTitle)
250: throws ObjectNotFoundException, DatabaseException {
251: // TODO Auto-generated method stub
252: return 0;
253: }
254:
255: }
|