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 org.apache.commons.logging.Log; 48 import org.apache.commons.logging.LogFactory; 49 import com.mvnforum.db.*; 50 import net.myvietnam.mvncore.db.DBUtils; 51 import net.myvietnam.mvncore.exception.*; 52 53 public class GroupPermissionDAOImplJDBC implements GroupPermissionDAO { 54 55 private static Log log = LogFactory.getLog(GroupPermissionDAOImplJDBC.class); 56 57 private static boolean m_dirty = true; 59 60 public GroupPermissionDAOImplJDBC() { 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 groupID, 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 GroupID, Permission"); 79 sql.append(" FROM " + TABLE_NAME); 80 sql.append(" WHERE GroupID = ? AND Permission = ?"); 81 try { 82 connection = DBUtils.getConnection(); 83 statement = connection.prepareStatement(sql.toString()); 84 statement.setInt(1, groupID); 85 statement.setInt(2, permission); 86 resultSet = statement.executeQuery(); 87 if (!resultSet.next()) { 88 throw new ObjectNotFoundException("Cannot find the primary key (" + groupID + ", " + permission + ") in table 'GroupPermission'."); 89 } 90 } catch(SQLException sqle) { 91 log.error("Sql Execution Error!", sqle); 92 throw new DatabaseException("Error executing SQL in GroupPermissionDAOImplJDBC.findByPrimaryKey."); 93 } finally { 94 DBUtils.closeResultSet(resultSet); 95 DBUtils.closeStatement(statement); 96 DBUtils.closeConnection(connection); 97 } 98 } 99 100 104 public void create(int groupID, int permission) 105 throws CreateException, DatabaseException, DuplicateKeyException, ForeignKeyNotFoundException { 106 107 try { 111 findByPrimaryKey(groupID, permission); 113 throw new DuplicateKeyException("Primary key already exists. Cannot create new GroupPermission with the same [GroupID, Permission] (" + groupID + ", " + permission + ")."); 115 } catch(ObjectNotFoundException e) { 116 } 118 119 try { 120 DAOFactory.getGroupsDAO().findByPrimaryKey(groupID); 123 } catch(ObjectNotFoundException e) { 124 throw new ForeignKeyNotFoundException("Foreign key refers to table 'Groups' does not exist. Cannot create new GroupPermission."); 125 } 126 127 Connection connection = null; 128 PreparedStatement statement = null; 129 StringBuffer sql = new StringBuffer (512); 130 sql.append("INSERT INTO " + TABLE_NAME + " (GroupID, Permission)"); 131 sql.append(" VALUES (?, ?)"); 132 try { 133 connection = DBUtils.getConnection(); 134 statement = connection.prepareStatement(sql.toString()); 135 136 statement.setInt(1, groupID); 137 statement.setInt(2, permission); 138 139 if (statement.executeUpdate() != 1) { 140 throw new CreateException("Error adding a row into table 'GroupPermission'."); 141 } 142 m_dirty = true; 143 } catch(SQLException sqle) { 144 log.error("Sql Execution Error!", sqle); 145 throw new DatabaseException("Error executing SQL in GroupPermissionDAOImplJDBC.create."); 146 } finally { 147 DBUtils.closeStatement(statement); 148 DBUtils.closeConnection(connection); 149 } 150 } 151 152 public void delete(int groupID, int permission) 153 throws DatabaseException, ObjectNotFoundException { 154 155 Connection connection = null; 156 PreparedStatement statement = null; 157 StringBuffer sql = new StringBuffer (512); 158 sql.append("DELETE FROM " + TABLE_NAME); 159 sql.append(" WHERE GroupID = ? AND Permission = ?"); 160 161 try { 162 connection = DBUtils.getConnection(); 163 statement = connection.prepareStatement(sql.toString()); 164 statement.setInt(1, groupID); 165 statement.setInt(2, permission); 166 if (statement.executeUpdate() != 1) { 167 throw new ObjectNotFoundException("Cannot delete a row in table GroupPermission where primary key = (" + groupID + ", " + permission + ")."); 168 } 169 m_dirty = true; 170 } catch(SQLException sqle) { 171 log.error("Sql Execution Error!", sqle); 172 throw new DatabaseException("Error executing SQL in GroupPermissionDAOImplJDBC.delete."); 173 } finally { 174 DBUtils.closeStatement(statement); 175 DBUtils.closeConnection(connection); 176 } 177 } 178 179 public void delete_inGroup(int groupID) 180 throws DatabaseException { 181 182 Connection connection = null; 183 PreparedStatement statement = null; 184 StringBuffer sql = new StringBuffer (512); 185 sql.append("DELETE FROM " + TABLE_NAME); 186 sql.append(" WHERE GroupID = ? "); 187 188 try { 189 connection = DBUtils.getConnection(); 190 statement = connection.prepareStatement(sql.toString()); 191 statement.setInt(1, groupID); 192 193 statement.executeUpdate(); 194 m_dirty = true; 195 } catch(SQLException sqle) { 196 log.error("Sql Execution Error!", sqle); 197 throw new DatabaseException("Error executing SQL in GroupPermissionDAOImplJDBC.delete_inGroup."); 198 } finally { 199 DBUtils.closeStatement(statement); 200 DBUtils.closeConnection(connection); 201 } 202 } 203 204 205 208 209 210 214 public Collection getBeans_inGroup(int groupID) 215 throws DatabaseException { 216 217 Connection connection = null; 218 PreparedStatement statement = null; 219 ResultSet resultSet = null; 220 Collection retValue = new ArrayList (); 221 StringBuffer sql = new StringBuffer (512); 222 sql.append("SELECT Permission"); 223 sql.append(" FROM " + TABLE_NAME); 224 sql.append(" WHERE GroupID = ?"); try { 227 connection = DBUtils.getConnection(); 228 statement = connection.prepareStatement(sql.toString()); 229 statement.setInt(1, groupID); 230 231 resultSet = statement.executeQuery(); 232 while (resultSet.next()) { 233 GroupPermissionBean bean = new GroupPermissionBean(); 234 bean.setGroupID(groupID); 235 bean.setPermission(resultSet.getInt("Permission")); 236 retValue.add(bean); 237 } 238 return retValue; 239 } catch(SQLException sqle) { 240 log.error("Sql Execution Error!", sqle); 241 throw new DatabaseException("Error executing SQL in GroupPermissionDAOImplJDBC.getBeans_inGroup."); 242 } finally { 243 DBUtils.closeResultSet(resultSet); 244 DBUtils.closeStatement(statement); 245 DBUtils.closeConnection(connection); 246 } 247 } 248 249 253 public Collection getDistinctGroups() 254 throws DatabaseException { 255 256 Connection connection = null; 257 PreparedStatement statement = null; 258 ResultSet resultSet = null; 259 Collection retValue = new ArrayList (); 260 StringBuffer sql = new StringBuffer (512); 261 sql.append("SELECT DISTINCT GroupID"); 262 sql.append(" FROM " + TABLE_NAME); 263 sql.append(" ORDER BY GroupID ASC "); 264 try { 265 connection = DBUtils.getConnection(); 266 statement = connection.prepareStatement(sql.toString()); 267 resultSet = statement.executeQuery(); 268 while (resultSet.next()) { 269 GroupPermissionBean bean = new GroupPermissionBean(); 270 bean.setGroupID(resultSet.getInt("GroupID")); 271 retValue.add(bean); 272 } 273 return retValue; 274 } catch(SQLException sqle) { 275 log.error("Sql Execution Error!", sqle); 276 throw new DatabaseException("Error executing SQL in GroupPermissionDAOImplJDBC.getDistinctGroups."); 277 } finally { 278 DBUtils.closeResultSet(resultSet); 279 DBUtils.closeStatement(statement); 280 DBUtils.closeConnection(connection); 281 } 282 } 283 284 } | Popular Tags |