View Javadoc
1 /* 2 * $Header: /cvsroot/ebxmlrr/ebxmlrr/src/share/com/sun/ebxml/registry/persistence/rdb/ClassificationSchemeDAO.java,v 1.40 2003/05/30 22:42:35 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.LifeCycleManagerImpl; 15 import com.sun.ebxml.registry.lcm.ObjectsNotFoundException; 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.ClassificationNode; 22 import org.oasis.ebxml.registry.bindings.rim.ClassificationScheme; 23 import org.oasis.ebxml.registry.bindings.rim.User; 24 import org.oasis.ebxml.registry.bindings.rim.types.NodeTypeType; 25 import org.oasis.ebxml.registry.bindings.rim.types.StabilityType; 26 import org.oasis.ebxml.registry.bindings.rim.types.StatusType; 27 28 import java.sql.ResultSet; 29 import java.sql.SQLException; 30 import java.sql.Statement; 31 import java.sql.Timestamp; 32 33 import java.util.ArrayList; 34 import java.util.HashMap; 35 import java.util.Iterator; 36 37 38 public class ClassificationSchemeDAO extends RegistryEntryDAO { 39 static BindingUtility bu = BindingUtility.getInstance(); 40 41 protected ClassificationSchemeDAO() { 42 } 43 44 public static String getTableNameStatic() { 45 return "ClassScheme"; 46 } 47 48 public String getTableName() { 49 return getTableNameStatic(); 50 } 51 52 /*** 53 * Does a bulk insert of a Collection of objects that match the type for this persister. 54 * 55 */ 56 public void insert(User user, java.sql.Connection connection, 57 ArrayList schemes) throws RegistryException { 58 if (schemes.size() == 0) { 59 return; 60 } 61 62 String schemeId = null; 63 Statement stmt = null; 64 65 try { 66 // Update the exisitng ClassificationScheme 67 schemes = updateExistingObjects(user, connection, schemes); 68 69 if (schemes.size() == 0) { 70 return; 71 } 72 73 System.err.println("Inserting " + schemes.size() + 74 " ClassificationSchemes"); 75 76 stmt = connection.createStatement(); 77 78 Iterator iter = schemes.iterator(); 79 ClassificationNodeDAO classificationNodeDAO = new ClassificationNodeDAO(); 80 81 ArrayList nestedNodes = new ArrayList(); 82 83 while (iter.hasNext()) { 84 ClassificationScheme scheme = (ClassificationScheme) iter.next(); 85 schemeId = scheme.getId(); 86 87 //Log.print(Log.TRACE, 8, "\tDATABASE EVENT: storing ClassificationScheme " ); 88 java.util.Date expiration = scheme.getExpiration(); 89 String expirationAsString = null; 90 91 if (expiration != null) { 92 expirationAsString = "'" + 93 (new Timestamp(expiration.getTime())) + "'"; 94 95 //??The timestamp is being truncated to work around a bug in PostgreSQL 7.2.2 JDBC driver 96 //expirationAsString = expirationAsString.substring(0,19); 97 } 98 99 int majorVersion = scheme.getMajorVersion(); 100 int minorVersion = scheme.getMinorVersion(); 101 StabilityType s = scheme.getStability(); 102 String stability = null; 103 104 if (s != null) { 105 stability = "'" + s + "'"; 106 } 107 108 StatusType st = scheme.getStatus(); 109 String status = "'Submitted'"; 110 111 if (st != null) { 112 status = "'" + st + "'"; 113 } 114 115 String userVersion = scheme.getUserVersion(); 116 117 if (userVersion != null) { 118 userVersion = "'" + userVersion + "'"; 119 } 120 121 String isInternal = "'F'"; 122 123 if (scheme.getIsInternal()) { 124 isInternal = "'T'"; 125 } else { 126 isInternal = "'F'"; 127 } 128 129 String nodeType = scheme.getNodeType().toString(); 130 131 if (nodeType == null) { 132 nodeType = "UniqueCode"; 133 } 134 135 String str = "INSERT INTO " + getTableName() + 136 " (accessControlPolicy" + ", id" + ", objectType" + 137 ", expiration" + ", majorVersion" + ", minorVersion" + 138 ", stability" + ", status" + ", userVersion" + 139 ", isInternal" + ", nodeType)"; 140 str += (" VALUES (null, " + //AccessControlPolicy 141 "'" + schemeId + "', " + "'ClassificationScheme', " + 142 expirationAsString + ", " + "1, " + "0, " + stability + ", " + 143 status + ", " + userVersion + ", " + isInternal + ", " + "'" + 144 nodeType + "')"); 145 146 System.err.println("stmt = " + str); 147 stmt.addBatch(str); 148 149 // Get the nested nodes 150 ClassificationNode[] nodes = scheme.getClassificationNode(); 151 152 for (int i = 0; i < nodes.length; i++) { 153 nestedNodes.add(nodes[i]); 154 } 155 } 156 157 if (schemes.size() > 0) { 158 int[] updateCounts = stmt.executeBatch(); 159 //stmt.close(); 160 super.insert(user, connection, schemes); 161 162 // Now generate the AE for inserting ClassificationScheme 163 generateAuditbleEvent(connection, schemes, "Created", user); 164 165 // Insert the nested nodes 166 classificationNodeDAO.insert(user, connection, nestedNodes); 167 } 168 } catch (java.sql.SQLException e) { 169 e.printStackTrace(); 170 171 RegistryException exception = new RegistryException(e); 172 throw exception; 173 } finally { 174 try { 175 if (stmt != null) 176 stmt.close(); 177 } catch (SQLException sqle) { 178 sqle.printStackTrace(); 179 } 180 } 181 182 } 183 184 public void update(User user, java.sql.Connection connection, 185 ArrayList schemes) 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, schemes, true, false); 191 } 192 193 public void update(User user, java.sql.Connection connection, 194 ArrayList schemes, boolean checkExistence, boolean checkAuthorization) 195 throws RegistryException { 196 if (schemes.size() == 0) { 197 return; 198 } 199 200 System.err.println("Updating " + schemes.size() + 201 " ClassificationSchemes"); 202 203 String schemeId = null; 204 Statement stmt = null; 205 206 try { 207 // Check whether the user can update the objects 208 if (checkAuthorization) { 209 AuthorizationServiceImpl authorization = AuthorizationServiceImpl.getInstance(); 210 authorization.checkAuthorization(user, 211 bu.getIdsFromRegistryObjects(schemes), 212 AuthorizationServiceImpl.UPDATE_OBJECTS_REQUEST); 213 } 214 215 stmt = connection.createStatement(); 216 Iterator iter = schemes.iterator(); 217 ClassificationNodeDAO classificationNodeDAO = new ClassificationNodeDAO(); 218 219 //BindingUtility bindingUtil = bu; 220 HashMap idMap = new HashMap(); 221 ArrayList schemesIds = new ArrayList(); 222 ArrayList nestedNodes = new ArrayList(); 223 224 while (iter.hasNext()) { 225 ClassificationScheme scheme = (ClassificationScheme) iter.next(); 226 schemeId = scheme.getId(); 227 228 // checking whether it exists 229 if (checkExistence && 230 !registryObjectExist(connection, schemeId, 231 getTableName())) { 232 ArrayList idList = new ArrayList(); 233 idList.add(schemeId); 234 throw new ObjectsNotFoundException(idList); 235 } 236 237 java.util.Date expiration = scheme.getExpiration(); 238 String expirationAsString = null; 239 240 if (expiration != null) { 241 expirationAsString = "'" + 242 (new Timestamp(expiration.getTime())) + "'"; 243 } 244 245 int majorVersion = scheme.getMajorVersion(); 246 int minorVersion = scheme.getMinorVersion(); 247 StabilityType s = scheme.getStability(); 248 String stability = null; 249 250 if (s != null) { 251 stability = "'" + s + "'"; 252 } 253 254 StatusType st = scheme.getStatus(); 255 String status = "'Submitted'"; 256 257 if (st != null) { 258 status = "'" + st + "'"; 259 } 260 261 String userVersion = scheme.getUserVersion(); 262 263 if (userVersion != null) { 264 userVersion = "'" + userVersion + "'"; 265 } 266 267 String str = "UPDATE " + getTableName() + 268 " SET accessControlPolicy=null" + ", expiration=" + 269 expirationAsString + ", stability=" + stability + 270 ", userVersion=" + userVersion + " WHERE id='" + schemeId + 271 "'"; 272 273 System.err.println("stmt = " + str); 274 stmt.addBatch(str); 275 276 schemesIds.add(schemeId); 277 278 ClassificationNode[] nodes = scheme.getClassificationNode(); 279 280 for (int i = 0; i < nodes.length; i++) { 281 nestedNodes.add(nodes[i]); 282 } 283 } 284 // end looping schemes 285 286 if (schemes.size() > 0) { 287 int[] updateCounts = stmt.executeBatch(); 288 //stmt.close(); 289 290 // Updated composed Name, Descrption, Slot, ExternalIdentifier and Classification 291 super.update(user, connection, schemes); 292 293 // Now generate the AE for updating ClassificationScheme 294 generateAuditbleEvent(connection, schemes, "Updated", user); 295 296 // Delete the composed ClassificationNode that was submitted 297 classificationNodeDAO.deleteByParentIds(user, connection, 298 schemesIds); 299 300 // Now insert the composed ClassificationNode 301 LifeCycleManagerImpl lcm = LifeCycleManagerImpl.getInstance(); 302 lcm.fixTemporaryIds(nestedNodes, idMap); 303 304 if (nestedNodes.size() > 0) { 305 classificationNodeDAO.insert(user, connection, nestedNodes); 306 } 307 } 308 } catch (java.sql.SQLException e) { 309 e.printStackTrace(); 310 311 RegistryException exception = new RegistryException(e); 312 throw exception; 313 } finally { 314 try { 315 if (stmt != null) 316 stmt.close(); 317 } catch (SQLException sqle) { 318 sqle.printStackTrace(); 319 } 320 } 321 322 } 323 324 // Should we make index on ClassifcationScheme in Classification???? 325 protected String checkClassificationReferences(java.sql.Connection conn, 326 String schemeId) throws RegistryException { 327 String classId = null; 328 Statement stmt = null; 329 330 try { 331 stmt = conn.createStatement(); 332 String sql = "SELECT id FROM Classification WHERE " + 333 "classificationScheme='" + schemeId + 334 "' AND classificationScheme IS NOT NULL"; 335 ResultSet rs = stmt.executeQuery(sql); 336 337 if (rs.next()) { 338 classId = rs.getString("id"); 339 } 340 341 //stmt.close(); 342 343 return classId; 344 } catch (SQLException e) { 345 throw new RegistryException(e); 346 } finally { 347 try { 348 if (stmt != null) 349 stmt.close(); 350 } catch (SQLException sqle) { 351 sqle.printStackTrace(); 352 } 353 } 354 355 } 356 357 // Should we make index on parent in ClassificationNode???? 358 protected String checkClassificationNodeReferences( 359 java.sql.Connection conn, String schemeId) throws RegistryException { 360 String nodeId = null; 361 Statement stmt = null; 362 363 try { 364 stmt = conn.createStatement(); 365 String sql = "SELECT id FROM ClassificationNode WHERE " + 366 "parent='" + schemeId + "' AND parent IS NOT NULL"; 367 ResultSet rs = stmt.executeQuery(sql); 368 369 if (rs.next()) { 370 nodeId = rs.getString("id"); 371 } 372 373 //stmt.close(); 374 375 return nodeId; 376 } catch (SQLException e) { 377 throw new RegistryException(e); 378 } finally { 379 try { 380 if (stmt != null) 381 stmt.close(); 382 } catch (SQLException sqle) { 383 sqle.printStackTrace(); 384 } 385 } 386 387 } 388 389 // Should we make index on identificationScheme in ExternalIdentifier???? 390 protected String checkExternalIdentifierReferences( 391 java.sql.Connection conn, String schemeId) throws RegistryException { 392 String extIdId = null; 393 Statement stmt = null; 394 395 try { 396 stmt = conn.createStatement(); 397 String sql = "SELECT id FROM ExternalIdentifier WHERE " + 398 "identificationScheme='" + schemeId + "'"; 399 ResultSet rs = stmt.executeQuery(sql); 400 401 if (rs.next()) { 402 extIdId = rs.getString("id"); 403 } 404 405 //stmt.close(); 406 407 return extIdId; 408 } catch (SQLException e) { 409 throw new RegistryException(e); 410 } finally { 411 try { 412 if (stmt != null) 413 stmt.close(); 414 } catch (SQLException sqle) { 415 sqle.printStackTrace(); 416 } 417 } 418 419 } 420 421 public void delete(User user, java.sql.Connection connection, 422 ArrayList schemesIds) throws RegistryException { 423 Iterator iter = schemesIds.iterator(); 424 425 // Check whether this ClassificationScheme is used in any Classification 426 while (iter.hasNext()) { 427 String schemeId = (String) iter.next(); 428 String classId = checkClassificationReferences(connection, schemeId); 429 430 if (classId != null) { 431 throw new ReferencesExistException("Classification " + classId + 432 " is referencing " + schemeId); 433 } 434 435 String nodeId = checkClassificationNodeReferences(connection, 436 schemeId); 437 438 if (nodeId != null) { 439 throw new ReferencesExistException("ClassificationNode " + 440 nodeId + " is referencing " + schemeId); 441 } 442 443 String extIdId = checkExternalIdentifierReferences(connection, 444 schemeId); 445 446 if (nodeId != null) { 447 throw new ReferencesExistException("ExternalIdentifier " + 448 extIdId + " is referencing " + schemeId); 449 } 450 } 451 452 super.delete(user, connection, schemesIds); 453 } 454 455 public void loadObjectFromResultSet(java.sql.Connection conn, Object obj, 456 ResultSet rs, ResponseOption responseOption, ArrayList objectRefs) 457 throws RegistryException { 458 try { 459 if (!(obj instanceof org.oasis.ebxml.registry.bindings.rim.ClassificationScheme)) { 460 throw new RegistryException("Unexpected object " + obj + 461 ". Was expecting org.oasis.ebxml.registry.bindings.rim.ClassificationScheme."); 462 } 463 464 ClassificationScheme scheme = (ClassificationScheme) obj; 465 super.loadObjectFromResultSet(conn, scheme, rs, responseOption, 466 objectRefs); 467 468 String isInternal = rs.getString("isInternal"); 469 470 if (isInternal.equals("T")) { 471 scheme.setIsInternal(true); 472 } else { 473 scheme.setIsInternal(false); 474 } 475 476 String nodeType = rs.getString("nodeType"); 477 scheme.setNodeType(NodeTypeType.valueOf(nodeType)); 478 } catch (SQLException e) { 479 e.printStackTrace(); 480 throw new RegistryException(e); 481 } 482 } 483 484 public ArrayList getLeafObjectList(java.sql.Connection conn, ResultSet rs, 485 ResponseOption responseOption, ArrayList objectRefs) 486 throws RegistryException { 487 ArrayList res = new ArrayList(); 488 489 try { 490 while (rs.next()) { 491 ClassificationScheme obj = new ClassificationScheme(); 492 loadObjectFromResultSet(conn, obj, rs, responseOption, 493 objectRefs); 494 495 res.add(obj); 496 } 497 } catch (SQLException e) { 498 e.printStackTrace(); 499 throw new RegistryException(e); 500 } 501 502 return res; 503 } 504 }

This page was automatically generated by Maven