View Javadoc
1 /* 2 * $Header: /cvsroot/ebxmlrr/ebxmlrr/src/share/com/sun/ebxml/registry/persistence/rdb/PostalAddressDAO.java,v 1.16 2003/05/30 22:42:37 waikei Exp $ 3 */ 4 5 package com.sun.ebxml.registry.persistence.rdb; 6 7 import java.util.*; 8 import java.sql.*; 9 10 import org.oasis.ebxml.registry.bindings.rim.*; 11 import org.oasis.ebxml.registry.bindings.rim.types.*; 12 13 import com.sun.ebxml.registry.*; 14 15 /*** 16 * 17 * @author Farrukh S. Najmi 18 * @version 19 */ 20 public class PostalAddressDAO { 21 protected PostalAddressDAO(){} 22 23 public static String getTableNameStatic() { 24 return "PostalAddress"; 25 } 26 27 public String getTableName() { 28 return getTableNameStatic(); 29 } 30 31 /*** 32 @param registryObjects is a ArrayList of Organizations or Users 33 @throws RegistryException if the RegistryObject is not Organization or User, 34 or it has SQLException when inserting their PostalAddress 35 */ 36 public void insert(java.sql.Connection connection, ArrayList registryObjects) throws RegistryException { 37 38 Statement stmt = null; 39 40 if (registryObjects.size()==0) { 41 return; 42 } 43 44 try { 45 /* 46 String sqlStr = "INSERT INTO " + getTableName() + 47 " VALUES( " + 48 "?, " + // city 49 "?, " + // country 50 "?, " + // postalCode 51 "?, " + // state 52 "?, " + // street 53 "?, " + // streetNum 54 "?)"; // Parent id 55 56 PreparedStatement stmt = connection.prepareStatement(sqlStr); 57 */ 58 59 stmt = connection.createStatement(); 60 61 62 Iterator rosIter = registryObjects.iterator(); 63 while(rosIter.hasNext()) { 64 Object ro = rosIter.next(); 65 String parentId = null; 66 PostalAddressType postalAddress = null; 67 if (ro instanceof Organization) { 68 Organization org = (Organization) ro; 69 postalAddress = org.getAddress(); 70 parentId = org.getId(); 71 } 72 else if (ro instanceof User) { 73 User user = (User) ro; 74 postalAddress = user.getAddress(); 75 parentId = user.getId(); 76 } 77 else { 78 throw new RegistryException("Incorrect RegistryObject"); 79 } 80 /* 81 stmt.setString(1, postalAddress.getCity()); 82 stmt.setString(2, postalAddress.getCountry()); 83 stmt.setString(3, postalAddress.getPostalCode()); 84 stmt.setString(4, postalAddress.getStateOrProvince()); 85 stmt.setString(5, postalAddress.getStreet()); 86 stmt.setString(6, postalAddress.getStreetNumber()); 87 stmt.setString(7, org.getId()); 88 stmt.addBatch();*/ 89 String city = postalAddress.getCity(); 90 if (city != null) { 91 city = "'" + city + "'"; 92 } 93 94 String country = postalAddress.getCountry(); 95 if (country != null) { 96 country = "'" + country + "'"; 97 } 98 99 String postalCode = postalAddress.getPostalCode(); 100 if (postalCode != null) { 101 postalCode ="'" + postalCode + "'"; 102 } 103 104 String state = postalAddress.getStateOrProvince(); 105 if (state != null) { 106 state ="'" + state + "'"; 107 } 108 109 String street = postalAddress.getStreet(); 110 if (street != null) { 111 street ="'" + street + "'"; 112 } 113 114 String streetNum = postalAddress.getStreetNumber(); 115 if (streetNum != null) { 116 streetNum ="'" + streetNum + "'"; 117 } 118 119 stmt.addBatch("INSERT INTO PostalAddress " + 120 "VALUES( " + 121 city + ", " + 122 country + ", " + 123 postalCode + ", " + 124 state + ", " + 125 street + ", " + 126 streetNum + ", " + 127 "'" + parentId + "' )"); 128 129 } // end looping all Organizations 130 131 if (registryObjects.size() > 0) { 132 int [] updateCounts = stmt.executeBatch(); 133 //stmt.close(); 134 } 135 } 136 catch(SQLException e) { 137 RegistryException exception = new RegistryException(e); 138 throw exception; 139 } finally { 140 try { 141 if (stmt != null) 142 stmt.close(); 143 } catch (SQLException sqle) { 144 sqle.printStackTrace(); 145 } 146 } 147 } 148 149 /*** 150 * Does a bulk insert of a Collection of objects that match the type for this persister. 151 * 152 */ 153 public void insert(java.sql.Connection connection, String parentId, ArrayList postalAddresss) throws RegistryException { 154 155 Statement stmt = null; 156 System.err.println("Inserting " + postalAddresss.size() + " PostalAddresss"); 157 158 if (postalAddresss.size()==0) { 159 return; 160 } 161 162 try { 163 stmt = connection.createStatement(); 164 165 Iterator iter = postalAddresss.iterator(); 166 while (iter.hasNext()) { 167 PostalAddressType postalAddress = (PostalAddressType)iter.next(); 168 169 //Log.print(Log.TRACE, 8, "\tDATABASE EVENT: storing PostalAddress " ); 170 171 String city = postalAddress.getCity(); 172 if (city != null) { 173 city = "'" + city + "'"; 174 } 175 176 String country = postalAddress.getCountry(); 177 if (country != null) { 178 country = "'" + country + "'"; 179 } 180 181 String postalCode = postalAddress.getPostalCode(); 182 if (postalCode != null) { 183 postalCode ="'" + postalCode + "'"; 184 } 185 186 String state = postalAddress.getStateOrProvince(); 187 if (state != null) { 188 state ="'" + state + "'"; 189 } 190 191 String street = postalAddress.getStreet(); 192 if (street != null) { 193 street ="'" + street + "'"; 194 } 195 196 String streetNum = postalAddress.getStreetNumber(); 197 if (streetNum != null) { 198 streetNum ="'" + streetNum + "'"; 199 } 200 201 stmt.addBatch("INSERT INTO PostalAddress " + 202 "VALUES( " + 203 city + ", " + 204 country + ", " + 205 postalCode + ", " + 206 state + ", " + 207 street + ", " + 208 streetNum + ", " + 209 "'" + parentId + "' )"); 210 } 211 212 if (postalAddresss.size() > 0) { 213 int [] updateCounts = stmt.executeBatch(); 214 //stmt.close(); 215 } 216 } 217 catch (java.sql.SQLException e) { 218 RegistryException exception = new RegistryException(e); 219 throw exception; 220 } finally { 221 try { 222 if (stmt != null) 223 stmt.close(); 224 } catch (SQLException sqle) { 225 sqle.printStackTrace(); 226 } 227 } 228 229 } 230 231 /*** 232 * Does a bulk update of a Collection of objects that match the type for this persister. 233 * 234 */ 235 public void update(java.sql.Connection connection, String parentId, ArrayList postalAddresss) throws RegistryException { 236 237 Statement stmt = null; 238 System.err.println("Updating " + postalAddresss.size() + " PostalAddresss"); 239 240 try 241 { 242 stmt = connection.createStatement(); 243 244 Iterator iter = postalAddresss.iterator(); 245 while (iter.hasNext()) 246 { 247 PostalAddress postalAddress = (PostalAddress)iter.next(); 248 249 250 String city = postalAddress.getCity(); 251 if (city != null) { 252 city = "'" + city + "'"; 253 } 254 255 String country = postalAddress.getCountry(); 256 if (country != null) { 257 country = "'" + country + "'"; 258 } 259 260 String postalCode = postalAddress.getPostalCode(); 261 if (postalCode != null) { 262 postalCode ="'" + postalCode + "'"; 263 } 264 265 String state = postalAddress.getStateOrProvince(); 266 if (state != null) { 267 state ="'" + state + "'"; 268 } 269 270 String street = postalAddress.getStreet(); 271 if (street != null) { 272 street ="'" + street + "'"; 273 } 274 275 String streetNum = postalAddress.getStreetNumber(); 276 if (streetNum != null) { 277 streetNum ="'" + streetNum + "'"; 278 } 279 280 stmt.addBatch("UPDATE PostalAddress " + 281 "SET city = " + city + ", " + 282 "SET country = " + country + ", " + 283 "SET postalCode = " + postalCode + ", " + 284 "SET state = " + state + ", " + 285 "SET street = " + street + ", " + 286 "SET streetNum = " + streetNum + " " + 287 " WHERE parent = '" + parentId + "' "); 288 } 289 290 int [] updateCounts = stmt.executeBatch(); 291 //stmt.close(); 292 } 293 catch (java.sql.SQLException e) { 294 RegistryException exception = new RegistryException(e); 295 throw exception; 296 } finally { 297 try { 298 if (stmt != null) 299 stmt.close(); 300 } catch (SQLException sqle) { 301 sqle.printStackTrace(); 302 } 303 } 304 } 305 306 /*** 307 * Does a bulk delete of a Collection of objects that match the type for this persister. 308 * 309 */ 310 public void delete(java.sql.Connection connection, ArrayList parentIds) throws RegistryException { 311 312 Statement stmt = null; 313 System.err.println("Deleting " + parentIds.size() + " PostalAddresss"); 314 315 316 try 317 { 318 stmt = connection.createStatement(); 319 320 Iterator iter = parentIds.iterator(); 321 while (iter.hasNext()) { 322 String parentId = (String)iter.next(); 323 324 325 stmt.addBatch("DELETE from PostalAddress WHERE parent = '" + 326 parentId + "' "); 327 } 328 329 int [] updateCounts = stmt.executeBatch(); 330 //stmt.close(); 331 } 332 catch (java.sql.SQLException e) { 333 RegistryException exception = new RegistryException(e); 334 throw exception; 335 } finally { 336 try { 337 if (stmt != null) 338 stmt.close(); 339 } catch (SQLException sqle) { 340 sqle.printStackTrace(); 341 } 342 } 343 } 344 345 public void loadObjectFromResultSet(java.sql.Connection conn, Object obj, ResultSet rs) throws RegistryException { 346 try { 347 if (!(obj instanceof org.oasis.ebxml.registry.bindings.rim.PostalAddressType)) { 348 throw new RegistryException("Unexpected object " + obj + ". Was expecting org.oasis.ebxml.registry.bindings.rim.PostalAddressType."); 349 } 350 351 PostalAddressType addr = (PostalAddressType)obj; 352 353 String city = rs.getString("city"); 354 addr.setCity(city); 355 356 String country = rs.getString("country"); 357 addr.setCountry(country); 358 359 String postalCode = rs.getString("postalCode"); 360 addr.setPostalCode(postalCode); 361 362 String stateOrProvince = rs.getString("state"); 363 addr.setStateOrProvince(stateOrProvince); 364 365 String street = rs.getString("street"); 366 addr.setStreet(street); 367 368 String streetNumber = rs.getString("streetNumber"); 369 addr.setStreetNumber(streetNumber); 370 371 } 372 catch (SQLException e) { 373 e.printStackTrace(); 374 throw new RegistryException(e); 375 } 376 } 377 378 void getPostalAddressByParent(java.sql.Connection conn, PostalAddressType address, String parentId) throws RegistryException { 379 380 Statement stmt = null; 381 try { 382 stmt = conn.createStatement(); 383 ResultSet rs = stmt.executeQuery("SELECT * FROM PostalAddress WHERE parent = '" + parentId + "'"); 384 385 if (rs.next()) { 386 loadObjectFromResultSet(conn, address, rs); 387 } 388 } 389 catch (SQLException e) { 390 e.printStackTrace(); 391 throw new RegistryException(e); 392 } finally { 393 try { 394 if (stmt != null) 395 stmt.close(); 396 } catch (SQLException sqle) { 397 sqle.printStackTrace(); 398 } 399 } 400 401 } 402 }

This page was automatically generated by Maven