001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/contrib/phpbb2mvnforum/src/org/mvnforum/phpbb2mvnforum/db/jdbc/MemberPermissionDAOImplJDBC.java,v 1.4 2007/01/15 10:27:12 dungbtm Exp $
003: * $Author: dungbtm $
004: * $Revision: 1.4 $
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.util.Collection;
047:
048: import org.mvnforum.util.DBUtils;
049:
050: import net.myvietnam.mvncore.exception.CreateException;
051: import net.myvietnam.mvncore.exception.DatabaseException;
052: import net.myvietnam.mvncore.exception.DuplicateKeyException;
053: import net.myvietnam.mvncore.exception.ForeignKeyNotFoundException;
054: import net.myvietnam.mvncore.exception.ObjectNotFoundException;
055:
056: import com.mvnforum.db.MemberPermissionDAO;
057:
058: public class MemberPermissionDAOImplJDBC implements MemberPermissionDAO {
059:
060: public void findByPrimaryKey(int memberID, int permission)
061: throws ObjectNotFoundException, DatabaseException {
062:
063: Connection connection = null;
064: PreparedStatement statement = null;
065: ResultSet resultSet = null;
066: StringBuffer sql = new StringBuffer(512);
067: sql.append("SELECT MemberID, Permission");
068: sql.append(" FROM " + TABLE_NAME);
069: sql.append(" WHERE MemberID = ? AND Permission = ?");
070: try {
071: connection = DBUtils.getMvnConnection();
072: statement = connection.prepareStatement(sql.toString());
073: statement.setInt(1, memberID);
074: statement.setInt(2, permission);
075: resultSet = statement.executeQuery();
076: if (!resultSet.next()) {
077: throw new ObjectNotFoundException(
078: "Cannot find the primary key (" + memberID
079: + ", " + permission
080: + ") in table 'MemberPermission'.");
081: }
082: } catch (SQLException sqle) {
083: throw new DatabaseException(
084: "Error executing SQL in MemberPermissionDAOImplJDBC.findByPrimaryKey.");
085: } finally {
086: DBUtils.closeResultSet(resultSet);
087: DBUtils.closeStatement(statement);
088: DBUtils.closeConnection(connection);
089: }
090: }
091:
092: public void create(int memberID, int permission)
093: throws CreateException, DatabaseException,
094: DuplicateKeyException, ForeignKeyNotFoundException {
095: try {
096: //Check if primary key already exists
097: findByPrimaryKey(memberID, permission);
098: //If so, then we have to throw an exception
099: throw new DuplicateKeyException(
100: "Primary key already exists. Cannot create new MemberPermission with the same [MemberID, Permission] ("
101: + memberID + ", " + permission + ").");
102: } catch (ObjectNotFoundException e) {
103: //Otherwise we can go ahead
104: }
105:
106: if (memberID != 0) {
107: DAOFactory factory = new DAOFactory();
108: try {
109: factory.getMemberDAO().findByPrimaryKey(memberID);
110: } catch (ObjectNotFoundException e) {
111: // TODO Auto-generated catch block
112: e.printStackTrace();
113: } catch (DatabaseException e) {
114: // TODO Auto-generated catch block
115: e.printStackTrace();
116: }
117: }
118:
119: Connection connection = null;
120: PreparedStatement statement = null;
121: StringBuffer sql = new StringBuffer(512);
122: sql.append("INSERT INTO " + TABLE_NAME
123: + " (MemberID, Permission)");
124: sql.append(" VALUES (?, ?)");
125: try {
126: connection = DBUtils.getMvnConnection();
127: statement = connection.prepareStatement(sql.toString());
128:
129: statement.setInt(1, memberID);
130: statement.setInt(2, permission);
131:
132: if (statement.executeUpdate() != 1) {
133: throw new CreateException(
134: "Error adding a row into table 'MemberPermission'.");
135: }
136: } catch (SQLException sqle) {
137: throw new DatabaseException(
138: "Error executing SQL in MemberPermissionDAOImplJDBC.create.");
139: } finally {
140: DBUtils.closeStatement(statement);
141: DBUtils.closeConnection(connection);
142: }
143:
144: }
145:
146: public void delete(int memberID, int permission)
147: throws DatabaseException, ObjectNotFoundException {
148: // TODO Auto-generated method stub
149:
150: }
151:
152: public void delete_inMember(int memberID) throws DatabaseException {
153: // TODO Auto-generated method stub
154:
155: }
156:
157: public Collection getBeans_inMember(int memberID)
158: throws DatabaseException {
159: // TODO Auto-generated method stub
160: return null;
161: }
162:
163: }
|