1 37 38 package com.sun.j2ee.blueprints.customer.dao; 39 40 import java.sql.Connection ; 41 import java.sql.PreparedStatement ; 42 import java.sql.ResultSet ; 43 import java.sql.SQLException ; 44 import javax.sql.DataSource ; 45 import javax.naming.InitialContext ; 46 import javax.naming.Context ; 47 import javax.naming.NamingException ; 48 import java.util.ArrayList ; 49 import java.util.Iterator ; 50 51 52 import com.sun.j2ee.blueprints.customer.ContactInformation; 53 import com.sun.j2ee.blueprints.customer.Address; 54 import com.sun.j2ee.blueprints.customer.Account; 55 import com.sun.j2ee.blueprints.util.tracer.Debug; 56 import com.sun.j2ee.blueprints.util.dao.DAOUtils; 57 import com.sun.j2ee.blueprints.util.dao.DAOSystemException; 58 59 65 public class PointbaseAccountDAO implements AccountDAO { 66 67 private final static String INSERT_ACCOUNT_QUERY_STR = "INSERT INTO " + 68 DatabaseNames.ACCOUNT_TABLE 69 + "(userid,email,firstname,lastname," 70 + "addr1,addr2,city,state,zip,country," 71 + "phone)" + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; 72 73 private final static String SELECT_USER_ID_QUERY_STR = 74 "SELECT userid FROM " + DatabaseNames.ACCOUNT_TABLE + 75 " WHERE userid = ?"; 76 77 private final static String SELECT_ACCOUNT_QUERY_STR = "SELECT "+ 78 "userid,email,firstname,lastname,"+ 79 "addr1,addr2,city,state,zip,country,phone"+ 80 " FROM " + DatabaseNames.ACCOUNT_TABLE + " WHERE userid = ?"; 81 82 83 public PointbaseAccountDAO() { 84 } 85 86 public void create(Account details) throws DAOSystemException, 87 AccountDAODupKeyException, 88 AccountDAODBUpdateException, 89 AccountDAOException { 90 insertAccount(details); 91 } 92 93 public Account getAccount(String userId) throws AccountDAOFinderException, 94 DAOSystemException { 95 return(selectAccount(userId)); 96 } 97 98 99 private void insertAccount(Account details) throws 100 DAOSystemException, 101 AccountDAODupKeyException, 102 AccountDAODBUpdateException, 103 AccountDAOException { 104 105 if (!isValidData(details.getUserId(), details.getContactInformation())) 106 throw new AccountDAOException("Illegal data values for insert"); 107 if (userExists(details.getUserId())) 108 throw new AccountDAODupKeyException("Account exists for "+ 109 details.getUserId()); 110 111 PreparedStatement stmt = null; 112 ContactInformation info = details.getContactInformation(); 113 114 Connection dbConnection = null; 115 try { 116 dbConnection = DAOUtils.getDBConnection(JNDINames.CUSTOMER_DATASOURCE); 117 stmt = dbConnection.prepareStatement(INSERT_ACCOUNT_QUERY_STR); 118 119 stmt.setString(1, details.getUserId().trim() ); 120 stmt.setString(2, info.getEMail().trim() ); 121 stmt.setString(3, info.getGivenName().trim() ); 122 stmt.setString(4, info.getFamilyName().trim() ); 123 stmt.setString(5, info.getAddress().getStreetName1().trim() ); 124 125 if (info.getAddress().getStreetName2() != null) 126 stmt.setString(6, info.getAddress().getStreetName2().trim() ); 127 else 128 stmt.setString(6, " "); 129 130 stmt.setString(7, info.getAddress().getCity().trim() ); 131 stmt.setString(8, info.getAddress().getState().trim() ); 132 stmt.setString(9, info.getAddress().getZipCode().trim() ); 133 stmt.setString(10,info.getAddress().getCountry().trim() ); 134 stmt.setString(11, info.getTelephone().trim() ); 135 136 int resultCount = stmt.executeUpdate(); 137 138 if ( resultCount != 1 ) { 139 throw new AccountDAODBUpdateException( 140 "ERROR in ACCOUNT_TABLE INSERT !! resultCount = " + 141 resultCount); 142 } 143 } catch(SQLException se) { 144 throw new DAOSystemException("SQLException while inserting new " + 145 "account; id = " + details.getUserId() + "\n", se); 146 } finally { 147 DAOUtils.closeStatement(stmt); 148 DAOUtils.closeConnection(dbConnection); 149 } 150 } 151 152 private boolean isValidData(String userId, ContactInformation info) { 153 if ( (userId == null) || 154 ( info.getEMail() == null) || 155 (info.getGivenName() == null) || (info.getFamilyName() == null) 156 || (info.getAddress().getStreetName1() == null) || 157 (info.getAddress().getCity() == null) || 158 (info.getAddress().getState() == null) || 159 (info.getAddress().getZipCode() == null) || 160 (info.getAddress().getCountry() == null) 161 || (info.getTelephone() == null) ) 162 return (false); 163 else 164 return (true); 165 } 166 167 private boolean userExists (String userId) throws DAOSystemException { 168 PreparedStatement stmt = null; 169 ResultSet result = null; 170 boolean returnValue = false; 171 172 Connection dbConnection = null; 173 try { 174 dbConnection = DAOUtils.getDBConnection(JNDINames.CUSTOMER_DATASOURCE); 175 stmt = dbConnection.prepareStatement(SELECT_USER_ID_QUERY_STR); 176 stmt.setString(1, userId.trim()); 177 result = stmt.executeQuery(); 178 if ( !result.next() ) { 179 returnValue = false; 180 } else { 181 userId = result.getString(1); 182 returnValue = true; 183 } 184 } catch(SQLException se) { 185 throw new DAOSystemException("SQLException while checking for an" 186 + " existing user - id -> " + userId + "\n", se); 187 } finally { 188 DAOUtils.closeResultSet(result); 189 DAOUtils.closeStatement(stmt); 190 DAOUtils.closeConnection(dbConnection); 191 } 192 return returnValue; 193 } 194 195 196 private Account selectAccount(String userId) throws 197 DAOSystemException, AccountDAOFinderException { 198 199 PreparedStatement stmt = null; 200 ResultSet result = null; 201 202 Connection dbConnection = null; 203 try { 204 dbConnection = DAOUtils.getDBConnection(JNDINames.CUSTOMER_DATASOURCE); 205 stmt = dbConnection.prepareStatement(SELECT_ACCOUNT_QUERY_STR); 206 stmt.setString(1, userId.trim()); 207 result = stmt.executeQuery(); 208 209 if ( !result.next() ) 210 throw new AccountDAOFinderException( 211 "No record for primary key " + userId); 212 213 int i = 1; 214 userId = result.getString(i++); 215 String email = result.getString(i++); 216 String firstName = result.getString(i++); 217 String lastName = result.getString(i++); 218 String street1 = result.getString(i++); 219 String street2 = result.getString(i++); 220 String city = result.getString(i++); 221 String state = result.getString(i++); 222 String zip = result.getString(i++); 223 String country = result.getString(i++); 224 String phone = result.getString(i++); 225 226 Address addr = new Address(street1, street2, city, state, zip, 227 country); 228 ContactInformation info = 229 new ContactInformation(lastName, firstName, phone, 230 email, addr); 231 return(new Account(userId, info)); 232 } catch(SQLException se) { 233 throw new DAOSystemException("SQLException while getting " + 234 "account; id = " + userId + " :\n", se); 235 } finally { 236 DAOUtils.closeResultSet(result); 237 DAOUtils.closeStatement(stmt); 238 DAOUtils.closeConnection(dbConnection); 239 } 240 } 241 } 242 | Popular Tags |