View Javadoc
1 /* 2 * $Header: /cvsroot/ebxmlrr/ebxmlrr/src/share/com/sun/ebxml/registry/persistence/rdb/ExtrinsicObjectDAO.java,v 1.42 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.ReferencesExistException; 16 import com.sun.ebxml.registry.security.authorization.AuthorizationServiceImpl; 17 import com.sun.ebxml.registry.util.BindingUtility; 18 19 import org.oasis.ebxml.registry.bindings.query.ResponseOption; 20 import org.oasis.ebxml.registry.bindings.rim.ExtrinsicObject; 21 import org.oasis.ebxml.registry.bindings.rim.User; 22 import org.oasis.ebxml.registry.bindings.rim.types.StabilityType; 23 import org.oasis.ebxml.registry.bindings.rim.types.StatusType; 24 25 import java.sql.ResultSet; 26 import java.sql.SQLException; 27 import java.sql.Statement; 28 import java.sql.Timestamp; 29 30 import java.util.ArrayList; 31 import java.util.Iterator; 32 33 34 public class ExtrinsicObjectDAO extends RegistryEntryDAO { 35 static BindingUtility bu = BindingUtility.getInstance(); 36 37 protected ExtrinsicObjectDAO() { 38 } 39 40 public static String getTableNameStatic() { 41 return "ExtrinsicObject"; 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 * 51 */ 52 public void insert(User user, java.sql.Connection connection, 53 ArrayList extrinsicObjects) throws RegistryException { 54 if (extrinsicObjects.size() == 0) { 55 return; 56 } 57 58 String extrinsicObjectId = null; 59 Statement stmt = null; 60 61 try { 62 // Update the exisitng ExtrinsicObject 63 extrinsicObjects = updateExistingObjects(user, connection, 64 extrinsicObjects); 65 66 if (extrinsicObjects.size() == 0) { 67 return; 68 } 69 70 System.err.println("Inserting " + extrinsicObjects.size() + 71 " ExtrinsicObjects"); 72 73 stmt = connection.createStatement(); 74 75 Iterator iter = extrinsicObjects.iterator(); 76 77 while (iter.hasNext()) { 78 ExtrinsicObject extrinsicObject = (ExtrinsicObject) iter.next(); 79 extrinsicObjectId = extrinsicObject.getId(); 80 81 //Log.print(Log.TRACE, 8, "\tDATABASE EVENT: storing ExtrinsicObject " ); 82 java.util.Date expiration = extrinsicObject.getExpiration(); 83 String expirationAsString = null; 84 85 if (expiration != null) { 86 expirationAsString = "'" + 87 (new Timestamp(expiration.getTime())) + "'"; 88 89 //??The timestamp is being truncated to work around a bug in PostgreSQL 7.2.2 JDBC driver 90 expirationAsString = expirationAsString.substring(0, 19); 91 } 92 93 int majorVersion = extrinsicObject.getMajorVersion(); 94 int minorVersion = extrinsicObject.getMinorVersion(); 95 96 StabilityType s = extrinsicObject.getStability(); 97 String stability = null; 98 99 if (s != null) { 100 stability = "'" + s + "'"; 101 } 102 103 StatusType st = extrinsicObject.getStatus(); 104 String status = "'Submitted'"; 105 106 if (st != null) { 107 status = "'" + st + "'"; 108 } 109 110 String userVersion = extrinsicObject.getUserVersion(); 111 112 if (userVersion != null) { 113 userVersion = "'" + userVersion + "'"; 114 } 115 116 String isOpaque; 117 118 if (extrinsicObject.getIsOpaque()) { 119 isOpaque = "'T'"; 120 } else { 121 isOpaque = "'F'"; 122 } 123 124 String mimeType = extrinsicObject.getMimeType(); 125 126 if (mimeType == null) { 127 mimeType = "application/octet-stream"; 128 } 129 130 String objectType = extrinsicObject.getObjectType(); 131 132 if (objectType == null) { 133 objectType = "Unknown"; 134 } 135 136 String str = "INSERT INTO ExtrinsicObject " + 137 " (accessControlPolicy" + ", id" + ", objectType" + 138 ", expiration" + ", majorVersion" + ", minorVersion" + 139 ", stability" + ", userVersion" + ", isOpaque" + 140 ", mimeType" + ", status)"; 141 str += (" VALUES (null, " + //AccessControlPolicy 142 "'" + extrinsicObject.getId() + "', " + "'" + objectType + 143 "', " + expirationAsString + ", " + majorVersion + ", " + 144 minorVersion + ", " + stability + ", " + userVersion + ", " + 145 isOpaque + ", " + "'" + mimeType + "', " + status + ")"); 146 147 System.err.println("stmt = " + str); 148 stmt.addBatch(str); 149 } 150 // end looping extrinsicObjects 151 152 if (extrinsicObjects.size() > 0) { 153 int[] updateCounts = stmt.executeBatch(); 154 //stmt.close(); 155 super.insert(user, connection, extrinsicObjects); 156 157 // Now generate the AE for inserting ExtrinsicObject 158 generateAuditbleEvent(connection, extrinsicObjects, "Created", 159 user); 160 } 161 } catch (java.sql.SQLException e) { 162 e.printStackTrace(); 163 164 RegistryException exception = new RegistryException(e); 165 throw exception; 166 } finally { 167 try { 168 if (stmt != null) 169 stmt.close(); 170 } catch (SQLException sqle) { 171 sqle.printStackTrace(); 172 } 173 } 174 } 175 176 public void update(User user, java.sql.Connection connection, 177 ArrayList extrinsicObjects) throws RegistryException { 178 /* 179 We do not need to check whether the user is authorized to update because the authorization is expected to be checked by higher level. 180 Should we move the authorization to persistance layer?????? 181 */ 182 update(user, connection, extrinsicObjects, true, false); 183 } 184 185 /*** 186 Does a bulk update of ArrayList of extrinsicObjects. 187 */ 188 public void update(User user, java.sql.Connection connection, 189 ArrayList extrinsicObjects, boolean checkExistence, 190 boolean checkAuthorization) throws RegistryException { 191 if (extrinsicObjects.size() == 0) { 192 return; 193 } 194 195 System.err.println("Updating " + extrinsicObjects.size() + 196 " ExtrinsicObjects"); 197 198 String extrinsicObjectId = null; 199 Statement stmt = null; 200 201 try { 202 // Check whether the user can update the objects 203 if (checkAuthorization) { 204 AuthorizationServiceImpl authorization = AuthorizationServiceImpl.getInstance(); 205 authorization.checkAuthorization(user, 206 bu.getIdsFromRegistryObjects(extrinsicObjects), 207 AuthorizationServiceImpl.UPDATE_OBJECTS_REQUEST); 208 } 209 210 stmt = connection.createStatement(); 211 Iterator iter = extrinsicObjects.iterator(); 212 213 while (iter.hasNext()) { 214 ExtrinsicObject extrinsicObject = (ExtrinsicObject) iter.next(); 215 extrinsicObjectId = extrinsicObject.getId(); 216 217 //Log.print(Log.TRACE, 8, "\tDATABASE EVENT: storing ExtrinsicObject " ); 218 // checking whether it exists 219 if (checkExistence && 220 !registryObjectExist(connection, extrinsicObjectId, 221 getTableName())) { 222 ArrayList idList = new ArrayList(); 223 idList.add(extrinsicObjectId); 224 throw new ObjectsNotFoundException(idList); 225 } 226 227 java.util.Date expiration = extrinsicObject.getExpiration(); 228 String expirationAsString = null; 229 230 if (expiration != null) { 231 expirationAsString = "'" + 232 (new Timestamp(expiration.getTime())) + "'"; 233 } 234 235 int majorVersion = extrinsicObject.getMajorVersion(); 236 int minorVersion = extrinsicObject.getMinorVersion(); 237 238 StabilityType s = extrinsicObject.getStability(); 239 String stability = null; 240 241 if (s != null) { 242 stability = "'" + s + "'"; 243 } 244 245 StatusType st = extrinsicObject.getStatus(); 246 String status = "'Submitted'"; 247 248 if (st != null) { 249 status = "'" + st + "'"; 250 } 251 252 String userVersion = extrinsicObject.getUserVersion(); 253 254 if (userVersion != null) { 255 userVersion = "'" + userVersion + "'"; 256 } 257 258 String objectType = extrinsicObject.getObjectType(); 259 260 if (objectType == null) { 261 objectType = "Unknown"; 262 } 263 264 // isOpaque and mimeType is non-mutable 265 String str = "UPDATE " + getTableName() + 266 " SET accessControlPolicy=null" + ", expiration=" + 267 expirationAsString + ", stability=" + stability + 268 ", userVersion=" + userVersion + ", objectType='" + 269 objectType + "'"; 270 str += (" WHERE id='" + extrinsicObjectId + "'"); 271 272 System.err.println("stmt = " + str); 273 stmt.addBatch(str); 274 } 275 // end looping extrinsicObjects 276 277 if (extrinsicObjects.size() > 0) { 278 int[] updateCounts = stmt.executeBatch(); 279 //stmt.close(); 280 281 // Updated composed Name, Descrption, Slot, ExternalIdentifier and Classification 282 super.update(user, connection, extrinsicObjects); 283 284 // Now generate the AE for updated ExtrinsicObject 285 generateAuditbleEvent(connection, extrinsicObjects, "Updated", 286 user); 287 } 288 } catch (java.sql.SQLException e) { 289 e.printStackTrace(); 290 291 RegistryException exception = new RegistryException(e); 292 throw exception; 293 } finally { 294 try { 295 if (stmt != null) 296 stmt.close(); 297 } catch (SQLException sqle) { 298 sqle.printStackTrace(); 299 } 300 } 301 } 302 303 // Should we make index on specificationObject in SpecificationLink???? 304 protected String checkSpecLinkReferences(java.sql.Connection conn, 305 String extObjectId) throws RegistryException { 306 String specLinkId = null; 307 Statement stmt = null; 308 309 try { 310 stmt = conn.createStatement(); 311 String sql = "SELECT id FROM SpecificationLink WHERE " + 312 "specificationObject='" + extObjectId + "'"; 313 ResultSet rs = stmt.executeQuery(sql); 314 315 if (rs.next()) { 316 specLinkId = rs.getString("id"); 317 } 318 319 //stmt.close(); 320 321 return specLinkId; 322 } catch (SQLException e) { 323 throw new RegistryException(e); 324 } finally { 325 try { 326 if (stmt != null) 327 stmt.close(); 328 } catch (SQLException sqle) { 329 sqle.printStackTrace(); 330 } 331 } 332 } 333 334 /*** 335 * Does a bulk delete of a Collection of objects that match the type for this persister. 336 * 337 */ 338 public void delete(User user, java.sql.Connection connection, 339 ArrayList extObjectsIds) throws RegistryException { 340 Iterator iter = extObjectsIds.iterator(); 341 342 // Check whether this ExtrinsicObject is specificationObject of any SpecificationLink 343 while (iter.hasNext()) { 344 String extObjectId = (String) iter.next(); 345 String specLinkId = checkSpecLinkReferences(connection, extObjectId); 346 347 if (specLinkId != null) { 348 throw new ReferencesExistException("SpecificationLink " + 349 specLinkId + " is referencing " + extObjectId); 350 } 351 } 352 353 super.delete(user, connection, extObjectsIds); 354 } 355 356 public void loadObjectFromResultSet(java.sql.Connection conn, Object obj, 357 ResultSet rs, ResponseOption responseOption, ArrayList objectRefs) 358 throws RegistryException { 359 try { 360 if (!(obj instanceof org.oasis.ebxml.registry.bindings.rim.ExtrinsicObject)) { 361 throw new RegistryException("Unexpected object " + obj + 362 ". Was expecting org.oasis.ebxml.registry.bindings.rim.ExtrinsicObject."); 363 } 364 365 ExtrinsicObject eo = (ExtrinsicObject) obj; 366 super.loadObjectFromResultSet(conn, obj, rs, responseOption, 367 objectRefs); 368 369 String isOpaque = rs.getString("isOpaque"); 370 371 if (isOpaque.equals("T")) { 372 eo.setIsOpaque(true); 373 } else { 374 eo.setIsOpaque(false); 375 } 376 377 String mimeType = rs.getString("mimeType"); 378 eo.setMimeType(mimeType); 379 } catch (SQLException e) { 380 e.printStackTrace(); 381 throw new RegistryException(e); 382 } 383 } 384 385 public ArrayList getLeafObjectList(java.sql.Connection conn, ResultSet rs, 386 ResponseOption responseOption, ArrayList objectRefs) 387 throws RegistryException { 388 ArrayList res = new ArrayList(); 389 390 try { 391 while (rs.next()) { 392 ExtrinsicObject obj = new ExtrinsicObject(); 393 loadObjectFromResultSet(conn, obj, rs, responseOption, 394 objectRefs); 395 396 res.add(obj); 397 } 398 } catch (SQLException e) { 399 e.printStackTrace(); 400 throw new RegistryException(e); 401 } 402 403 return res; 404 } 405 406 /*** 407 Return the ArrayList of id of objects which are not ExtrinsicObject 408 @deprecated Originally it is used by SQLPersistenceManagerImpl. Now no methods 409 use it. 410 */ 411 public ArrayList extrinsicObjectsExist(java.sql.Connection connection, 412 ArrayList objectsIds) throws RegistryException { 413 ArrayList notExist = new ArrayList(); 414 Iterator iter = objectsIds.iterator(); 415 Statement stmt = null; 416 417 try { 418 stmt = connection.createStatement(); 419 420 while (iter.hasNext()) { 421 String id = (String) iter.next(); 422 ResultSet rs = stmt.executeQuery("select id from " + 423 getTableName() + " where id = '" + id + "'"); 424 425 if (!rs.next()) { 426 notExist.add(id); 427 } 428 } 429 430 return notExist; 431 } catch (SQLException e) { 432 e.printStackTrace(); 433 throw new RegistryException(e); 434 } finally { 435 try { 436 if (stmt != null) 437 stmt.close(); 438 } catch (SQLException sqle) { 439 sqle.printStackTrace(); 440 } 441 } 442 } 443 }

This page was automatically generated by Maven