1 package org.mvnforum.phpbb2mvnforum.db.jdbc; 2 3 import java.sql.Connection ; 4 import java.sql.PreparedStatement ; 5 import java.sql.ResultSet ; 6 import java.sql.SQLException ; 7 import java.sql.Timestamp ; 8 import java.util.Collection ; 9 10 import net.myvietnam.mvncore.exception.AssertionException; 11 import net.myvietnam.mvncore.exception.CreateException; 12 import net.myvietnam.mvncore.exception.DatabaseException; 13 import net.myvietnam.mvncore.exception.DuplicateKeyException; 14 import net.myvietnam.mvncore.exception.ForeignKeyNotFoundException; 15 import net.myvietnam.mvncore.exception.ObjectNotFoundException; 16 17 import org.mvnforum.util.DBUtils; 18 19 import com.mvnforum.db.GroupsBean; 21 import com.mvnforum.db.GroupsDAO; 22 23 public class GroupsDAOImplJDBC implements GroupsDAO { 24 25 public void findByPrimaryKey(int groupID) 26 throws ObjectNotFoundException, DatabaseException { 27 28 Connection connection = null; 29 PreparedStatement statement = null; 30 ResultSet resultSet = null; 31 StringBuffer sql = new StringBuffer (512); 32 sql.append("SELECT GroupName"); 33 sql.append(" FROM " + TABLE_NAME); 34 sql.append(" WHERE GroupID = ?"); 35 try { 36 connection = DBUtils.getMvnConnection(); 37 statement = connection.prepareStatement(sql.toString()); 38 statement.setInt(1, groupID); 39 resultSet = statement.executeQuery(); 40 if (!resultSet.next()) { 41 throw new ObjectNotFoundException("Cannot find the primary key (" + groupID + ") in table 'Groups'."); 42 } 43 } catch (SQLException sqle) { 44 throw new DatabaseException("Error executing SQL in GroupsDAOImplJDBC.findByPrimaryKey."); 45 } finally { 46 DBUtils.closeResultSet(resultSet); 47 DBUtils.closeStatement(statement); 48 DBUtils.closeConnection(connection); 49 } 50 } 51 52 public void findByAlternateKey_GroupName(String groupName) 53 throws ObjectNotFoundException, DatabaseException { 54 55 Connection connection = null; 56 PreparedStatement statement = null; 57 ResultSet resultSet = null; 58 StringBuffer sql = new StringBuffer (512); 59 sql.append("SELECT GroupID"); 60 sql.append(" FROM " + TABLE_NAME); 61 sql.append(" WHERE GroupName = ?"); 62 try { 63 connection = DBUtils.getMvnConnection(); 64 statement = connection.prepareStatement(sql.toString()); 65 statement.setString(1, groupName); 66 resultSet = statement.executeQuery(); 67 if (!resultSet.next()) { 68 throw new ObjectNotFoundException("Cannot find the alternate key [GroupName] (" + groupName 69 + ") in table 'Groups'."); 70 } 71 } catch (SQLException sqle) { 72 sqle.printStackTrace(); 73 throw new DatabaseException("Error executing SQL in GroupsDAOImplJDBC.findByAlternateKey_GroupName."); 74 } finally { 75 DBUtils.closeResultSet(resultSet); 76 DBUtils.closeStatement(statement); 77 DBUtils.closeConnection(connection); 78 } 79 } 80 81 public void create(String groupOwnerName, String groupName, String groupDesc, int groupOption, 82 Timestamp groupCreationDate, Timestamp groupModifiedDate) 83 throws CreateException, DatabaseException, DuplicateKeyException, ForeignKeyNotFoundException { 84 85 int groupOwnerID = 0; 86 87 try { 88 findByAlternateKey_GroupName(groupName); 90 throw new DuplicateKeyException( 92 "Alternate key already exists. Cannot create new Groups with the same [GroupName] (" + groupName 93 + ")."); 94 } catch (ObjectNotFoundException e) { 95 } 97 98 try { 99 DAOFactory factory = new DAOFactory (); 102 groupOwnerID = 0; 103 if ((groupOwnerName != null) && (groupOwnerName.length() > 0)) { try { 105 groupOwnerID = factory.getMemberDAO().getMemberFromMemberName(groupOwnerName).getMemberID(); 106 } catch (ObjectNotFoundException ex) { 107 throw new ObjectNotFoundException("ASSERTION: This should never happen."); 109 } 110 } 111 } catch (ObjectNotFoundException e) { 112 throw new ForeignKeyNotFoundException( 113 "Foreign key refers to table 'Member' does not exist. Cannot create new Groups."); 114 } 115 116 Connection connection = null; 117 PreparedStatement statement = null; 118 StringBuffer sql = new StringBuffer (512); 119 sql 120 .append("INSERT INTO " 121 + TABLE_NAME 122 + " (GroupOwnerID, GroupOwnerName, GroupName, GroupDesc, GroupOption, GroupCreationDate, GroupModifiedDate)"); 123 sql.append(" VALUES (?, ?, ?, ?, ?, ?, ?)"); 124 try { 125 connection = DBUtils.getMvnConnection(); 126 statement = connection.prepareStatement(sql.toString()); 127 128 statement.setInt(1, groupOwnerID); 129 statement.setString(2, groupOwnerName); 130 statement.setString(3, groupName); 131 statement.setString(4, groupDesc); 132 statement.setInt(5, groupOption); 133 statement.setTimestamp(6, groupCreationDate); 134 statement.setTimestamp(7, groupModifiedDate); 135 136 if (statement.executeUpdate() != 1) { 137 throw new CreateException("Error adding a row into table 'Groups'."); 138 } 139 } catch (SQLException sqle) { 140 throw new DatabaseException("Error executing SQL in GroupsDAOImplJDBC.create."); 141 } finally { 142 DBUtils.closeStatement(statement); 143 DBUtils.closeConnection(connection); 144 } 145 } 146 147 public void delete(int groupID) 148 throws DatabaseException, ObjectNotFoundException { 149 151 } 152 153 public void update(int groupID, String groupName, String groupDesc, Timestamp groupModifiedDate) 154 throws ObjectNotFoundException, DatabaseException, DuplicateKeyException { 155 157 } 158 159 public void updateOwner(int groupID, String groupOwnerName, Timestamp groupModifiedDate) 160 throws ObjectNotFoundException, DatabaseException, ForeignKeyNotFoundException { 161 163 } 164 165 public GroupsBean getGroup(int groupID) 166 throws ObjectNotFoundException, DatabaseException { 167 return null; 169 } 170 171 public Collection getMyGroups(int memberID) 172 throws DatabaseException { 173 return null; 175 } 176 177 public Collection getGroups() 178 throws DatabaseException { 179 return null; 181 } 182 183 public int getNumberOfGroups() 184 throws AssertionException, DatabaseException { 185 return 0; 187 } 188 189 public int getGroupIDFromGroupName(String groupName) 190 throws ObjectNotFoundException, DatabaseException { 191 return 0; 193 } 194 195 } 196 | Popular Tags |