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