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.ResultSet; 14 import java.sql.SQLException; 15 import java.sql.Statement; 16 import java.sql.Timestamp; 17 import java.util.ArrayList; 18 import java.util.Iterator; 19 20 import org.oasis.ebxml.registry.bindings.query.ResponseOption; 21 import org.oasis.ebxml.registry.bindings.rim.Association; 22 import org.oasis.ebxml.registry.bindings.rim.RegistryObjectList; 23 import org.oasis.ebxml.registry.bindings.rim.RegistryObjectType; 24 import org.oasis.ebxml.registry.bindings.rim.RegistryPackage; 25 import org.oasis.ebxml.registry.bindings.rim.User; 26 import org.oasis.ebxml.registry.bindings.rim.types.StabilityType; 27 import org.oasis.ebxml.registry.bindings.rim.types.StatusType; 28 29 import com.sun.ebxml.registry.RegistryException; 30 import com.sun.ebxml.registry.lcm.ObjectsNotFoundException; 31 import com.sun.ebxml.registry.security.authorization.AuthorizationServiceImpl; 32 import com.sun.ebxml.registry.util.BindingUtility; 33 import com.sun.ebxml.registry.util.UUIDFactory; 34 35 36 /*** 37 * Package instances are RegistryEntries that group logically related 38 * RegistryEntries together. One use of a Package is to allow operations to 39 * be performed on an entire package of objects. For example all objects belonging 40 * to a Package may be deleted in a single request. 41 * 42 * <p><DL><DT><B>Capability Level: 1 </B><DD>This interface is required to be implemented by JAXR Providers at or above capability level 1.</DL> 43 * 44 * 45 */ 46 public class RegistryPackageDAO extends RegistryEntryDAO { 47 protected RegistryPackageDAO(){} 48 49 static BindingUtility bu = BindingUtility.getInstance(); 50 51 public static String getTableNameStatic() { 52 return "RegistryPackage"; 53 } 54 55 public String getTableName() { 56 return getTableNameStatic(); 57 } 58 /*** 59 * Does a bulk insert of a Collection of objects that match the type for this persister. 60 * 61 */ 62 public void insert(User user, java.sql.Connection connection, ArrayList pkgs) 63 throws RegistryException { 64 if (pkgs.size()==0) { 65 return; 66 } 67 String pkgId = null; 68 // For inserting the Association of associationType = "HasMembers" 69 AssociationDAO assDAO = new AssociationDAO(); 70 // It stores all the composed objects of all the RegistryPackages 71 ArrayList composedObjects = new ArrayList(); 72 // It stores all the associations between the RegistryPackages 73 ArrayList associations = new ArrayList(); 74 Statement stmt = null; 75 try { 76 // Update the exisitng RegistryPackage 77 pkgs = updateExistingObjects(user, connection , pkgs); 78 if (pkgs.size()==0) { 79 return; 80 } 81 82 System.err.println("Inserting " + pkgs.size() + " RegistryPackages"); 83 84 stmt = connection.createStatement(); 85 86 Iterator iter = pkgs.iterator(); 87 while (iter.hasNext()) { 88 RegistryPackage pkg = (RegistryPackage)iter.next(); 89 pkgId = pkg.getId(); 90 //Log.print(Log.TRACE, 8, "\tDATABASE EVENT: storing RegistryPackage " ); 91 92 java.util.Date expiration = pkg.getExpiration(); 93 String expirationAsString = null; 94 if (expiration != null) { 95 expirationAsString = "'" + (new Timestamp(expiration.getTime())) + "'"; 96 //??The timestamp is being truncated to work around a bug in PostgreSQL 7.2.2 JDBC driver 97 expirationAsString = expirationAsString.substring(0,19); 98 } 99 100 int majorVersion = pkg.getMajorVersion(); 101 int minorVersion = pkg.getMinorVersion(); 102 103 StabilityType s = pkg.getStability(); 104 String stability = null; 105 if (s != null) { 106 stability = "'" + s + "'"; 107 } 108 109 StatusType st = pkg.getStatus(); 110 String status = "'Submitted'"; 111 if (st != null) { 112 status = "'" + st + "'"; 113 } 114 115 String userVersion = pkg.getUserVersion(); 116 if (userVersion != null) { 117 userVersion = "'" + userVersion + "'"; 118 } 119 120 String str = "INSERT INTO " + getTableName() + 121 " (accessControlPolicy" + 122 ", id" + 123 ", objectType" + 124 ", expiration" + 125 ", majorVersion" + 126 ", minorVersion" + 127 ", stability" + 128 ", status" + 129 ", userVersion)"; 130 str += " VALUES (null, " + //AccessControlPolicy 131 "'" + pkgId + "', " + 132 "'RegistryPackage', " + 133 expirationAsString + ", " + 134 majorVersion + ", " + 135 minorVersion + ", " + 136 stability + ", " + 137 status + ", " + 138 userVersion + ")"; 139 140 System.err.println("stmt = '" + str + "'"); 141 stmt.addBatch(str); 142 143 // Get any composed members of the the package 144 RegistryObjectList roList = pkg.getRegistryObjectList(); 145 ArrayList al = bu.getRegistryObjectList(roList); 146 composedObjects.addAll(al); 147 148 // Create the Associations associating the package to the 149 // registry objects under the RegistryObjectList 150 Iterator roIter = al.iterator(); 151 while (roIter.hasNext()) { 152 RegistryObjectType ro = (RegistryObjectType)roIter.next(); 153 Association ass = new Association(); 154 ass.setId("urn:uuid:" + UUIDFactory.getInstance().newUUID()); 155 ass.setSourceObject(pkg); 156 ass.setTargetObject(ro); 157 ass.setAssociationType("HasMember"); 158 associations.add(ass); 159 } 160 161 } // end looping the ArrayList of RegistryPackage 162 163 if (pkgs.size() > 0) { 164 int [] updateCounts = stmt.executeBatch(); 165 //stmt.close(); 166 super.insert(user, connection, pkgs); 167 // Now generate the AE for inserting RegistryPackage 168 generateAuditbleEvent(connection, pkgs, "Created" 169 , user); 170 // Insert the composed objects and create associations 171 assDAO.insert(user, connection, associations); 172 SQLPersistenceManagerImpl.getInstance().insert(user, connection, composedObjects, false); 173 } 174 } 175 catch (java.sql.SQLException e) { 176 e.printStackTrace(); 177 RegistryException exception = new RegistryException(e); 178 throw exception; 179 } finally { 180 try { 181 if (stmt != null) 182 stmt.close(); 183 } catch (SQLException sqle) { 184 sqle.printStackTrace(); 185 } 186 } 187 } 188 189 public void update(User user, java.sql.Connection connection 190 , ArrayList pkgs) throws RegistryException { 191 /* 192 We do not need to check whether the user is authorized to update because the authorization is expected to be checked by higher level. 193 Should we move the authorization to persistance layer?????? 194 */ 195 update(user, connection, pkgs, true, false); 196 197 } 198 199 public void update(User user, java.sql.Connection connection, ArrayList pkgs, 200 boolean checkExistence, boolean checkAuthorization) throws RegistryException { 201 202 if (pkgs.size()==0) { 203 return; 204 } 205 System.err.println("Updating " + pkgs.size() + " RegistryPackages"); 206 String pkgId = null; 207 Statement stmt = null; 208 try { 209 // Check whether the user can update the objects 210 if (checkAuthorization) { 211 AuthorizationServiceImpl authorization = AuthorizationServiceImpl.getInstance(); 212 authorization.checkAuthorization(user, bu.getIdsFromRegistryObjects(pkgs), AuthorizationServiceImpl.UPDATE_OBJECTS_REQUEST); 213 } 214 stmt = connection.createStatement(); 215 Iterator iter = pkgs.iterator(); 216 while (iter.hasNext()) { 217 RegistryPackage pkg = (RegistryPackage)iter.next(); 218 pkgId = pkg.getId(); 219 //Log.print(Log.TRACE, 8, "\tDATABASE EVENT: updating RegistryPackage " ); 220 221 // checking whether it exists 222 if (checkExistence && !registryObjectExist(connection, pkgId, getTableName())) { 223 ArrayList idList = new ArrayList(); 224 idList.add(pkgId); 225 throw new ObjectsNotFoundException(idList); 226 } 227 228 java.util.Date expiration = pkg.getExpiration(); 229 String expirationAsString = null; 230 if (expiration != null) { 231 expirationAsString = "'" + (new Timestamp(expiration.getTime())) + "'"; 232 } 233 234 int majorVersion = pkg.getMajorVersion(); 235 int minorVersion = pkg.getMinorVersion(); 236 237 StabilityType s = pkg.getStability(); 238 String stability = null; 239 if (s != null) { 240 stability = "'" + s + "'"; 241 } 242 243 StatusType st = pkg.getStatus(); 244 String status = "'Submitted'"; 245 if (st != null) { 246 status = "'" + st + "'"; 247 } 248 249 String userVersion = pkg.getUserVersion(); 250 if (userVersion != null) { 251 userVersion = "'" + userVersion + "'"; 252 } 253 254 String str = "UPDATE " + getTableName() + 255 " SET accessControlPolicy=null" + 256 ", expiration=" + expirationAsString + 257 ", stability=" + stability + 258 ", userVersion=" + userVersion; 259 str += " WHERE id='" + pkgId + "'"; 260 261 System.err.println("stmt = '" + str + "'"); 262 stmt.addBatch(str); 263 //Now insert any composed members of the the package 264 //RegistryObjectList roList = pkg.getRegistryObjectList(); 265 //ArrayList al = bu.getRegistryObjectList(roList); 266 } // end looping pkgs 267 if (pkgs.size() > 0) { 268 int [] updateCounts = stmt.executeBatch(); 269 //stmt.close(); 270 // Updated composed Name, Descrption, Slot, ExternalIdentifier and Classification 271 super.update(user, connection, pkgs); 272 // Now generate the AE for inserting RegistryPackage 273 generateAuditbleEvent(connection, pkgs, "Updated", user); 274 } 275 } 276 catch (java.sql.SQLException e) { 277 e.printStackTrace(); 278 RegistryException exception = new RegistryException(e); 279 throw exception; 280 } finally { 281 try { 282 if (stmt != null) 283 stmt.close(); 284 } catch (SQLException sqle) { 285 sqle.printStackTrace(); 286 } 287 } 288 } 289 290 public void loadObjectFromResultSet(java.sql.Connection conn, Object obj, ResultSet rs, ResponseOption responseOption, ArrayList objectRefs) throws RegistryException { 291 try { 292 if (!(obj instanceof org.oasis.ebxml.registry.bindings.rim.RegistryPackage)) { 293 throw new RegistryException("Unexpected object " + obj + ". Was expecting org.oasis.ebxml.registry.bindings.rim.RegistryPackage."); 294 } 295 296 RegistryPackage pkg = (RegistryPackage)obj; 297 super.loadObjectFromResultSet(conn, obj, rs, responseOption, objectRefs); 298 299 conn.setAutoCommit(false); //Just to preserve the try/catch block. 300 } 301 catch (SQLException e) { 302 e.printStackTrace(); 303 throw new RegistryException(e); 304 } 305 } 306 307 public ArrayList getLeafObjectList(java.sql.Connection conn, ResultSet rs, ResponseOption responseOption, ArrayList objectRefs) throws RegistryException { 308 ArrayList res = new ArrayList(); 309 310 try { 311 while(rs.next()) { 312 RegistryPackage obj = new RegistryPackage(); 313 loadObjectFromResultSet(conn, obj, rs, responseOption, objectRefs); 314 315 res.add(obj); 316 } 317 } 318 catch (SQLException e) { 319 e.printStackTrace(); 320 throw new RegistryException(e); 321 } 322 323 324 return res; 325 } 326 }

This page was automatically generated by Maven