001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/contrib/phpbb2mvnforum/src/org/mvnforum/phpbb2mvnforum/db/jdbc/CategoryDAOImplJDBC.java,v 1.6 2007/01/15 10:27:12 dungbtm Exp $
003: * $Author: dungbtm $
004: * $Revision: 1.6 $
005: * $Date: 2007/01/15 10:27:12 $
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.sql.Timestamp;
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.phpbb2mvnforum.db.CategoryDAO;
055: import org.mvnforum.util.DBUtils;
056:
057: public class CategoryDAOImplJDBC implements CategoryDAO {
058:
059: public void findByPrimaryKey(int categoryID)
060: throws ObjectNotFoundException, DatabaseException {
061:
062: Connection connection = null;
063: PreparedStatement statement = null;
064: ResultSet resultSet = null;
065: StringBuffer sql = new StringBuffer(512);
066: sql.append("SELECT CategoryID");
067: sql.append(" FROM " + TABLE_NAME);
068: sql.append(" WHERE CategoryID = ?");
069: try {
070: connection = DBUtils.getMvnConnection();
071: statement = connection.prepareStatement(sql.toString());
072: statement.setInt(1, categoryID);
073: resultSet = statement.executeQuery();
074: if (!resultSet.next()) {
075: throw new ObjectNotFoundException(
076: "Cannot find the primary key (" + categoryID
077: + ") in table 'category'.");
078: }
079: } catch (SQLException sqle) {
080: throw new DatabaseException(
081: "Error executing SQL in categoryDAOImplJDBC.findByPrimaryKey.");
082: } finally {
083: DBUtils.closeResultSet(resultSet);
084: DBUtils.closeStatement(statement);
085: DBUtils.closeConnection(connection);
086: }
087: }
088:
089: public void findByAlternateKey_CategoryName(String categoryName)
090: throws ObjectNotFoundException, DatabaseException {
091:
092: Connection connection = null;
093: PreparedStatement statement = null;
094: ResultSet resultSet = null;
095: StringBuffer sql = new StringBuffer(512);
096: sql.append("SELECT CategoryName");
097: sql.append(" FROM " + TABLE_NAME);
098: sql.append(" WHERE CategoryName = ?");
099: try {
100: connection = DBUtils.getMvnConnection();
101: statement = connection.prepareStatement(sql.toString());
102: statement.setString(1, categoryName);
103: resultSet = statement.executeQuery();
104: if (!resultSet.next()) {
105: throw new ObjectNotFoundException(
106: "Cannot find the alternate key [CategoryName] ("
107: + categoryName
108: + ") in table 'category'.");
109: }
110: } catch (SQLException sqle) {
111: throw new DatabaseException(
112: "Error executing SQL in categoryDAOImplJDBC.findByAlternateKey_CategoryName.");
113: } finally {
114: DBUtils.closeResultSet(resultSet);
115: DBUtils.closeStatement(statement);
116: DBUtils.closeConnection(connection);
117: }
118: }
119:
120: /*
121: * Included columns: CategoryID, ParentCategoryID, CategoryName, CategoryDesc, CategoryCreationDate,
122: * CategoryModifiedDate, CategoryOrder, CategoryOption, CategoryStatus
123: * Excluded columns:
124: */
125: public void create(int categoryID, int parentCategoryID,
126: String categoryName, String categoryDesc,
127: Timestamp categoryCreationDate,
128: Timestamp categoryModifiedDate, int categoryOrder,
129: int categoryOption, int categoryStatus)
130: throws CreateException,
131: DatabaseException/*, DuplicateKeyException*/,
132: DuplicateKeyException {
133:
134: // @todo: comment this try-catch block if the needed columns dont have attribute 'include'
135: // If this is the case, then it is highly recommended that you regenerate this method with the attribute 'include' turned on
136: // However, if primary key is a auto_increament column, then you can safely delete this block
137: try {
138: //Check if primary key already exists
139: findByPrimaryKey(categoryID);
140: //If so, then we have to throw an exception
141: throw new DuplicateKeyException(
142: "Primary key already exists. Cannot create new category with the same [CategoryID] ("
143: + categoryID + ").");
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_CategoryName(categoryName);
153: //If so, then we have to throw an exception
154: throw new DuplicateKeyException(
155: "Alternate key already exists. Cannot create new category with the same [CategoryName] ("
156: + categoryName + ").");
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: + " (CategoryID, ParentCategoryID, CategoryName, CategoryDesc, CategoryCreationDate, CategoryModifiedDate, CategoryOrder, CategoryOption, CategoryStatus)");
168: sql.append(" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
169: try {
170: connection = DBUtils.getMvnConnection();
171: statement = connection.prepareStatement(sql.toString());
172:
173: statement.setInt(1, categoryID);
174: statement.setInt(2, parentCategoryID);
175: statement.setString(3, categoryName);
176: statement.setString(4, categoryDesc);
177: statement.setTimestamp(5, categoryCreationDate);
178: statement.setTimestamp(6, categoryModifiedDate);
179: statement.setInt(7, categoryOrder);
180: statement.setInt(8, categoryOption);
181: statement.setInt(9, categoryStatus);
182:
183: if (statement.executeUpdate() != 1) {
184: throw new CreateException(
185: "Error adding a row into table 'category'.");
186: }
187: } catch (SQLException sqle) {
188: throw new DatabaseException(
189: "Error executing SQL in categoryDAOImplJDBC.create.");
190: } finally {
191: DBUtils.closeStatement(statement);
192: DBUtils.closeConnection(connection);
193: }
194: }
195:
196: public void createMultiple(Collection beans) {
197: // TODO Auto-generated method stub
198:
199: }
200: }
|