View Javadoc
1 /* 2 * $Header: /cvsroot/ebxmlrr/ebxmlrr/src/share/com/sun/ebxml/registry/persistence/rdb/TelephoneNumberDAO.java,v 1.18 2003/05/30 22:42:39 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 * 18 * @author kwalsh 19 * @version 20 */ 21 public class TelephoneNumberDAO { 22 protected TelephoneNumberDAO(){} 23 24 public static String getTableNameStatic() { 25 return "TelephoneNumber"; 26 } 27 28 public String getTableName() { 29 return getTableNameStatic(); 30 } 31 32 /*** 33 @param registryObjects is a ArrayList of Organizations or Users 34 @throws RegistryException if the RegistryObject is not Organization or User, 35 or it has SQLException when inserting their PostalAddress 36 */ 37 public void insert(java.sql.Connection connection, ArrayList registryObjects) throws RegistryException { 38 39 Statement stmt = null; 40 41 if (registryObjects.size()==0) { 42 return; 43 } 44 45 try { 46 stmt = connection.createStatement(); 47 48 /* 49 String sqlStr = "INSERT INTO " + getTableName() + 50 " VALUES( " + 51 "?, " + //areaCode 52 "?, " + //countryCode 53 "?, " + //extension 54 "?, " + //number 55 "?, " + //phoneType 56 "?, " + // url 57 "?)"; // parent Id 58 59 PreparedStatement stmt = connection.prepareStatement(sqlStr); 60 */ 61 62 Iterator rosIter = registryObjects.iterator(); 63 while(rosIter.hasNext()) { 64 Object ro = rosIter.next(); 65 String parentId = null; 66 TelephoneNumber [] telephoneNumbers = null; 67 if (ro instanceof Organization) { 68 Organization org = (Organization) ro; 69 telephoneNumbers = org.getTelephoneNumber(); 70 parentId = org.getId(); 71 } 72 else if (ro instanceof User) { 73 User user = (User) ro; 74 telephoneNumbers = user.getTelephoneNumber(); 75 parentId = user.getId(); 76 } 77 else { 78 throw new RegistryException("Incorrect RegistryObject"); 79 } 80 81 for (int i=0; i < telephoneNumbers.length; i++) { 82 /* 83 stmt.setString(1, telephoneNumbers[i].getAreaCode()); 84 stmt.setString(2, telephoneNumbers[i].getCountryCode()); 85 stmt.setString(3, telephoneNumbers[i].getExtension()); 86 stmt.setString(4, telephoneNumbers[i].getNumber()); 87 stmt.setString(5, telephoneNumbers[i].getPhoneType()); 88 stmt.setString(6, telephoneNumbers[i].getUrl()); 89 stmt.setString(7, orgId); 90 stmt.addBatch(); 91 */ 92 String areaCode = telephoneNumbers[i].getAreaCode(); 93 if (areaCode != null) { 94 areaCode = "'" + areaCode + "'"; 95 } 96 97 String countryCode = telephoneNumbers[i].getCountryCode(); 98 if (countryCode != null) { 99 countryCode = "'" + countryCode + "'"; 100 } 101 102 String extension = telephoneNumbers[i].getExtension(); 103 if (extension != null) { 104 extension = "'" + extension + "'"; 105 } 106 107 String number = telephoneNumbers[i].getNumber(); 108 if (number != null) { 109 number = "'" + number + "'"; 110 } 111 112 String phoneType = telephoneNumbers[i].getPhoneType(); 113 if (phoneType != null) { 114 phoneType = "'" + phoneType + "'"; 115 } 116 117 String url = telephoneNumbers[i].getUrl(); 118 if (url != null) { 119 url = "'" + url + "'"; 120 } 121 122 String str = "INSERT INTO TelephoneNumber " + 123 "VALUES( " + 124 areaCode + ", " + 125 countryCode + ", " + 126 extension + ", " + 127 number + ", " + 128 phoneType + ", " + 129 url + ", " + 130 "'" + parentId + "' )"; 131 132 System.err.println("stmt = '" + str + "'"); 133 stmt.addBatch(str); 134 } 135 } 136 137 int [] updateCounts = stmt.executeBatch(); 138 //stmt.close(); 139 } 140 catch (java.sql.SQLException e) { 141 e.printStackTrace(); 142 RegistryException exception = new RegistryException(e); 143 throw exception; 144 } finally { 145 try { 146 if (stmt != null) 147 stmt.close(); 148 } catch (SQLException sqle) { 149 sqle.printStackTrace(); 150 } 151 } 152 } 153 154 /*** 155 * Does a bulk insert of a Collection of objects that match the type for this persister. 156 * 157 */ 158 public void insert(java.sql.Connection connection, String parentId, ArrayList telephoneNumbers) throws RegistryException { 159 160 Statement stmt = null; 161 162 if (telephoneNumbers.size()==0) { 163 return; 164 } 165 166 System.err.println("Inserting " + telephoneNumbers.size() + " TelephoneNumbers"); 167 168 try { 169 stmt = connection.createStatement(); 170 171 Iterator iter = telephoneNumbers.iterator(); 172 while (iter.hasNext()) { 173 TelephoneNumberType telephoneNumber = (TelephoneNumberType)iter.next(); 174 175 //Log.print(Log.TRACE, 8, "\tDATABASE EVENT: storing TelephoneNumber " ); 176 177 String areaCode = telephoneNumber.getAreaCode(); 178 if (areaCode != null) { 179 areaCode = "'" + areaCode + "'"; 180 } 181 182 String countryCode = telephoneNumber.getCountryCode(); 183 if (countryCode != null) { 184 countryCode = "'" + countryCode + "'"; 185 } 186 187 String extension = telephoneNumber.getExtension(); 188 if (extension != null) { 189 extension = "'" + extension + "'"; 190 } 191 192 String number = telephoneNumber.getNumber(); 193 if (number != null) { 194 number = "'" + number + "'"; 195 } 196 197 String phoneType = telephoneNumber.getPhoneType(); 198 if (phoneType != null) { 199 phoneType = "'" + phoneType + "'"; 200 } 201 202 String url = telephoneNumber.getUrl(); 203 if (url != null) { 204 url = "'" + url + "'"; 205 } 206 207 String str = "INSERT INTO TelephoneNumber " + 208 "VALUES( " + 209 areaCode + ", " + 210 countryCode + ", " + 211 extension + ", " + 212 number + ", " + 213 phoneType + ", " + 214 url + ", " + 215 "'" + parentId + "' )"; 216 217 System.err.println("stmt = '" + str + "'"); 218 stmt.addBatch(str); 219 } 220 221 if (telephoneNumbers.size() > 0) { 222 int [] updateCounts = stmt.executeBatch(); 223 //stmt.close(); 224 } 225 } 226 catch (java.sql.SQLException e) { 227 e.printStackTrace(); 228 RegistryException exception = new RegistryException(e); 229 throw exception; 230 } finally { 231 try { 232 if (stmt != null) 233 stmt.close(); 234 } catch (SQLException sqle) { 235 sqle.printStackTrace(); 236 } 237 } 238 } 239 240 241 /*** 242 * Does a bulk delete of a Collection of objects that match the type for this persister. 243 * 244 */ 245 public void delete(java.sql.Connection connection, ArrayList parentIds) throws RegistryException { 246 System.err.println("Deleting " + parentIds.size() + " TelephoneNumbers"); 247 Statement stmt = null; 248 249 250 try 251 { 252 stmt = connection.createStatement(); 253 254 Iterator iter = parentIds.iterator(); 255 while (iter.hasNext()) { 256 String parentId = (String)iter.next(); 257 258 259 stmt.addBatch("DELETE from TelephoneNumber WHERE parent = '" + 260 parentId + "' "); 261 } 262 263 int [] updateCounts = stmt.executeBatch(); 264 //stmt.close(); 265 } 266 catch (java.sql.SQLException e) { 267 RegistryException exception = new RegistryException(e); 268 throw exception; 269 } finally { 270 try { 271 if (stmt != null) 272 stmt.close(); 273 } catch (SQLException sqle) { 274 sqle.printStackTrace(); 275 } 276 } 277 } 278 279 public void loadObjectFromResultSet(java.sql.Connection conn, Object obj, ResultSet rs) throws RegistryException { 280 try { 281 if (!(obj instanceof org.oasis.ebxml.registry.bindings.rim.TelephoneNumberType)) { 282 throw new RegistryException("Unexpected object " + obj + ". Was expecting org.oasis.ebxml.registry.bindings.rim.TelephoneNumberType."); 283 } 284 285 TelephoneNumberType phone = (TelephoneNumberType)obj; 286 287 String areaCode = rs.getString("areaCode"); 288 phone.setAreaCode(areaCode); 289 290 String countryCode = rs.getString("countryCode"); 291 phone.setCountryCode(countryCode); 292 293 String extension = rs.getString("extension"); 294 phone.setExtension(extension); 295 296 String number = rs.getString("number_"); 297 phone.setNumber(number); 298 299 String phoneType = rs.getString("phoneType"); 300 phone.setPhoneType(phoneType); 301 302 String url = rs.getString("url"); 303 phone.setUrl(url); 304 305 } 306 catch (SQLException e) { 307 e.printStackTrace(); 308 throw new RegistryException(e); 309 } 310 } 311 312 ArrayList getTelephoneNumbersByParent(java.sql.Connection conn, String parentId, Class telephoneClass) throws RegistryException { 313 ArrayList phones = new ArrayList(); 314 Statement stmt = null; 315 316 try { 317 stmt = conn.createStatement(); 318 ResultSet rs = stmt.executeQuery("SELECT * FROM TelephoneNumber WHERE parent = '" + parentId + "'"); 319 320 while (rs.next()) { 321 Object obj = telephoneClass.newInstance(); 322 loadObjectFromResultSet(conn, obj, rs); 323 phones.add(obj); 324 } 325 } 326 catch (SQLException e) { 327 e.printStackTrace(); 328 throw new RegistryException(e); 329 } 330 catch (InstantiationException e) { 331 e.printStackTrace(); 332 throw new RegistryException(e); 333 } 334 catch (IllegalAccessException e) { 335 e.printStackTrace(); 336 throw new RegistryException(e); 337 } finally { 338 try { 339 if (stmt != null) 340 stmt.close(); 341 } catch (SQLException sqle) { 342 sqle.printStackTrace(); 343 } 344 } 345 346 return phones; 347 } 348 349 }

This page was automatically generated by Maven