View Javadoc
1 /* 2 * ==================================================================== 3 * 4 * This code is subject to the freebxml License, Version 1.1 5 * 6 * Copyright (c) 2001 - 2003 freebxml.org. All rights reserved. 7 * 8 * ==================================================================== 9 */ 10 11 package com.sun.ebxml.registry.persistence.rdb; 12 13 import java.sql.Connection; 14 import java.sql.ResultSet; 15 import java.sql.SQLException; 16 import java.sql.Statement; 17 import java.util.ArrayList; 18 import java.util.HashMap; 19 import java.util.Iterator; 20 21 import org.oasis.ebxml.registry.bindings.query.ResponseOption; 22 import org.oasis.ebxml.registry.bindings.rim.Association; 23 import org.oasis.ebxml.registry.bindings.rim.ObjectRef; 24 import org.oasis.ebxml.registry.bindings.rim.User; 25 26 import com.sun.ebxml.registry.RegistryException; 27 import com.sun.ebxml.registry.lcm.AssociateToDeprecatedRegistryEntryException; 28 import com.sun.ebxml.registry.lcm.ObjectsNotFoundException; 29 import com.sun.ebxml.registry.lcm.ReferencedObjectNotFoundException; 30 import com.sun.ebxml.registry.security.OwnerNotFoundException; 31 import com.sun.ebxml.registry.security.authorization.AuthorizationServiceImpl; 32 import com.sun.ebxml.registry.util.BindingUtility; 33 import com.sun.ebxml.registry.util.RegistryProperties; 34 35 public class AssociationDAO extends RegistryObjectDAO { 36 protected AssociationDAO(){} 37 38 static BindingUtility bu = BindingUtility.getInstance(); 39 40 public static String getTableNameStatic() { 41 return "Association"; 42 } 43 44 public String getTableName() { 45 return getTableNameStatic(); 46 } 47 48 /*** 49 * Does a bulk insert of a Collection of objects that match the type for this persister. 50 * If a association is extramural and the another involved party submits the 51 * same association, the association will be confirmed. Once after the confirmation, 52 * further submitting this association is considered as updating it. 53 */ 54 public void insert(User user, java.sql.Connection connection, 55 ArrayList associations) throws RegistryException { 56 Statement stmt = null; 57 if (associations.size()==0) { 58 return; 59 } 60 try { 61 62 boolean skipAssociationConfirmation = Boolean.valueOf(RegistryProperties.getInstance().getProperty("ebxmlrr.persistence.rdb.skipAssociationConfirmation")).booleanValue(); 63 64 if (!skipAssociationConfirmation) { 65 //First process (and remove from list) any associations 66 //that may be extramural associations being confirmed. 67 processAnyConfirmations(user, connection, associations); 68 } 69 70 //Now do normal insert processing for new associations 71 associations = updateExistingObjects(user, connection , associations); 72 // Check any new associations 73 if (associations.size()==0) { 74 return; 75 } 76 System.err.println("Inserting " + associations.size() + " Associations"); 77 78 stmt = connection.createStatement(); 79 Iterator iter = associations.iterator(); 80 81 //System.err.println(associations.size() + "!!!!!"); 82 83 while (iter.hasNext()) { 84 Association association = (Association)iter.next(); 85 /* 86 Check whether the referenced objects exist in database and whether 87 the registry entries are deprecated 88 */ 89 checkReferencedObjects(connection, association); 90 91 String id = association.getId(); 92 String srcId = bu.getObjectId(association.getSourceObject()); 93 String targetId = bu.getObjectId(association.getTargetObject()); 94 95 String associationType = association.getAssociationType(); 96 if (associationType != null) { 97 associationType = "'" + associationType + "'"; 98 } 99 100 String isConfirmedBySourceOwner = "'F'"; 101 String isConfirmedByTargetOwner = "'F'"; 102 User sourceOwner = null; 103 User targetOwner = null; 104 105 try { 106 sourceOwner = RegistryObjectDAO.getOwner(connection, srcId); 107 } 108 catch (OwnerNotFoundException e) { 109 sourceOwner = user; //??Dangerous assumption. Should look in submitted objects to make sure. 110 } 111 112 try { 113 targetOwner = RegistryObjectDAO.getOwner(connection, targetId); 114 } 115 catch (OwnerNotFoundException e) { 116 targetOwner = user; //??Dangerous assumption. Should look in submitted objects to make sure. 117 } 118 119 120 if (user.getId().equals(sourceOwner.getId())) { 121 isConfirmedBySourceOwner = "'T'"; 122 } 123 124 if (user.getId().equals(targetOwner.getId())) { 125 isConfirmedByTargetOwner = "'T'"; 126 } 127 128 129 String str = "INSERT INTO Association " + 130 "VALUES(null, " + //AccessControlPolicy 131 "'" + id + "', " + 132 "'Association', " + 133 associationType + ", " + 134 "'" + srcId + "', " + 135 "'" + targetId + "', " + 136 isConfirmedBySourceOwner + ", " + 137 isConfirmedByTargetOwner + " )"; 138 139 System.err.println("stmt = " + str); 140 stmt.addBatch(str); 141 142 } 143 144 if (associations.size() > 0) { 145 int [] updateCounts = stmt.executeBatch(); 146 //stmt.close(); 147 super.insert(user, connection, associations); 148 // Now generate the AE for inserting Association 149 generateAuditbleEvent(connection, associations, "Created", user); 150 } 151 } 152 catch (java.sql.SQLException e) { 153 e.printStackTrace(); 154 RegistryException exception = new RegistryException(e); 155 throw exception; 156 } finally { 157 try { 158 if (stmt != null) 159 stmt.close(); 160 } catch (SQLException sqle) { 161 sqle.printStackTrace(); 162 } 163 } 164 165 } 166 167 /*** 168 * Get the HashMap of id to whether it is confirmed. The value is of Boolean 169 */ 170 171 private HashMap getIdIsConfirmedMap(Connection conn, ArrayList associations) throws RegistryException { 172 HashMap map = new HashMap(); 173 Statement stmt = null; 174 175 if (associations.size()==0) { 176 return map; 177 } 178 try { 179 stmt = conn.createStatement(); 180 String ids = "("; 181 Iterator iter = associations.iterator(); 182 while (iter.hasNext()) { 183 Association association = (Association)iter.next(); 184 String id = association.getId(); 185 if (iter.hasNext()) { 186 ids += "'" + id + "',"; 187 } 188 else { 189 ids += "'" + id + "')"; 190 } 191 } // end looping the associations to get the ids 192 String sql = "SELECT id, isConfirmedBySourceOwner, isConfirmedByTargetOwner FROM " 193 + getTableName() + " WHERE id IN " + ids; 194 ResultSet rs = stmt.executeQuery(sql); 195 while(rs.next()) { 196 String id = rs.getString(1); 197 //boolean test = rs.getBoolean(2); 198 String isConfirmedBySourceOwner = rs.getString(2); 199 String isConfirmedByTargetOwner = rs.getString(3); 200 if ((isConfirmedBySourceOwner.substring(0,0).equalsIgnoreCase("T")) && (isConfirmedByTargetOwner.substring(0,0).equalsIgnoreCase("T"))) { 201 //map.put(id, new Boolean(true)); 202 map.put(id, Boolean.TRUE); 203 } 204 else { 205 //map.put(id, new Boolean(false)); 206 map.put(id, Boolean.FALSE); 207 } 208 } 209 return map; 210 } 211 catch (java.sql.SQLException e) { 212 e.printStackTrace(); 213 RegistryException exception = new RegistryException(e); 214 throw exception; 215 } finally { 216 try { 217 if (stmt != null) 218 stmt.close(); 219 } catch (SQLException sqle) { 220 sqle.printStackTrace(); 221 } 222 } 223 } 224 225 public void update(User user, java.sql.Connection connection 226 , ArrayList associations) throws RegistryException { 227 /* 228 We do not need to check whether the user is authorized to update because the authorization is expected to be checked by higher level. 229 Should we move the authorization to persistance layer?????? 230 */ 231 update(user, connection, associations, true, false); 232 } 233 234 235 /*** 236 * Does a buld update of ArrayList of Association. All the attributes are not mutable. 237 */ 238 public void update(User user, java.sql.Connection connection, 239 ArrayList associations, boolean checkExistence, boolean checkAuthorization) throws RegistryException { 240 if (associations.size()==0) { 241 return; 242 } 243 244 // Check whether the user can update the objects 245 if (checkAuthorization) { 246 AuthorizationServiceImpl authorization = AuthorizationServiceImpl.getInstance(); 247 authorization.checkAuthorization(user, bu.getIdsFromRegistryObjects(associations), AuthorizationServiceImpl.UPDATE_OBJECTS_REQUEST); 248 } 249 250 System.err.println("Updating " + associations.size() + " Associations"); 251 //HashMap idsIsConfirmedMap = getIdIsConfirmedMap(connection, associations); 252 Iterator iter = associations.iterator(); 253 //System.err.println(associations.size() + "!!!!!"); 254 String userId = user.getId(); 255 256 while (iter.hasNext()) { 257 Association association = (Association)iter.next(); 258 String assId = association.getId(); 259 // checking whether it exists 260 if (checkExistence && !registryObjectExist(connection, assId, getTableName())) { 261 ArrayList idList = new ArrayList(); 262 idList.add(assId); 263 throw new ObjectsNotFoundException(idList); 264 } 265 266 /* 267 if (!((Boolean)idsIsConfirmedMap.get(assId)).booleanValue()) { 268 // The extramural association is not yet confirmed 269 if (!userId.equals(sourceOwnerId) && !userId.equals(targetOwnerId)) { 270 // The user is neither source owner or target owner 271 ArrayList ids = new ArrayList(); 272 ids.add(assId); 273 throw new ObjectsNotFoundException(ids); 274 } 275 }*/ 276 // Now generate the AE for updating Association 277 generateAuditbleEvent(connection, association, "Updated", user); 278 } // end looping associations 279 280 if (associations.size() > 0) { 281 // Updated composed Name, Descrption, Slot, ExternalIdentifier and Classification 282 super.update(user, connection, associations); 283 } 284 } 285 286 /*** 287 * Check whether the referenced objects exist in database and whether 288 * the registry entries are deprecated 289 */ 290 private void checkReferencedObjects(Connection conn, Association association) throws 291 SQLException, ReferencedObjectNotFoundException, AssociateToDeprecatedRegistryEntryException { 292 Object sourceObject = association.getSourceObject(); 293 String srcId = null; 294 295 Object targetObject = association.getTargetObject(); 296 String targetId = null; 297 298 //Only need to check refs where ref is of type ObjectRef because that is the case where the object actually resides in the registry. 299 //When the ref is of type RegistryObjectType then the object does not catually reside in the registry but was part of the submission. 300 if (!((sourceObject instanceof ObjectRef) || (targetObject instanceof ObjectRef))) { 301 return; 302 } 303 304 if (sourceObject instanceof ObjectRef) { 305 srcId = ((ObjectRef)sourceObject).getId(); 306 } 307 308 if (targetObject instanceof ObjectRef) { 309 targetId = ((ObjectRef)targetObject).getId(); 310 } 311 312 String ids = ""; 313 String assId = association.getId(); 314 315 if (srcId != null) { 316 ids = "'" + srcId + "'"; 317 } 318 319 if ((srcId != null) && (targetId != null)) { 320 ids = ids + ","; 321 } 322 323 if (targetId != null) { 324 ids = ids + " '" + targetId + "'"; 325 } 326 327 //System.out.println(ids + "!!!!"); 328 329 String sql = "SELECT ro.id, re.status FROM" + 330 " RegistryObject ro LEFT OUTER JOIN RegistryEntry re" + 331 " ON ro.id=re.id" + 332 " WHERE ro.id IN (" + ids + ")"; 333 334 System.err.println(sql); 335 336 Statement stmt = conn.createStatement(); 337 ResultSet rs = stmt.executeQuery(sql); 338 339 HashMap idStatusMap = new HashMap(); 340 while(rs.next()) { 341 //System.out.println(rs.getString(1)); 342 //System.out.println(rs.getString(2)); 343 String id = rs.getString(1); 344 String status = rs.getString(2); 345 if (id != null) { 346 idStatusMap.put(id, status); 347 } 348 } 349 350 if (srcId != null) { 351 if (!idStatusMap.containsKey(srcId)) { 352 throw new ReferencedObjectNotFoundException("The sourceObject " 353 + srcId + " referenced by Association " + assId 354 + " does not exist" ); 355 } 356 String status = (String)idStatusMap.get(srcId); 357 if (status != null && status.equalsIgnoreCase("deprecated")) { 358 // It is a deprecated registry entry 359 throw new AssociateToDeprecatedRegistryEntryException(srcId 360 , targetId, srcId); 361 } 362 } 363 if (targetId != null) { 364 if (!idStatusMap.containsKey(targetId)) { 365 throw new ReferencedObjectNotFoundException("The targetObject " 366 + srcId + " referenced by Association " + assId 367 + " does not exist" ); 368 } 369 String status = (String)idStatusMap.get(targetId); 370 if (status != null && status.equalsIgnoreCase("deprecated")) { 371 // It is a deprecated registry entry 372 throw new AssociateToDeprecatedRegistryEntryException(srcId 373 , targetId, targetId); 374 } 375 } 376 377 stmt.close(); 378 } 379 380 /*** 381 * Processes any associations that are clones of existing associations but are submitted by 382 * the owner of the source or target object to indicate their confirmation of the association. 383 */ 384 private void processAnyConfirmations(User user, java.sql.Connection connection, ArrayList associations) throws SQLException, RegistryException { 385 HashMap idToExramuralAssocMap = null; 386 387 Statement stmt = connection.createStatement(); 388 389 //First find which associations already exists in db. 390 //They may be extramural associations being confirmed. 391 392 HashMap idToNewAssocMap = new HashMap(); 393 StringBuffer query = new StringBuffer("SELECT * FROM Association ass WHERE ass.id IN ( "); 394 Iterator iter = associations.iterator(); 395 while (iter.hasNext()) { 396 Association newAssoc = (Association)iter.next(); 397 String id = newAssoc.getId(); 398 idToNewAssocMap.put(id, newAssoc); 399 400 if (iter.hasNext()) { 401 query.append("'" + id + "', "); 402 } 403 else { 404 query.append("'" + id + "' )"); 405 } 406 } 407 408 ResultSet rs = stmt.executeQuery(query.toString()); 409 410 ResponseOption responseOption = new ResponseOption(); 411 responseOption.setReturnComposedObjects(false); 412 413 while (rs.next()) { 414 Association oldAssoc = new Association(); 415 loadObjectFromResultSet(connection, oldAssoc, rs, responseOption, new ArrayList()); 416 417 String id = oldAssoc.getId(); 418 Association newAssoc = (Association)idToNewAssocMap.get(id); 419 420 boolean isConfirmedBySourceOwner = false; 421 boolean isConfirmedByTargetOwner = false; 422 if (rs.getString("isConfirmedBySourceOwner").equals("T")) { 423 isConfirmedBySourceOwner = true; 424 } 425 if (rs.getString("isConfirmedByTargetOwner").equals("T")) { 426 isConfirmedByTargetOwner = true; 427 } 428 429 if (isExtramuralAssociationConfirmation(isConfirmedBySourceOwner, isConfirmedByTargetOwner 430 , oldAssoc, newAssoc)) { 431 confirmAssociation(user, connection, oldAssoc, newAssoc); 432 433 //Remove from original list so that it does not get inserted later 434 associations.remove(newAssoc); 435 } 436 } 437 438 stmt.close(); 439 440 441 } 442 443 444 445 private void confirmAssociation(User user, java.sql.Connection connection, Association oldAssoc, Association newAssoc) throws RegistryException, SQLException { 446 String sourceId = bu.getObjectId(oldAssoc.getSourceObject()); 447 String targetId = bu.getObjectId(oldAssoc.getTargetObject()); 448 449 User sourceOwner = null; 450 User targetOwner = null; 451 452 try { 453 sourceOwner = RegistryObjectDAO.getOwner(connection, sourceId); 454 } 455 catch (OwnerNotFoundException e) { 456 sourceOwner = user; //??Dangerous assumption. Should look in submitted objects to make sure. 457 } 458 459 try { 460 targetOwner = RegistryObjectDAO.getOwner(connection, targetId); 461 } 462 catch (OwnerNotFoundException e) { 463 targetOwner = user; //??Dangerous assumption. Should look in submitted objects to make sure. 464 } 465 466 String userId = user.getId(); 467 468 Statement stmt = connection.createStatement(); 469 if (userId.equals(sourceOwner.getId())) { 470 stmt.executeUpdate("UPDATE Association SET isConfirmedBySourceOwner='T' WHERE id = '" + oldAssoc.getId() + "' "); 471 } 472 else if (userId.equals(targetOwner.getId())) { 473 stmt.executeUpdate("UPDATE Association SET isConfirmedByTargetOwner='T' WHERE id = '" + oldAssoc.getId() + "' "); 474 } 475 else { 476 throw new RegistryException("User with id " + userId + " cannot confirm association with id " + oldAssoc.getId()); 477 } 478 479 stmt.close(); 480 } 481 482 private boolean isExtramuralAssociationConfirmation(boolean isConfirmedBySourceOwner, boolean isConfirmedByTargetOwner 483 , Association oldAssoc, Association newAssoc) throws RegistryException { 484 boolean extramuralConfirm = false; 485 486 String oldSourceId = bu.getObjectId(oldAssoc.getSourceObject()); 487 String oldTargetId = bu.getObjectId(oldAssoc.getTargetObject()); 488 String newSourceId = bu.getObjectId(newAssoc.getSourceObject()); 489 String newTargetId = bu.getObjectId(newAssoc.getTargetObject()); 490 491 //match on following 3 attributes is enough, and really not yet confirmed 492 if ((oldAssoc.getAssociationType().equals(newAssoc.getAssociationType())) && 493 (oldSourceId.equals(newSourceId)) && 494 (oldTargetId.equals(newTargetId)) && 495 (!isConfirmedBySourceOwner || !isConfirmedByTargetOwner) 496 ) { 497 498 extramuralConfirm = true; 499 } 500 501 return extramuralConfirm; 502 } 503 504 505 public void loadObjectFromResultSet(java.sql.Connection conn, Object obj, ResultSet rs, ResponseOption responseOption, ArrayList objectRefs) throws RegistryException { 506 try { 507 if (!(obj instanceof org.oasis.ebxml.registry.bindings.rim.Association)) { 508 throw new RegistryException("Unexpected object " + obj + ". Was expecting org.oasis.ebxml.registry.bindings.rim.Association."); 509 } 510 511 Association ass = (Association)obj; 512 513 super.loadObjectFromResultSet(conn, ass, rs, responseOption, objectRefs); 514 String associationType = rs.getString("associationType"); 515 ass.setAssociationType(associationType); 516 517 String sourceObjectId = rs.getString("sourceObject"); 518 ObjectRef so = new ObjectRef(); 519 objectRefs.add(so); 520 so.setId(sourceObjectId); 521 ass.setSourceObject(so); 522 523 String targetObjectId = rs.getString("targetObject"); 524 ObjectRef to = new ObjectRef(); 525 objectRefs.add(to); 526 to.setId(targetObjectId); 527 ass.setTargetObject(to); 528 } 529 catch (SQLException e) { 530 e.printStackTrace(); 531 throw new RegistryException(e); 532 } 533 } 534 535 public ArrayList getLeafObjectList(java.sql.Connection conn, ResultSet rs, ResponseOption responseOption, ArrayList objectRefs) throws RegistryException { 536 ArrayList res = new ArrayList(); 537 538 try { 539 while(rs.next()) { 540 Association obj = new Association(); 541 loadObjectFromResultSet(conn, obj, rs, responseOption, objectRefs); 542 543 res.add(obj); 544 } 545 } 546 catch (SQLException e) { 547 e.printStackTrace(); 548 throw new RegistryException(e); 549 } 550 551 552 return res; 553 } 554 } 555 556 557 558

This page was automatically generated by Maven