001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/db/jdbc/GroupPermissionDAOImplJDBC.java,v 1.10 2007/10/09 11:09:20 lexuanttkhtn Exp $
003: * $Author: lexuanttkhtn $
004: * $Revision: 1.10 $
005: * $Date: 2007/10/09 11:09:20 $
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: Minh Nguyen
039: * @author: Mai Nguyen
040: */
041: package com.mvnforum.db.jdbc;
042:
043: import java.sql.*;
044: import java.util.ArrayList;
045: import java.util.Collection;
046:
047: import org.apache.commons.logging.Log;
048: import org.apache.commons.logging.LogFactory;
049: import com.mvnforum.db.*;
050: import net.myvietnam.mvncore.db.DBUtils;
051: import net.myvietnam.mvncore.exception.*;
052:
053: public class GroupPermissionDAOImplJDBC implements GroupPermissionDAO {
054:
055: private static Log log = LogFactory
056: .getLog(GroupPermissionDAOImplJDBC.class);
057:
058: // this variable will support caching if cache for this class is needed
059: private static boolean m_dirty = true;
060:
061: public GroupPermissionDAOImplJDBC() {
062: }
063:
064: protected static boolean isDirty() {
065: return m_dirty;
066: }
067:
068: protected static void setDirty(boolean dirty) {
069: m_dirty = dirty;
070: }
071:
072: public void findByPrimaryKey(int groupID, int permission)
073: throws ObjectNotFoundException, DatabaseException {
074:
075: Connection connection = null;
076: PreparedStatement statement = null;
077: ResultSet resultSet = null;
078: StringBuffer sql = new StringBuffer(512);
079: sql.append("SELECT GroupID, Permission");
080: sql.append(" FROM " + TABLE_NAME);
081: sql.append(" WHERE GroupID = ? AND Permission = ?");
082: try {
083: connection = DBUtils.getConnection();
084: statement = connection.prepareStatement(sql.toString());
085: statement.setInt(1, groupID);
086: statement.setInt(2, permission);
087: resultSet = statement.executeQuery();
088: if (!resultSet.next()) {
089: throw new ObjectNotFoundException(
090: "Cannot find the primary key (" + groupID
091: + ", " + permission
092: + ") in table 'GroupPermission'.");
093: }
094: } catch (SQLException sqle) {
095: log.error("Sql Execution Error!", sqle);
096: throw new DatabaseException(
097: "Error executing SQL in GroupPermissionDAOImplJDBC.findByPrimaryKey.");
098: } finally {
099: DBUtils.closeResultSet(resultSet);
100: DBUtils.closeStatement(statement);
101: DBUtils.closeConnection(connection);
102: }
103: }
104:
105: /*
106: * Included columns: GroupID, Permission
107: * Excluded columns:
108: */
109: public void create(int groupID, int permission)
110: throws CreateException, DatabaseException,
111: DuplicateKeyException, ForeignKeyNotFoundException {
112:
113: // @todo: comment this try-catch block if the needed columns dont have attribute 'include'
114: // If this is the case, then it is highly recommended that you regenerate this method with the attribute 'include' turned on
115: // However, if primary key is a auto_increament column, then you can safely delete this block
116: try {
117: //Check if primary key already exists
118: findByPrimaryKey(groupID, permission);
119: //If so, then we have to throw an exception
120: throw new DuplicateKeyException(
121: "Primary key already exists. Cannot create new GroupPermission with the same [GroupID, Permission] ("
122: + groupID + ", " + permission + ").");
123: } catch (ObjectNotFoundException e) {
124: //Otherwise we can go ahead
125: }
126:
127: try {
128: // @todo: modify the parameter list as needed
129: // You may have to regenerate this method if the needed columns dont have attribute 'include'
130: DAOFactory.getGroupsDAO().findByPrimaryKey(groupID);
131: } catch (ObjectNotFoundException e) {
132: throw new ForeignKeyNotFoundException(
133: "Foreign key refers to table 'Groups' does not exist. Cannot create new GroupPermission.");
134: }
135:
136: Connection connection = null;
137: PreparedStatement statement = null;
138: StringBuffer sql = new StringBuffer(512);
139: sql.append("INSERT INTO " + TABLE_NAME
140: + " (GroupID, Permission)");
141: sql.append(" VALUES (?, ?)");
142: try {
143: connection = DBUtils.getConnection();
144: statement = connection.prepareStatement(sql.toString());
145:
146: statement.setInt(1, groupID);
147: statement.setInt(2, permission);
148:
149: if (statement.executeUpdate() != 1) {
150: throw new CreateException(
151: "Error adding a row into table 'GroupPermission'.");
152: }
153: m_dirty = true;
154: } catch (SQLException sqle) {
155: log.error("Sql Execution Error!", sqle);
156: throw new DatabaseException(
157: "Error executing SQL in GroupPermissionDAOImplJDBC.create.");
158: } finally {
159: DBUtils.closeStatement(statement);
160: DBUtils.closeConnection(connection);
161: }
162: }
163:
164: public void delete(int groupID, int permission)
165: throws DatabaseException, ObjectNotFoundException {
166:
167: Connection connection = null;
168: PreparedStatement statement = null;
169: StringBuffer sql = new StringBuffer(512);
170: sql.append("DELETE FROM " + TABLE_NAME);
171: sql.append(" WHERE GroupID = ? AND Permission = ?");
172:
173: try {
174: connection = DBUtils.getConnection();
175: statement = connection.prepareStatement(sql.toString());
176: statement.setInt(1, groupID);
177: statement.setInt(2, permission);
178: if (statement.executeUpdate() != 1) {
179: throw new ObjectNotFoundException(
180: "Cannot delete a row in table GroupPermission where primary key = ("
181: + groupID + ", " + permission + ").");
182: }
183: m_dirty = true;
184: } catch (SQLException sqle) {
185: log.error("Sql Execution Error!", sqle);
186: throw new DatabaseException(
187: "Error executing SQL in GroupPermissionDAOImplJDBC.delete.");
188: } finally {
189: DBUtils.closeStatement(statement);
190: DBUtils.closeConnection(connection);
191: }
192: }
193:
194: public void delete_inGroup(int groupID) throws DatabaseException {
195:
196: Connection connection = null;
197: PreparedStatement statement = null;
198: StringBuffer sql = new StringBuffer(512);
199: sql.append("DELETE FROM " + TABLE_NAME);
200: sql.append(" WHERE GroupID = ? ");
201:
202: try {
203: connection = DBUtils.getConnection();
204: statement = connection.prepareStatement(sql.toString());
205: statement.setInt(1, groupID);
206:
207: statement.executeUpdate();
208: m_dirty = true;
209: } catch (SQLException sqle) {
210: log.error("Sql Execution Error!", sqle);
211: throw new DatabaseException(
212: "Error executing SQL in GroupPermissionDAOImplJDBC.delete_inGroup.");
213: } finally {
214: DBUtils.closeStatement(statement);
215: DBUtils.closeConnection(connection);
216: }
217: }
218:
219: /************************************************
220: * Customized methods come below
221: ************************************************/
222:
223: /*
224: * Included columns: Permission
225: * Excluded columns: GroupID
226: */
227: public Collection getBeans_inGroup(int groupID)
228: throws DatabaseException {
229:
230: Connection connection = null;
231: PreparedStatement statement = null;
232: ResultSet resultSet = null;
233: Collection retValue = new ArrayList();
234: StringBuffer sql = new StringBuffer(512);
235: sql.append("SELECT Permission");
236: sql.append(" FROM " + TABLE_NAME);
237: sql.append(" WHERE GroupID = ?"); // @todo: uncomment as needed
238: //sql.append(" ORDER BY ColumnName ASC|DESC "); // @todo: uncomment as needed
239: try {
240: connection = DBUtils.getConnection();
241: statement = connection.prepareStatement(sql.toString());
242: statement.setInt(1, groupID);
243:
244: resultSet = statement.executeQuery();
245: while (resultSet.next()) {
246: GroupPermissionBean bean = new GroupPermissionBean();
247: bean.setGroupID(groupID);
248: bean.setPermission(resultSet.getInt("Permission"));
249: retValue.add(bean);
250: }
251: return retValue;
252: } catch (SQLException sqle) {
253: log.error("Sql Execution Error!", sqle);
254: throw new DatabaseException(
255: "Error executing SQL in GroupPermissionDAOImplJDBC.getBeans_inGroup.");
256: } finally {
257: DBUtils.closeResultSet(resultSet);
258: DBUtils.closeStatement(statement);
259: DBUtils.closeConnection(connection);
260: }
261: }
262:
263: /*
264: * Included columns: GroupID
265: * Excluded columns: Permission
266: */
267: public Collection getDistinctGroups() throws DatabaseException {
268:
269: Connection connection = null;
270: PreparedStatement statement = null;
271: ResultSet resultSet = null;
272: Collection retValue = new ArrayList();
273: StringBuffer sql = new StringBuffer(512);
274: sql.append("SELECT DISTINCT GroupID");
275: sql.append(" FROM " + TABLE_NAME);
276: sql.append(" ORDER BY GroupID ASC ");
277: try {
278: connection = DBUtils.getConnection();
279: statement = connection.prepareStatement(sql.toString());
280: resultSet = statement.executeQuery();
281: while (resultSet.next()) {
282: GroupPermissionBean bean = new GroupPermissionBean();
283: bean.setGroupID(resultSet.getInt("GroupID"));
284: retValue.add(bean);
285: }
286: return retValue;
287: } catch (SQLException sqle) {
288: log.error("Sql Execution Error!", sqle);
289: throw new DatabaseException(
290: "Error executing SQL in GroupPermissionDAOImplJDBC.getDistinctGroups.");
291: } finally {
292: DBUtils.closeResultSet(resultSet);
293: DBUtils.closeStatement(statement);
294: DBUtils.closeConnection(connection);
295: }
296: }
297:
298: }// end of class GroupPermissionDAOImplJDBC
|