View Javadoc
1 /* 2 * $Header: /cvsroot/ebxmlrr/ebxmlrr/src/share/com/sun/ebxml/registry/persistence/rdb/OrganizationDAO.java,v 1.37 2003/05/30 22:42:36 waikei Exp $ 3 * ==================================================================== 4 * 5 * This code is subject to the freebxml License, Version 1.1 6 * 7 * Copyright (c) 2001 - 2003 freebxml.org. All rights reserved. 8 * 9 * ==================================================================== 10 */ 11 package com.sun.ebxml.registry.persistence.rdb; 12 13 import com.sun.ebxml.registry.RegistryException; 14 import com.sun.ebxml.registry.lcm.ObjectsNotFoundException; 15 import com.sun.ebxml.registry.lcm.ReferencedObjectNotFoundException; 16 import com.sun.ebxml.registry.lcm.ReferencesExistException; 17 import com.sun.ebxml.registry.security.authorization.AuthorizationServiceImpl; 18 import com.sun.ebxml.registry.util.BindingUtility; 19 20 import org.oasis.ebxml.registry.bindings.query.ResponseOption; 21 import org.oasis.ebxml.registry.bindings.rim.Address; 22 import org.oasis.ebxml.registry.bindings.rim.ObjectRef; 23 import org.oasis.ebxml.registry.bindings.rim.Organization; 24 import org.oasis.ebxml.registry.bindings.rim.TelephoneNumber; 25 import org.oasis.ebxml.registry.bindings.rim.User; 26 27 import java.sql.ResultSet; 28 import java.sql.SQLException; 29 import java.sql.Statement; 30 31 import java.util.ArrayList; 32 import java.util.Iterator; 33 34 35 /*** 36 * 37 * @author Farrukh S. Najmi 38 * @author Adrian Chong 39 * @version 40 */ 41 public class OrganizationDAO extends RegistryObjectDAO { 42 static BindingUtility bu = BindingUtility.getInstance(); 43 44 protected OrganizationDAO() { 45 } 46 47 public static String getTableNameStatic() { 48 return "Organization"; 49 } 50 51 public String getTableName() { 52 return getTableNameStatic(); 53 } 54 55 /*** 56 * Does a bulk insert of a Collection of objects that match the type for this persister. 57 * 58 */ 59 public void insert(User user, java.sql.Connection connection, ArrayList orgs) 60 throws RegistryException { 61 if (orgs.size() == 0) { 62 return; 63 } 64 65 String orgId = null; 66 Statement stmt = null; 67 68 try { 69 // Update the exisitng Organization 70 orgs = updateExistingObjects(user, connection, orgs); 71 72 if (orgs.size() == 0) { 73 return; 74 } 75 76 System.err.println("Inserting " + orgs.size() + " Organizations"); 77 78 stmt = connection.createStatement(); 79 80 Iterator iter = orgs.iterator(); 81 PostalAddressDAO postalAddressDAO = new PostalAddressDAO(); 82 TelephoneNumberDAO telephoneNumberDAO = new TelephoneNumberDAO(); 83 84 while (iter.hasNext()) { 85 Organization org = (Organization) iter.next(); 86 orgId = org.getId(); 87 88 //Log.print(Log.TRACE, 8, "\tDATABASE EVENT: storing Organization " ); 89 90 /* The organization is expected to be existing in database, check 91 whether it really exists and the objectType is User 92 */ 93 if (org.getPrimaryContact() instanceof ObjectRef) { 94 //System.err.println("ObjectRef@@"); 95 ObjectRef primaryContactObjectRef = (ObjectRef) org.getPrimaryContact(); 96 String objectType = getObjectType(connection, 97 primaryContactObjectRef.getId(), 98 super.getTableName()); 99 100 if (objectType == null) { 101 throw new ReferencedObjectNotFoundException( 102 "The primaryContact " + 103 primaryContactObjectRef.getId() + 104 " referenced by Organization " + org.getId() + 105 " does not exist"); 106 } 107 108 if (!objectType.equalsIgnoreCase("User")) { 109 throw new ReferencedObjectNotFoundException( 110 "The object " + primaryContactObjectRef.getId() + 111 " referenced by Organization " + org.getId() + 112 " exists, but the objectType is not User"); 113 } 114 } 115 116 /* Check whether the referenced parent exists if the parent is 117 specified by ObjectRef 118 */ 119 if ((org.getParent() != null) && 120 org.getParent() instanceof ObjectRef) { 121 ObjectRef parentObjectRef = (ObjectRef) org.getParent(); 122 String objectType = getObjectType(connection, 123 parentObjectRef.getId(), super.getTableName()); 124 125 if (objectType == null) { 126 throw new ReferencedObjectNotFoundException( 127 "The parent " + parentObjectRef.getId() + 128 " referenced by Organization " + org.getId() + 129 " does not exist"); 130 } 131 132 if (!objectType.equalsIgnoreCase("Organization")) { 133 throw new ReferencedObjectNotFoundException( 134 "The object " + parentObjectRef.getId() + 135 " referenced by Organization " + org.getId() + 136 " exists, but the objectType is not Organization"); 137 } 138 } 139 140 String parentId = bu.getObjectId(org.getParent()); 141 142 if (parentId != null) { 143 parentId = "'" + parentId + "'"; 144 } 145 146 String primaryContactId = bu.getObjectId(org.getPrimaryContact()); 147 148 String str = "INSERT INTO Organization " + "VALUES(null, " + //AccessControlPolicy 149 "'" + org.getId() + "', " + "'Organization', " + parentId + 150 ", " + "'" + primaryContactId + "' )"; 151 152 System.err.println("stmt = '" + str + "'"); 153 stmt.addBatch(str); 154 } 155 156 if (orgs.size() > 0) { 157 int[] updateCounts = stmt.executeBatch(); 158 //stmt.close(); 159 super.insert(user, connection, orgs); 160 161 // Now generate the AE for inserting Organization 162 generateAuditbleEvent(connection, orgs, "Created", user); 163 postalAddressDAO.insert(connection, orgs); 164 telephoneNumberDAO.insert(connection, orgs); 165 } 166 } catch (java.sql.SQLException e) { 167 e.printStackTrace(); 168 169 RegistryException exception = new RegistryException(e); 170 throw exception; 171 } finally { 172 try { 173 if (stmt != null) 174 stmt.close(); 175 } catch (SQLException sqle) { 176 sqle.printStackTrace(); 177 } 178 } 179 } 180 181 /*** 182 Handles update through UpdateObjectsRequest. 183 */ 184 public void update(User user, java.sql.Connection connection, ArrayList orgs) 185 throws RegistryException { 186 /* 187 We do not need to check whether the user is authorized to update because the authorization is expected to be checked by higher level. 188 Should we move the authorization to persistance layer?????? 189 */ 190 update(user, connection, orgs, true, false); 191 } 192 193 public void update(User user, java.sql.Connection connection, 194 ArrayList orgs, boolean checkExistence, boolean checkAuthorization) 195 throws RegistryException { 196 if (orgs.size() == 0) { 197 return; 198 } 199 200 System.err.println("Updating " + orgs.size() + " Organizations"); 201 202 String orgId = null; 203 Statement stmt = null; 204 205 try { 206 // Check whether the user can update the objects 207 if (checkAuthorization) { 208 AuthorizationServiceImpl authorization = AuthorizationServiceImpl.getInstance(); 209 authorization.checkAuthorization(user, 210 bu.getIdsFromRegistryObjects(orgs), 211 AuthorizationServiceImpl.UPDATE_OBJECTS_REQUEST); 212 } 213 214 stmt = connection.createStatement(); 215 216 Iterator iter = orgs.iterator(); 217 PostalAddressDAO postalAddressDAO = new PostalAddressDAO(); 218 TelephoneNumberDAO telephoneNumberDAO = new TelephoneNumberDAO(); 219 ArrayList orgsIds = new ArrayList(); 220 221 while (iter.hasNext()) { 222 Organization org = (Organization) iter.next(); 223 orgId = org.getId(); 224 225 //Log.print(Log.TRACE, 8, "\tDATABASE EVENT: updating Organization " ); 226 // checking whether it exists 227 if (checkExistence && 228 !registryObjectExist(connection, orgId, getTableName())) { 229 ArrayList idList = new ArrayList(); 230 idList.add(orgId); 231 throw new ObjectsNotFoundException(idList); 232 } 233 234 // check whether it really exists and the objectType is User 235 if (org.getPrimaryContact() instanceof ObjectRef) { 236 ObjectRef primaryContactObjectRef = (ObjectRef) org.getPrimaryContact(); 237 String objectType = getObjectType(connection, 238 primaryContactObjectRef.getId(), 239 super.getTableName()); 240 241 if (objectType == null) { 242 throw new ReferencedObjectNotFoundException( 243 "The primaryContact " + 244 primaryContactObjectRef.getId() + 245 " referenced by Organization " + org.getId() + 246 " does not exist"); 247 } 248 249 if (!objectType.equalsIgnoreCase("User")) { 250 throw new ReferencedObjectNotFoundException( 251 "The object " + primaryContactObjectRef.getId() + 252 " referenced by Organization " + org.getId() + 253 " exists, but the objectType is not User"); 254 } 255 } 256 257 /* Check whether the referenced parent exists if the parent is 258 specified by ObjectRef 259 */ 260 if ((org.getParent() != null) && 261 org.getParent() instanceof ObjectRef) { 262 ObjectRef parentObjectRef = (ObjectRef) org.getParent(); 263 String objectType = getObjectType(connection, 264 parentObjectRef.getId(), super.getTableName()); 265 266 if (objectType == null) { 267 throw new ReferencedObjectNotFoundException( 268 "The parent " + parentObjectRef.getId() + 269 " referenced by Organization " + org.getId() + 270 " does not exist"); 271 } 272 273 if (!objectType.equalsIgnoreCase("Organization")) { 274 throw new ReferencedObjectNotFoundException( 275 "The object " + parentObjectRef.getId() + 276 " referenced by Organization " + org.getId() + 277 " exists, but the objectType is not Organization"); 278 } 279 } 280 281 String parentId = bu.getObjectId(org.getParent()); 282 283 if (parentId != null) { 284 parentId = "'" + parentId + "'"; 285 } 286 287 // primaryContact is non-mutable 288 String str = "UPDATE " + getTableName() + 289 " SET accessControlPolicy=null" + ", parent=" + parentId + 290 " WHERE id='" + orgId + "'"; 291 292 System.err.println("stmt = '" + str + "'"); 293 stmt.addBatch(str); 294 295 orgsIds.add(orgId); 296 } 297 298 if (orgs.size() > 0) { 299 int[] updateCounts = stmt.executeBatch(); 300 //stmt.close(); 301 302 // Updated composed Name, Descrption, Slot, ExternalIdentifier and Classification 303 super.update(user, connection, orgs); 304 305 // Now generate the AE for updating Organization 306 generateAuditbleEvent(connection, orgs, "Updated", user); 307 postalAddressDAO.delete(connection, orgsIds); 308 postalAddressDAO.insert(connection, orgs); 309 telephoneNumberDAO.delete(connection, orgsIds); 310 telephoneNumberDAO.insert(connection, orgs); 311 } 312 } catch (java.sql.SQLException e) { 313 e.printStackTrace(); 314 315 RegistryException exception = new RegistryException(e); 316 throw exception; 317 } finally { 318 try { 319 if (stmt != null) 320 stmt.close(); 321 } catch (SQLException sqle) { 322 sqle.printStackTrace(); 323 } 324 } 325 } 326 327 // Should we make index on parent in Organization???? 328 protected String checkOrganizationReferences(java.sql.Connection conn, 329 String orgId) throws RegistryException { 330 String referencingOrgId = null; 331 Statement stmt = null; 332 333 try { 334 stmt = conn.createStatement(); 335 String sql = "SELECT id FROM Organization WHERE " + "parent='" + 336 orgId + "' AND parent IS NOT NULL"; 337 ResultSet rs = stmt.executeQuery(sql); 338 339 if (rs.next()) { 340 referencingOrgId = rs.getString("id"); 341 } 342 343 //stmt.close(); 344 345 return referencingOrgId; 346 } catch (SQLException e) { 347 throw new RegistryException(e); 348 } finally { 349 try { 350 if (stmt != null) 351 stmt.close(); 352 } catch (SQLException sqle) { 353 sqle.printStackTrace(); 354 } 355 } 356 } 357 358 // Should we make index on organization in User_???? 359 protected String checkUserReferences(java.sql.Connection conn, String orgId) 360 throws RegistryException { 361 String userId = null; 362 Statement stmt = null; 363 364 try { 365 stmt = conn.createStatement(); 366 String sql = "SELECT id FROM User_ WHERE " + "organization='" + 367 orgId + "'"; 368 ResultSet rs = stmt.executeQuery(sql); 369 370 if (rs.next()) { 371 userId = rs.getString("id"); 372 } 373 374 //stmt.close(); 375 376 return userId; 377 } catch (SQLException e) { 378 throw new RegistryException(e); 379 } finally { 380 try { 381 if (stmt != null) 382 stmt.close(); 383 } catch (SQLException sqle) { 384 sqle.printStackTrace(); 385 } 386 } 387 } 388 389 /*** 390 * Does a bulk delete of a Collection of objects that match the type for this persister. 391 * 392 */ 393 public void delete(User user, java.sql.Connection connection, 394 ArrayList orgsIds) throws RegistryException { 395 Iterator iter = orgsIds.iterator(); 396 397 // Check whether this Organization is used parent of any other Organization 398 while (iter.hasNext()) { 399 String orgId = (String) iter.next(); 400 String referencingOrgId = checkOrganizationReferences(connection, 401 orgId); 402 403 if (referencingOrgId != null) { 404 throw new ReferencesExistException("Organization " + 405 referencingOrgId + " is referencing " + orgId); 406 } 407 408 String userId = checkUserReferences(connection, orgId); 409 410 if (userId != null) { 411 throw new ReferencesExistException("User " + userId + 412 " is referencing " + orgId); 413 } 414 } 415 416 super.delete(user, connection, orgsIds); 417 418 PostalAddressDAO postalAddressDAO = new PostalAddressDAO(); 419 postalAddressDAO.delete(connection, orgsIds); 420 421 TelephoneNumberDAO telephoneNumberDAO = new TelephoneNumberDAO(); 422 telephoneNumberDAO.delete(connection, orgsIds); 423 } 424 425 public void loadObjectFromResultSet(java.sql.Connection conn, Object obj, 426 ResultSet rs, ResponseOption responseOption, ArrayList objectRefs) 427 throws RegistryException { 428 try { 429 if (!(obj instanceof org.oasis.ebxml.registry.bindings.rim.Organization)) { 430 throw new RegistryException("Unexpected object " + obj + 431 ". Was expecting org.oasis.ebxml.registry.bindings.rim.Organization."); 432 } 433 434 Organization org = (Organization) obj; 435 super.loadObjectFromResultSet(conn, obj, rs, responseOption, 436 objectRefs); 437 438 String parentId = rs.getString("parent"); 439 ObjectRef or = null; 440 441 if (parentId != null) { 442 or = new ObjectRef(); 443 or.setId(parentId); 444 objectRefs.add(or); 445 } 446 447 org.setParent(or); 448 449 String primaryContactId = rs.getString("primaryContact"); 450 or = new ObjectRef(); 451 or.setId(primaryContactId); 452 objectRefs.add(or); 453 org.setPrimaryContact(or); 454 455 Address address = new Address(); 456 PostalAddressDAO postalAddressDAO = new PostalAddressDAO(); 457 postalAddressDAO.getPostalAddressByParent(conn, address, org.getId()); 458 org.setAddress(address); 459 460 TelephoneNumberDAO telephoneNumberDAO = new TelephoneNumberDAO(); 461 ArrayList phones = telephoneNumberDAO.getTelephoneNumbersByParent(conn, 462 org.getId(), TelephoneNumber.class); 463 TelephoneNumber[] phones1 = new TelephoneNumber[phones.size()]; 464 465 Iterator iter = phones.iterator(); 466 int i = 0; 467 468 while (iter.hasNext()) { 469 phones1[i++] = (TelephoneNumber) iter.next(); 470 } 471 472 org.setTelephoneNumber(phones1); 473 } catch (SQLException e) { 474 e.printStackTrace(); 475 throw new RegistryException(e); 476 } 477 } 478 479 public ArrayList getLeafObjectList(java.sql.Connection conn, ResultSet rs, 480 ResponseOption responseOption, ArrayList objectRefs) 481 throws RegistryException { 482 ArrayList res = new ArrayList(); 483 484 try { 485 while (rs.next()) { 486 Organization obj = new Organization(); 487 loadObjectFromResultSet(conn, obj, rs, responseOption, 488 objectRefs); 489 490 res.add(obj); 491 } 492 } catch (SQLException e) { 493 e.printStackTrace(); 494 throw new RegistryException(e); 495 } 496 497 return res; 498 } 499 }

This page was automatically generated by Maven