1 41 package com.mvnforum.db.jdbc; 42 43 import java.sql.*; 44 import java.util.ArrayList ; 45 import java.util.Collection ; 46 47 import com.mvnforum.db.*; 48 import net.myvietnam.mvncore.db.DBUtils; 49 import net.myvietnam.mvncore.exception.*; 50 import org.apache.commons.logging.Log; 51 import org.apache.commons.logging.LogFactory; 52 53 public class MemberPermissionDAOImplJDBC implements MemberPermissionDAO { 54 55 private static Log log = LogFactory.getLog(MemberPermissionDAOImplJDBC.class); 56 57 private static boolean m_dirty = true; 59 60 public MemberPermissionDAOImplJDBC() { 61 } 62 63 protected static boolean isDirty() { 64 return m_dirty; 65 } 66 67 protected static void setDirty(boolean dirty) { 68 m_dirty = dirty; 69 } 70 71 public void findByPrimaryKey(int memberID, int permission) 72 throws ObjectNotFoundException, DatabaseException { 73 74 Connection connection = null; 75 PreparedStatement statement = null; 76 ResultSet resultSet = null; 77 StringBuffer sql = new StringBuffer (512); 78 sql.append("SELECT MemberID, Permission"); 79 sql.append(" FROM " + TABLE_NAME); 80 sql.append(" WHERE MemberID = ? AND Permission = ?"); 81 try { 82 connection = DBUtils.getConnection(); 83 statement = connection.prepareStatement(sql.toString()); 84 statement.setInt(1, memberID); 85 statement.setInt(2, permission); 86 resultSet = statement.executeQuery(); 87 if (!resultSet.next()) { 88 throw new ObjectNotFoundException("Cannot find the primary key (" + memberID + ", " + permission + ") in table 'MemberPermission'."); 89 } 90 } catch (SQLException sqle) { 91 log.error("Sql Execution Error!", sqle); 92 throw new DatabaseException("Error executing SQL in MemberPermissionDAOImplJDBC.findByPrimaryKey."); 93 } finally { 94 DBUtils.closeResultSet(resultSet); 95 DBUtils.closeStatement(statement); 96 DBUtils.closeConnection(connection); 97 } 98 } 99 100 104 public void create(int memberID, int permission) 105 throws CreateException, DatabaseException, DuplicateKeyException, ForeignKeyNotFoundException { 106 107 try { 111 findByPrimaryKey(memberID, permission); 113 throw new DuplicateKeyException("Primary key already exists. Cannot create new MemberPermission with the same [MemberID, Permission] (" + memberID + ", " + permission + ")."); 115 } catch (ObjectNotFoundException e) { 116 } 118 119 if (memberID != 0) { 120 try { 121 DAOFactory.getMemberDAO().findByPrimaryKey(memberID); 124 } catch (ObjectNotFoundException e) { 125 throw new ForeignKeyNotFoundException("Foreign key refers to table 'mvnforumMember' does not exist. Cannot create new MemberPermission."); 126 } 127 } 128 129 Connection connection = null; 130 PreparedStatement statement = null; 131 StringBuffer sql = new StringBuffer (512); 132 sql.append("INSERT INTO " + TABLE_NAME + " (MemberID, Permission)"); 133 sql.append(" VALUES (?, ?)"); 134 try { 135 connection = DBUtils.getConnection(); 136 statement = connection.prepareStatement(sql.toString()); 137 138 statement.setInt(1, memberID); 139 statement.setInt(2, permission); 140 141 if (statement.executeUpdate() != 1) { 142 throw new CreateException("Error adding a row into table 'MemberPermission'."); 143 } 144 m_dirty = true; 145 } catch (SQLException sqle) { 146 log.error("Sql Execution Error!", sqle); 147 throw new DatabaseException("Error executing SQL in MemberPermissionDAOImplJDBC.create."); 148 } finally { 149 DBUtils.closeStatement(statement); 150 DBUtils.closeConnection(connection); 151 } 152 } 153 154 public void delete(int memberID, int permission) 155 throws DatabaseException, ObjectNotFoundException { 156 157 Connection connection = null; 158 PreparedStatement statement = null; 159 StringBuffer sql = new StringBuffer (512); 160 sql.append("DELETE FROM " + TABLE_NAME); 161 sql.append(" WHERE MemberID = ? AND Permission = ?"); 162 163 try { 164 connection = DBUtils.getConnection(); 165 statement = connection.prepareStatement(sql.toString()); 166 statement.setInt(1, memberID); 167 statement.setInt(2, permission); 168 if (statement.executeUpdate() != 1) { 169 throw new ObjectNotFoundException("Cannot delete a row in table MemberPermission where primary key = (" + memberID + ", " + permission + ")."); 170 } 171 m_dirty = true; 172 } catch (SQLException sqle) { 173 log.error("Sql Execution Error!", sqle); 174 throw new DatabaseException("Error executing SQL in MemberPermissionDAOImplJDBC.delete."); 175 } finally { 176 DBUtils.closeStatement(statement); 177 DBUtils.closeConnection(connection); 178 } 179 } 180 181 public void delete_inMember(int memberID) 182 throws DatabaseException { 183 184 Connection connection = null; 185 PreparedStatement statement = null; 186 StringBuffer sql = new StringBuffer (512); 187 sql.append("DELETE FROM " + TABLE_NAME); 188 sql.append(" WHERE MemberID = ?"); 189 190 try { 191 connection = DBUtils.getConnection(); 192 statement = connection.prepareStatement(sql.toString()); 193 statement.setInt(1, memberID); 194 statement.executeUpdate(); 195 m_dirty = true; 196 } catch (SQLException sqle) { 197 log.error("Sql Execution Error!", sqle); 198 throw new DatabaseException("Error executing SQL in MemberPermissionDAOImplJDBC.delete_inMember."); 199 } finally { 200 DBUtils.closeStatement(statement); 201 DBUtils.closeConnection(connection); 202 } 203 } 204 205 209 public Collection getBeans_inMember(int memberID) 210 throws DatabaseException { 211 212 Connection connection = null; 213 PreparedStatement statement = null; 214 ResultSet resultSet = null; 215 Collection retValue = new ArrayList (); 216 StringBuffer sql = new StringBuffer (512); 217 sql.append("SELECT Permission"); 218 sql.append(" FROM " + TABLE_NAME); 219 sql.append(" WHERE MemberID = ?"); 220 try { 221 connection = DBUtils.getConnection(); 222 statement = connection.prepareStatement(sql.toString()); 223 statement.setInt(1, memberID); 224 resultSet = statement.executeQuery(); 225 while (resultSet.next()) { 226 MemberPermissionBean bean = new MemberPermissionBean(); 227 bean.setMemberID(memberID); 228 bean.setPermission(resultSet.getInt("Permission")); 229 retValue.add(bean); 230 } 231 return retValue; 232 } catch (SQLException sqle) { 233 log.error("Sql Execution Error!", sqle); 234 throw new DatabaseException("Error executing SQL in MemberPermissionDAOImplJDBC.getBeans_inMember."); 235 } finally { 236 DBUtils.closeResultSet(resultSet); 237 DBUtils.closeStatement(statement); 238 DBUtils.closeConnection(connection); 239 } 240 } 241 242 } | Popular Tags |